Neale Ranns | c667ffd | 2018-06-27 18:59:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | /** |
| 17 | * @brief Handle IPv4 header options in the data-path |
| 18 | */ |
| 19 | |
| 20 | #include <vnet/ip/ip.h> |
| 21 | |
| 22 | typedef enum ip4_options_next_t_ |
| 23 | { |
| 24 | IP4_OPTIONS_NEXT_PUNT, |
| 25 | IP4_OPTIONS_NEXT_LOCAL, |
| 26 | IP4_OPTIONS_N_NEXT, |
| 27 | } ip4_options_next_t; |
| 28 | |
| 29 | typedef struct ip4_options_trace_t_ |
| 30 | { |
| 31 | u8 option[4]; |
| 32 | } ip4_options_trace_t; |
| 33 | |
| 34 | VLIB_NODE_FN (ip4_options_node) (vlib_main_t * vm, |
| 35 | vlib_node_runtime_t * node, |
| 36 | vlib_frame_t * frame) |
| 37 | { |
| 38 | uword n_left_from, n_left_to_next, next_index; |
| 39 | u32 *from, *to_next; |
| 40 | |
| 41 | from = vlib_frame_vector_args (frame); |
| 42 | n_left_from = frame->n_vectors; |
| 43 | next_index = 0; |
| 44 | |
| 45 | while (n_left_from > 0) |
| 46 | { |
| 47 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 48 | |
| 49 | /* |
| 50 | * IP options packets, when properly used, are very low rate, |
| 51 | * so this code is not dual-looped for extra performance. |
| 52 | */ |
| 53 | while (n_left_from > 0 && n_left_to_next > 0) |
| 54 | { |
| 55 | ip4_options_next_t next; |
| 56 | ip4_header_t *ip4; |
| 57 | vlib_buffer_t *b; |
| 58 | u8 *options; |
| 59 | u32 bi; |
| 60 | |
| 61 | bi = from[0]; |
| 62 | from += 1; |
| 63 | n_left_from -= 1; |
| 64 | to_next[0] = bi; |
| 65 | to_next += 1; |
| 66 | n_left_to_next -= 1; |
| 67 | |
| 68 | b = vlib_get_buffer (vm, bi); |
| 69 | ip4 = vlib_buffer_get_current (b); |
| 70 | next = IP4_OPTIONS_NEXT_PUNT; |
| 71 | |
| 72 | options = (u8 *) (ip4 + 1); |
| 73 | |
| 74 | /* |
| 75 | * mask out the copy flag to leave the option type |
| 76 | */ |
| 77 | switch (options[0] & 0x7f) |
| 78 | { |
| 79 | case IP4_ROUTER_ALERT_OPTION: |
| 80 | /* |
| 81 | * if it's an IGMP packet, pass up the local stack |
| 82 | */ |
| 83 | if (IP_PROTOCOL_IGMP == ip4->protocol) |
| 84 | { |
BenoƮt Ganne | 688d276 | 2022-02-07 18:07:39 +0100 | [diff] [blame] | 85 | ip_lookup_set_buffer_fib_index ( |
| 86 | ip4_main.fib_index_by_sw_if_index, b); |
Neale Ranns | c667ffd | 2018-06-27 18:59:03 -0700 | [diff] [blame] | 87 | next = IP4_OPTIONS_NEXT_LOCAL; |
| 88 | } |
| 89 | break; |
| 90 | default: |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | if (b->flags & VLIB_BUFFER_IS_TRACED) |
| 95 | { |
| 96 | ip4_options_trace_t *t = |
| 97 | vlib_add_trace (vm, node, b, sizeof (*t)); |
| 98 | |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 99 | clib_memcpy_fast (t->option, options, 4); |
Neale Ranns | c667ffd | 2018-06-27 18:59:03 -0700 | [diff] [blame] | 100 | } |
| 101 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, |
| 102 | n_left_to_next, bi, next); |
| 103 | |
| 104 | } |
| 105 | |
| 106 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 107 | } |
| 108 | return frame->n_vectors; |
| 109 | } |
| 110 | |
| 111 | u8 * |
| 112 | format_ip4_options_trace (u8 * s, va_list * args) |
| 113 | { |
| 114 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 115 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 116 | ip4_options_trace_t *t = va_arg (*args, ip4_options_trace_t *); |
| 117 | u32 indent = format_get_indent (s); |
| 118 | |
| 119 | s = format (s, "%Uoption:[0x%x,0x%x,0x%x,0x%x]", |
| 120 | format_white_space, indent, |
| 121 | t->option[0], t->option[1], t->option[2], t->option[3]); |
| 122 | return s; |
| 123 | } |
| 124 | |
| 125 | /* *INDENT-OFF* */ |
| 126 | VLIB_REGISTER_NODE (ip4_options_node) = { |
| 127 | .name = "ip4-options", |
| 128 | .vector_size = sizeof (u32), |
| 129 | |
| 130 | .n_next_nodes = IP4_OPTIONS_N_NEXT, |
| 131 | .next_nodes = { |
| 132 | [IP4_OPTIONS_NEXT_PUNT] = "ip4-punt", |
| 133 | [IP4_OPTIONS_NEXT_LOCAL] = "ip4-local", |
| 134 | }, |
| 135 | .format_buffer = format_ip4_header, |
| 136 | .format_trace = format_ip4_options_trace, |
| 137 | }; |
| 138 | /* *INDENT-ON* */ |
| 139 | |
| 140 | /* |
| 141 | * fd.io coding-style-patch-verification: ON |
| 142 | * |
| 143 | * Local Variables: |
| 144 | * eval: (c-set-style "gnu") |
| 145 | * End: |
| 146 | */ |