Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | * ip/icmp6.c: ip6 icmp |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 20 | * Permission is hereby granted, free of charge, to any person obtaining |
| 21 | * a copy of this software and associated documentation files (the |
| 22 | * "Software"), to deal in the Software without restriction, including |
| 23 | * without limitation the rights to use, copy, modify, merge, publish, |
| 24 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 25 | * permit persons to whom the Software is furnished to do so, subject to |
| 26 | * the following conditions: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 32 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 34 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 35 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 36 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 37 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 38 | */ |
| 39 | |
| 40 | #include <vlib/vlib.h> |
| 41 | #include <vnet/ip/ip.h> |
| 42 | #include <vnet/pg/pg.h> |
Ole Troan | 8034a36 | 2021-08-11 13:54:14 +0200 | [diff] [blame] | 43 | #include <vnet/ip/ip_sas.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 45 | static u8 * |
| 46 | format_ip6_icmp_type_and_code (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | { |
| 48 | icmp6_type_t type = va_arg (*args, int); |
| 49 | u8 code = va_arg (*args, int); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 50 | char *t = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | |
| 52 | #define _(n,f) case n: t = #f; break; |
| 53 | |
| 54 | switch (type) |
| 55 | { |
| 56 | foreach_icmp6_type; |
| 57 | |
| 58 | default: |
| 59 | break; |
| 60 | } |
| 61 | |
| 62 | #undef _ |
| 63 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 64 | if (!t) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | return format (s, "unknown 0x%x", type); |
| 66 | |
| 67 | s = format (s, "%s", t); |
| 68 | |
| 69 | t = 0; |
| 70 | switch ((type << 8) | code) |
| 71 | { |
| 72 | #define _(a,n,f) case (ICMP6_##a << 8) | (n): t = #f; break; |
| 73 | |
| 74 | foreach_icmp6_code; |
| 75 | |
| 76 | #undef _ |
| 77 | } |
| 78 | |
| 79 | if (t) |
| 80 | s = format (s, " %s", t); |
| 81 | |
| 82 | return s; |
| 83 | } |
| 84 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 85 | static u8 * |
| 86 | format_icmp6_header (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 88 | icmp46_header_t *icmp = va_arg (*args, icmp46_header_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | u32 max_header_bytes = va_arg (*args, u32); |
| 90 | |
| 91 | /* Nothing to do. */ |
| 92 | if (max_header_bytes < sizeof (icmp[0])) |
| 93 | return format (s, "ICMP header truncated"); |
| 94 | |
| 95 | s = format (s, "ICMP %U checksum 0x%x", |
| 96 | format_ip6_icmp_type_and_code, icmp->type, icmp->code, |
| 97 | clib_net_to_host_u16 (icmp->checksum)); |
| 98 | |
| 99 | if (max_header_bytes >= |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 100 | sizeof (icmp6_neighbor_solicitation_or_advertisement_header_t) && |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | (icmp->type == ICMP6_neighbor_solicitation || |
| 102 | icmp->type == ICMP6_neighbor_advertisement)) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 103 | { |
| 104 | icmp6_neighbor_solicitation_or_advertisement_header_t *icmp6_nd = |
| 105 | (icmp6_neighbor_solicitation_or_advertisement_header_t *) icmp; |
| 106 | s = format (s, "\n target address %U", |
| 107 | format_ip6_address, &icmp6_nd->target_address); |
| 108 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | |
| 110 | return s; |
| 111 | } |
| 112 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 113 | u8 * |
| 114 | format_icmp6_input_trace (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | { |
| 116 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *); |
| 117 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 118 | icmp6_input_trace_t *t = va_arg (*va, icmp6_input_trace_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | |
| 120 | s = format (s, "%U", |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 121 | format_ip6_header, t->packet_data, sizeof (t->packet_data)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | |
| 123 | return s; |
| 124 | } |
| 125 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 126 | static char *icmp_error_strings[] = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | #define _(f,s) s, |
| 128 | foreach_icmp6_error |
| 129 | #undef _ |
| 130 | }; |
| 131 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 132 | typedef enum |
| 133 | { |
Neale Ranns | 1afe952 | 2019-10-25 07:20:42 -0700 | [diff] [blame] | 134 | ICMP_INPUT_NEXT_PUNT, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | ICMP_INPUT_N_NEXT, |
| 136 | } icmp_input_next_t; |
| 137 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 138 | typedef struct |
| 139 | { |
| 140 | uword *type_and_code_by_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 141 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 142 | uword *type_by_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | |
| 144 | /* Vector dispatch table indexed by [icmp type]. */ |
| 145 | u8 input_next_index_by_type[256]; |
| 146 | |
| 147 | /* Max valid code indexed by icmp type. */ |
| 148 | u8 max_valid_code_by_type[256]; |
| 149 | |
| 150 | /* hop_limit must be >= this value for this icmp type. */ |
| 151 | u8 min_valid_hop_limit_by_type[256]; |
| 152 | |
| 153 | u8 min_valid_length_by_type[256]; |
| 154 | } icmp6_main_t; |
| 155 | |
| 156 | icmp6_main_t icmp6_main; |
| 157 | |
| 158 | static uword |
| 159 | ip6_icmp_input (vlib_main_t * vm, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 160 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 162 | icmp6_main_t *im = &icmp6_main; |
| 163 | u32 *from, *to_next; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | u32 n_left_from, n_left_to_next, next_index; |
| 165 | |
| 166 | from = vlib_frame_vector_args (frame); |
| 167 | n_left_from = frame->n_vectors; |
| 168 | next_index = node->cached_next_index; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 169 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | if (node->flags & VLIB_NODE_FLAG_TRACE) |
| 171 | vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors, |
| 172 | /* stride */ 1, |
| 173 | sizeof (icmp6_input_trace_t)); |
| 174 | |
| 175 | while (n_left_from > 0) |
| 176 | { |
| 177 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 178 | |
| 179 | while (n_left_from > 0 && n_left_to_next > 0) |
| 180 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 181 | vlib_buffer_t *b0; |
| 182 | ip6_header_t *ip0; |
| 183 | icmp46_header_t *icmp0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 184 | icmp6_type_t type0; |
| 185 | u32 bi0, next0, error0, len0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 186 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | bi0 = to_next[0] = from[0]; |
| 188 | |
| 189 | from += 1; |
| 190 | n_left_from -= 1; |
| 191 | to_next += 1; |
| 192 | n_left_to_next -= 1; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 193 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 194 | b0 = vlib_get_buffer (vm, bi0); |
| 195 | ip0 = vlib_buffer_get_current (b0); |
| 196 | icmp0 = ip6_next_header (ip0); |
| 197 | type0 = icmp0->type; |
| 198 | |
| 199 | error0 = ICMP6_ERROR_NONE; |
| 200 | |
| 201 | next0 = im->input_next_index_by_type[type0]; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 202 | error0 = |
Neale Ranns | 1afe952 | 2019-10-25 07:20:42 -0700 | [diff] [blame] | 203 | next0 == ICMP_INPUT_NEXT_PUNT ? ICMP6_ERROR_UNKNOWN_TYPE : error0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 204 | |
| 205 | /* Check code is valid for type. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 206 | error0 = |
| 207 | icmp0->code > |
| 208 | im->max_valid_code_by_type[type0] ? |
| 209 | ICMP6_ERROR_INVALID_CODE_FOR_TYPE : error0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 210 | |
| 211 | /* Checksum is already validated by ip6_local node so we don't need to check that. */ |
| 212 | |
| 213 | /* Check that hop limit == 255 for certain types. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 214 | error0 = |
| 215 | ip0->hop_limit < |
| 216 | im->min_valid_hop_limit_by_type[type0] ? |
| 217 | ICMP6_ERROR_INVALID_HOP_LIMIT_FOR_TYPE : error0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 218 | |
| 219 | len0 = clib_net_to_host_u16 (ip0->payload_length); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 220 | error0 = |
| 221 | len0 < |
| 222 | im->min_valid_length_by_type[type0] ? |
| 223 | ICMP6_ERROR_LENGTH_TOO_SMALL_FOR_TYPE : error0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 224 | |
| 225 | b0->error = node->errors[error0]; |
| 226 | |
Neale Ranns | 1afe952 | 2019-10-25 07:20:42 -0700 | [diff] [blame] | 227 | next0 = error0 != ICMP6_ERROR_NONE ? ICMP_INPUT_NEXT_PUNT : next0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | |
| 229 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 230 | to_next, n_left_to_next, |
| 231 | bi0, next0); |
| 232 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 233 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 234 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 235 | } |
| 236 | |
| 237 | return frame->n_vectors; |
| 238 | } |
| 239 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 240 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | VLIB_REGISTER_NODE (ip6_icmp_input_node) = { |
| 242 | .function = ip6_icmp_input, |
| 243 | .name = "ip6-icmp-input", |
| 244 | |
| 245 | .vector_size = sizeof (u32), |
| 246 | |
| 247 | .format_trace = format_icmp6_input_trace, |
| 248 | |
| 249 | .n_errors = ARRAY_LEN (icmp_error_strings), |
| 250 | .error_strings = icmp_error_strings, |
| 251 | |
| 252 | .n_next_nodes = 1, |
| 253 | .next_nodes = { |
Neale Ranns | 1afe952 | 2019-10-25 07:20:42 -0700 | [diff] [blame] | 254 | [ICMP_INPUT_NEXT_PUNT] = "ip6-punt", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 255 | }, |
| 256 | }; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 257 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 258 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 259 | typedef enum |
| 260 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | ICMP6_ECHO_REQUEST_NEXT_LOOKUP, |
| 262 | ICMP6_ECHO_REQUEST_NEXT_OUTPUT, |
| 263 | ICMP6_ECHO_REQUEST_N_NEXT, |
| 264 | } icmp6_echo_request_next_t; |
| 265 | |
| 266 | static uword |
| 267 | ip6_icmp_echo_request (vlib_main_t * vm, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 268 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 269 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 270 | u32 *from, *to_next; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 271 | u32 n_left_from, n_left_to_next, next_index; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 272 | ip6_main_t *im = &ip6_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 273 | |
| 274 | from = vlib_frame_vector_args (frame); |
| 275 | n_left_from = frame->n_vectors; |
| 276 | next_index = node->cached_next_index; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 277 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 278 | if (node->flags & VLIB_NODE_FLAG_TRACE) |
| 279 | vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors, |
| 280 | /* stride */ 1, |
| 281 | sizeof (icmp6_input_trace_t)); |
| 282 | |
| 283 | while (n_left_from > 0) |
| 284 | { |
| 285 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 286 | |
| 287 | while (n_left_from > 2 && n_left_to_next > 2) |
| 288 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 289 | vlib_buffer_t *p0, *p1; |
| 290 | ip6_header_t *ip0, *ip1; |
| 291 | icmp46_header_t *icmp0, *icmp1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 292 | ip6_address_t tmp0, tmp1; |
| 293 | ip_csum_t sum0, sum1; |
| 294 | u32 bi0, bi1; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 295 | u32 fib_index0, fib_index1; |
| 296 | u32 next0 = ICMP6_ECHO_REQUEST_NEXT_LOOKUP; |
| 297 | u32 next1 = ICMP6_ECHO_REQUEST_NEXT_LOOKUP; |
| 298 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 299 | bi0 = to_next[0] = from[0]; |
| 300 | bi1 = to_next[1] = from[1]; |
| 301 | |
| 302 | from += 2; |
| 303 | n_left_from -= 2; |
| 304 | to_next += 2; |
| 305 | n_left_to_next -= 2; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 306 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 307 | p0 = vlib_get_buffer (vm, bi0); |
| 308 | p1 = vlib_get_buffer (vm, bi1); |
| 309 | ip0 = vlib_buffer_get_current (p0); |
| 310 | ip1 = vlib_buffer_get_current (p1); |
| 311 | icmp0 = ip6_next_header (ip0); |
| 312 | icmp1 = ip6_next_header (ip1); |
| 313 | |
| 314 | /* Check icmp type to echo reply and update icmp checksum. */ |
| 315 | sum0 = icmp0->checksum; |
| 316 | sum1 = icmp1->checksum; |
| 317 | |
| 318 | ASSERT (icmp0->type == ICMP6_echo_request); |
| 319 | ASSERT (icmp1->type == ICMP6_echo_request); |
| 320 | sum0 = ip_csum_update (sum0, ICMP6_echo_request, ICMP6_echo_reply, |
| 321 | icmp46_header_t, type); |
| 322 | sum1 = ip_csum_update (sum1, ICMP6_echo_request, ICMP6_echo_reply, |
| 323 | icmp46_header_t, type); |
| 324 | |
| 325 | icmp0->checksum = ip_csum_fold (sum0); |
| 326 | icmp1->checksum = ip_csum_fold (sum1); |
| 327 | |
| 328 | icmp0->type = ICMP6_echo_reply; |
| 329 | icmp1->type = ICMP6_echo_reply; |
| 330 | |
| 331 | /* Swap source and destination address. */ |
| 332 | tmp0 = ip0->src_address; |
| 333 | tmp1 = ip1->src_address; |
| 334 | |
| 335 | ip0->src_address = ip0->dst_address; |
| 336 | ip1->src_address = ip1->dst_address; |
| 337 | |
| 338 | ip0->dst_address = tmp0; |
| 339 | ip1->dst_address = tmp1; |
| 340 | |
| 341 | /* New hop count. */ |
| 342 | ip0->hop_limit = im->host_config.ttl; |
| 343 | ip1->hop_limit = im->host_config.ttl; |
| 344 | |
Neale Ranns | 53da221 | 2018-02-24 02:11:19 -0800 | [diff] [blame] | 345 | /* Determine the correct lookup fib indices... */ |
| 346 | fib_index0 = vec_elt (im->fib_index_by_sw_if_index, |
| 347 | vnet_buffer (p0)->sw_if_index[VLIB_RX]); |
| 348 | vnet_buffer (p0)->sw_if_index[VLIB_TX] = fib_index0; |
| 349 | /* Determine the correct lookup fib indices... */ |
| 350 | fib_index1 = vec_elt (im->fib_index_by_sw_if_index, |
| 351 | vnet_buffer (p1)->sw_if_index[VLIB_RX]); |
| 352 | vnet_buffer (p1)->sw_if_index[VLIB_TX] = fib_index1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 353 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 354 | /* verify speculative enqueues, maybe switch current next frame */ |
| 355 | /* if next0==next1==next_index then nothing special needs to be done */ |
| 356 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, |
| 357 | to_next, n_left_to_next, |
| 358 | bi0, bi1, next0, next1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 359 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 360 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 361 | while (n_left_from > 0 && n_left_to_next > 0) |
| 362 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 363 | vlib_buffer_t *p0; |
| 364 | ip6_header_t *ip0; |
| 365 | icmp46_header_t *icmp0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 366 | u32 bi0; |
| 367 | ip6_address_t tmp0; |
| 368 | ip_csum_t sum0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 369 | u32 fib_index0; |
| 370 | u32 next0 = ICMP6_ECHO_REQUEST_NEXT_LOOKUP; |
| 371 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 372 | bi0 = to_next[0] = from[0]; |
| 373 | |
| 374 | from += 1; |
| 375 | n_left_from -= 1; |
| 376 | to_next += 1; |
| 377 | n_left_to_next -= 1; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 378 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 379 | p0 = vlib_get_buffer (vm, bi0); |
| 380 | ip0 = vlib_buffer_get_current (p0); |
| 381 | icmp0 = ip6_next_header (ip0); |
| 382 | |
| 383 | /* Check icmp type to echo reply and update icmp checksum. */ |
| 384 | sum0 = icmp0->checksum; |
| 385 | |
| 386 | ASSERT (icmp0->type == ICMP6_echo_request); |
| 387 | sum0 = ip_csum_update (sum0, ICMP6_echo_request, ICMP6_echo_reply, |
| 388 | icmp46_header_t, type); |
| 389 | |
| 390 | icmp0->checksum = ip_csum_fold (sum0); |
| 391 | |
| 392 | icmp0->type = ICMP6_echo_reply; |
| 393 | |
| 394 | /* Swap source and destination address. */ |
| 395 | tmp0 = ip0->src_address; |
| 396 | ip0->src_address = ip0->dst_address; |
| 397 | ip0->dst_address = tmp0; |
| 398 | |
| 399 | ip0->hop_limit = im->host_config.ttl; |
| 400 | |
Neale Ranns | 53da221 | 2018-02-24 02:11:19 -0800 | [diff] [blame] | 401 | /* if the packet is link local, we'll bounce through the link-local |
| 402 | * table with the RX interface correctly set */ |
| 403 | fib_index0 = vec_elt (im->fib_index_by_sw_if_index, |
| 404 | vnet_buffer (p0)->sw_if_index[VLIB_RX]); |
| 405 | vnet_buffer (p0)->sw_if_index[VLIB_TX] = fib_index0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 406 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 407 | /* Verify speculative enqueue, maybe switch current next frame */ |
| 408 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 409 | to_next, n_left_to_next, |
| 410 | bi0, next0); |
| 411 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 412 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 413 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 414 | } |
| 415 | |
| 416 | vlib_error_count (vm, ip6_icmp_input_node.index, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 417 | ICMP6_ERROR_ECHO_REPLIES_SENT, frame->n_vectors); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 418 | |
| 419 | return frame->n_vectors; |
| 420 | } |
| 421 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 422 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 423 | VLIB_REGISTER_NODE (ip6_icmp_echo_request_node,static) = { |
| 424 | .function = ip6_icmp_echo_request, |
| 425 | .name = "ip6-icmp-echo-request", |
| 426 | |
| 427 | .vector_size = sizeof (u32), |
| 428 | |
| 429 | .format_trace = format_icmp6_input_trace, |
| 430 | |
| 431 | .n_next_nodes = ICMP6_ECHO_REQUEST_N_NEXT, |
| 432 | .next_nodes = { |
| 433 | [ICMP6_ECHO_REQUEST_NEXT_LOOKUP] = "ip6-lookup", |
| 434 | [ICMP6_ECHO_REQUEST_NEXT_OUTPUT] = "interface-output", |
| 435 | }, |
| 436 | }; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 437 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 438 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 439 | typedef enum |
| 440 | { |
Ole Troan | cda9482 | 2016-01-07 14:37:25 +0100 | [diff] [blame] | 441 | IP6_ICMP_ERROR_NEXT_DROP, |
| 442 | IP6_ICMP_ERROR_NEXT_LOOKUP, |
| 443 | IP6_ICMP_ERROR_N_NEXT, |
| 444 | } ip6_icmp_error_next_t; |
| 445 | |
| 446 | void |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 447 | icmp6_error_set_vnet_buffer (vlib_buffer_t * b, u8 type, u8 code, u32 data) |
Ole Troan | cda9482 | 2016-01-07 14:37:25 +0100 | [diff] [blame] | 448 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 449 | vnet_buffer (b)->ip.icmp.type = type; |
| 450 | vnet_buffer (b)->ip.icmp.code = code; |
| 451 | vnet_buffer (b)->ip.icmp.data = data; |
Ole Troan | cda9482 | 2016-01-07 14:37:25 +0100 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | static u8 |
| 455 | icmp6_icmp_type_to_error (u8 type) |
| 456 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 457 | switch (type) |
| 458 | { |
| 459 | case ICMP6_destination_unreachable: |
| 460 | return ICMP6_ERROR_DEST_UNREACH_SENT; |
| 461 | case ICMP6_packet_too_big: |
| 462 | return ICMP6_ERROR_PACKET_TOO_BIG_SENT; |
| 463 | case ICMP6_time_exceeded: |
| 464 | return ICMP6_ERROR_TTL_EXPIRE_SENT; |
| 465 | case ICMP6_parameter_problem: |
| 466 | return ICMP6_ERROR_PARAM_PROBLEM_SENT; |
| 467 | default: |
| 468 | return ICMP6_ERROR_DROP; |
| 469 | } |
Ole Troan | cda9482 | 2016-01-07 14:37:25 +0100 | [diff] [blame] | 470 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 471 | |
| 472 | static uword |
Ole Troan | cda9482 | 2016-01-07 14:37:25 +0100 | [diff] [blame] | 473 | ip6_icmp_error (vlib_main_t * vm, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 474 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 475 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 476 | u32 *from, *to_next; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 477 | uword n_left_from, n_left_to_next; |
Ole Troan | cda9482 | 2016-01-07 14:37:25 +0100 | [diff] [blame] | 478 | ip6_icmp_error_next_t next_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 479 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 480 | from = vlib_frame_vector_args (frame); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 481 | n_left_from = frame->n_vectors; |
| 482 | next_index = node->cached_next_index; |
| 483 | |
| 484 | if (node->flags & VLIB_NODE_FLAG_TRACE) |
| 485 | vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 486 | /* stride */ 1, |
| 487 | sizeof (icmp6_input_trace_t)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 488 | |
| 489 | while (n_left_from > 0) |
| 490 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 491 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 492 | |
| 493 | while (n_left_from > 0 && n_left_to_next > 0) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 494 | { |
Ole Troan | da7f7b6 | 2019-03-11 13:15:54 +0100 | [diff] [blame] | 495 | /* |
| 496 | * Duplicate first buffer and free the original chain. Keep |
| 497 | * as much of the original packet as possible, within the |
| 498 | * minimum MTU. We chat "a little" here by keeping whatever |
| 499 | * is available in the first buffer. |
| 500 | */ |
| 501 | |
| 502 | u32 pi0 = ~0; |
| 503 | u32 org_pi0 = from[0]; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 504 | u32 next0 = IP6_ICMP_ERROR_NEXT_LOOKUP; |
| 505 | u8 error0 = ICMP6_ERROR_NONE; |
Ole Troan | da7f7b6 | 2019-03-11 13:15:54 +0100 | [diff] [blame] | 506 | vlib_buffer_t *p0, *org_p0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 507 | ip6_header_t *ip0, *out_ip0; |
| 508 | icmp46_header_t *icmp0; |
Ole Troan | 8034a36 | 2021-08-11 13:54:14 +0200 | [diff] [blame] | 509 | u32 sw_if_index0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 510 | int bogus_length; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 511 | |
Ole Troan | da7f7b6 | 2019-03-11 13:15:54 +0100 | [diff] [blame] | 512 | org_p0 = vlib_get_buffer (vm, org_pi0); |
| 513 | p0 = vlib_buffer_copy_no_chain (vm, org_p0, &pi0); |
Ole Troan | da7f7b6 | 2019-03-11 13:15:54 +0100 | [diff] [blame] | 514 | if (!p0 || pi0 == ~0) /* Out of buffers */ |
| 515 | continue; |
| 516 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 517 | /* Speculatively enqueue p0 to the current next frame */ |
| 518 | to_next[0] = pi0; |
| 519 | from += 1; |
| 520 | to_next += 1; |
| 521 | n_left_from -= 1; |
| 522 | n_left_to_next -= 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 523 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 524 | ip0 = vlib_buffer_get_current (p0); |
| 525 | sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 526 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 527 | /* Add IP header and ICMPv6 header including a 4 byte data field */ |
Ole Troan | da7f7b6 | 2019-03-11 13:15:54 +0100 | [diff] [blame] | 528 | vlib_buffer_advance (p0, |
| 529 | -(sizeof (ip6_header_t) + |
| 530 | sizeof (icmp46_header_t) + 4)); |
Ole Troan | da6e11b | 2018-05-23 11:21:42 +0200 | [diff] [blame] | 531 | |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 532 | vnet_buffer (p0)->sw_if_index[VLIB_TX] = ~0; |
| 533 | p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; |
Ole Troan | da6e11b | 2018-05-23 11:21:42 +0200 | [diff] [blame] | 534 | p0->current_length = |
| 535 | p0->current_length > 1280 ? 1280 : p0->current_length; |
| 536 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 537 | out_ip0 = vlib_buffer_get_current (p0); |
| 538 | icmp0 = (icmp46_header_t *) & out_ip0[1]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 539 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 540 | /* Fill ip header fields */ |
| 541 | out_ip0->ip_version_traffic_class_and_flow_label = |
| 542 | clib_host_to_net_u32 (0x6 << 28); |
Ole Troan | 9fb8755 | 2016-01-13 22:30:43 +0100 | [diff] [blame] | 543 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 544 | out_ip0->payload_length = |
| 545 | clib_host_to_net_u16 (p0->current_length - sizeof (ip6_header_t)); |
| 546 | out_ip0->protocol = IP_PROTOCOL_ICMP6; |
| 547 | out_ip0->hop_limit = 0xff; |
| 548 | out_ip0->dst_address = ip0->src_address; |
Ole Troan | 8034a36 | 2021-08-11 13:54:14 +0200 | [diff] [blame] | 549 | /* Prefer a source address from "offending interface" */ |
| 550 | if (!ip6_sas_by_sw_if_index (sw_if_index0, &out_ip0->dst_address, |
| 551 | &out_ip0->src_address)) |
| 552 | { /* interface has no IP6 address - should not happen */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 553 | next0 = IP6_ICMP_ERROR_NEXT_DROP; |
| 554 | error0 = ICMP6_ERROR_DROP; |
| 555 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 556 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 557 | /* Fill icmp header fields */ |
| 558 | icmp0->type = vnet_buffer (p0)->ip.icmp.type; |
| 559 | icmp0->code = vnet_buffer (p0)->ip.icmp.code; |
| 560 | *((u32 *) (icmp0 + 1)) = |
| 561 | clib_host_to_net_u32 (vnet_buffer (p0)->ip.icmp.data); |
| 562 | icmp0->checksum = 0; |
| 563 | icmp0->checksum = |
| 564 | ip6_tcp_udp_icmp_compute_checksum (vm, p0, out_ip0, |
| 565 | &bogus_length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 566 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 567 | /* Update error status */ |
Ole Troan | cda9482 | 2016-01-07 14:37:25 +0100 | [diff] [blame] | 568 | if (error0 == ICMP6_ERROR_NONE) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 569 | error0 = icmp6_icmp_type_to_error (icmp0->type); |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 570 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 571 | vlib_error_count (vm, node->node_index, error0, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 572 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 573 | /* Verify speculative enqueue, maybe switch current next frame */ |
| 574 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 575 | to_next, n_left_to_next, |
| 576 | pi0, next0); |
| 577 | } |
| 578 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Kingwel Xie | 27431dd | 2019-03-20 21:47:17 -0400 | [diff] [blame] | 581 | /* |
| 582 | * push the original buffers to error-drop, so that |
| 583 | * they can get the error counters handled, then freed |
| 584 | */ |
| 585 | vlib_buffer_enqueue_to_single_next (vm, node, |
| 586 | vlib_frame_vector_args (frame), |
| 587 | IP6_ICMP_ERROR_NEXT_DROP, |
| 588 | frame->n_vectors); |
| 589 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 590 | return frame->n_vectors; |
| 591 | } |
| 592 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 593 | /* *INDENT-OFF* */ |
Ole Troan | cda9482 | 2016-01-07 14:37:25 +0100 | [diff] [blame] | 594 | VLIB_REGISTER_NODE (ip6_icmp_error_node) = { |
| 595 | .function = ip6_icmp_error, |
| 596 | .name = "ip6-icmp-error", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 597 | .vector_size = sizeof (u32), |
| 598 | |
| 599 | .n_errors = ARRAY_LEN (icmp_error_strings), |
| 600 | .error_strings = icmp_error_strings, |
| 601 | |
Ole Troan | cda9482 | 2016-01-07 14:37:25 +0100 | [diff] [blame] | 602 | .n_next_nodes = IP6_ICMP_ERROR_N_NEXT, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 603 | .next_nodes = { |
Ole Troan | 282093f | 2018-09-19 12:38:51 +0200 | [diff] [blame] | 604 | [IP6_ICMP_ERROR_NEXT_DROP] = "error-drop", |
Ole Troan | cda9482 | 2016-01-07 14:37:25 +0100 | [diff] [blame] | 605 | [IP6_ICMP_ERROR_NEXT_LOOKUP] = "ip6-lookup", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 606 | }, |
| 607 | |
| 608 | .format_trace = format_icmp6_input_trace, |
| 609 | }; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 610 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 611 | |
| 612 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 613 | static uword |
| 614 | unformat_icmp_type_and_code (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 615 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 616 | icmp46_header_t *h = va_arg (*args, icmp46_header_t *); |
| 617 | icmp6_main_t *cm = &icmp6_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 618 | u32 i; |
| 619 | |
| 620 | if (unformat_user (input, unformat_vlib_number_by_name, |
| 621 | cm->type_and_code_by_name, &i)) |
| 622 | { |
| 623 | h->type = (i >> 8) & 0xff; |
| 624 | h->code = (i >> 0) & 0xff; |
| 625 | } |
| 626 | else if (unformat_user (input, unformat_vlib_number_by_name, |
| 627 | cm->type_by_name, &i)) |
| 628 | { |
| 629 | h->type = i; |
| 630 | h->code = 0; |
| 631 | } |
| 632 | else |
| 633 | return 0; |
| 634 | |
| 635 | return 1; |
| 636 | } |
| 637 | |
| 638 | static void |
| 639 | icmp6_pg_edit_function (pg_main_t * pg, |
| 640 | pg_stream_t * s, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 641 | pg_edit_group_t * g, u32 * packets, u32 n_packets) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 642 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 643 | vlib_main_t *vm = vlib_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 644 | u32 ip_offset, icmp_offset; |
| 645 | int bogus_length; |
| 646 | |
| 647 | icmp_offset = g->start_byte_offset; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 648 | ip_offset = (g - 1)->start_byte_offset; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 649 | |
| 650 | while (n_packets >= 1) |
| 651 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 652 | vlib_buffer_t *p0; |
| 653 | ip6_header_t *ip0; |
| 654 | icmp46_header_t *icmp0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 655 | |
| 656 | p0 = vlib_get_buffer (vm, packets[0]); |
| 657 | n_packets -= 1; |
| 658 | packets += 1; |
| 659 | |
| 660 | ASSERT (p0->current_data == 0); |
| 661 | ip0 = (void *) (p0->data + ip_offset); |
| 662 | icmp0 = (void *) (p0->data + icmp_offset); |
| 663 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 664 | icmp0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0, |
| 665 | &bogus_length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 666 | ASSERT (bogus_length == 0); |
| 667 | } |
| 668 | } |
| 669 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 670 | typedef struct |
| 671 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 672 | pg_edit_t type, code; |
| 673 | pg_edit_t checksum; |
| 674 | } pg_icmp46_header_t; |
| 675 | |
| 676 | always_inline void |
| 677 | pg_icmp_header_init (pg_icmp46_header_t * p) |
| 678 | { |
| 679 | /* Initialize fields that are not bit fields in the IP header. */ |
| 680 | #define _(f) pg_edit_init (&p->f, icmp46_header_t, f); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 681 | _(type); |
| 682 | _(code); |
| 683 | _(checksum); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 684 | #undef _ |
| 685 | } |
| 686 | |
| 687 | static uword |
| 688 | unformat_pg_icmp_header (unformat_input_t * input, va_list * args) |
| 689 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 690 | pg_stream_t *s = va_arg (*args, pg_stream_t *); |
| 691 | pg_icmp46_header_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 692 | u32 group_index; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 693 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 694 | p = pg_create_edit_group (s, sizeof (p[0]), sizeof (icmp46_header_t), |
| 695 | &group_index); |
| 696 | pg_icmp_header_init (p); |
| 697 | |
| 698 | p->checksum.type = PG_EDIT_UNSPECIFIED; |
| 699 | |
| 700 | { |
| 701 | icmp46_header_t tmp; |
| 702 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 703 | if (!unformat (input, "ICMP %U", unformat_icmp_type_and_code, &tmp)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 704 | goto error; |
| 705 | |
| 706 | pg_edit_set_fixed (&p->type, tmp.type); |
| 707 | pg_edit_set_fixed (&p->code, tmp.code); |
| 708 | } |
| 709 | |
| 710 | /* Parse options. */ |
| 711 | while (1) |
| 712 | { |
| 713 | if (unformat (input, "checksum %U", |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 714 | unformat_pg_edit, unformat_pg_number, &p->checksum)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 715 | ; |
| 716 | |
| 717 | /* Can't parse input: try next protocol level. */ |
| 718 | else |
| 719 | break; |
| 720 | } |
| 721 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 722 | if (!unformat_user (input, unformat_pg_payload, s)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 723 | goto error; |
| 724 | |
| 725 | if (p->checksum.type == PG_EDIT_UNSPECIFIED) |
| 726 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 727 | pg_edit_group_t *g = pg_stream_get_group (s, group_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 728 | g->edit_function = icmp6_pg_edit_function; |
| 729 | g->edit_function_opaque = 0; |
| 730 | } |
| 731 | |
| 732 | return 1; |
| 733 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 734 | error: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 735 | /* Free up any edits we may have added. */ |
| 736 | pg_free_edit_group (s); |
| 737 | return 0; |
| 738 | } |
| 739 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 740 | void |
| 741 | icmp6_register_type (vlib_main_t * vm, icmp6_type_t type, u32 node_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 742 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 743 | icmp6_main_t *im = &icmp6_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 744 | |
Damjan Marion | 2c29d75 | 2015-12-18 10:26:56 +0100 | [diff] [blame] | 745 | ASSERT ((int) type < ARRAY_LEN (im->input_next_index_by_type)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 746 | im->input_next_index_by_type[type] |
| 747 | = vlib_node_add_next (vm, ip6_icmp_input_node.index, node_index); |
| 748 | } |
| 749 | |
| 750 | static clib_error_t * |
| 751 | icmp6_init (vlib_main_t * vm) |
| 752 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 753 | ip_main_t *im = &ip_main; |
| 754 | ip_protocol_info_t *pi; |
| 755 | icmp6_main_t *cm = &icmp6_main; |
| 756 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 757 | |
| 758 | error = vlib_call_init_function (vm, ip_main_init); |
| 759 | |
| 760 | if (error) |
| 761 | return error; |
| 762 | |
| 763 | pi = ip_get_protocol_info (im, IP_PROTOCOL_ICMP6); |
| 764 | pi->format_header = format_icmp6_header; |
| 765 | pi->unformat_pg_edit = unformat_pg_icmp_header; |
| 766 | |
| 767 | cm->type_by_name = hash_create_string (0, sizeof (uword)); |
| 768 | #define _(n,t) hash_set_mem (cm->type_by_name, #t, (n)); |
| 769 | foreach_icmp6_type; |
| 770 | #undef _ |
| 771 | |
| 772 | cm->type_and_code_by_name = hash_create_string (0, sizeof (uword)); |
| 773 | #define _(a,n,t) hash_set_mem (cm->type_by_name, #t, (n) | (ICMP6_##a << 8)); |
| 774 | foreach_icmp6_code; |
| 775 | #undef _ |
| 776 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 777 | clib_memset (cm->input_next_index_by_type, |
Neale Ranns | 1afe952 | 2019-10-25 07:20:42 -0700 | [diff] [blame] | 778 | ICMP_INPUT_NEXT_PUNT, sizeof (cm->input_next_index_by_type)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 779 | clib_memset (cm->max_valid_code_by_type, 0, |
| 780 | sizeof (cm->max_valid_code_by_type)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 781 | |
| 782 | #define _(a,n,t) cm->max_valid_code_by_type[ICMP6_##a] = clib_max (cm->max_valid_code_by_type[ICMP6_##a], n); |
| 783 | foreach_icmp6_code; |
| 784 | #undef _ |
| 785 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 786 | clib_memset (cm->min_valid_hop_limit_by_type, 0, |
| 787 | sizeof (cm->min_valid_hop_limit_by_type)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 788 | cm->min_valid_hop_limit_by_type[ICMP6_router_solicitation] = 255; |
| 789 | cm->min_valid_hop_limit_by_type[ICMP6_router_advertisement] = 255; |
| 790 | cm->min_valid_hop_limit_by_type[ICMP6_neighbor_solicitation] = 255; |
| 791 | cm->min_valid_hop_limit_by_type[ICMP6_neighbor_advertisement] = 255; |
| 792 | cm->min_valid_hop_limit_by_type[ICMP6_redirect] = 255; |
| 793 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 794 | clib_memset (cm->min_valid_length_by_type, sizeof (icmp46_header_t), |
| 795 | sizeof (cm->min_valid_length_by_type)); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 796 | cm->min_valid_length_by_type[ICMP6_router_solicitation] = |
| 797 | sizeof (icmp6_neighbor_discovery_header_t); |
| 798 | cm->min_valid_length_by_type[ICMP6_router_advertisement] = |
| 799 | sizeof (icmp6_router_advertisement_header_t); |
| 800 | cm->min_valid_length_by_type[ICMP6_neighbor_solicitation] = |
| 801 | sizeof (icmp6_neighbor_solicitation_or_advertisement_header_t); |
| 802 | cm->min_valid_length_by_type[ICMP6_neighbor_advertisement] = |
| 803 | sizeof (icmp6_neighbor_solicitation_or_advertisement_header_t); |
| 804 | cm->min_valid_length_by_type[ICMP6_redirect] = |
| 805 | sizeof (icmp6_redirect_header_t); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 806 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 807 | icmp6_register_type (vm, ICMP6_echo_request, |
| 808 | ip6_icmp_echo_request_node.index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 809 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 810 | return (NULL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | VLIB_INIT_FUNCTION (icmp6_init); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 814 | |
| 815 | /* |
| 816 | * fd.io coding-style-patch-verification: ON |
| 817 | * |
| 818 | * Local Variables: |
| 819 | * eval: (c-set-style "gnu") |
| 820 | * End: |
| 821 | */ |