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