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> |
Neale Ranns | 918c161 | 2019-02-21 23:34:59 -0800 | [diff] [blame] | 26 | #include <vnet/ipsec/ipsec_io.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 27 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 28 | #define foreach_ipsec_input_error \ |
| 29 | _(RX_PKTS, "IPSec pkts received") \ |
| 30 | _(RX_POLICY_MATCH, "IPSec policy match") \ |
| 31 | _(RX_POLICY_NO_MATCH, "IPSec policy not matched") \ |
| 32 | _(RX_POLICY_BYPASS, "IPSec policy bypass") \ |
| 33 | _(RX_POLICY_DISCARD, "IPSec policy discard") |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 34 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 35 | typedef enum |
| 36 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 37 | #define _(sym,str) IPSEC_INPUT_ERROR_##sym, |
| 38 | foreach_ipsec_input_error |
| 39 | #undef _ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 40 | IPSEC_INPUT_N_ERROR, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 41 | } ipsec_input_error_t; |
| 42 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 43 | static char *ipsec_input_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | #define _(sym,string) string, |
| 45 | foreach_ipsec_input_error |
| 46 | #undef _ |
| 47 | }; |
| 48 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 49 | typedef struct |
| 50 | { |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 51 | ip_protocol_t proto; |
Pierre Pfister | 6221927 | 2018-11-26 09:29:00 +0100 | [diff] [blame] | 52 | u32 spd; |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 53 | u32 policy_index; |
Matus Fabian | 5539a07 | 2016-09-07 05:57:09 -0700 | [diff] [blame] | 54 | u32 sa_id; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | u32 spi; |
| 56 | u32 seq; |
| 57 | } ipsec_input_trace_t; |
| 58 | |
| 59 | /* packet trace format function */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 60 | static u8 * |
| 61 | format_ipsec_input_trace (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | { |
| 63 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 64 | 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] | 65 | ipsec_input_trace_t *t = va_arg (*args, ipsec_input_trace_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | |
Guillaume Solignac | 8f818cc | 2019-05-15 12:02:33 +0200 | [diff] [blame] | 67 | s = format (s, "%U: sa_id %u spd %u policy %d spi %u (0x%08x) seq %u", |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 68 | format_ip_protocol, t->proto, t->sa_id, |
Guillaume Solignac | 8f818cc | 2019-05-15 12:02:33 +0200 | [diff] [blame] | 69 | t->spd, t->policy_index, t->spi, t->spi, t->seq); |
Matus Fabian | 5539a07 | 2016-09-07 05:57:09 -0700 | [diff] [blame] | 70 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | return s; |
| 72 | } |
| 73 | |
| 74 | always_inline ipsec_policy_t * |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 75 | ipsec_input_policy_match (ipsec_spd_t * spd, u32 sa, u32 da, |
| 76 | ipsec_spd_policy_type_t policy_type) |
| 77 | { |
| 78 | ipsec_main_t *im = &ipsec_main; |
| 79 | ipsec_policy_t *p; |
| 80 | u32 *i; |
| 81 | |
| 82 | vec_foreach (i, spd->policies[policy_type]) |
| 83 | { |
| 84 | p = pool_elt_at_index (im->policies, *i); |
| 85 | |
| 86 | if (da < clib_net_to_host_u32 (p->laddr.start.ip4.as_u32)) |
| 87 | continue; |
| 88 | |
| 89 | if (da > clib_net_to_host_u32 (p->laddr.stop.ip4.as_u32)) |
| 90 | continue; |
| 91 | |
| 92 | if (sa < clib_net_to_host_u32 (p->raddr.start.ip4.as_u32)) |
| 93 | continue; |
| 94 | |
| 95 | if (sa > clib_net_to_host_u32 (p->raddr.stop.ip4.as_u32)) |
| 96 | continue; |
| 97 | |
| 98 | return p; |
| 99 | } |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | always_inline ipsec_policy_t * |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 104 | 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] | 105 | { |
| 106 | ipsec_main_t *im = &ipsec_main; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 107 | ipsec_policy_t *p; |
| 108 | ipsec_sa_t *s; |
| 109 | u32 *i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 110 | |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 111 | vec_foreach (i, spd->policies[IPSEC_SPD_POLICY_IP4_INBOUND_PROTECT]) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 112 | { |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 113 | p = pool_elt_at_index (im->policies, *i); |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 114 | s = ipsec_sa_get (p->sa_index); |
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 | if (spi != s->spi) |
| 117 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 119 | if (ipsec_sa_is_set_IS_TUNNEL (s)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 120 | { |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 121 | if (da != clib_net_to_host_u32 (s->tunnel.t_dst.ip.ip4.as_u32)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 122 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 123 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 124 | if (sa != clib_net_to_host_u32 (s->tunnel.t_src.ip.ip4.as_u32)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 125 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 127 | return p; |
| 128 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | |
Neale Ranns | d2029bc | 2019-07-11 09:31:19 +0000 | [diff] [blame] | 130 | if (da < clib_net_to_host_u32 (p->laddr.start.ip4.as_u32)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 131 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | |
Neale Ranns | d2029bc | 2019-07-11 09:31:19 +0000 | [diff] [blame] | 133 | if (da > clib_net_to_host_u32 (p->laddr.stop.ip4.as_u32)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 134 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | |
Neale Ranns | d2029bc | 2019-07-11 09:31:19 +0000 | [diff] [blame] | 136 | if (sa < clib_net_to_host_u32 (p->raddr.start.ip4.as_u32)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 137 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | |
Neale Ranns | d2029bc | 2019-07-11 09:31:19 +0000 | [diff] [blame] | 139 | if (sa > clib_net_to_host_u32 (p->raddr.stop.ip4.as_u32)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 140 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 142 | return p; |
| 143 | } |
| 144 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | always_inline uword |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 148 | ip6_addr_match_range (ip6_address_t * a, ip6_address_t * la, |
| 149 | ip6_address_t * ua) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 151 | if ((memcmp (a->as_u64, la->as_u64, 2 * sizeof (u64)) >= 0) && |
| 152 | (memcmp (a->as_u64, ua->as_u64, 2 * sizeof (u64)) <= 0)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | return 1; |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | always_inline ipsec_policy_t * |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 158 | ipsec6_input_protect_policy_match (ipsec_spd_t * spd, |
| 159 | ip6_address_t * sa, |
| 160 | ip6_address_t * da, u32 spi) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | { |
| 162 | ipsec_main_t *im = &ipsec_main; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 163 | ipsec_policy_t *p; |
| 164 | ipsec_sa_t *s; |
| 165 | u32 *i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 167 | vec_foreach (i, spd->policies[IPSEC_SPD_POLICY_IP6_INBOUND_PROTECT]) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 168 | { |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 169 | p = pool_elt_at_index (im->policies, *i); |
Neale Ranns | c5fe57d | 2021-02-25 16:01:28 +0000 | [diff] [blame] | 170 | s = ipsec_sa_get (p->sa_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 172 | if (spi != s->spi) |
| 173 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 174 | |
Damjan Marion | d709cbc | 2019-03-26 13:16:42 +0100 | [diff] [blame] | 175 | if (ipsec_sa_is_set_IS_TUNNEL (s)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 176 | { |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 177 | if (!ip6_address_is_equal (sa, &s->tunnel.t_src.ip.ip6)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 178 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 179 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 180 | if (!ip6_address_is_equal (da, &s->tunnel.t_dst.ip.ip6)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 181 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 182 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 183 | return p; |
| 184 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 185 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 186 | if (!ip6_addr_match_range (sa, &p->raddr.start.ip6, &p->raddr.stop.ip6)) |
| 187 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 189 | if (!ip6_addr_match_range (da, &p->laddr.start.ip6, &p->laddr.stop.ip6)) |
| 190 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 192 | return p; |
| 193 | } |
| 194 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Damjan Marion | d770cfc | 2019-09-02 19:00:33 +0200 | [diff] [blame] | 197 | extern vlib_node_registration_t ipsec4_input_node; |
Jean-Mickael Guerin | 8941ec2 | 2016-03-04 14:14:21 +0100 | [diff] [blame] | 198 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 199 | VLIB_NODE_FN (ipsec4_input_node) (vlib_main_t * vm, |
| 200 | vlib_node_runtime_t * node, |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 201 | vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | { |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 203 | u32 n_left_from, *from, thread_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 204 | ipsec_main_t *im = &ipsec_main; |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 205 | u64 ipsec_unprocessed = 0, ipsec_matched = 0; |
| 206 | u64 ipsec_dropped = 0, ipsec_bypassed = 0; |
| 207 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE]; |
| 208 | vlib_buffer_t **b = bufs; |
| 209 | u16 nexts[VLIB_FRAME_SIZE], *next; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 210 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 211 | from = vlib_frame_vector_args (frame); |
| 212 | n_left_from = frame->n_vectors; |
| 213 | next = nexts; |
| 214 | vlib_get_buffers (vm, from, bufs, n_left_from); |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 215 | thread_index = vm->thread_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 217 | |
| 218 | while (n_left_from > 0) |
| 219 | { |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 220 | u32 next32, pi0; |
| 221 | ip4_header_t *ip0; |
| 222 | esp_header_t *esp0 = NULL; |
| 223 | ah_header_t *ah0; |
| 224 | ip4_ipsec_config_t *c0; |
| 225 | ipsec_spd_t *spd0; |
| 226 | ipsec_policy_t *p0 = NULL; |
| 227 | u8 has_space0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 229 | if (n_left_from > 2) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 230 | { |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 231 | vlib_prefetch_buffer_data (b[1], LOAD); |
| 232 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 233 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 234 | b[0]->flags |= VNET_BUFFER_F_IS_IP4; |
| 235 | b[0]->flags &= ~VNET_BUFFER_F_IS_IP6; |
| 236 | c0 = vnet_feature_next_with_data (&next32, b[0], sizeof (c0[0])); |
| 237 | next[0] = (u16) next32; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 238 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 239 | spd0 = pool_elt_at_index (im->spds, c0->spd_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 240 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 241 | ip0 = vlib_buffer_get_current (b[0]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 242 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 243 | if (PREDICT_TRUE |
| 244 | (ip0->protocol == IP_PROTOCOL_IPSEC_ESP |
| 245 | || ip0->protocol == IP_PROTOCOL_UDP)) |
| 246 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 247 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 248 | esp0 = (esp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0)); |
| 249 | if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_UDP)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 250 | { |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 251 | /* FIXME Skip, if not a UDP encapsulated packet */ |
| 252 | esp0 = (esp_header_t *) ((u8 *) esp0 + sizeof (udp_header_t)); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 253 | } |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 254 | |
| 255 | p0 = ipsec_input_protect_policy_match (spd0, |
| 256 | clib_net_to_host_u32 |
| 257 | (ip0->src_address.as_u32), |
| 258 | clib_net_to_host_u32 |
| 259 | (ip0->dst_address.as_u32), |
| 260 | clib_net_to_host_u32 |
| 261 | (esp0->spi)); |
| 262 | |
| 263 | has_space0 = |
| 264 | vlib_buffer_has_space (b[0], |
| 265 | (clib_address_t) (esp0 + 1) - |
| 266 | (clib_address_t) ip0); |
| 267 | |
| 268 | if (PREDICT_TRUE ((p0 != NULL) & (has_space0))) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 269 | { |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 270 | ipsec_matched += 1; |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 271 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 272 | pi0 = p0 - im->policies; |
| 273 | vlib_increment_combined_counter |
| 274 | (&ipsec_spd_policy_counters, |
| 275 | thread_index, pi0, 1, clib_net_to_host_u16 (ip0->length)); |
Benoît Ganne | f7f4964 | 2019-10-25 15:26:27 +0200 | [diff] [blame] | 276 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 277 | vnet_buffer (b[0])->ipsec.sad_index = p0->sa_index; |
| 278 | next[0] = im->esp4_decrypt_next_index; |
| 279 | vlib_buffer_advance (b[0], ((u8 *) esp0 - (u8 *) ip0)); |
| 280 | goto trace0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 281 | } |
Kingwel Xie | c69ac31 | 2019-02-04 01:49:29 -0800 | [diff] [blame] | 282 | else |
| 283 | { |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 284 | p0 = 0; |
| 285 | pi0 = ~0; |
| 286 | }; |
| 287 | |
| 288 | p0 = ipsec_input_policy_match (spd0, |
| 289 | clib_net_to_host_u32 |
| 290 | (ip0->src_address.as_u32), |
| 291 | clib_net_to_host_u32 |
| 292 | (ip0->dst_address.as_u32), |
| 293 | IPSEC_SPD_POLICY_IP4_INBOUND_BYPASS); |
| 294 | if (PREDICT_TRUE ((p0 != NULL))) |
| 295 | { |
| 296 | ipsec_bypassed += 1; |
Zachary Leaf | fbab65b | 2021-06-07 03:01:07 -0500 | [diff] [blame] | 297 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 298 | pi0 = p0 - im->policies; |
Zachary Leaf | fbab65b | 2021-06-07 03:01:07 -0500 | [diff] [blame] | 299 | vlib_increment_combined_counter ( |
| 300 | &ipsec_spd_policy_counters, thread_index, pi0, 1, |
| 301 | clib_net_to_host_u16 (ip0->length)); |
| 302 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 303 | goto trace0; |
| 304 | } |
| 305 | else |
| 306 | { |
| 307 | p0 = 0; |
| 308 | pi0 = ~0; |
| 309 | }; |
| 310 | |
| 311 | p0 = ipsec_input_policy_match (spd0, |
| 312 | clib_net_to_host_u32 |
| 313 | (ip0->src_address.as_u32), |
| 314 | clib_net_to_host_u32 |
| 315 | (ip0->dst_address.as_u32), |
| 316 | IPSEC_SPD_POLICY_IP4_INBOUND_DISCARD); |
| 317 | if (PREDICT_TRUE ((p0 != NULL))) |
| 318 | { |
| 319 | ipsec_dropped += 1; |
Zachary Leaf | fbab65b | 2021-06-07 03:01:07 -0500 | [diff] [blame] | 320 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 321 | pi0 = p0 - im->policies; |
Zachary Leaf | fbab65b | 2021-06-07 03:01:07 -0500 | [diff] [blame] | 322 | vlib_increment_combined_counter ( |
| 323 | &ipsec_spd_policy_counters, thread_index, pi0, 1, |
| 324 | clib_net_to_host_u16 (ip0->length)); |
| 325 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 326 | next[0] = IPSEC_INPUT_NEXT_DROP; |
| 327 | goto trace0; |
| 328 | } |
| 329 | else |
| 330 | { |
| 331 | p0 = 0; |
| 332 | pi0 = ~0; |
| 333 | }; |
Zachary Leaf | 26fec71 | 2021-10-26 10:05:58 -0500 | [diff] [blame] | 334 | |
| 335 | /* Drop by default if no match on PROTECT, BYPASS or DISCARD */ |
| 336 | ipsec_unprocessed += 1; |
| 337 | next[0] = IPSEC_INPUT_NEXT_DROP; |
| 338 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 339 | trace0: |
| 340 | if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) && |
| 341 | PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
| 342 | { |
| 343 | ipsec_input_trace_t *tr = |
| 344 | vlib_add_trace (vm, node, b[0], sizeof (*tr)); |
| 345 | |
| 346 | tr->proto = ip0->protocol; |
| 347 | tr->sa_id = p0 ? p0->sa_id : ~0; |
| 348 | tr->spi = has_space0 ? clib_net_to_host_u32 (esp0->spi) : ~0; |
| 349 | tr->seq = has_space0 ? clib_net_to_host_u32 (esp0->seq) : ~0; |
| 350 | tr->spd = spd0->id; |
| 351 | tr->policy_index = pi0; |
| 352 | } |
| 353 | } |
| 354 | else if (ip0->protocol == IP_PROTOCOL_IPSEC_AH) |
| 355 | { |
| 356 | ah0 = (ah_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0)); |
| 357 | p0 = ipsec_input_protect_policy_match (spd0, |
| 358 | clib_net_to_host_u32 |
| 359 | (ip0->src_address.as_u32), |
| 360 | clib_net_to_host_u32 |
| 361 | (ip0->dst_address.as_u32), |
| 362 | clib_net_to_host_u32 |
| 363 | (ah0->spi)); |
| 364 | |
| 365 | has_space0 = |
| 366 | vlib_buffer_has_space (b[0], |
| 367 | (clib_address_t) (ah0 + 1) - |
| 368 | (clib_address_t) ip0); |
| 369 | |
| 370 | if (PREDICT_TRUE ((p0 != NULL) & (has_space0))) |
| 371 | { |
| 372 | ipsec_matched += 1; |
| 373 | |
| 374 | pi0 = p0 - im->policies; |
| 375 | vlib_increment_combined_counter |
| 376 | (&ipsec_spd_policy_counters, |
| 377 | thread_index, pi0, 1, clib_net_to_host_u16 (ip0->length)); |
| 378 | |
| 379 | vnet_buffer (b[0])->ipsec.sad_index = p0->sa_index; |
| 380 | next[0] = im->ah4_decrypt_next_index; |
| 381 | goto trace1; |
| 382 | } |
| 383 | else |
| 384 | { |
| 385 | p0 = 0; |
| 386 | pi0 = ~0; |
Kingwel Xie | c69ac31 | 2019-02-04 01:49:29 -0800 | [diff] [blame] | 387 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 388 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 389 | p0 = ipsec_input_policy_match (spd0, |
| 390 | clib_net_to_host_u32 |
| 391 | (ip0->src_address.as_u32), |
| 392 | clib_net_to_host_u32 |
| 393 | (ip0->dst_address.as_u32), |
| 394 | IPSEC_SPD_POLICY_IP4_INBOUND_BYPASS); |
| 395 | if (PREDICT_TRUE ((p0 != NULL))) |
| 396 | { |
| 397 | ipsec_bypassed += 1; |
Zachary Leaf | fbab65b | 2021-06-07 03:01:07 -0500 | [diff] [blame] | 398 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 399 | pi0 = p0 - im->policies; |
Zachary Leaf | fbab65b | 2021-06-07 03:01:07 -0500 | [diff] [blame] | 400 | vlib_increment_combined_counter ( |
| 401 | &ipsec_spd_policy_counters, thread_index, pi0, 1, |
| 402 | clib_net_to_host_u16 (ip0->length)); |
| 403 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 404 | goto trace1; |
| 405 | } |
| 406 | else |
| 407 | { |
| 408 | p0 = 0; |
| 409 | pi0 = ~0; |
| 410 | }; |
| 411 | |
| 412 | p0 = ipsec_input_policy_match (spd0, |
| 413 | clib_net_to_host_u32 |
| 414 | (ip0->src_address.as_u32), |
| 415 | clib_net_to_host_u32 |
| 416 | (ip0->dst_address.as_u32), |
| 417 | IPSEC_SPD_POLICY_IP4_INBOUND_DISCARD); |
| 418 | if (PREDICT_TRUE ((p0 != NULL))) |
| 419 | { |
| 420 | ipsec_dropped += 1; |
Zachary Leaf | fbab65b | 2021-06-07 03:01:07 -0500 | [diff] [blame] | 421 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 422 | pi0 = p0 - im->policies; |
Zachary Leaf | fbab65b | 2021-06-07 03:01:07 -0500 | [diff] [blame] | 423 | vlib_increment_combined_counter ( |
| 424 | &ipsec_spd_policy_counters, thread_index, pi0, 1, |
| 425 | clib_net_to_host_u16 (ip0->length)); |
| 426 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 427 | next[0] = IPSEC_INPUT_NEXT_DROP; |
| 428 | goto trace1; |
| 429 | } |
| 430 | else |
| 431 | { |
| 432 | p0 = 0; |
| 433 | pi0 = ~0; |
| 434 | }; |
Zachary Leaf | 26fec71 | 2021-10-26 10:05:58 -0500 | [diff] [blame] | 435 | |
| 436 | /* Drop by default if no match on PROTECT, BYPASS or DISCARD */ |
| 437 | ipsec_unprocessed += 1; |
| 438 | next[0] = IPSEC_INPUT_NEXT_DROP; |
| 439 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 440 | trace1: |
| 441 | if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) && |
| 442 | PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) |
| 443 | { |
| 444 | ipsec_input_trace_t *tr = |
| 445 | vlib_add_trace (vm, node, b[0], sizeof (*tr)); |
| 446 | |
| 447 | tr->proto = ip0->protocol; |
| 448 | tr->sa_id = p0 ? p0->sa_id : ~0; |
| 449 | tr->spi = has_space0 ? clib_net_to_host_u32 (ah0->spi) : ~0; |
| 450 | tr->seq = has_space0 ? clib_net_to_host_u32 (ah0->seq_no) : ~0; |
| 451 | tr->spd = spd0->id; |
| 452 | tr->policy_index = pi0; |
| 453 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 454 | } |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 455 | else |
| 456 | { |
| 457 | ipsec_unprocessed += 1; |
| 458 | } |
| 459 | n_left_from -= 1; |
| 460 | b += 1; |
| 461 | next += 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 462 | } |
Kingwel Xie | c69ac31 | 2019-02-04 01:49:29 -0800 | [diff] [blame] | 463 | |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 464 | vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 465 | |
Kingwel Xie | c69ac31 | 2019-02-04 01:49:29 -0800 | [diff] [blame] | 466 | vlib_node_increment_counter (vm, ipsec4_input_node.index, |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 467 | IPSEC_INPUT_ERROR_RX_PKTS, frame->n_vectors); |
| 468 | |
| 469 | vlib_node_increment_counter (vm, ipsec4_input_node.index, |
| 470 | IPSEC_INPUT_ERROR_RX_POLICY_MATCH, |
Kingwel Xie | c69ac31 | 2019-02-04 01:49:29 -0800 | [diff] [blame] | 471 | ipsec_matched); |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 472 | |
| 473 | vlib_node_increment_counter (vm, ipsec4_input_node.index, |
| 474 | IPSEC_INPUT_ERROR_RX_POLICY_NO_MATCH, |
| 475 | ipsec_unprocessed); |
| 476 | |
| 477 | vlib_node_increment_counter (vm, ipsec4_input_node.index, |
| 478 | IPSEC_INPUT_ERROR_RX_POLICY_DISCARD, |
| 479 | ipsec_dropped); |
| 480 | |
| 481 | vlib_node_increment_counter (vm, ipsec4_input_node.index, |
| 482 | IPSEC_INPUT_ERROR_RX_POLICY_BYPASS, |
| 483 | ipsec_bypassed); |
| 484 | |
| 485 | return frame->n_vectors; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 489 | /* *INDENT-OFF* */ |
Damjan Marion | d770cfc | 2019-09-02 19:00:33 +0200 | [diff] [blame] | 490 | VLIB_REGISTER_NODE (ipsec4_input_node) = { |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 491 | .name = "ipsec4-input-feature", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 492 | .vector_size = sizeof (u32), |
| 493 | .format_trace = format_ipsec_input_trace, |
| 494 | .type = VLIB_NODE_TYPE_INTERNAL, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 495 | .n_errors = ARRAY_LEN(ipsec_input_error_strings), |
| 496 | .error_strings = ipsec_input_error_strings, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 497 | .n_next_nodes = IPSEC_INPUT_N_NEXT, |
| 498 | .next_nodes = { |
| 499 | #define _(s,n) [IPSEC_INPUT_NEXT_##s] = n, |
| 500 | foreach_ipsec_input_next |
| 501 | #undef _ |
| 502 | }, |
| 503 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 504 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 505 | |
Damjan Marion | d770cfc | 2019-09-02 19:00:33 +0200 | [diff] [blame] | 506 | extern vlib_node_registration_t ipsec6_input_node; |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 507 | |
Klement Sekera | b8f3544 | 2018-10-29 13:38:19 +0100 | [diff] [blame] | 508 | |
| 509 | VLIB_NODE_FN (ipsec6_input_node) (vlib_main_t * vm, |
| 510 | vlib_node_runtime_t * node, |
| 511 | vlib_frame_t * from_frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 512 | { |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 513 | u32 n_left_from, *from, next_index, *to_next, thread_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 514 | ipsec_main_t *im = &ipsec_main; |
Kingwel Xie | c69ac31 | 2019-02-04 01:49:29 -0800 | [diff] [blame] | 515 | u32 ipsec_unprocessed = 0; |
| 516 | u32 ipsec_matched = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 517 | |
| 518 | from = vlib_frame_vector_args (from_frame); |
| 519 | n_left_from = from_frame->n_vectors; |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 520 | thread_index = vm->thread_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 521 | |
| 522 | next_index = node->cached_next_index; |
| 523 | |
| 524 | while (n_left_from > 0) |
| 525 | { |
| 526 | u32 n_left_to_next; |
| 527 | |
| 528 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 529 | |
| 530 | while (n_left_from > 0 && n_left_to_next > 0) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 531 | { |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 532 | u32 bi0, next0, pi0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 533 | vlib_buffer_t *b0; |
| 534 | ip6_header_t *ip0; |
| 535 | esp_header_t *esp0; |
| 536 | ip4_ipsec_config_t *c0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 537 | ipsec_spd_t *spd0; |
Matus Fabian | 5539a07 | 2016-09-07 05:57:09 -0700 | [diff] [blame] | 538 | ipsec_policy_t *p0 = 0; |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 539 | ah_header_t *ah0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 540 | u32 header_size = sizeof (ip0[0]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 541 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 542 | bi0 = to_next[0] = from[0]; |
| 543 | from += 1; |
| 544 | n_left_from -= 1; |
| 545 | to_next += 1; |
| 546 | n_left_to_next -= 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 547 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 548 | b0 = vlib_get_buffer (vm, bi0); |
Szymon Sliwa | 65a2727 | 2018-05-09 14:28:08 +0200 | [diff] [blame] | 549 | b0->flags |= VNET_BUFFER_F_IS_IP6; |
| 550 | b0->flags &= ~VNET_BUFFER_F_IS_IP4; |
Damjan Marion | 7d98a12 | 2018-07-19 20:42:08 +0200 | [diff] [blame] | 551 | c0 = vnet_feature_next_with_data (&next0, b0, sizeof (c0[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 552 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 553 | spd0 = pool_elt_at_index (im->spds, c0->spd_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 554 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 555 | ip0 = vlib_buffer_get_current (b0); |
| 556 | esp0 = (esp_header_t *) ((u8 *) ip0 + header_size); |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 557 | ah0 = (ah_header_t *) ((u8 *) ip0 + header_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 558 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 559 | if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_ESP)) |
| 560 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 561 | #if 0 |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 562 | clib_warning |
| 563 | ("packet received from %U to %U spi %u size %u spd_id %u", |
| 564 | format_ip6_address, &ip0->src_address, format_ip6_address, |
| 565 | &ip0->dst_address, clib_net_to_host_u32 (esp0->spi), |
| 566 | clib_net_to_host_u16 (ip0->payload_length) + header_size, |
| 567 | spd0->id); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 568 | #endif |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 569 | p0 = ipsec6_input_protect_policy_match (spd0, |
| 570 | &ip0->src_address, |
| 571 | &ip0->dst_address, |
| 572 | clib_net_to_host_u32 |
| 573 | (esp0->spi)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 574 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 575 | if (PREDICT_TRUE (p0 != 0)) |
| 576 | { |
Kingwel Xie | c69ac31 | 2019-02-04 01:49:29 -0800 | [diff] [blame] | 577 | ipsec_matched += 1; |
| 578 | |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 579 | pi0 = p0 - im->policies; |
| 580 | vlib_increment_combined_counter |
| 581 | (&ipsec_spd_policy_counters, |
| 582 | thread_index, pi0, 1, |
| 583 | clib_net_to_host_u16 (ip0->payload_length) + |
| 584 | header_size); |
Kingwel Xie | c69ac31 | 2019-02-04 01:49:29 -0800 | [diff] [blame] | 585 | |
Damjan Marion | 9c6ae5f | 2016-11-15 23:20:01 +0100 | [diff] [blame] | 586 | vnet_buffer (b0)->ipsec.sad_index = p0->sa_index; |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 587 | next0 = im->esp6_decrypt_next_index; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 588 | vlib_buffer_advance (b0, header_size); |
| 589 | goto trace0; |
| 590 | } |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 591 | else |
| 592 | { |
| 593 | pi0 = ~0; |
Zachary Leaf | 26fec71 | 2021-10-26 10:05:58 -0500 | [diff] [blame] | 594 | ipsec_unprocessed += 1; |
| 595 | next0 = IPSEC_INPUT_NEXT_DROP; |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 596 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 597 | } |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 598 | else if (ip0->protocol == IP_PROTOCOL_IPSEC_AH) |
| 599 | { |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 600 | p0 = ipsec6_input_protect_policy_match (spd0, |
| 601 | &ip0->src_address, |
| 602 | &ip0->dst_address, |
| 603 | clib_net_to_host_u32 |
| 604 | (ah0->spi)); |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 605 | |
| 606 | if (PREDICT_TRUE (p0 != 0)) |
| 607 | { |
Kingwel Xie | c69ac31 | 2019-02-04 01:49:29 -0800 | [diff] [blame] | 608 | ipsec_matched += 1; |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 609 | pi0 = p0 - im->policies; |
| 610 | vlib_increment_combined_counter |
| 611 | (&ipsec_spd_policy_counters, |
| 612 | thread_index, pi0, 1, |
| 613 | clib_net_to_host_u16 (ip0->payload_length) + |
| 614 | header_size); |
Kingwel Xie | c69ac31 | 2019-02-04 01:49:29 -0800 | [diff] [blame] | 615 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 616 | vnet_buffer (b0)->ipsec.sad_index = p0->sa_index; |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 617 | next0 = im->ah6_decrypt_next_index; |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 618 | goto trace0; |
| 619 | } |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 620 | else |
| 621 | { |
| 622 | pi0 = ~0; |
Zachary Leaf | 26fec71 | 2021-10-26 10:05:58 -0500 | [diff] [blame] | 623 | ipsec_unprocessed += 1; |
| 624 | next0 = IPSEC_INPUT_NEXT_DROP; |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 625 | } |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 626 | } |
Kingwel Xie | c69ac31 | 2019-02-04 01:49:29 -0800 | [diff] [blame] | 627 | else |
| 628 | { |
| 629 | ipsec_unprocessed += 1; |
| 630 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 631 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 632 | trace0: |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 633 | if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) && |
| 634 | PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 635 | { |
| 636 | ipsec_input_trace_t *tr = |
| 637 | vlib_add_trace (vm, node, b0, sizeof (*tr)); |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 638 | |
| 639 | if (p0) |
| 640 | tr->sa_id = p0->sa_id; |
| 641 | tr->proto = ip0->protocol; |
| 642 | tr->spi = clib_net_to_host_u32 (esp0->spi); |
| 643 | tr->seq = clib_net_to_host_u32 (esp0->seq); |
| 644 | tr->spd = spd0->id; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 645 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 646 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 647 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 648 | n_left_to_next, bi0, next0); |
| 649 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 650 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 651 | } |
Kingwel Xie | c69ac31 | 2019-02-04 01:49:29 -0800 | [diff] [blame] | 652 | |
Klement Sekera | be5a5dd | 2018-10-09 16:05:48 +0200 | [diff] [blame] | 653 | vlib_node_increment_counter (vm, ipsec6_input_node.index, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 654 | IPSEC_INPUT_ERROR_RX_PKTS, |
Kingwel Xie | c69ac31 | 2019-02-04 01:49:29 -0800 | [diff] [blame] | 655 | from_frame->n_vectors - ipsec_unprocessed); |
| 656 | |
| 657 | vlib_node_increment_counter (vm, ipsec6_input_node.index, |
ShivaShankarK | 0546483 | 2020-04-14 14:01:03 +0530 | [diff] [blame] | 658 | IPSEC_INPUT_ERROR_RX_POLICY_MATCH, |
Kingwel Xie | c69ac31 | 2019-02-04 01:49:29 -0800 | [diff] [blame] | 659 | ipsec_matched); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 660 | |
| 661 | return from_frame->n_vectors; |
| 662 | } |
| 663 | |
| 664 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 665 | /* *INDENT-OFF* */ |
Damjan Marion | d770cfc | 2019-09-02 19:00:33 +0200 | [diff] [blame] | 666 | VLIB_REGISTER_NODE (ipsec6_input_node) = { |
Pierre Pfister | 057b356 | 2018-12-10 17:01:01 +0100 | [diff] [blame] | 667 | .name = "ipsec6-input-feature", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 668 | .vector_size = sizeof (u32), |
| 669 | .format_trace = format_ipsec_input_trace, |
| 670 | .type = VLIB_NODE_TYPE_INTERNAL, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 671 | .n_errors = ARRAY_LEN(ipsec_input_error_strings), |
| 672 | .error_strings = ipsec_input_error_strings, |
Kingwel Xie | c69ac31 | 2019-02-04 01:49:29 -0800 | [diff] [blame] | 673 | .n_next_nodes = IPSEC_INPUT_N_NEXT, |
| 674 | .next_nodes = { |
| 675 | #define _(s,n) [IPSEC_INPUT_NEXT_##s] = n, |
| 676 | foreach_ipsec_input_next |
| 677 | #undef _ |
| 678 | }, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 679 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 680 | /* *INDENT-ON* */ |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 681 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 682 | /* |
| 683 | * fd.io coding-style-patch-verification: ON |
| 684 | * |
| 685 | * Local Variables: |
| 686 | * eval: (c-set-style "gnu") |
| 687 | * End: |
| 688 | */ |