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