“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * ah_encrypt.c : IPSec AH 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> |
| 21 | |
| 22 | #include <vnet/ipsec/ipsec.h> |
| 23 | #include <vnet/ipsec/esp.h> |
| 24 | #include <vnet/ipsec/ah.h> |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame^] | 25 | #include <vnet/ipsec/ipsec.api_enum.h> |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 26 | #include <vnet/tunnel/tunnel_dp.h> |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 27 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 28 | #define foreach_ah_encrypt_next \ |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 29 | _ (DROP, "error-drop") \ |
| 30 | _ (HANDOFF, "handoff") \ |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 31 | _ (INTERFACE_OUTPUT, "interface-output") |
| 32 | |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 33 | |
| 34 | #define _(v, s) AH_ENCRYPT_NEXT_##v, |
| 35 | typedef enum |
| 36 | { |
| 37 | foreach_ah_encrypt_next |
| 38 | #undef _ |
| 39 | AH_ENCRYPT_N_NEXT, |
| 40 | } ah_encrypt_next_t; |
| 41 | |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 42 | typedef struct |
| 43 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 44 | u32 sa_index; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 45 | u32 spi; |
Neale Ranns | 3833ffd | 2019-03-21 14:34:09 +0000 | [diff] [blame] | 46 | u32 seq_lo; |
| 47 | u32 seq_hi; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 48 | ipsec_integ_alg_t integ_alg; |
| 49 | } ah_encrypt_trace_t; |
| 50 | |
| 51 | /* packet trace format function */ |
| 52 | static u8 * |
| 53 | format_ah_encrypt_trace (u8 * s, va_list * args) |
| 54 | { |
| 55 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 56 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 57 | ah_encrypt_trace_t *t = va_arg (*args, ah_encrypt_trace_t *); |
| 58 | |
Guillaume Solignac | 8f818cc | 2019-05-15 12:02:33 +0200 | [diff] [blame] | 59 | s = format (s, "ah: sa-index %d spi %u (0x%08x) seq %u:%u integrity %U", |
| 60 | t->sa_index, t->spi, t->spi, t->seq_hi, t->seq_lo, |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 61 | format_ipsec_integ_alg, t->integ_alg); |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 62 | return s; |
| 63 | } |
| 64 | |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 65 | static_always_inline void |
| 66 | ah_process_ops (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 67 | vnet_crypto_op_t * ops, vlib_buffer_t * b[], u16 * nexts) |
| 68 | { |
| 69 | u32 n_fail, n_ops = vec_len (ops); |
| 70 | vnet_crypto_op_t *op = ops; |
| 71 | |
| 72 | if (n_ops == 0) |
| 73 | return; |
| 74 | |
| 75 | n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops); |
| 76 | |
| 77 | while (n_fail) |
| 78 | { |
| 79 | ASSERT (op - ops < n_ops); |
| 80 | |
| 81 | if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED) |
| 82 | { |
| 83 | u32 bi = op->user_data; |
| 84 | b[bi]->error = node->errors[AH_ENCRYPT_ERROR_CRYPTO_ENGINE_ERROR]; |
| 85 | nexts[bi] = AH_ENCRYPT_NEXT_DROP; |
| 86 | n_fail--; |
| 87 | } |
| 88 | op++; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | typedef struct |
| 93 | { |
| 94 | union |
| 95 | { |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 96 | /* Variable fields in the IP header not covered by the AH |
| 97 | * integrity check */ |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 98 | struct |
| 99 | { |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 100 | u32 ip_version_traffic_class_and_flow_label; |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 101 | u8 hop_limit; |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 102 | }; |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 103 | struct |
| 104 | { |
| 105 | u8 ttl; |
| 106 | u8 tos; |
| 107 | }; |
| 108 | }; |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 109 | u8 skip; |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 110 | i16 current_data; |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 111 | u32 sa_index; |
| 112 | } ah_encrypt_packet_data_t; |
| 113 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 114 | always_inline uword |
| 115 | ah_encrypt_inline (vlib_main_t * vm, |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 116 | vlib_node_runtime_t * node, vlib_frame_t * frame, |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 117 | int is_ip6) |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 118 | { |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 119 | u32 n_left, *from, thread_index; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 120 | int icv_size = 0; |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 121 | from = vlib_frame_vector_args (frame); |
| 122 | n_left = frame->n_vectors; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 123 | ipsec_main_t *im = &ipsec_main; |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 124 | ah_encrypt_packet_data_t pkt_data[VLIB_FRAME_SIZE], *pd = pkt_data; |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 125 | thread_index = vm->thread_index; |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 126 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs; |
| 127 | u16 nexts[VLIB_FRAME_SIZE], *next = nexts; |
| 128 | ipsec_per_thread_data_t *ptd = vec_elt_at_index (im->ptd, thread_index); |
| 129 | ipsec_sa_t *sa0 = 0; |
| 130 | ip4_and_ah_header_t *ih0, *oh0 = 0; |
| 131 | ip6_and_ah_header_t *ih6_0, *oh6_0 = 0; |
| 132 | u32 current_sa_index = ~0, current_sa_bytes = 0, current_sa_pkts = 0; |
| 133 | const static ip4_header_t ip4_hdr_template = { |
| 134 | .ip_version_and_header_length = 0x45, |
| 135 | .protocol = IP_PROTOCOL_IPSEC_AH, |
| 136 | }; |
| 137 | const static ip6_header_t ip6_hdr_template = { |
| 138 | .ip_version_traffic_class_and_flow_label = 0x60, |
| 139 | .protocol = IP_PROTOCOL_IPSEC_AH, |
| 140 | }; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 141 | |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 142 | clib_memset (pkt_data, 0, VLIB_FRAME_SIZE * sizeof (pkt_data[0])); |
| 143 | vlib_get_buffers (vm, from, b, n_left); |
| 144 | vec_reset_length (ptd->crypto_ops); |
| 145 | vec_reset_length (ptd->integ_ops); |
| 146 | |
| 147 | while (n_left > 0) |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 148 | { |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 149 | u8 ip_hdr_size; |
| 150 | u8 next_hdr_type; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 151 | |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 152 | if (vnet_buffer (b[0])->ipsec.sad_index != current_sa_index) |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 153 | { |
Filip Tehlar | 98d6ee7 | 2019-06-03 23:36:10 +0000 | [diff] [blame] | 154 | if (current_sa_index != ~0) |
| 155 | vlib_increment_combined_counter (&ipsec_sa_counters, thread_index, |
| 156 | current_sa_index, |
| 157 | current_sa_pkts, |
| 158 | current_sa_bytes); |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 159 | current_sa_index = vnet_buffer (b[0])->ipsec.sad_index; |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 160 | sa0 = ipsec_sa_get (current_sa_index); |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 161 | |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 162 | current_sa_bytes = current_sa_pkts = 0; |
| 163 | } |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 164 | |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 165 | pd->sa_index = current_sa_index; |
| 166 | next[0] = AH_ENCRYPT_NEXT_DROP; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 167 | |
Neale Ranns | 1a52d37 | 2021-02-04 11:33:32 +0000 | [diff] [blame] | 168 | if (PREDICT_FALSE (~0 == sa0->thread_index)) |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 169 | { |
| 170 | /* this is the first packet to use this SA, claim the SA |
| 171 | * for this thread. this could happen simultaneously on |
| 172 | * another thread */ |
Neale Ranns | 1a52d37 | 2021-02-04 11:33:32 +0000 | [diff] [blame] | 173 | clib_atomic_cmp_and_swap (&sa0->thread_index, ~0, |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 174 | ipsec_sa_assign_thread (thread_index)); |
| 175 | } |
| 176 | |
Neale Ranns | 1a52d37 | 2021-02-04 11:33:32 +0000 | [diff] [blame] | 177 | if (PREDICT_TRUE (thread_index != sa0->thread_index)) |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 178 | { |
Neale Ranns | aa7d766 | 2021-02-10 08:42:49 +0000 | [diff] [blame] | 179 | vnet_buffer (b[0])->ipsec.thread_index = sa0->thread_index; |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 180 | next[0] = AH_ENCRYPT_NEXT_HANDOFF; |
| 181 | goto next; |
| 182 | } |
| 183 | |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 184 | if (PREDICT_FALSE (esp_seq_advance (sa0))) |
| 185 | { |
| 186 | b[0]->error = node->errors[AH_ENCRYPT_ERROR_SEQ_CYCLED]; |
| 187 | pd->skip = 1; |
| 188 | goto next; |
| 189 | } |
| 190 | |
| 191 | current_sa_pkts += 1; |
| 192 | current_sa_bytes += b[0]->current_length; |
| 193 | |
| 194 | ssize_t adv; |
| 195 | ih0 = vlib_buffer_get_current (b[0]); |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 196 | |
| 197 | if (PREDICT_TRUE (ipsec_sa_is_set_IS_TUNNEL (sa0))) |
| 198 | { |
| 199 | if (is_ip6) |
| 200 | adv = -sizeof (ip6_and_ah_header_t); |
| 201 | else |
| 202 | adv = -sizeof (ip4_and_ah_header_t); |
| 203 | } |
| 204 | else |
| 205 | { |
| 206 | adv = -sizeof (ah_header_t); |
| 207 | } |
| 208 | |
| 209 | icv_size = sa0->integ_icv_size; |
| 210 | const u8 padding_len = ah_calc_icv_padding_len (icv_size, is_ip6); |
| 211 | adv -= padding_len; |
| 212 | /* transport mode save the eth header before it is overwritten */ |
| 213 | if (PREDICT_FALSE (!ipsec_sa_is_set_IS_TUNNEL (sa0))) |
| 214 | { |
Klement Sekera | 4515545 | 2019-06-19 11:26:34 +0000 | [diff] [blame] | 215 | const u32 l2_len = vnet_buffer (b[0])->ip.save_rewrite_length; |
| 216 | u8 *l2_hdr_in = (u8 *) vlib_buffer_get_current (b[0]) - l2_len; |
| 217 | |
| 218 | u8 *l2_hdr_out = l2_hdr_in + adv - icv_size; |
| 219 | |
| 220 | clib_memcpy_le32 (l2_hdr_out, l2_hdr_in, l2_len); |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | vlib_buffer_advance (b[0], adv - icv_size); |
| 224 | |
| 225 | if (is_ip6) |
| 226 | { |
| 227 | ih6_0 = (ip6_and_ah_header_t *) ih0; |
| 228 | ip_hdr_size = sizeof (ip6_header_t); |
| 229 | oh6_0 = vlib_buffer_get_current (b[0]); |
| 230 | pd->current_data = b[0]->current_data; |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 231 | pd->hop_limit = ih6_0->ip6.hop_limit; |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 232 | |
| 233 | oh6_0->ip6.ip_version_traffic_class_and_flow_label = |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 234 | ih6_0->ip6.ip_version_traffic_class_and_flow_label; |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 235 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 236 | if (PREDICT_FALSE (ipsec_sa_is_set_IS_TUNNEL (sa0))) |
| 237 | { |
| 238 | ip6_set_dscp_network_order (&oh6_0->ip6, sa0->tunnel.t_dscp); |
| 239 | tunnel_encap_fixup_6o6 (sa0->tunnel_flags, &ih6_0->ip6, |
| 240 | &oh6_0->ip6); |
| 241 | } |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 242 | pd->ip_version_traffic_class_and_flow_label = |
| 243 | oh6_0->ip6.ip_version_traffic_class_and_flow_label; |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 244 | |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 245 | if (PREDICT_TRUE (ipsec_sa_is_set_IS_TUNNEL (sa0))) |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 246 | { |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 247 | next_hdr_type = IP_PROTOCOL_IPV6; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 248 | } |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 249 | else |
| 250 | { |
| 251 | next_hdr_type = ih6_0->ip6.protocol; |
| 252 | memmove (oh6_0, ih6_0, sizeof (ip6_header_t)); |
| 253 | } |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 254 | |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 255 | clib_memcpy_fast (&oh6_0->ip6, &ip6_hdr_template, 8); |
| 256 | oh6_0->ah.reserved = 0; |
| 257 | oh6_0->ah.nexthdr = next_hdr_type; |
| 258 | oh6_0->ah.spi = clib_net_to_host_u32 (sa0->spi); |
| 259 | oh6_0->ah.seq_no = clib_net_to_host_u32 (sa0->seq); |
| 260 | oh6_0->ip6.payload_length = |
| 261 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0]) - |
| 262 | sizeof (ip6_header_t)); |
| 263 | oh6_0->ah.hdrlen = |
| 264 | (sizeof (ah_header_t) + icv_size + padding_len) / 4 - 2; |
| 265 | } |
| 266 | else |
| 267 | { |
| 268 | ip_hdr_size = sizeof (ip4_header_t); |
| 269 | oh0 = vlib_buffer_get_current (b[0]); |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 270 | pd->ttl = ih0->ip4.ttl; |
| 271 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 272 | if (PREDICT_FALSE (ipsec_sa_is_set_IS_TUNNEL (sa0))) |
| 273 | { |
| 274 | if (sa0->tunnel.t_dscp) |
| 275 | pd->tos = sa0->tunnel.t_dscp << 2; |
| 276 | else |
| 277 | { |
| 278 | pd->tos = ih0->ip4.tos; |
| 279 | |
| 280 | if (!(sa0->tunnel_flags & |
| 281 | TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_DSCP)) |
| 282 | pd->tos &= 0x3; |
| 283 | if (!(sa0->tunnel_flags & |
| 284 | TUNNEL_ENCAP_DECAP_FLAG_ENCAP_COPY_ECN)) |
| 285 | pd->tos &= 0xfc; |
| 286 | } |
| 287 | } |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 288 | else |
| 289 | { |
| 290 | pd->tos = ih0->ip4.tos; |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 291 | } |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 292 | |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 293 | pd->current_data = b[0]->current_data; |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 294 | clib_memset (oh0, 0, sizeof (ip4_and_ah_header_t)); |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 295 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 296 | if (PREDICT_TRUE (ipsec_sa_is_set_IS_TUNNEL (sa0))) |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 297 | { |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 298 | next_hdr_type = IP_PROTOCOL_IP_IN_IP; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 299 | } |
| 300 | else |
| 301 | { |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 302 | next_hdr_type = ih0->ip4.protocol; |
| 303 | memmove (oh0, ih0, sizeof (ip4_header_t)); |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 306 | clib_memcpy_fast (&oh0->ip4, &ip4_hdr_template, |
| 307 | sizeof (ip4_header_t) - |
| 308 | sizeof (ip4_address_pair_t)); |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 309 | |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 310 | oh0->ip4.length = |
| 311 | clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0])); |
| 312 | oh0->ah.spi = clib_net_to_host_u32 (sa0->spi); |
| 313 | oh0->ah.seq_no = clib_net_to_host_u32 (sa0->seq); |
| 314 | oh0->ah.nexthdr = next_hdr_type; |
| 315 | oh0->ah.hdrlen = |
| 316 | (sizeof (ah_header_t) + icv_size + padding_len) / 4 - 2; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 317 | } |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 318 | |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 319 | if (PREDICT_TRUE (!is_ip6 && ipsec_sa_is_set_IS_TUNNEL (sa0) && |
| 320 | !ipsec_sa_is_set_IS_TUNNEL_V6 (sa0))) |
| 321 | { |
| 322 | clib_memcpy_fast (&oh0->ip4.address_pair, |
| 323 | &sa0->ip4_hdr.address_pair, |
Neale Ranns | f3a6622 | 2020-01-02 05:04:00 +0000 | [diff] [blame] | 324 | sizeof (ip4_address_pair_t)); |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 325 | |
Neale Ranns | 72f2a3a | 2019-06-17 15:43:38 +0000 | [diff] [blame] | 326 | next[0] = sa0->dpo.dpoi_next_node; |
| 327 | vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = sa0->dpo.dpoi_index; |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 328 | } |
| 329 | else if (is_ip6 && ipsec_sa_is_set_IS_TUNNEL (sa0) && |
| 330 | ipsec_sa_is_set_IS_TUNNEL_V6 (sa0)) |
| 331 | { |
| 332 | clib_memcpy_fast (&oh6_0->ip6.src_address, |
| 333 | &sa0->ip6_hdr.src_address, |
| 334 | sizeof (ip6_address_t) * 2); |
Neale Ranns | 72f2a3a | 2019-06-17 15:43:38 +0000 | [diff] [blame] | 335 | next[0] = sa0->dpo.dpoi_next_node; |
| 336 | vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = sa0->dpo.dpoi_index; |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | if (PREDICT_TRUE (sa0->integ_op_id)) |
| 340 | { |
| 341 | vnet_crypto_op_t *op; |
| 342 | vec_add2_aligned (ptd->integ_ops, op, 1, CLIB_CACHE_LINE_BYTES); |
| 343 | vnet_crypto_op_init (op, sa0->integ_op_id); |
| 344 | op->src = vlib_buffer_get_current (b[0]); |
| 345 | op->len = b[0]->current_length; |
| 346 | op->digest = vlib_buffer_get_current (b[0]) + ip_hdr_size + |
| 347 | sizeof (ah_header_t); |
| 348 | clib_memset (op->digest, 0, icv_size); |
| 349 | op->digest_len = icv_size; |
| 350 | op->key_index = sa0->integ_key_index; |
| 351 | op->user_data = b - bufs; |
| 352 | if (ipsec_sa_is_set_USE_ESN (sa0)) |
| 353 | { |
| 354 | u32 seq_hi = clib_host_to_net_u32 (sa0->seq_hi); |
| 355 | |
| 356 | op->len += sizeof (seq_hi); |
| 357 | clib_memcpy (op->src + b[0]->current_length, &seq_hi, |
| 358 | sizeof (seq_hi)); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | if (!ipsec_sa_is_set_IS_TUNNEL (sa0)) |
| 363 | { |
| 364 | next[0] = AH_ENCRYPT_NEXT_INTERFACE_OUTPUT; |
| 365 | vlib_buffer_advance (b[0], -sizeof (ethernet_header_t)); |
| 366 | } |
| 367 | |
| 368 | next: |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 369 | if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
| 370 | { |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 371 | sa0 = ipsec_sa_get (pd->sa_index); |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 372 | ah_encrypt_trace_t *tr = |
| 373 | vlib_add_trace (vm, node, b[0], sizeof (*tr)); |
| 374 | tr->spi = sa0->spi; |
| 375 | tr->seq_lo = sa0->seq; |
| 376 | tr->seq_hi = sa0->seq_hi; |
| 377 | tr->integ_alg = sa0->integ_alg; |
| 378 | tr->sa_index = pd->sa_index; |
| 379 | } |
| 380 | |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 381 | n_left -= 1; |
| 382 | next += 1; |
| 383 | pd += 1; |
| 384 | b += 1; |
| 385 | } |
| 386 | |
| 387 | n_left = frame->n_vectors; |
| 388 | next = nexts; |
| 389 | pd = pkt_data; |
| 390 | b = bufs; |
| 391 | |
| 392 | vlib_node_increment_counter (vm, node->node_index, |
| 393 | AH_ENCRYPT_ERROR_RX_PKTS, n_left); |
| 394 | vlib_increment_combined_counter (&ipsec_sa_counters, thread_index, |
| 395 | current_sa_index, current_sa_pkts, |
| 396 | current_sa_bytes); |
| 397 | |
| 398 | ah_process_ops (vm, node, ptd->integ_ops, bufs, nexts); |
| 399 | |
| 400 | while (n_left) |
| 401 | { |
| 402 | if (pd->skip) |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 403 | goto next_pkt; |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 404 | |
| 405 | if (is_ip6) |
| 406 | { |
| 407 | oh6_0 = (ip6_and_ah_header_t *) (b[0]->data + pd->current_data); |
| 408 | oh6_0->ip6.hop_limit = pd->hop_limit; |
| 409 | oh6_0->ip6.ip_version_traffic_class_and_flow_label = |
| 410 | pd->ip_version_traffic_class_and_flow_label; |
| 411 | } |
| 412 | else |
| 413 | { |
| 414 | oh0 = (ip4_and_ah_header_t *) (b[0]->data + pd->current_data); |
| 415 | oh0->ip4.ttl = pd->ttl; |
| 416 | oh0->ip4.tos = pd->tos; |
| 417 | oh0->ip4.checksum = ip4_header_checksum (&oh0->ip4); |
| 418 | } |
| 419 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 420 | next_pkt: |
Filip Tehlar | 1197449 | 2019-04-17 07:16:39 +0000 | [diff] [blame] | 421 | n_left -= 1; |
| 422 | next += 1; |
| 423 | pd += 1; |
| 424 | b += 1; |
| 425 | } |
| 426 | |
| 427 | n_left = frame->n_vectors; |
| 428 | vlib_buffer_enqueue_to_next (vm, node, from, nexts, n_left); |
| 429 | |
| 430 | return n_left; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 431 | } |
| 432 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 433 | VLIB_NODE_FN (ah4_encrypt_node) (vlib_main_t * vm, |
| 434 | vlib_node_runtime_t * node, |
| 435 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 436 | { |
| 437 | return ah_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ ); |
| 438 | } |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 439 | |
| 440 | /* *INDENT-OFF* */ |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 441 | VLIB_REGISTER_NODE (ah4_encrypt_node) = { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 442 | .name = "ah4-encrypt", |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 443 | .vector_size = sizeof (u32), |
| 444 | .format_trace = format_ah_encrypt_trace, |
| 445 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 446 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame^] | 447 | .n_errors = AH_ENCRYPT_N_ERROR, |
| 448 | .error_counters = ah_encrypt_error_counters, |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 449 | |
| 450 | .n_next_nodes = AH_ENCRYPT_N_NEXT, |
| 451 | .next_nodes = { |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 452 | [AH_ENCRYPT_NEXT_DROP] = "ip4-drop", |
| 453 | [AH_ENCRYPT_NEXT_HANDOFF] = "ah4-encrypt-handoff", |
| 454 | [AH_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output", |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 455 | }, |
| 456 | }; |
| 457 | /* *INDENT-ON* */ |
| 458 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 459 | VLIB_NODE_FN (ah6_encrypt_node) (vlib_main_t * vm, |
| 460 | vlib_node_runtime_t * node, |
| 461 | vlib_frame_t * from_frame) |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 462 | { |
| 463 | return ah_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ ); |
| 464 | } |
| 465 | |
| 466 | /* *INDENT-OFF* */ |
| 467 | VLIB_REGISTER_NODE (ah6_encrypt_node) = { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 468 | .name = "ah6-encrypt", |
| 469 | .vector_size = sizeof (u32), |
| 470 | .format_trace = format_ah_encrypt_trace, |
| 471 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 472 | |
Neale Ranns | 93688d7 | 2022-08-09 03:34:51 +0000 | [diff] [blame^] | 473 | .n_errors = AH_ENCRYPT_N_ERROR, |
| 474 | .error_counters = ah_encrypt_error_counters, |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 475 | |
| 476 | .n_next_nodes = AH_ENCRYPT_N_NEXT, |
| 477 | .next_nodes = { |
Neale Ranns | f62a8c0 | 2019-04-02 08:13:33 +0000 | [diff] [blame] | 478 | [AH_ENCRYPT_NEXT_DROP] = "ip6-drop", |
| 479 | [AH_ENCRYPT_NEXT_HANDOFF] = "ah6-encrypt-handoff", |
| 480 | [AH_ENCRYPT_NEXT_INTERFACE_OUTPUT] = "interface-output", |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 481 | }, |
| 482 | }; |
| 483 | /* *INDENT-ON* */ |
| 484 | |
Neale Ranns | 2d49830 | 2021-02-25 08:38:58 +0000 | [diff] [blame] | 485 | #ifndef CLIB_MARCH_VARIANT |
| 486 | |
| 487 | static clib_error_t * |
| 488 | ah_encrypt_init (vlib_main_t *vm) |
| 489 | { |
| 490 | ipsec_main_t *im = &ipsec_main; |
| 491 | |
| 492 | im->ah4_enc_fq_index = |
| 493 | vlib_frame_queue_main_init (ah4_encrypt_node.index, 0); |
| 494 | im->ah6_enc_fq_index = |
| 495 | vlib_frame_queue_main_init (ah6_encrypt_node.index, 0); |
| 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | VLIB_INIT_FUNCTION (ah_encrypt_init); |
| 501 | |
| 502 | #endif |
| 503 | |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 504 | /* |
| 505 | * fd.io coding-style-patch-verification: ON |
| 506 | * |
| 507 | * Local Variables: |
| 508 | * eval: (c-set-style "gnu") |
| 509 | * End: |
| 510 | */ |