“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * ah_decrypt.c : IPSec AH decrypt node |
| 3 | * |
| 4 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <vnet/vnet.h> |
| 19 | #include <vnet/api_errno.h> |
| 20 | #include <vnet/ip/ip.h> |
| 21 | |
| 22 | #include <vnet/ipsec/ipsec.h> |
| 23 | #include <vnet/ipsec/esp.h> |
| 24 | #include <vnet/ipsec/ah.h> |
Neale Ranns | 918c161 | 2019-02-21 23:34:59 -0800 | [diff] [blame] | 25 | #include <vnet/ipsec/ipsec_io.h> |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 26 | #include <vnet/ipsec/ipsec.api_enum.h> |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 27 | |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 28 | #define foreach_ah_decrypt_next \ |
| 29 | _(DROP, "error-drop") \ |
| 30 | _(IP4_INPUT, "ip4-input") \ |
| 31 | _(IP6_INPUT, "ip6-input") \ |
| 32 | _(HANDOFF, "handoff") |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 33 | |
| 34 | #define _(v, s) AH_DECRYPT_NEXT_##v, |
| 35 | typedef enum |
| 36 | { |
| 37 | foreach_ah_decrypt_next |
| 38 | #undef _ |
| 39 | AH_DECRYPT_N_NEXT, |
| 40 | } ah_decrypt_next_t; |
| 41 | |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 42 | typedef struct |
| 43 | { |
| 44 | ipsec_integ_alg_t integ_alg; |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 45 | u32 seq_num; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 46 | } ah_decrypt_trace_t; |
| 47 | |
| 48 | /* packet trace format function */ |
| 49 | static u8 * |
| 50 | format_ah_decrypt_trace (u8 * s, va_list * args) |
| 51 | { |
| 52 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 53 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 54 | ah_decrypt_trace_t *t = va_arg (*args, ah_decrypt_trace_t *); |
| 55 | |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 56 | s = format (s, "ah: integrity %U seq-num %d", |
| 57 | format_ipsec_integ_alg, t->integ_alg, t->seq_num); |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 58 | return s; |
| 59 | } |
| 60 | |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 61 | typedef struct |
| 62 | { |
| 63 | union |
| 64 | { |
| 65 | struct |
| 66 | { |
| 67 | u8 hop_limit; |
| 68 | u8 nexthdr; |
| 69 | u32 ip_version_traffic_class_and_flow_label; |
| 70 | }; |
| 71 | |
| 72 | struct |
| 73 | { |
| 74 | u8 ttl; |
| 75 | u8 tos; |
| 76 | }; |
| 77 | }; |
| 78 | u32 sa_index; |
| 79 | u32 seq; |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 80 | u32 seq_hi; |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 81 | u8 icv_padding_len; |
| 82 | u8 icv_size; |
| 83 | u8 ip_hdr_size; |
| 84 | i16 current_data; |
| 85 | u8 nexthdr_cached; |
| 86 | } ah_decrypt_packet_data_t; |
| 87 | |
| 88 | static_always_inline void |
| 89 | ah_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 90 | vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts) |
| 91 | { |
| 92 | u32 n_fail, n_ops = vec_len (ops); |
| 93 | vnet_crypto_op_t *op = ops; |
| 94 | |
| 95 | if (n_ops == 0) |
| 96 | return; |
| 97 | |
| 98 | n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops); |
| 99 | |
| 100 | while (n_fail) |
| 101 | { |
| 102 | ASSERT (op - ops < n_ops); |
| 103 | |
| 104 | if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED) |
| 105 | { |
| 106 | u32 bi = op->user_data; |
| 107 | b[bi]->error = node->errors[AH_DECRYPT_ERROR_INTEG_ERROR]; |
| 108 | nexts[bi] = AH_DECRYPT_NEXT_DROP; |
| 109 | n_fail--; |
| 110 | } |
| 111 | op++; |
| 112 | } |
| 113 | } |
| 114 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 115 | always_inline uword |
| 116 | ah_decrypt_inline (vlib_main_t * vm, |
| 117 | vlib_node_runtime_t * node, vlib_frame_t * from_frame, |
| 118 | int is_ip6) |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 119 | { |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 120 | u32 n_left, *from; |
| 121 | u32 thread_index = vm->thread_index; |
| 122 | u16 buffer_data_size = vlib_buffer_get_default_data_size (vm); |
| 123 | ah_decrypt_packet_data_t pkt_data[VLIB_FRAME_SIZE], *pd = pkt_data; |
| 124 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; |
| 125 | u16 nexts[VLIB_FRAME_SIZE], *next = nexts; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 126 | ipsec_main_t *im = &ipsec_main; |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 127 | ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, thread_index); |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 128 | from = vlib_frame_vector_args (from_frame); |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 129 | n_left = from_frame->n_vectors; |
| 130 | ipsec_sa_t *sa0 = 0; |
| 131 | u32 current_sa_index = ~0, current_sa_bytes = 0, current_sa_pkts = 0; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 132 | |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 133 | clib_memset (pkt_data, 0, VLIB_FRAME_SIZE * sizeof (pkt_data[0])); |
| 134 | vlib_get_buffers (vm, from, b, n_left); |
| 135 | clib_memset_u16 (nexts, -1, n_left); |
| 136 | vec_reset_length (ptd->integ_ops); |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 137 | |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 138 | while (n_left > 0) |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 139 | { |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 140 | ah_header_t *ah0; |
| 141 | ip4_header_t *ih4; |
| 142 | ip6_header_t *ih6; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 143 | |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 144 | if (vnet_buffer (b[0])->ipsec.sad_index != current_sa_index) |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 145 | { |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 146 | if (current_sa_index != ~0) |
| 147 | vlib_increment_combined_counter (&ipsec_sa_counters, thread_index, |
| 148 | current_sa_index, |
| 149 | current_sa_pkts, |
| 150 | current_sa_bytes); |
| 151 | current_sa_index = vnet_buffer (b[0])->ipsec.sad_index; |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 152 | sa0 = ipsec_sa_get (current_sa_index); |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 153 | |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 154 | current_sa_bytes = current_sa_pkts = 0; |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 155 | vlib_prefetch_combined_counter (&ipsec_sa_counters, |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 156 | thread_index, current_sa_index); |
| 157 | } |
| 158 | |
Neale Ranns | 1a52d37 | 2021-02-04 11:33:32 +0000 | [diff] [blame] | 159 | if (PREDICT_FALSE (~0 == sa0->thread_index)) |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 160 | { |
| 161 | /* this is the first packet to use this SA, claim the SA |
| 162 | * for this thread. this could happen simultaneously on |
| 163 | * another thread */ |
Neale Ranns | 1a52d37 | 2021-02-04 11:33:32 +0000 | [diff] [blame] | 164 | clib_atomic_cmp_and_swap (&sa0->thread_index, ~0, |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 165 | ipsec_sa_assign_thread (thread_index)); |
| 166 | } |
| 167 | |
Neale Ranns | 1a52d37 | 2021-02-04 11:33:32 +0000 | [diff] [blame] | 168 | if (PREDICT_TRUE (thread_index != sa0->thread_index)) |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 169 | { |
Neale Ranns | aa7d766 | 2021-02-10 08:42:49 +0000 | [diff] [blame] | 170 | vnet_buffer (b[0])->ipsec.thread_index = sa0->thread_index; |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 171 | next[0] = AH_DECRYPT_NEXT_HANDOFF; |
| 172 | goto next; |
| 173 | } |
| 174 | |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 175 | pd->sa_index = current_sa_index; |
| 176 | |
| 177 | ih4 = vlib_buffer_get_current (b[0]); |
| 178 | ih6 = vlib_buffer_get_current (b[0]); |
| 179 | pd->current_data = b[0]->current_data; |
| 180 | |
| 181 | if (is_ip6) |
| 182 | { |
| 183 | ip6_ext_header_t *prev = NULL; |
Klement Sekera | 769145c | 2019-03-06 11:59:57 +0100 | [diff] [blame] | 184 | ah0 = |
| 185 | ip6_ext_header_find (vm, b[0], ih6, IP_PROTOCOL_IPSEC_AH, &prev); |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 186 | pd->ip_hdr_size = sizeof (ip6_header_t); |
| 187 | ASSERT ((u8 *) ah0 - (u8 *) ih6 == pd->ip_hdr_size); |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | if (ip4_is_fragment (ih4)) |
| 192 | { |
| 193 | b[0]->error = node->errors[AH_DECRYPT_ERROR_DROP_FRAGMENTS]; |
| 194 | next[0] = AH_DECRYPT_NEXT_DROP; |
| 195 | goto next; |
| 196 | } |
| 197 | pd->ip_hdr_size = ip4_header_bytes (ih4); |
| 198 | ah0 = (ah_header_t *) ((u8 *) ih4 + pd->ip_hdr_size); |
| 199 | } |
| 200 | |
| 201 | pd->seq = clib_host_to_net_u32 (ah0->seq_no); |
| 202 | |
| 203 | /* anti-replay check */ |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 204 | if (ipsec_sa_anti_replay_and_sn_advance (sa0, pd->seq, ~0, false, |
| 205 | &pd->seq_hi)) |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 206 | { |
| 207 | b[0]->error = node->errors[AH_DECRYPT_ERROR_REPLAY]; |
| 208 | next[0] = AH_DECRYPT_NEXT_DROP; |
| 209 | goto next; |
| 210 | } |
| 211 | |
| 212 | current_sa_bytes += b[0]->current_length; |
| 213 | current_sa_pkts += 1; |
| 214 | |
| 215 | pd->icv_size = sa0->integ_icv_size; |
| 216 | pd->nexthdr_cached = ah0->nexthdr; |
| 217 | if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE)) |
| 218 | { |
| 219 | if (PREDICT_FALSE (ipsec_sa_is_set_USE_ESN (sa0) && |
| 220 | pd->current_data + b[0]->current_length |
| 221 | + sizeof (u32) > buffer_data_size)) |
| 222 | { |
| 223 | b[0]->error = node->errors[AH_DECRYPT_ERROR_NO_TAIL_SPACE]; |
| 224 | next[0] = AH_DECRYPT_NEXT_DROP; |
| 225 | goto next; |
| 226 | } |
| 227 | |
| 228 | vnet_crypto_op_t *op; |
| 229 | vec_add2_aligned (ptd->integ_ops, op, 1, CLIB_CACHE_LINE_BYTES); |
| 230 | vnet_crypto_op_init (op, sa0->integ_op_id); |
| 231 | |
| 232 | op->src = (u8 *) ih4; |
| 233 | op->len = b[0]->current_length; |
| 234 | op->digest = (u8 *) ih4 - pd->icv_size; |
| 235 | op->flags = VNET_CRYPTO_OP_FLAG_HMAC_CHECK; |
| 236 | op->digest_len = pd->icv_size; |
| 237 | op->key_index = sa0->integ_key_index; |
| 238 | op->user_data = b - bufs; |
| 239 | if (ipsec_sa_is_set_USE_ESN (sa0)) |
| 240 | { |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 241 | u32 seq_hi = clib_host_to_net_u32 (pd->seq_hi); |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 242 | |
| 243 | op->len += sizeof (seq_hi); |
| 244 | clib_memcpy (op->src + b[0]->current_length, &seq_hi, |
| 245 | sizeof (seq_hi)); |
| 246 | } |
| 247 | clib_memcpy (op->digest, ah0->auth_data, pd->icv_size); |
| 248 | clib_memset (ah0->auth_data, 0, pd->icv_size); |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 249 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 250 | if (is_ip6) |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 251 | { |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 252 | pd->ip_version_traffic_class_and_flow_label = |
| 253 | ih6->ip_version_traffic_class_and_flow_label; |
| 254 | pd->hop_limit = ih6->hop_limit; |
| 255 | ih6->ip_version_traffic_class_and_flow_label = 0x60; |
| 256 | ih6->hop_limit = 0; |
| 257 | pd->nexthdr = ah0->nexthdr; |
| 258 | pd->icv_padding_len = |
| 259 | ah_calc_icv_padding_len (pd->icv_size, 1 /* is_ipv6 */ ); |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 260 | } |
| 261 | else |
| 262 | { |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 263 | pd->tos = ih4->tos; |
| 264 | pd->ttl = ih4->ttl; |
| 265 | ih4->tos = 0; |
| 266 | ih4->ttl = 0; |
| 267 | ih4->checksum = 0; |
| 268 | pd->icv_padding_len = |
| 269 | ah_calc_icv_padding_len (pd->icv_size, 0 /* is_ipv6 */ ); |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 270 | } |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 271 | } |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 272 | |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 273 | next: |
| 274 | n_left -= 1; |
| 275 | pd += 1; |
| 276 | next += 1; |
| 277 | b += 1; |
| 278 | } |
Neale Ranns | de84727 | 2018-11-28 01:38:34 -0800 | [diff] [blame] | 279 | |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 280 | n_left = from_frame->n_vectors; |
| 281 | next = nexts; |
| 282 | pd = pkt_data; |
| 283 | b = bufs; |
| 284 | |
| 285 | vlib_node_increment_counter (vm, node->node_index, AH_DECRYPT_ERROR_RX_PKTS, |
| 286 | n_left); |
| 287 | vlib_increment_combined_counter (&ipsec_sa_counters, thread_index, |
| 288 | current_sa_index, current_sa_pkts, |
| 289 | current_sa_bytes); |
| 290 | |
| 291 | ah_process_ops (vm, node, ptd->integ_ops, bufs, nexts); |
| 292 | |
| 293 | while (n_left > 0) |
| 294 | { |
| 295 | ip4_header_t *oh4; |
| 296 | ip6_header_t *oh6; |
Neale Ranns | e11203e | 2021-09-21 12:34:19 +0000 | [diff] [blame] | 297 | u64 n_lost = 0; |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 298 | |
| 299 | if (next[0] < AH_DECRYPT_N_NEXT) |
| 300 | goto trace; |
| 301 | |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 302 | sa0 = ipsec_sa_get (pd->sa_index); |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 303 | |
| 304 | if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE)) |
| 305 | { |
Neale Ranns | e11203e | 2021-09-21 12:34:19 +0000 | [diff] [blame] | 306 | /* redo the anti-reply check. see esp_decrypt for details */ |
Neale Ranns | 5b89110 | 2021-06-28 13:31:28 +0000 | [diff] [blame] | 307 | if (ipsec_sa_anti_replay_and_sn_advance (sa0, pd->seq, pd->seq_hi, |
| 308 | true, NULL)) |
Neale Ranns | 3b9374f | 2019-08-01 04:45:15 -0700 | [diff] [blame] | 309 | { |
| 310 | b[0]->error = node->errors[AH_DECRYPT_ERROR_REPLAY]; |
| 311 | next[0] = AH_DECRYPT_NEXT_DROP; |
| 312 | goto trace; |
| 313 | } |
Neale Ranns | e11203e | 2021-09-21 12:34:19 +0000 | [diff] [blame] | 314 | n_lost = ipsec_sa_anti_replay_advance (sa0, thread_index, pd->seq, |
| 315 | pd->seq_hi); |
| 316 | vlib_prefetch_simple_counter (&ipsec_sa_lost_counters, thread_index, |
| 317 | pd->sa_index); |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | u16 ah_hdr_len = sizeof (ah_header_t) + pd->icv_size |
| 321 | + pd->icv_padding_len; |
| 322 | vlib_buffer_advance (b[0], pd->ip_hdr_size + ah_hdr_len); |
| 323 | b[0]->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; |
| 324 | |
| 325 | if (PREDICT_TRUE (ipsec_sa_is_set_IS_TUNNEL (sa0))) |
| 326 | { /* tunnel mode */ |
| 327 | if (PREDICT_TRUE (pd->nexthdr_cached == IP_PROTOCOL_IP_IN_IP)) |
| 328 | next[0] = AH_DECRYPT_NEXT_IP4_INPUT; |
| 329 | else if (pd->nexthdr_cached == IP_PROTOCOL_IPV6) |
| 330 | next[0] = AH_DECRYPT_NEXT_IP6_INPUT; |
| 331 | else |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 332 | { |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 333 | b[0]->error = node->errors[AH_DECRYPT_ERROR_DECRYPTION_FAILED]; |
| 334 | next[0] = AH_DECRYPT_NEXT_DROP; |
Damjan Marion | 1f4e1cb | 2019-03-28 19:19:31 +0100 | [diff] [blame] | 335 | goto trace; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 336 | } |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 337 | } |
| 338 | else |
| 339 | { /* transport mode */ |
| 340 | if (is_ip6) |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 341 | { |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 342 | vlib_buffer_advance (b[0], -sizeof (ip6_header_t)); |
| 343 | oh6 = vlib_buffer_get_current (b[0]); |
| 344 | if (ah_hdr_len >= sizeof (ip6_header_t)) |
| 345 | clib_memcpy (oh6, b[0]->data + pd->current_data, |
| 346 | sizeof (ip6_header_t)); |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 347 | else |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 348 | memmove (oh6, b[0]->data + pd->current_data, |
| 349 | sizeof (ip6_header_t)); |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 350 | |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 351 | next[0] = AH_DECRYPT_NEXT_IP6_INPUT; |
| 352 | oh6->protocol = pd->nexthdr; |
| 353 | oh6->hop_limit = pd->hop_limit; |
| 354 | oh6->ip_version_traffic_class_and_flow_label = |
| 355 | pd->ip_version_traffic_class_and_flow_label; |
| 356 | oh6->payload_length = |
| 357 | clib_host_to_net_u16 (vlib_buffer_length_in_chain |
| 358 | (vm, b[0]) - sizeof (ip6_header_t)); |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 359 | } |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 360 | else |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 361 | { |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 362 | vlib_buffer_advance (b[0], -sizeof (ip4_header_t)); |
| 363 | oh4 = vlib_buffer_get_current (b[0]); |
| 364 | if (ah_hdr_len >= sizeof (ip4_header_t)) |
| 365 | clib_memcpy (oh4, b[0]->data + pd->current_data, |
| 366 | sizeof (ip4_header_t)); |
| 367 | else |
| 368 | memmove (oh4, b[0]->data + pd->current_data, |
| 369 | sizeof (ip4_header_t)); |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 370 | |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 371 | next[0] = AH_DECRYPT_NEXT_IP4_INPUT; |
| 372 | oh4->ip_version_and_header_length = 0x45; |
| 373 | oh4->fragment_id = 0; |
| 374 | oh4->flags_and_fragment_offset = 0; |
| 375 | oh4->protocol = pd->nexthdr_cached; |
| 376 | oh4->length = |
| 377 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0])); |
| 378 | oh4->ttl = pd->ttl; |
| 379 | oh4->tos = pd->tos; |
| 380 | oh4->checksum = ip4_header_checksum (oh4); |
| 381 | } |
| 382 | } |
| 383 | |
Neale Ranns | e11203e | 2021-09-21 12:34:19 +0000 | [diff] [blame] | 384 | if (PREDICT_FALSE (n_lost)) |
| 385 | vlib_increment_simple_counter (&ipsec_sa_lost_counters, thread_index, |
| 386 | pd->sa_index, n_lost); |
| 387 | |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 388 | vnet_buffer (b[0])->sw_if_index[VLIB_TX] = (u32) ~ 0; |
| 389 | trace: |
| 390 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
| 391 | { |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 392 | sa0 = ipsec_sa_get (vnet_buffer (b[0])->ipsec.sad_index); |
Filip Tehlar | 6230eea | 2019-05-21 08:11:21 +0000 | [diff] [blame] | 393 | ah_decrypt_trace_t *tr = |
| 394 | vlib_add_trace (vm, node, b[0], sizeof (*tr)); |
| 395 | tr->integ_alg = sa0->integ_alg; |
| 396 | tr->seq_num = pd->seq; |
| 397 | } |
| 398 | |
| 399 | n_left -= 1; |
| 400 | pd += 1; |
| 401 | next += 1; |
| 402 | b += 1; |
| 403 | } |
| 404 | |
| 405 | n_left = from_frame->n_vectors; |
| 406 | vlib_buffer_enqueue_to_next (vm, node, from, nexts, n_left); |
| 407 | |
| 408 | return n_left; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 409 | } |
| 410 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 411 | VLIB_NODE_FN (ah4_decrypt_node) (vlib_main_t * vm, |
| 412 | vlib_node_runtime_t * node, |
| 413 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 414 | { |
| 415 | return ah_decrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ ); |
| 416 | } |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 417 | |
| 418 | /* *INDENT-OFF* */ |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 419 | VLIB_REGISTER_NODE (ah4_decrypt_node) = { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 420 | .name = "ah4-decrypt", |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 421 | .vector_size = sizeof (u32), |
| 422 | .format_trace = format_ah_decrypt_trace, |
| 423 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 424 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 425 | .n_errors = AH_DECRYPT_N_ERROR, |
| 426 | .error_counters = ah_decrypt_error_counters, |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 427 | |
| 428 | .n_next_nodes = AH_DECRYPT_N_NEXT, |
| 429 | .next_nodes = { |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 430 | [AH_DECRYPT_NEXT_DROP] = "ip4-drop", |
| 431 | [AH_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum", |
| 432 | [AH_DECRYPT_NEXT_IP6_INPUT] = "ip6-input", |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 433 | [AH_DECRYPT_NEXT_HANDOFF] = "ah4-decrypt-handoff", |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 434 | }, |
| 435 | }; |
| 436 | /* *INDENT-ON* */ |
| 437 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 438 | VLIB_NODE_FN (ah6_decrypt_node) (vlib_main_t * vm, |
| 439 | vlib_node_runtime_t * node, |
| 440 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 441 | { |
| 442 | return ah_decrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ ); |
| 443 | } |
| 444 | |
| 445 | /* *INDENT-OFF* */ |
| 446 | VLIB_REGISTER_NODE (ah6_decrypt_node) = { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 447 | .name = "ah6-decrypt", |
| 448 | .vector_size = sizeof (u32), |
| 449 | .format_trace = format_ah_decrypt_trace, |
| 450 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 451 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame] | 452 | .n_errors = AH_DECRYPT_N_ERROR, |
| 453 | .error_counters = ah_decrypt_error_counters, |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 454 | |
| 455 | .n_next_nodes = AH_DECRYPT_N_NEXT, |
| 456 | .next_nodes = { |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 457 | [AH_DECRYPT_NEXT_DROP] = "ip6-drop", |
| 458 | [AH_DECRYPT_NEXT_IP4_INPUT] = "ip4-input-no-checksum", |
| 459 | [AH_DECRYPT_NEXT_IP6_INPUT] = "ip6-input", |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 460 | [AH_DECRYPT_NEXT_HANDOFF] = "ah6-decrypt-handoff", |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 461 | }, |
| 462 | }; |
| 463 | /* *INDENT-ON* */ |
| 464 | |
Neale Ranns | 2d49830 | 2021-02-25 08:38:58 +0000 | [diff] [blame] | 465 | #ifndef CLIB_MARCH_VARIANT |
| 466 | |
| 467 | static clib_error_t * |
| 468 | ah_decrypt_init (vlib_main_t *vm) |
| 469 | { |
| 470 | ipsec_main_t *im = &ipsec_main; |
| 471 | |
| 472 | im->ah4_dec_fq_index = |
| 473 | vlib_frame_queue_main_init (ah4_decrypt_node.index, 0); |
| 474 | im->ah6_dec_fq_index = |
| 475 | vlib_frame_queue_main_init (ah6_decrypt_node.index, 0); |
| 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | VLIB_INIT_FUNCTION (ah_decrypt_init); |
| 481 | |
| 482 | #endif |
| 483 | |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 484 | /* |
| 485 | * fd.io coding-style-patch-verification: ON |
| 486 | * |
| 487 | * Local Variables: |
| 488 | * eval: (c-set-style "gnu") |
| 489 | * End: |
| 490 | */ |