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