diff options
author | “mukeshyadav1984” <mukyadav@cisco.com> | 2017-11-23 02:39:33 -0800 |
---|---|---|
committer | Damjan Marion <dmarion.lists@gmail.com> | 2017-11-28 12:26:30 +0000 |
commit | 430ac939d115b59e3f7f704645c6f88878223e1b (patch) | |
tree | ca5bbc6e7ab3c60316ed602f9a637ff423203f96 | |
parent | b3eeb6a5dd17627f56f5a9f299950c96f952e7a1 (diff) |
IPSec AH protocol enhancement in VPP native core
Change-Id: Iec5804d768485f4015bbf732d8d19ef2f24e6939
Signed-off-by: “mukeshyadav1984” <mukyadav@cisco.com>
-rw-r--r-- | src/vnet.am | 3 | ||||
-rw-r--r-- | src/vnet/ipsec/ah.h | 60 | ||||
-rw-r--r-- | src/vnet/ipsec/ah_decrypt.c | 343 | ||||
-rw-r--r-- | src/vnet/ipsec/ah_encrypt.c | 344 | ||||
-rw-r--r-- | src/vnet/ipsec/esp.h | 53 | ||||
-rw-r--r-- | src/vnet/ipsec/esp_decrypt.c | 11 | ||||
-rw-r--r-- | src/vnet/ipsec/esp_encrypt.c | 9 | ||||
-rw-r--r-- | src/vnet/ipsec/ipsec.c | 13 | ||||
-rw-r--r-- | src/vnet/ipsec/ipsec.h | 12 | ||||
-rw-r--r-- | src/vnet/ipsec/ipsec_cli.c | 19 | ||||
-rw-r--r-- | src/vnet/ipsec/ipsec_if_in.c | 8 | ||||
-rw-r--r-- | src/vnet/ipsec/ipsec_input.c | 63 | ||||
-rw-r--r-- | src/vnet/ipsec/ipsec_output.c | 9 |
13 files changed, 892 insertions, 55 deletions
diff --git a/src/vnet.am b/src/vnet.am index 7dc4b7df55f..72e67dcb298 100644 --- a/src/vnet.am +++ b/src/vnet.am @@ -425,6 +425,8 @@ libvnet_la_SOURCES += \ vnet/ipsec/esp_format.c \ vnet/ipsec/esp_encrypt.c \ vnet/ipsec/esp_decrypt.c \ + vnet/ipsec/ah_decrypt.c \ + vnet/ipsec/ah_encrypt.c \ vnet/ipsec/ikev2.c \ vnet/ipsec/ikev2_crypto.c \ vnet/ipsec/ikev2_cli.c \ @@ -441,6 +443,7 @@ libvnet_la_SOURCES += \ nobase_include_HEADERS += \ vnet/ipsec/ipsec.h \ vnet/ipsec/esp.h \ + vnet/ipsec/ah.h \ vnet/ipsec/ikev2.h \ vnet/ipsec/ikev2_priv.h \ vnet/ipsec/ipsec.api.h diff --git a/src/vnet/ipsec/ah.h b/src/vnet/ipsec/ah.h new file mode 100644 index 00000000000..37fc29a1ced --- /dev/null +++ b/src/vnet/ipsec/ah.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2015 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef __AH_H__ +#define __AH_H__ + + +#include <vnet/ip/ip.h> +#include <vnet/ipsec/ipsec.h> + +#include <openssl/hmac.h> +#include <openssl/rand.h> +#include <openssl/evp.h> + + +typedef struct +{ + unsigned char nexthdr; + unsigned char hdrlen; + unsigned short reserved; + unsigned int spi; + unsigned int seq_no; + unsigned char auth_data[0]; +} ah_header_t; + + +/* *INDENT-OFF* */ +typedef CLIB_PACKED (struct { + ip4_header_t ip4; + ah_header_t ah; +}) ip4_and_ah_header_t; +/* *INDENT-ON* */ + +/* *INDENT-OFF* */ +typedef CLIB_PACKED (struct { + ip6_header_t ip6; + ah_header_t ah; +}) ip6_and_ah_header_t; +/* *INDENT-ON* */ + +#endif /* __AH_H__ */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/ipsec/ah_decrypt.c b/src/vnet/ipsec/ah_decrypt.c new file mode 100644 index 00000000000..c487d82e34a --- /dev/null +++ b/src/vnet/ipsec/ah_decrypt.c @@ -0,0 +1,343 @@ +/* + * ah_decrypt.c : IPSec AH decrypt node + * + * Copyright (c) 2015 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <vnet/vnet.h> +#include <vnet/api_errno.h> +#include <vnet/ip/ip.h> + +#include <vnet/ipsec/ipsec.h> +#include <vnet/ipsec/esp.h> +#include <vnet/ipsec/ah.h> + +#define foreach_ah_decrypt_next \ +_(DROP, "error-drop") \ +_(IP4_INPUT, "ip4-input") \ +_(IP6_INPUT, "ip6-input") \ +_(IPSEC_GRE_INPUT, "ipsec-gre-input") + +#define _(v, s) AH_DECRYPT_NEXT_##v, +typedef enum +{ + foreach_ah_decrypt_next +#undef _ + AH_DECRYPT_N_NEXT, +} ah_decrypt_next_t; + + +#define foreach_ah_decrypt_error \ + _(RX_PKTS, "AH pkts received") \ + _(DECRYPTION_FAILED, "AH decryption failed") \ + _(INTEG_ERROR, "Integrity check failed") \ + _(REPLAY, "SA replayed packet") \ + _(NOT_IP, "Not IP packet (dropped)") + + +typedef enum +{ +#define _(sym,str) AH_DECRYPT_ERROR_##sym, + foreach_ah_decrypt_error +#undef _ + AH_DECRYPT_N_ERROR, +} ah_decrypt_error_t; + +static char *ah_decrypt_error_strings[] = { +#define _(sym,string) string, + foreach_ah_decrypt_error +#undef _ +}; + +typedef struct +{ + ipsec_integ_alg_t integ_alg; +} ah_decrypt_trace_t; + +/* packet trace format function */ +static u8 * +format_ah_decrypt_trace (u8 * s, va_list * args) +{ + CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); + CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); + ah_decrypt_trace_t *t = va_arg (*args, ah_decrypt_trace_t *); + + s = format (s, "ah: integrity %U", format_ipsec_integ_alg, t->integ_alg); + return s; +} + +static uword +ah_decrypt_node_fn (vlib_main_t * vm, + vlib_node_runtime_t * node, vlib_frame_t * from_frame) +{ + u32 n_left_from, *from, next_index, *to_next; + ipsec_main_t *im = &ipsec_main; + ipsec_proto_main_t *em = &ipsec_proto_main; + from = vlib_frame_vector_args (from_frame); + n_left_from = from_frame->n_vectors; + int icv_size = 0; + + next_index = node->cached_next_index; + + while (n_left_from > 0) + { + u32 n_left_to_next; + + vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); + + while (n_left_from > 0 && n_left_to_next > 0) + { + u32 i_bi0; + u32 next0; + vlib_buffer_t *i_b0; + ah_header_t *ah0; + ipsec_sa_t *sa0; + u32 sa_index0 = ~0; + u32 seq; + ip4_header_t *ih4 = 0, *oh4 = 0; + ip6_header_t *ih6 = 0, *oh6 = 0; + u8 tunnel_mode = 1; + u8 transport_ip6 = 0; + u8 ip_hdr_size = 0; + u8 tos = 0; + u8 ttl = 0; + + + i_bi0 = from[0]; + from += 1; + n_left_from -= 1; + n_left_to_next -= 1; + + next0 = AH_DECRYPT_NEXT_DROP; + + i_b0 = vlib_get_buffer (vm, i_bi0); + to_next[0] = i_bi0; + to_next += 1; + ih4 = vlib_buffer_get_current (i_b0); + ip_hdr_size = ip4_header_bytes (ih4); + ah0 = (ah_header_t *) ((u8 *) ih4 + ip_hdr_size); + + sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index; + sa0 = pool_elt_at_index (im->sad, sa_index0); + + seq = clib_host_to_net_u32 (ah0->seq_no); + /* anti-replay check */ + //TODO UT remaining + if (sa0->use_anti_replay) + { + int rv = 0; + + if (PREDICT_TRUE (sa0->use_esn)) + rv = esp_replay_check_esn (sa0, seq); + else + rv = esp_replay_check (sa0, seq); + + if (PREDICT_FALSE (rv)) + { + clib_warning ("anti-replay SPI %u seq %u", sa0->spi, seq); + vlib_node_increment_counter (vm, ah_decrypt_node.index, + AH_DECRYPT_ERROR_REPLAY, 1); + to_next[0] = i_bi0; + to_next += 1; + goto trace; + } + } + + + sa0->total_data_size += i_b0->current_length; + icv_size = + em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size; + if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE)) + { + u8 sig[64]; + u8 digest[64]; + memset (sig, 0, sizeof (sig)); + memset (digest, 0, sizeof (digest)); + u8 *icv = + vlib_buffer_get_current (i_b0) + ip_hdr_size + + sizeof (ah_header_t); + memcpy (digest, icv, icv_size); + memset (icv, 0, icv_size); + + if ((ih4->ip_version_and_header_length & 0xF0) == 0x40) + { + tos = ih4->tos; + ttl = ih4->ttl; + ih4->tos = 0; + ih4->ttl = 0; + ih4->checksum = 0; + ih4->flags_and_fragment_offset = 0; + } //TODO else part for IPv6 + hmac_calc (sa0->integ_alg, sa0->integ_key, sa0->integ_key_len, + (u8 *) ih4, i_b0->current_length, sig, sa0->use_esn, + sa0->seq_hi); + + if (PREDICT_FALSE (memcmp (digest, sig, icv_size))) + { + vlib_node_increment_counter (vm, ah_decrypt_node.index, + AH_DECRYPT_ERROR_INTEG_ERROR, + 1); + to_next[0] = i_bi0; + to_next += 1; + goto trace; + } + + //TODO UT remaining + if (PREDICT_TRUE (sa0->use_anti_replay)) + { + if (PREDICT_TRUE (sa0->use_esn)) + esp_replay_advance_esn (sa0, seq); + else + esp_replay_advance (sa0, seq); + } + + } + + + vlib_buffer_advance (i_b0, + ip_hdr_size + sizeof (ah_header_t) + icv_size); + i_b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; + + /* transport mode */ + if (PREDICT_FALSE (!sa0->is_tunnel && !sa0->is_tunnel_ip6)) + { + tunnel_mode = 0; + + if (PREDICT_TRUE + ((ih4->ip_version_and_header_length & 0xF0) != 0x40)) + { + if (PREDICT_TRUE + ((ih4->ip_version_and_header_length & 0xF0) == 0x60)) + transport_ip6 = 1; + else + { + clib_warning ("next header: 0x%x", ah0->nexthdr); + vlib_node_increment_counter (vm, ah_decrypt_node.index, + AH_DECRYPT_ERROR_NOT_IP, + 1); + goto trace; + } + } + } + + if (PREDICT_TRUE (tunnel_mode)) + { + if (PREDICT_TRUE (ah0->nexthdr == IP_PROTOCOL_IP_IN_IP)) + next0 = AH_DECRYPT_NEXT_IP4_INPUT; + else if (ah0->nexthdr == IP_PROTOCOL_IPV6) + next0 = AH_DECRYPT_NEXT_IP6_INPUT; + else + { + clib_warning ("next header: 0x%x", ah0->nexthdr); + vlib_node_increment_counter (vm, ah_decrypt_node.index, + AH_DECRYPT_ERROR_DECRYPTION_FAILED, + 1); + goto trace; + } + } + /* transport mode */ + else + { + if (PREDICT_FALSE (transport_ip6)) + { + ih6 = + (ip6_header_t *) (i_b0->data + + sizeof (ethernet_header_t)); + vlib_buffer_advance (i_b0, -sizeof (ip6_header_t)); + oh6 = vlib_buffer_get_current (i_b0); + memmove (oh6, ih6, sizeof (ip6_header_t)); + + next0 = AH_DECRYPT_NEXT_IP6_INPUT; + oh6->protocol = ah0->nexthdr; + oh6->payload_length = + clib_host_to_net_u16 (vlib_buffer_length_in_chain + (vm, i_b0) - sizeof (ip6_header_t)); + } + else + { + vlib_buffer_advance (i_b0, -sizeof (ip4_header_t)); + oh4 = vlib_buffer_get_current (i_b0); + memmove (oh4, ih4, sizeof (ip4_header_t)); + + next0 = AH_DECRYPT_NEXT_IP4_INPUT; + oh4->ip_version_and_header_length = 0x45; + oh4->fragment_id = 0; + oh4->flags_and_fragment_offset = 0; + oh4->protocol = ah0->nexthdr; + oh4->length = + clib_host_to_net_u16 (vlib_buffer_length_in_chain + (vm, i_b0)); + oh4->ttl = ttl; + oh4->tos = tos; + oh4->checksum = ip4_header_checksum (oh4); + } + } + + /* for IPSec-GRE tunnel next node is ipsec-gre-input */ + if (PREDICT_FALSE + ((vnet_buffer (i_b0)->ipsec.flags) & + IPSEC_FLAG_IPSEC_GRE_TUNNEL)) + next0 = AH_DECRYPT_NEXT_IPSEC_GRE_INPUT; + + + vnet_buffer (i_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0; + trace: + if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED)) + { + i_b0->flags |= VLIB_BUFFER_IS_TRACED; + ah_decrypt_trace_t *tr = + vlib_add_trace (vm, node, i_b0, sizeof (*tr)); + tr->integ_alg = sa0->integ_alg; + } + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, + n_left_to_next, i_bi0, next0); + } + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + } + vlib_node_increment_counter (vm, ah_decrypt_node.index, + AH_DECRYPT_ERROR_RX_PKTS, + from_frame->n_vectors); + + return from_frame->n_vectors; +} + + +/* *INDENT-OFF* */ +VLIB_REGISTER_NODE (ah_decrypt_node) = { + .function = ah_decrypt_node_fn, + .name = "ah-decrypt", + .vector_size = sizeof (u32), + .format_trace = format_ah_decrypt_trace, + .type = VLIB_NODE_TYPE_INTERNAL, + + .n_errors = ARRAY_LEN(ah_decrypt_error_strings), + .error_strings = ah_decrypt_error_strings, + + .n_next_nodes = AH_DECRYPT_N_NEXT, + .next_nodes = { +#define _(s,n) [AH_DECRYPT_NEXT_##s] = n, + foreach_ah_decrypt_next +#undef _ + }, +}; +/* *INDENT-ON* */ + +VLIB_NODE_FUNCTION_MULTIARCH (ah_decrypt_node, ah_decrypt_node_fn) +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/ipsec/ah_encrypt.c b/src/vnet/ipsec/ah_encrypt.c new file mode 100644 index 00000000000..6619d872013 --- /dev/null +++ b/src/vnet/ipsec/ah_encrypt.c @@ -0,0 +1,344 @@ +/* + * ah_encrypt.c : IPSec AH encrypt node + * + * Copyright (c) 2015 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <vnet/vnet.h> +#include <vnet/api_errno.h> +#include <vnet/ip/ip.h> + +#include <vnet/ipsec/ipsec.h> +#include <vnet/ipsec/esp.h> +#include <vnet/ipsec/ah.h> + +#define foreach_ah_encrypt_next \ +_(DROP, "error-drop") \ +_(IP4_LOOKUP, "ip4-lookup") \ +_(IP6_LOOKUP, "ip6-lookup") \ +_(INTERFACE_OUTPUT, "interface-output") + +#define _(v, s) AH_ENCRYPT_NEXT_##v, +typedef enum +{ + foreach_ah_encrypt_next +#undef _ + AH_ENCRYPT_N_NEXT, +} ah_encrypt_next_t; + +#define foreach_ah_encrypt_error \ + _(RX_PKTS, "AH pkts received") \ + _(SEQ_CYCLED, "sequence number cycled") + + +typedef enum +{ +#define _(sym,str) AH_ENCRYPT_ERROR_##sym, + foreach_ah_encrypt_error +#undef _ + AH_ENCRYPT_N_ERROR, +} ah_encrypt_error_t; + +static char *ah_encrypt_error_strings[] = { +#define _(sym,string) string, + foreach_ah_encrypt_error +#undef _ +}; + +vlib_node_registration_t ah_encrypt_node; + +typedef struct +{ + u32 spi; + u32 seq; + ipsec_integ_alg_t integ_alg; +} ah_encrypt_trace_t; + +/* packet trace format function */ +static u8 * +format_ah_encrypt_trace (u8 * s, va_list * args) +{ + CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); + CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); + ah_encrypt_trace_t *t = va_arg (*args, ah_encrypt_trace_t *); + + s = format (s, "ah: spi %u seq %u integrity %U", + t->spi, t->seq, format_ipsec_integ_alg, t->integ_alg); + return s; +} + +static uword +ah_encrypt_node_fn (vlib_main_t * vm, + vlib_node_runtime_t * node, vlib_frame_t * from_frame) +{ + u32 n_left_from, *from, *to_next = 0, next_index; + int icv_size = 0; + from = vlib_frame_vector_args (from_frame); + n_left_from = from_frame->n_vectors; + ipsec_main_t *im = &ipsec_main; + ipsec_proto_main_t *em = &ipsec_proto_main; + next_index = node->cached_next_index; + + while (n_left_from > 0) + { + u32 n_left_to_next; + + vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); + + while (n_left_from > 0 && n_left_to_next > 0) + { + u32 i_bi0, next0; + vlib_buffer_t *i_b0 = 0; + u32 sa_index0; + ipsec_sa_t *sa0; + ip4_and_ah_header_t *ih0, *oh0 = 0; + ip6_and_ah_header_t *ih6_0, *oh6_0 = 0; + u8 is_ipv6; + u8 ip_hdr_size; + u8 next_hdr_type; + u8 transport_mode = 0; + u8 tos = 0; + u8 ttl = 0; + + i_bi0 = from[0]; + from += 1; + n_left_from -= 1; + n_left_to_next -= 1; + next0 = AH_ENCRYPT_NEXT_DROP; + + i_b0 = vlib_get_buffer (vm, i_bi0); + to_next[0] = i_bi0; + to_next += 1; + sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index; + sa0 = pool_elt_at_index (im->sad, sa_index0); + + if (PREDICT_FALSE (esp_seq_advance (sa0))) + { + clib_warning ("sequence number counter has cycled SPI %u", + sa0->spi); + vlib_node_increment_counter (vm, ah_encrypt_node.index, + AH_ENCRYPT_ERROR_SEQ_CYCLED, 1); + //TODO need to confirm if below is needed + to_next[0] = i_bi0; + to_next += 1; + goto trace; + } + + + sa0->total_data_size += i_b0->current_length; + + ssize_t adv; + ih0 = vlib_buffer_get_current (i_b0); + ttl = ih0->ip4.ttl; + tos = ih0->ip4.tos; + + is_ipv6 = (ih0->ip4.ip_version_and_header_length & 0xF0) == 0x60; + /* is ipv6 */ + if (PREDICT_TRUE (sa0->is_tunnel)) + { + if (PREDICT_TRUE (!is_ipv6)) + adv = -sizeof (ip4_and_ah_header_t); + else + adv = -sizeof (ip6_and_ah_header_t); + } + else + { + adv = -sizeof (ah_header_t); + } + + icv_size = + em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size; + /*transport mode save the eth header before it is overwritten */ + if (PREDICT_FALSE (!sa0->is_tunnel)) + { + ethernet_header_t *ieh0 = (ethernet_header_t *) + ((u8 *) vlib_buffer_get_current (i_b0) - + sizeof (ethernet_header_t)); + ethernet_header_t *oeh0 = + (ethernet_header_t *) ((u8 *) ieh0 + (adv - icv_size)); + clib_memcpy (oeh0, ieh0, sizeof (ethernet_header_t)); + } + + vlib_buffer_advance (i_b0, adv - icv_size); + + /* is ipv6 */ + if (PREDICT_FALSE (is_ipv6)) + { + ih6_0 = (ip6_and_ah_header_t *) ih0; + ip_hdr_size = sizeof (ip6_header_t); + oh6_0 = vlib_buffer_get_current (i_b0); + + if (PREDICT_TRUE (sa0->is_tunnel)) + { + next_hdr_type = IP_PROTOCOL_IPV6; + oh6_0->ip6.ip_version_traffic_class_and_flow_label = + ih6_0->ip6.ip_version_traffic_class_and_flow_label; + } + else + { + next_hdr_type = ih6_0->ip6.protocol; + memmove (oh6_0, ih6_0, sizeof (ip6_header_t)); + } + + oh6_0->ip6.protocol = IP_PROTOCOL_IPSEC_AH; + oh6_0->ip6.hop_limit = 254; + oh6_0->ah.spi = clib_net_to_host_u32 (sa0->spi); + oh6_0->ah.seq_no = clib_net_to_host_u32 (sa0->seq); + oh6_0->ip6.payload_length = + clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, i_b0) - + sizeof (ip6_header_t)); + } + else + { + ip_hdr_size = sizeof (ip4_header_t); + oh0 = vlib_buffer_get_current (i_b0); + memset (oh0, 0, sizeof (ip4_and_ah_header_t)); + + if (PREDICT_TRUE (sa0->is_tunnel)) + { + next_hdr_type = IP_PROTOCOL_IP_IN_IP; + } + else + { + next_hdr_type = ih0->ip4.protocol; + memmove (oh0, ih0, sizeof (ip4_header_t)); + } + + oh0->ip4.length = + clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, i_b0)); + oh0->ip4.ip_version_and_header_length = 0x45; + oh0->ip4.fragment_id = 0; + oh0->ip4.flags_and_fragment_offset = 0; + oh0->ip4.ttl = 0; + oh0->ip4.tos = 0; + oh0->ip4.protocol = IP_PROTOCOL_IPSEC_AH; + oh0->ah.spi = clib_net_to_host_u32 (sa0->spi); + oh0->ah.seq_no = clib_net_to_host_u32 (sa0->seq); + oh0->ip4.checksum = 0; + oh0->ah.nexthdr = next_hdr_type; + oh0->ah.hdrlen = 4; + } + + + + if (PREDICT_TRUE + (!is_ipv6 && sa0->is_tunnel && !sa0->is_tunnel_ip6)) + { + oh0->ip4.src_address.as_u32 = sa0->tunnel_src_addr.ip4.as_u32; + oh0->ip4.dst_address.as_u32 = sa0->tunnel_dst_addr.ip4.as_u32; + + next0 = AH_ENCRYPT_NEXT_IP4_LOOKUP; + vnet_buffer (i_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0; + } + else if (is_ipv6 && sa0->is_tunnel && sa0->is_tunnel_ip6) + { + oh6_0->ip6.src_address.as_u64[0] = + sa0->tunnel_src_addr.ip6.as_u64[0]; + oh6_0->ip6.src_address.as_u64[1] = + sa0->tunnel_src_addr.ip6.as_u64[1]; + oh6_0->ip6.dst_address.as_u64[0] = + sa0->tunnel_dst_addr.ip6.as_u64[0]; + oh6_0->ip6.dst_address.as_u64[1] = + sa0->tunnel_dst_addr.ip6.as_u64[1]; + + next0 = AH_ENCRYPT_NEXT_IP6_LOOKUP; + vnet_buffer (i_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0; + } + else + { + transport_mode = 1; + next0 = AH_ENCRYPT_NEXT_INTERFACE_OUTPUT; + } + + u8 sig[64]; + memset (sig, 0, sizeof (sig)); + u8 *digest = NULL; + { + digest = vlib_buffer_get_current (i_b0) + ip_hdr_size + icv_size; + memset (digest, 0, icv_size); + } + + hmac_calc (sa0->integ_alg, sa0->integ_key, + sa0->integ_key_len, + (u8 *) vlib_buffer_get_current (i_b0), + i_b0->current_length, sig, sa0->use_esn, sa0->seq_hi); + + memcpy (digest, (char *) &sig[0], 12); + + if (PREDICT_FALSE (is_ipv6)) + { + } + else + { + oh0->ip4.ttl = ttl; + oh0->ip4.tos = tos; + oh0->ip4.checksum = ip4_header_checksum (&oh0->ip4); + } + + if (transport_mode) + vlib_buffer_advance (i_b0, -sizeof (ethernet_header_t));; + + trace: + if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED)) + { + i_b0->flags |= VLIB_BUFFER_IS_TRACED; + ah_encrypt_trace_t *tr = + vlib_add_trace (vm, node, i_b0, sizeof (*tr)); + tr->spi = sa0->spi; + tr->seq = sa0->seq - 1; + tr->integ_alg = sa0->integ_alg; + } + + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, + to_next, n_left_to_next, i_bi0, + next0); + } + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + } + vlib_node_increment_counter (vm, ah_encrypt_node.index, + AH_ENCRYPT_ERROR_RX_PKTS, + from_frame->n_vectors); + + return from_frame->n_vectors; +} + + +/* *INDENT-OFF* */ +VLIB_REGISTER_NODE (ah_encrypt_node) = { + .function = ah_encrypt_node_fn, + .name = "ah-encrypt", + .vector_size = sizeof (u32), + .format_trace = format_ah_encrypt_trace, + .type = VLIB_NODE_TYPE_INTERNAL, + + .n_errors = ARRAY_LEN(ah_encrypt_error_strings), + .error_strings = ah_encrypt_error_strings, + + .n_next_nodes = AH_ENCRYPT_N_NEXT, + .next_nodes = { +#define _(s,n) [AH_ENCRYPT_NEXT_##s] = n, + foreach_ah_encrypt_next +#undef _ + }, +}; +/* *INDENT-ON* */ + +VLIB_NODE_FUNCTION_MULTIARCH (ah_encrypt_node, ah_encrypt_node_fn) +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/ipsec/esp.h b/src/vnet/ipsec/esp.h index d9ab1d855a8..82e3c961e1a 100644 --- a/src/vnet/ipsec/esp.h +++ b/src/vnet/ipsec/esp.h @@ -52,13 +52,13 @@ typedef CLIB_PACKED (struct { typedef struct { const EVP_CIPHER *type; -} esp_crypto_alg_t; +} ipsec_proto_main_crypto_alg_t; typedef struct { const EVP_MD *md; u8 trunc_size; -} esp_integ_alg_t; +} ipsec_proto_main_integ_alg_t; typedef struct { @@ -83,16 +83,16 @@ typedef struct ipsec_crypto_alg_t last_encrypt_alg; ipsec_crypto_alg_t last_decrypt_alg; ipsec_integ_alg_t last_integ_alg; -} esp_main_per_thread_data_t; +} ipsec_proto_main_per_thread_data_t; typedef struct { - esp_crypto_alg_t *esp_crypto_algs; - esp_integ_alg_t *esp_integ_algs; - esp_main_per_thread_data_t *per_thread_data; -} esp_main_t; + ipsec_proto_main_crypto_alg_t *ipsec_proto_main_crypto_algs; + ipsec_proto_main_integ_alg_t *ipsec_proto_main_integ_algs; + ipsec_proto_main_per_thread_data_t *per_thread_data; +} ipsec_proto_main_t; -extern esp_main_t esp_main; +extern ipsec_proto_main_t ipsec_proto_main; #define ESP_WINDOW_SIZE (64) #define ESP_SEQ_MAX (4294967295UL) @@ -244,38 +244,41 @@ esp_seq_advance (ipsec_sa_t * sa) } always_inline void -esp_init () +ipsec_proto_init () { - esp_main_t *em = &esp_main; + ipsec_proto_main_t *em = &ipsec_proto_main; vlib_thread_main_t *tm = vlib_get_thread_main (); memset (em, 0, sizeof (em[0])); - vec_validate (em->esp_crypto_algs, IPSEC_CRYPTO_N_ALG - 1); - em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].type = EVP_aes_128_cbc (); - em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].type = EVP_aes_192_cbc (); - em->esp_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].type = EVP_aes_256_cbc (); + vec_validate (em->ipsec_proto_main_crypto_algs, IPSEC_CRYPTO_N_ALG - 1); + em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].type = + EVP_aes_128_cbc (); + em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].type = + EVP_aes_192_cbc (); + em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].type = + EVP_aes_256_cbc (); - vec_validate (em->esp_integ_algs, IPSEC_INTEG_N_ALG - 1); - esp_integ_alg_t *i; + vec_validate (em->ipsec_proto_main_integ_algs, IPSEC_INTEG_N_ALG - 1); + ipsec_proto_main_integ_alg_t *i; - i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA1_96]; + i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA1_96]; i->md = EVP_sha1 (); i->trunc_size = 12; - i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_96]; + i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_256_96]; i->md = EVP_sha256 (); i->trunc_size = 12; - i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_256_128]; + i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_256_128]; i->md = EVP_sha256 (); i->trunc_size = 16; - i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_384_192]; + i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_384_192]; i->md = EVP_sha384 (); i->trunc_size = 24; - i = &em->esp_integ_algs[IPSEC_INTEG_ALG_SHA_512_256]; + i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_512_256]; i->md = EVP_sha512 (); i->trunc_size = 32; @@ -303,7 +306,7 @@ hmac_calc (ipsec_integ_alg_t alg, int key_len, u8 * data, int data_len, u8 * signature, u8 use_esn, u32 seq_hi) { - esp_main_t *em = &esp_main; + ipsec_proto_main_t *em = &ipsec_proto_main; u32 thread_index = vlib_get_thread_index (); #if OPENSSL_VERSION_NUMBER >= 0x10100000L HMAC_CTX *ctx = em->per_thread_data[thread_index].hmac_ctx; @@ -315,12 +318,12 @@ hmac_calc (ipsec_integ_alg_t alg, ASSERT (alg < IPSEC_INTEG_N_ALG); - if (PREDICT_FALSE (em->esp_integ_algs[alg].md == 0)) + if (PREDICT_FALSE (em->ipsec_proto_main_integ_algs[alg].md == 0)) return 0; if (PREDICT_FALSE (alg != em->per_thread_data[thread_index].last_integ_alg)) { - md = em->esp_integ_algs[alg].md; + md = em->ipsec_proto_main_integ_algs[alg].md; em->per_thread_data[thread_index].last_integ_alg = alg; } @@ -332,7 +335,7 @@ hmac_calc (ipsec_integ_alg_t alg, HMAC_Update (ctx, (u8 *) & seq_hi, sizeof (seq_hi)); HMAC_Final (ctx, signature, &len); - return em->esp_integ_algs[alg].trunc_size; + return em->ipsec_proto_main_integ_algs[alg].trunc_size; } #endif /* __ESP_H__ */ diff --git a/src/vnet/ipsec/esp_decrypt.c b/src/vnet/ipsec/esp_decrypt.c index 81ebbe55301..fac40358f16 100644 --- a/src/vnet/ipsec/esp_decrypt.c +++ b/src/vnet/ipsec/esp_decrypt.c @@ -84,7 +84,7 @@ always_inline void esp_decrypt_aes_cbc (ipsec_crypto_alg_t alg, u8 * in, u8 * out, size_t in_len, u8 * key, u8 * iv) { - esp_main_t *em = &esp_main; + ipsec_proto_main_t *em = &ipsec_proto_main; u32 thread_index = vlib_get_thread_index (); #if OPENSSL_VERSION_NUMBER >= 0x10100000L EVP_CIPHER_CTX *ctx = em->per_thread_data[thread_index].decrypt_ctx; @@ -96,13 +96,13 @@ esp_decrypt_aes_cbc (ipsec_crypto_alg_t alg, ASSERT (alg < IPSEC_CRYPTO_N_ALG); - if (PREDICT_FALSE (em->esp_crypto_algs[alg].type == 0)) + if (PREDICT_FALSE (em->ipsec_proto_main_crypto_algs[alg].type == 0)) return; if (PREDICT_FALSE (alg != em->per_thread_data[thread_index].last_decrypt_alg)) { - cipher = em->esp_crypto_algs[alg].type; + cipher = em->ipsec_proto_main_crypto_algs[alg].type; em->per_thread_data[thread_index].last_decrypt_alg = alg; } @@ -118,7 +118,7 @@ esp_decrypt_node_fn (vlib_main_t * vm, { u32 n_left_from, *from, next_index, *to_next; ipsec_main_t *im = &ipsec_main; - esp_main_t *em = &esp_main; + ipsec_proto_main_t *em = &ipsec_proto_main; u32 *recycle = 0; from = vlib_frame_vector_args (from_frame); n_left_from = from_frame->n_vectors; @@ -200,7 +200,8 @@ esp_decrypt_node_fn (vlib_main_t * vm, if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE)) { u8 sig[64]; - int icv_size = em->esp_integ_algs[sa0->integ_alg].trunc_size; + int icv_size = + em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size; memset (sig, 0, sizeof (sig)); u8 *icv = vlib_buffer_get_current (i_b0) + i_b0->current_length - diff --git a/src/vnet/ipsec/esp_encrypt.c b/src/vnet/ipsec/esp_encrypt.c index 7bf1256acca..3e196b34869 100644 --- a/src/vnet/ipsec/esp_encrypt.c +++ b/src/vnet/ipsec/esp_encrypt.c @@ -22,7 +22,7 @@ #include <vnet/ipsec/ipsec.h> #include <vnet/ipsec/esp.h> -esp_main_t esp_main; +ipsec_proto_main_t ipsec_proto_main; #define foreach_esp_encrypt_next \ _(DROP, "error-drop") \ @@ -88,7 +88,7 @@ always_inline void esp_encrypt_aes_cbc (ipsec_crypto_alg_t alg, u8 * in, u8 * out, size_t in_len, u8 * key, u8 * iv) { - esp_main_t *em = &esp_main; + ipsec_proto_main_t *em = &ipsec_proto_main; u32 thread_index = vlib_get_thread_index (); #if OPENSSL_VERSION_NUMBER >= 0x10100000L EVP_CIPHER_CTX *ctx = em->per_thread_data[thread_index].encrypt_ctx; @@ -100,13 +100,14 @@ esp_encrypt_aes_cbc (ipsec_crypto_alg_t alg, ASSERT (alg < IPSEC_CRYPTO_N_ALG); - if (PREDICT_FALSE (em->esp_crypto_algs[alg].type == IPSEC_CRYPTO_ALG_NONE)) + if (PREDICT_FALSE + (em->ipsec_proto_main_crypto_algs[alg].type == IPSEC_CRYPTO_ALG_NONE)) return; if (PREDICT_FALSE (alg != em->per_thread_data[thread_index].last_encrypt_alg)) { - cipher = em->esp_crypto_algs[alg].type; + cipher = em->ipsec_proto_main_crypto_algs[alg].type; em->per_thread_data[thread_index].last_encrypt_alg = alg; } diff --git a/src/vnet/ipsec/ipsec.c b/src/vnet/ipsec/ipsec.c index cd05c1bb9bf..ab3c83b02f5 100644 --- a/src/vnet/ipsec/ipsec.c +++ b/src/vnet/ipsec/ipsec.c @@ -23,6 +23,7 @@ #include <vnet/ipsec/ipsec.h> #include <vnet/ipsec/ikev2.h> #include <vnet/ipsec/esp.h> +#include <vnet/ipsec/ah.h> ipsec_main_t ipsec_main; @@ -567,8 +568,18 @@ ipsec_init (vlib_main_t * vm) ASSERT (node); im->esp_decrypt_node_index = node->index; + node = vlib_get_node_by_name (vm, (u8 *) "ah-encrypt"); + ASSERT (node); + im->ah_encrypt_node_index = node->index; + + node = vlib_get_node_by_name (vm, (u8 *) "ah-decrypt"); + ASSERT (node); + im->ah_decrypt_node_index = node->index; + im->esp_encrypt_next_index = IPSEC_OUTPUT_NEXT_ESP_ENCRYPT; im->esp_decrypt_next_index = IPSEC_INPUT_NEXT_ESP_DECRYPT; + im->ah_encrypt_next_index = IPSEC_OUTPUT_NEXT_AH_ENCRYPT; + im->ah_decrypt_next_index = IPSEC_INPUT_NEXT_AH_DECRYPT; im->cb.check_support_cb = ipsec_check_support; @@ -578,7 +589,7 @@ ipsec_init (vlib_main_t * vm) if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init))) return error; - esp_init (); + ipsec_proto_init (); if ((error = ikev2_init (vm))) return error; diff --git a/src/vnet/ipsec/ipsec.h b/src/vnet/ipsec/ipsec.h index 1572e5501e2..e59cfcc4520 100644 --- a/src/vnet/ipsec/ipsec.h +++ b/src/vnet/ipsec/ipsec.h @@ -23,7 +23,8 @@ #define foreach_ipsec_output_next \ _(DROP, "error-drop") \ -_(ESP_ENCRYPT, "esp-encrypt") +_(ESP_ENCRYPT, "esp-encrypt") \ +_(AH_ENCRYPT, "ah-encrypt") #define _(v, s) IPSEC_OUTPUT_NEXT_##v, typedef enum @@ -36,7 +37,8 @@ typedef enum #define foreach_ipsec_input_next \ _(DROP, "error-drop") \ -_(ESP_DECRYPT, "esp-decrypt") +_(ESP_DECRYPT, "esp-decrypt") \ +_(AH_DECRYPT, "ah-decrypt") #define _(v, s) IPSEC_INPUT_NEXT_##v, typedef enum @@ -278,9 +280,13 @@ typedef struct u32 error_drop_node_index; u32 esp_encrypt_node_index; u32 esp_decrypt_node_index; + u32 ah_encrypt_node_index; + u32 ah_decrypt_node_index; /* next node indeces */ u32 esp_encrypt_next_index; u32 esp_decrypt_next_index; + u32 ah_encrypt_next_index; + u32 ah_decrypt_next_index; /* callbacks */ ipsec_main_callbacks_t cb; @@ -290,6 +296,8 @@ extern ipsec_main_t ipsec_main; extern vlib_node_registration_t esp_encrypt_node; extern vlib_node_registration_t esp_decrypt_node; +extern vlib_node_registration_t ah_encrypt_node; +extern vlib_node_registration_t ah_decrypt_node; extern vlib_node_registration_t ipsec_if_output_node; extern vlib_node_registration_t ipsec_if_input_node; diff --git a/src/vnet/ipsec/ipsec_cli.c b/src/vnet/ipsec/ipsec_cli.c index 0e034402b5c..711403ff81a 100644 --- a/src/vnet/ipsec/ipsec_cli.c +++ b/src/vnet/ipsec/ipsec_cli.c @@ -96,9 +96,7 @@ ipsec_sa_add_del_command_fn (vlib_main_t * vm, sa.protocol = IPSEC_PROTOCOL_ESP; else if (unformat (line_input, "ah")) { - //sa.protocol = IPSEC_PROTOCOL_AH; - error = clib_error_return (0, "unsupported security protocol 'AH'"); - goto done; + sa.protocol = IPSEC_PROTOCOL_AH; } else if (unformat (line_input, "crypto-key %U", unformat_hex_string, &ck)) @@ -339,6 +337,21 @@ ipsec_policy_add_del_command_fn (vlib_main_t * vm, } } + /* Check if SA is for IPv6/AH which is not supported. Return error if TRUE. */ + if (p.sa_id) + { + uword *p1; + ipsec_main_t *im = &ipsec_main; + ipsec_sa_t *sa = 0; + p1 = hash_get (im->sa_index_by_sa_id, p.sa_id); + sa = pool_elt_at_index (im->sad, p1[0]); + if (sa && sa->protocol == IPSEC_PROTOCOL_AH && is_add && p.is_ipv6) + { + error = clib_error_return (0, "AH not supported for IPV6: '%U'", + format_unformat_error, line_input); + goto done; + } + } ipsec_add_del_policy (vm, &p, is_add); if (is_ip_any) { diff --git a/src/vnet/ipsec/ipsec_if_in.c b/src/vnet/ipsec/ipsec_if_in.c index b0761224fbb..2627e7c1ca6 100644 --- a/src/vnet/ipsec/ipsec_if_in.c +++ b/src/vnet/ipsec/ipsec_if_in.c @@ -65,7 +65,7 @@ ipsec_if_input_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node, ipsec_main_t *im = &ipsec_main; vnet_main_t *vnm = im->vnet_main; vnet_interface_main_t *vim = &vnm->interface_main; - esp_main_t *em = &esp_main; + ipsec_proto_main_t *em = &ipsec_proto_main; u32 *from, *to_next = 0, next_index; u32 n_left_from, last_sw_if_index = ~0; u32 thread_index = vlib_get_thread_index (); @@ -130,7 +130,9 @@ ipsec_if_input_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node, else { sa0 = pool_elt_at_index (im->sad, t->input_sa_index); - icv_len = em->esp_integ_algs[sa0->integ_alg].trunc_size; + icv_len = + em->ipsec_proto_main_integ_algs[sa0-> + integ_alg].trunc_size; /* length = packet length - ESP/tunnel overhead */ n_bytes -= n_packets * (sizeof (ip4_header_t) + @@ -178,7 +180,7 @@ ipsec_if_input_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node, if (last_t) { sa0 = pool_elt_at_index (im->sad, last_t->input_sa_index); - icv_len = em->esp_integ_algs[sa0->integ_alg].trunc_size; + icv_len = em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size; n_bytes -= n_packets * (sizeof (ip4_header_t) + sizeof (esp_header_t) + sizeof (esp_footer_t) + 16 /* aes-cbc IV */ + diff --git a/src/vnet/ipsec/ipsec_input.c b/src/vnet/ipsec/ipsec_input.c index f27058bb778..9aa5654c9da 100644 --- a/src/vnet/ipsec/ipsec_input.c +++ b/src/vnet/ipsec/ipsec_input.c @@ -22,6 +22,7 @@ #include <vnet/ipsec/ipsec.h> #include <vnet/ipsec/esp.h> +#include <vnet/ipsec/ah.h> #define foreach_ipsec_input_error \ _(RX_PKTS, "IPSEC pkts received") \ @@ -194,6 +195,7 @@ ipsec_input_ip4_node_fn (vlib_main_t * vm, vlib_buffer_t *b0; ip4_header_t *ip0; esp_header_t *esp0; + ah_header_t *ah0; ip4_ipsec_config_t *c0; ipsec_spd_t *spd0; ipsec_policy_t *p0 = 0; @@ -213,7 +215,6 @@ ipsec_input_ip4_node_fn (vlib_main_t * vm, spd0 = pool_elt_at_index (im->spds, c0->spd_index); ip0 = vlib_buffer_get_current (b0); - esp0 = (esp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0)); if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_ESP)) { @@ -226,6 +227,7 @@ ipsec_input_ip4_node_fn (vlib_main_t * vm, clib_net_to_host_u16 (ip0->length), spd0->id); #endif + esp0 = (esp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0)); p0 = ipsec_input_protect_policy_match (spd0, clib_net_to_host_u32 (ip0->src_address. @@ -246,21 +248,60 @@ ipsec_input_ip4_node_fn (vlib_main_t * vm, vlib_buffer_advance (b0, ip4_header_bytes (ip0)); goto trace0; } + + /* FIXME bypass and discard */ + trace0: + if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) + { + ipsec_input_trace_t *tr = + vlib_add_trace (vm, node, b0, sizeof (*tr)); + if (ip0->protocol == IP_PROTOCOL_IPSEC_ESP) + { + if (p0) + tr->sa_id = p0->sa_id; + tr->spi = clib_host_to_net_u32 (esp0->spi); + tr->seq = clib_host_to_net_u32 (esp0->seq); + } + } + } - /* FIXME bypass and discard */ - trace0: - if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) + if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_AH)) { - ipsec_input_trace_t *tr = - vlib_add_trace (vm, node, b0, sizeof (*tr)); - if (ip0->protocol == IP_PROTOCOL_IPSEC_ESP) + ah0 = (ah_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0)); + p0 = ipsec_input_protect_policy_match (spd0, + clib_net_to_host_u32 + (ip0->src_address. + as_u32), + clib_net_to_host_u32 + (ip0->dst_address. + as_u32), + clib_net_to_host_u32 + (ah0->spi)); + + if (PREDICT_TRUE (p0 != 0)) { - if (p0) - tr->sa_id = p0->sa_id; - tr->spi = clib_host_to_net_u32 (esp0->spi); - tr->seq = clib_host_to_net_u32 (esp0->seq); + p0->counter.packets++; + p0->counter.bytes += clib_net_to_host_u16 (ip0->length); + vnet_buffer (b0)->ipsec.sad_index = p0->sa_index; + vnet_buffer (b0)->ipsec.flags = 0; + next0 = im->ah_decrypt_next_index; + goto trace1; + } + /* FIXME bypass and discard */ + trace1: + if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) + { + ipsec_input_trace_t *tr = + vlib_add_trace (vm, node, b0, sizeof (*tr)); + if (ip0->protocol == IP_PROTOCOL_IPSEC_ESP) + { + if (p0) + tr->sa_id = p0->sa_id; + tr->spi = clib_host_to_net_u32 (ah0->spi); + tr->seq = clib_host_to_net_u32 (ah0->seq_no); + } } } diff --git a/src/vnet/ipsec/ipsec_output.c b/src/vnet/ipsec/ipsec_output.c index 1b8070d651a..e86292c0d17 100644 --- a/src/vnet/ipsec/ipsec_output.c +++ b/src/vnet/ipsec/ipsec_output.c @@ -270,8 +270,15 @@ ipsec_output_inline (vlib_main_t * vm, vlib_node_runtime_t * node, { if (p0->policy == IPSEC_POLICY_ACTION_PROTECT) { + u32 sa_index = 0; + ipsec_sa_t *sa = 0; nc_protect++; - next_node_index = im->esp_encrypt_node_index; + sa_index = ipsec_get_sa_index_by_sa_id (p0->sa_id); + sa = pool_elt_at_index (im->sad, sa_index); + if (sa->protocol == IPSEC_PROTOCOL_ESP) + next_node_index = im->esp_encrypt_node_index; + else + next_node_index = im->ah_encrypt_node_index; vnet_buffer (b0)->ipsec.sad_index = p0->sa_index; vlib_buffer_advance (b0, iph_offset); p0->counter.packets++; |