Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * decap.c : IPSec tunnel decapsulation |
| 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> |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 21 | #include <vnet/feature/feature.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 22 | |
| 23 | #include <vnet/ipsec/ipsec.h> |
| 24 | #include <vnet/ipsec/esp.h> |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 25 | #include <vnet/ipsec/ah.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 26 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 27 | #define foreach_ipsec_input_error \ |
| 28 | _(RX_PKTS, "IPSEC pkts received") \ |
| 29 | _(DECRYPTION_FAILED, "IPSEC decryption failed") |
| 30 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 31 | typedef enum |
| 32 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 33 | #define _(sym,str) IPSEC_INPUT_ERROR_##sym, |
| 34 | foreach_ipsec_input_error |
| 35 | #undef _ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 36 | IPSEC_INPUT_N_ERROR, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 37 | } ipsec_input_error_t; |
| 38 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 39 | static char *ipsec_input_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 40 | #define _(sym,string) string, |
| 41 | foreach_ipsec_input_error |
| 42 | #undef _ |
| 43 | }; |
| 44 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 45 | typedef struct |
| 46 | { |
Pierre Pfister | 6221927 | 2018-11-26 09:29:00 +0100 | [diff] [blame] | 47 | u32 spd; |
Matus Fabian | 5539a07 | 2016-09-07 05:57:09 -0700 | [diff] [blame] | 48 | u32 sa_id; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | u32 spi; |
| 50 | u32 seq; |
| 51 | } ipsec_input_trace_t; |
| 52 | |
| 53 | /* packet trace format function */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 54 | static u8 * |
| 55 | format_ipsec_input_trace (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | { |
| 57 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 58 | 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] | 59 | ipsec_input_trace_t *t = va_arg (*args, ipsec_input_trace_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | |
Matus Fabian | 5539a07 | 2016-09-07 05:57:09 -0700 | [diff] [blame] | 61 | if (t->spi == 0 && t->seq == 0) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 62 | { |
Matus Fabian | 5539a07 | 2016-09-07 05:57:09 -0700 | [diff] [blame] | 63 | s = format (s, "esp: no esp packet"); |
| 64 | return s; |
| 65 | } |
| 66 | |
| 67 | if (t->sa_id != 0) |
| 68 | { |
Pierre Pfister | 6221927 | 2018-11-26 09:29:00 +0100 | [diff] [blame] | 69 | s = |
| 70 | format (s, "esp: sa_id %u spd %u spi %u seq %u", t->sa_id, t->spd, |
| 71 | t->spi, t->seq); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 72 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | else |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 74 | { |
Pierre Pfister | 6221927 | 2018-11-26 09:29:00 +0100 | [diff] [blame] | 75 | s = |
| 76 | format (s, "esp: no sa spd %u spi %u seq %u", t->spd, t->spi, t->seq); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 77 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | return s; |
| 79 | } |
| 80 | |
| 81 | always_inline ipsec_policy_t * |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 82 | ipsec_input_protect_policy_match (ipsec_spd_t * spd, u32 sa, u32 da, u32 spi) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | { |
| 84 | ipsec_main_t *im = &ipsec_main; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 85 | ipsec_policy_t *p; |
| 86 | ipsec_sa_t *s; |
| 87 | u32 *i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 89 | vec_foreach (i, spd->ipv4_inbound_protect_policy_indices) |
| 90 | { |
| 91 | p = pool_elt_at_index (spd->policies, *i); |
| 92 | s = pool_elt_at_index (im->sad, p->sa_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 94 | if (spi != s->spi) |
| 95 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 97 | if (s->is_tunnel) |
| 98 | { |
| 99 | if (da != clib_net_to_host_u32 (s->tunnel_dst_addr.ip4.as_u32)) |
| 100 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 102 | if (sa != clib_net_to_host_u32 (s->tunnel_src_addr.ip4.as_u32)) |
| 103 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 105 | return p; |
| 106 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 108 | if (da < clib_net_to_host_u32 (p->laddr.start.ip4.as_u32)) |
| 109 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 111 | if (da > clib_net_to_host_u32 (p->laddr.stop.ip4.as_u32)) |
| 112 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 114 | if (sa < clib_net_to_host_u32 (p->raddr.start.ip4.as_u32)) |
| 115 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 117 | if (sa > clib_net_to_host_u32 (p->raddr.stop.ip4.as_u32)) |
| 118 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 120 | return p; |
| 121 | } |
| 122 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | always_inline uword |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 126 | ip6_addr_match_range (ip6_address_t * a, ip6_address_t * la, |
| 127 | ip6_address_t * ua) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 129 | if ((memcmp (a->as_u64, la->as_u64, 2 * sizeof (u64)) >= 0) && |
| 130 | (memcmp (a->as_u64, ua->as_u64, 2 * sizeof (u64)) <= 0)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 131 | return 1; |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | always_inline ipsec_policy_t * |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 136 | ipsec6_input_protect_policy_match (ipsec_spd_t * spd, |
| 137 | ip6_address_t * sa, |
| 138 | ip6_address_t * da, u32 spi) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | { |
| 140 | ipsec_main_t *im = &ipsec_main; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 141 | ipsec_policy_t *p; |
| 142 | ipsec_sa_t *s; |
| 143 | u32 *i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 145 | vec_foreach (i, spd->ipv6_inbound_protect_policy_indices) |
| 146 | { |
| 147 | p = pool_elt_at_index (spd->policies, *i); |
| 148 | s = pool_elt_at_index (im->sad, p->sa_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 150 | if (spi != s->spi) |
| 151 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 152 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 153 | if (s->is_tunnel) |
| 154 | { |
| 155 | if (!ip6_address_is_equal (sa, &s->tunnel_src_addr.ip6)) |
| 156 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 158 | if (!ip6_address_is_equal (da, &s->tunnel_dst_addr.ip6)) |
| 159 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 160 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 161 | return p; |
| 162 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 164 | if (!ip6_addr_match_range (sa, &p->raddr.start.ip6, &p->raddr.stop.ip6)) |
| 165 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 167 | if (!ip6_addr_match_range (da, &p->laddr.start.ip6, &p->laddr.stop.ip6)) |
| 168 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 169 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 170 | return p; |
| 171 | } |
| 172 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 175 | static vlib_node_registration_t ipsec4_input_node; |
Jean-Mickael Guerin | 8941ec2 | 2016-03-04 14:14:21 +0100 | [diff] [blame] | 176 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 177 | VLIB_NODE_FN (ipsec4_input_node) (vlib_main_t * vm, |
| 178 | vlib_node_runtime_t * node, |
| 179 | vlib_frame_t * from_frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | u32 n_left_from, *from, next_index, *to_next; |
| 182 | ipsec_main_t *im = &ipsec_main; |
| 183 | |
| 184 | from = vlib_frame_vector_args (from_frame); |
| 185 | n_left_from = from_frame->n_vectors; |
| 186 | |
| 187 | next_index = node->cached_next_index; |
| 188 | |
| 189 | while (n_left_from > 0) |
| 190 | { |
| 191 | u32 n_left_to_next; |
| 192 | |
| 193 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 194 | |
| 195 | while (n_left_from > 0 && n_left_to_next > 0) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 196 | { |
| 197 | u32 bi0, next0; |
| 198 | vlib_buffer_t *b0; |
| 199 | ip4_header_t *ip0; |
| 200 | esp_header_t *esp0; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 201 | ah_header_t *ah0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 202 | ip4_ipsec_config_t *c0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 203 | ipsec_spd_t *spd0; |
Matus Fabian | 5539a07 | 2016-09-07 05:57:09 -0700 | [diff] [blame] | 204 | ipsec_policy_t *p0 = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 206 | bi0 = to_next[0] = from[0]; |
| 207 | from += 1; |
| 208 | n_left_from -= 1; |
| 209 | to_next += 1; |
| 210 | n_left_to_next -= 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 211 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 212 | b0 = vlib_get_buffer (vm, bi0); |
Szymon Sliwa | 65a2727 | 2018-05-09 14:28:08 +0200 | [diff] [blame] | 213 | b0->flags |= VNET_BUFFER_F_IS_IP4; |
| 214 | b0->flags &= ~VNET_BUFFER_F_IS_IP6; |
Damjan Marion | 7d98a12 | 2018-07-19 20:42:08 +0200 | [diff] [blame] | 215 | c0 = vnet_feature_next_with_data (&next0, b0, sizeof (c0[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 217 | spd0 = pool_elt_at_index (im->spds, c0->spd_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 218 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 219 | ip0 = vlib_buffer_get_current (b0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 220 | |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 221 | if (PREDICT_TRUE |
| 222 | (ip0->protocol == IP_PROTOCOL_IPSEC_ESP |
| 223 | || ip0->protocol == IP_PROTOCOL_UDP)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 224 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 225 | #if 0 |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 226 | clib_warning |
| 227 | ("packet received from %U to %U spi %u size %u spd_id %u", |
| 228 | format_ip4_address, ip0->src_address.as_u8, |
| 229 | format_ip4_address, ip0->dst_address.as_u8, |
| 230 | clib_net_to_host_u32 (esp0->spi), |
| 231 | clib_net_to_host_u16 (ip0->length), spd0->id); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 232 | #endif |
Matus Fabian | 5539a07 | 2016-09-07 05:57:09 -0700 | [diff] [blame] | 233 | |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 234 | esp0 = (esp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0)); |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 235 | if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_UDP)) |
| 236 | { |
| 237 | esp0 = |
| 238 | (esp_header_t *) ((u8 *) esp0 + sizeof (udp_header_t)); |
| 239 | } |
| 240 | /* FIXME TODO missing check whether there is enough data inside |
| 241 | * IP/UDP to contain ESP header & stuff ? */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 242 | p0 = ipsec_input_protect_policy_match (spd0, |
| 243 | clib_net_to_host_u32 |
Matus Fabian | 5539a07 | 2016-09-07 05:57:09 -0700 | [diff] [blame] | 244 | (ip0->src_address. |
| 245 | as_u32), |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 246 | clib_net_to_host_u32 |
Matus Fabian | 5539a07 | 2016-09-07 05:57:09 -0700 | [diff] [blame] | 247 | (ip0->dst_address. |
| 248 | as_u32), |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 249 | clib_net_to_host_u32 |
| 250 | (esp0->spi)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 251 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 252 | if (PREDICT_TRUE (p0 != 0)) |
| 253 | { |
| 254 | p0->counter.packets++; |
| 255 | p0->counter.bytes += clib_net_to_host_u16 (ip0->length); |
Damjan Marion | 9c6ae5f | 2016-11-15 23:20:01 +0100 | [diff] [blame] | 256 | vnet_buffer (b0)->ipsec.sad_index = p0->sa_index; |
| 257 | vnet_buffer (b0)->ipsec.flags = 0; |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 258 | next0 = im->esp4_decrypt_next_index; |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 259 | vlib_buffer_advance (b0, ((u8 *) esp0 - (u8 *) ip0)); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 260 | goto trace0; |
| 261 | } |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 262 | |
| 263 | /* FIXME bypass and discard */ |
| 264 | trace0: |
| 265 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 266 | { |
| 267 | ipsec_input_trace_t *tr = |
| 268 | vlib_add_trace (vm, node, b0, sizeof (*tr)); |
Klement Sekera | 4b089f2 | 2018-04-17 18:04:57 +0200 | [diff] [blame] | 269 | if (ip0->protocol == IP_PROTOCOL_IPSEC_ESP || |
| 270 | ip0->protocol == IP_PROTOCOL_UDP) |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 271 | { |
| 272 | if (p0) |
| 273 | tr->sa_id = p0->sa_id; |
| 274 | tr->spi = clib_host_to_net_u32 (esp0->spi); |
| 275 | tr->seq = clib_host_to_net_u32 (esp0->seq); |
Pierre Pfister | 6221927 | 2018-11-26 09:29:00 +0100 | [diff] [blame] | 276 | tr->spd = spd0->id; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 280 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 281 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 282 | |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 283 | if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_AH)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 284 | { |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 285 | ah0 = (ah_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0)); |
| 286 | p0 = ipsec_input_protect_policy_match (spd0, |
| 287 | clib_net_to_host_u32 |
| 288 | (ip0->src_address. |
| 289 | as_u32), |
| 290 | clib_net_to_host_u32 |
| 291 | (ip0->dst_address. |
| 292 | as_u32), |
| 293 | clib_net_to_host_u32 |
| 294 | (ah0->spi)); |
| 295 | |
| 296 | if (PREDICT_TRUE (p0 != 0)) |
Matus Fabian | 5539a07 | 2016-09-07 05:57:09 -0700 | [diff] [blame] | 297 | { |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 298 | p0->counter.packets++; |
| 299 | p0->counter.bytes += clib_net_to_host_u16 (ip0->length); |
| 300 | vnet_buffer (b0)->ipsec.sad_index = p0->sa_index; |
| 301 | vnet_buffer (b0)->ipsec.flags = 0; |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 302 | next0 = im->ah4_decrypt_next_index; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 303 | goto trace1; |
| 304 | } |
| 305 | /* FIXME bypass and discard */ |
| 306 | trace1: |
| 307 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 308 | { |
| 309 | ipsec_input_trace_t *tr = |
| 310 | vlib_add_trace (vm, node, b0, sizeof (*tr)); |
| 311 | if (ip0->protocol == IP_PROTOCOL_IPSEC_ESP) |
| 312 | { |
| 313 | if (p0) |
| 314 | tr->sa_id = p0->sa_id; |
| 315 | tr->spi = clib_host_to_net_u32 (ah0->spi); |
| 316 | tr->seq = clib_host_to_net_u32 (ah0->seq_no); |
Pierre Pfister | 6221927 | 2018-11-26 09:29:00 +0100 | [diff] [blame] | 317 | tr->spd = spd0->id; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 318 | } |
Matus Fabian | 5539a07 | 2016-09-07 05:57:09 -0700 | [diff] [blame] | 319 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 320 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 321 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 322 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 323 | to_next, n_left_to_next, bi0, |
| 324 | next0); |
| 325 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 326 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 327 | } |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 328 | vlib_node_increment_counter (vm, ipsec4_input_node.index, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 329 | IPSEC_INPUT_ERROR_RX_PKTS, |
| 330 | from_frame->n_vectors); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 331 | |
| 332 | return from_frame->n_vectors; |
| 333 | } |
| 334 | |
| 335 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 336 | /* *INDENT-OFF* */ |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 337 | VLIB_REGISTER_NODE (ipsec4_input_node,static) = { |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame^] | 338 | .name = "ipsec4-input-feature", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 339 | .vector_size = sizeof (u32), |
| 340 | .format_trace = format_ipsec_input_trace, |
| 341 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 342 | |
| 343 | .n_errors = ARRAY_LEN(ipsec_input_error_strings), |
| 344 | .error_strings = ipsec_input_error_strings, |
| 345 | |
| 346 | .n_next_nodes = IPSEC_INPUT_N_NEXT, |
| 347 | .next_nodes = { |
| 348 | #define _(s,n) [IPSEC_INPUT_NEXT_##s] = n, |
| 349 | foreach_ipsec_input_next |
| 350 | #undef _ |
| 351 | }, |
| 352 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 353 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 354 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 355 | static vlib_node_registration_t ipsec6_input_node; |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 356 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 357 | |
| 358 | VLIB_NODE_FN (ipsec6_input_node) (vlib_main_t * vm, |
| 359 | vlib_node_runtime_t * node, |
| 360 | vlib_frame_t * from_frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 361 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 362 | u32 n_left_from, *from, next_index, *to_next; |
| 363 | ipsec_main_t *im = &ipsec_main; |
| 364 | |
| 365 | from = vlib_frame_vector_args (from_frame); |
| 366 | n_left_from = from_frame->n_vectors; |
| 367 | |
| 368 | next_index = node->cached_next_index; |
| 369 | |
| 370 | while (n_left_from > 0) |
| 371 | { |
| 372 | u32 n_left_to_next; |
| 373 | |
| 374 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 375 | |
| 376 | while (n_left_from > 0 && n_left_to_next > 0) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 377 | { |
| 378 | u32 bi0, next0; |
| 379 | vlib_buffer_t *b0; |
| 380 | ip6_header_t *ip0; |
| 381 | esp_header_t *esp0; |
| 382 | ip4_ipsec_config_t *c0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 383 | ipsec_spd_t *spd0; |
Matus Fabian | 5539a07 | 2016-09-07 05:57:09 -0700 | [diff] [blame] | 384 | ipsec_policy_t *p0 = 0; |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 385 | ah_header_t *ah0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 386 | u32 header_size = sizeof (ip0[0]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 387 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 388 | bi0 = to_next[0] = from[0]; |
| 389 | from += 1; |
| 390 | n_left_from -= 1; |
| 391 | to_next += 1; |
| 392 | n_left_to_next -= 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 393 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 394 | b0 = vlib_get_buffer (vm, bi0); |
Szymon Sliwa | 65a2727 | 2018-05-09 14:28:08 +0200 | [diff] [blame] | 395 | b0->flags |= VNET_BUFFER_F_IS_IP6; |
| 396 | b0->flags &= ~VNET_BUFFER_F_IS_IP4; |
Damjan Marion | 7d98a12 | 2018-07-19 20:42:08 +0200 | [diff] [blame] | 397 | c0 = vnet_feature_next_with_data (&next0, b0, sizeof (c0[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 398 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 399 | spd0 = pool_elt_at_index (im->spds, c0->spd_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 400 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 401 | ip0 = vlib_buffer_get_current (b0); |
| 402 | esp0 = (esp_header_t *) ((u8 *) ip0 + header_size); |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 403 | ah0 = (ah_header_t *) ((u8 *) ip0 + header_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 404 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 405 | if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_ESP)) |
| 406 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 407 | #if 0 |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 408 | clib_warning |
| 409 | ("packet received from %U to %U spi %u size %u spd_id %u", |
| 410 | format_ip6_address, &ip0->src_address, format_ip6_address, |
| 411 | &ip0->dst_address, clib_net_to_host_u32 (esp0->spi), |
| 412 | clib_net_to_host_u16 (ip0->payload_length) + header_size, |
| 413 | spd0->id); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 414 | #endif |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 415 | p0 = ipsec6_input_protect_policy_match (spd0, |
| 416 | &ip0->src_address, |
| 417 | &ip0->dst_address, |
| 418 | clib_net_to_host_u32 |
| 419 | (esp0->spi)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 420 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 421 | if (PREDICT_TRUE (p0 != 0)) |
| 422 | { |
| 423 | p0->counter.packets++; |
| 424 | p0->counter.bytes += |
| 425 | clib_net_to_host_u16 (ip0->payload_length); |
| 426 | p0->counter.bytes += header_size; |
Damjan Marion | 9c6ae5f | 2016-11-15 23:20:01 +0100 | [diff] [blame] | 427 | vnet_buffer (b0)->ipsec.sad_index = p0->sa_index; |
| 428 | vnet_buffer (b0)->ipsec.flags = 0; |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 429 | next0 = im->esp6_decrypt_next_index; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 430 | vlib_buffer_advance (b0, header_size); |
| 431 | goto trace0; |
| 432 | } |
| 433 | } |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 434 | else if (ip0->protocol == IP_PROTOCOL_IPSEC_AH) |
| 435 | { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 436 | p0 = ipsec6_input_protect_policy_match (spd0, |
| 437 | &ip0->src_address, |
| 438 | &ip0->dst_address, |
| 439 | clib_net_to_host_u32 |
| 440 | (ah0->spi)); |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 441 | |
| 442 | if (PREDICT_TRUE (p0 != 0)) |
| 443 | { |
| 444 | p0->counter.packets++; |
| 445 | p0->counter.bytes += |
| 446 | clib_net_to_host_u16 (ip0->payload_length); |
| 447 | p0->counter.bytes += header_size; |
| 448 | vnet_buffer (b0)->ipsec.sad_index = p0->sa_index; |
| 449 | vnet_buffer (b0)->ipsec.flags = 0; |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 450 | next0 = im->ah6_decrypt_next_index; |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 451 | goto trace0; |
| 452 | } |
| 453 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 454 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 455 | trace0: |
| 456 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 457 | { |
| 458 | ipsec_input_trace_t *tr = |
| 459 | vlib_add_trace (vm, node, b0, sizeof (*tr)); |
Matus Fabian | 5539a07 | 2016-09-07 05:57:09 -0700 | [diff] [blame] | 460 | if (ip0->protocol == IP_PROTOCOL_IPSEC_ESP) |
| 461 | { |
| 462 | if (p0) |
| 463 | tr->sa_id = p0->sa_id; |
| 464 | tr->spi = clib_host_to_net_u32 (esp0->spi); |
| 465 | tr->seq = clib_host_to_net_u32 (esp0->seq); |
Pierre Pfister | 6221927 | 2018-11-26 09:29:00 +0100 | [diff] [blame] | 466 | tr->spd = spd0->id; |
Matus Fabian | 5539a07 | 2016-09-07 05:57:09 -0700 | [diff] [blame] | 467 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 468 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 469 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 470 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 471 | n_left_to_next, bi0, next0); |
| 472 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 473 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 474 | } |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 475 | vlib_node_increment_counter (vm, ipsec6_input_node.index, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 476 | IPSEC_INPUT_ERROR_RX_PKTS, |
| 477 | from_frame->n_vectors); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 478 | |
| 479 | return from_frame->n_vectors; |
| 480 | } |
| 481 | |
| 482 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 483 | /* *INDENT-OFF* */ |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 484 | VLIB_REGISTER_NODE (ipsec6_input_node,static) = { |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame^] | 485 | .name = "ipsec6-input-feature", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 486 | .vector_size = sizeof (u32), |
| 487 | .format_trace = format_ipsec_input_trace, |
| 488 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 489 | |
| 490 | .n_errors = ARRAY_LEN(ipsec_input_error_strings), |
| 491 | .error_strings = ipsec_input_error_strings, |
| 492 | |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame^] | 493 | .sibling_of = "ipsec4-input-feature", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 494 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 495 | /* *INDENT-ON* */ |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 496 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 497 | /* |
| 498 | * fd.io coding-style-patch-verification: ON |
| 499 | * |
| 500 | * Local Variables: |
| 501 | * eval: (c-set-style "gnu") |
| 502 | * End: |
| 503 | */ |