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/ip4_source_check.c: IP v4 check source address (unicast RPF check) |
| 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 <vnet/ip/ip.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 41 | #include <vnet/fib/ip4_fib.h> |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 42 | #include <vnet/fib/fib_urpf_list.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 43 | #include <vnet/dpo/load_balance.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 45 | /** |
| 46 | * @file |
| 47 | * @brief IPv4 Unicast Source Check. |
| 48 | * |
| 49 | * This file contains the IPv4 interface unicast source check. |
| 50 | */ |
| 51 | |
| 52 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 53 | typedef struct |
| 54 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | u8 packet_data[64]; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 56 | index_t urpf; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | } ip4_source_check_trace_t; |
| 58 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 59 | static u8 * |
| 60 | format_ip4_source_check_trace (u8 * s, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | { |
| 62 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *); |
| 63 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 64 | ip4_source_check_trace_t *t = va_arg (*va, ip4_source_check_trace_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | |
| 66 | s = format (s, "%U", |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 67 | format_ip4_header, t->packet_data, sizeof (t->packet_data)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | |
| 69 | return s; |
| 70 | } |
| 71 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 72 | typedef enum |
| 73 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | IP4_SOURCE_CHECK_NEXT_DROP, |
| 75 | IP4_SOURCE_CHECK_N_NEXT, |
| 76 | } ip4_source_check_next_t; |
| 77 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 78 | typedef enum |
| 79 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | IP4_SOURCE_CHECK_REACHABLE_VIA_RX, |
| 81 | IP4_SOURCE_CHECK_REACHABLE_VIA_ANY, |
| 82 | } ip4_source_check_type_t; |
| 83 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 84 | typedef union |
| 85 | { |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 86 | u32 fib_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | } ip4_source_check_config_t; |
| 88 | |
| 89 | always_inline uword |
| 90 | ip4_source_check_inline (vlib_main_t * vm, |
| 91 | vlib_node_runtime_t * node, |
| 92 | vlib_frame_t * frame, |
| 93 | ip4_source_check_type_t source_check_type) |
| 94 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 95 | u32 n_left_from, *from, *to_next; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | u32 next_index; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 97 | vlib_node_runtime_t *error_node = |
| 98 | vlib_node_get_runtime (vm, ip4_input_node.index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | |
| 100 | from = vlib_frame_vector_args (frame); |
| 101 | n_left_from = frame->n_vectors; |
| 102 | next_index = node->cached_next_index; |
| 103 | |
| 104 | if (node->flags & VLIB_NODE_FLAG_TRACE) |
| 105 | vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors, |
| 106 | /* stride */ 1, |
| 107 | sizeof (ip4_source_check_trace_t)); |
| 108 | |
| 109 | while (n_left_from > 0) |
| 110 | { |
| 111 | u32 n_left_to_next; |
| 112 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 113 | 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] | 114 | |
| 115 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 116 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 117 | vlib_buffer_t *p0, *p1; |
| 118 | ip4_header_t *ip0, *ip1; |
| 119 | ip4_fib_mtrie_t *mtrie0, *mtrie1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | ip4_fib_mtrie_leaf_t leaf0, leaf1; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 121 | ip4_source_check_config_t *c0, *c1; |
| 122 | const load_balance_t *lb0, *lb1; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 123 | u32 pi0, next0, pass0, lb_index0; |
| 124 | u32 pi1, next1, pass1, lb_index1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | |
| 126 | /* Prefetch next iteration. */ |
| 127 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 128 | vlib_buffer_t *p2, *p3; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | |
| 130 | p2 = vlib_get_buffer (vm, from[2]); |
| 131 | p3 = vlib_get_buffer (vm, from[3]); |
| 132 | |
| 133 | vlib_prefetch_buffer_header (p2, LOAD); |
| 134 | vlib_prefetch_buffer_header (p3, LOAD); |
| 135 | |
| 136 | CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD); |
| 137 | CLIB_PREFETCH (p3->data, sizeof (ip1[0]), LOAD); |
| 138 | } |
| 139 | |
| 140 | pi0 = to_next[0] = from[0]; |
| 141 | pi1 = to_next[1] = from[1]; |
| 142 | from += 2; |
| 143 | to_next += 2; |
| 144 | n_left_from -= 2; |
| 145 | n_left_to_next -= 2; |
| 146 | |
| 147 | p0 = vlib_get_buffer (vm, pi0); |
| 148 | p1 = vlib_get_buffer (vm, pi1); |
| 149 | |
| 150 | ip0 = vlib_buffer_get_current (p0); |
| 151 | ip1 = vlib_buffer_get_current (p1); |
| 152 | |
Damjan Marion | 7d98a12 | 2018-07-19 20:42:08 +0200 | [diff] [blame] | 153 | c0 = vnet_feature_next_with_data (&next0, p0, sizeof (c0[0])); |
| 154 | c1 = vnet_feature_next_with_data (&next1, p1, sizeof (c1[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 155 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 156 | mtrie0 = &ip4_fib_get (c0->fib_index)->mtrie; |
| 157 | mtrie1 = &ip4_fib_get (c1->fib_index)->mtrie; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 158 | |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 159 | leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, &ip0->src_address); |
| 160 | leaf1 = ip4_fib_mtrie_lookup_step_one (mtrie1, &ip1->src_address); |
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 | leaf0 = |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 163 | ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 2); |
| 164 | leaf1 = |
| 165 | ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 2); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 166 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 167 | leaf0 = |
| 168 | ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 3); |
| 169 | leaf1 = |
| 170 | ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 3); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 172 | lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0); |
| 173 | lb_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 174 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 175 | lb0 = load_balance_get (lb_index0); |
| 176 | lb1 = load_balance_get (lb_index1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 177 | |
| 178 | /* Pass multicast. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 179 | pass0 = ip4_address_is_multicast (&ip0->src_address) |
| 180 | || ip0->src_address.as_u32 == clib_host_to_net_u32 (0xFFFFFFFF); |
| 181 | pass1 = ip4_address_is_multicast (&ip1->src_address) |
| 182 | || ip1->src_address.as_u32 == clib_host_to_net_u32 (0xFFFFFFFF); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 183 | |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 184 | if (IP4_SOURCE_CHECK_REACHABLE_VIA_RX == source_check_type) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 185 | { |
| 186 | pass0 |= fib_urpf_check (lb0->lb_urpf, |
| 187 | vnet_buffer (p0)->sw_if_index |
| 188 | [VLIB_RX]); |
| 189 | pass1 |= |
| 190 | fib_urpf_check (lb1->lb_urpf, |
| 191 | vnet_buffer (p1)->sw_if_index[VLIB_RX]); |
| 192 | } |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 193 | else |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 194 | { |
| 195 | pass0 |= fib_urpf_check_size (lb0->lb_urpf); |
| 196 | pass1 |= fib_urpf_check_size (lb1->lb_urpf); |
| 197 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 198 | next0 = (pass0 ? next0 : IP4_SOURCE_CHECK_NEXT_DROP); |
| 199 | next1 = (pass1 ? next1 : IP4_SOURCE_CHECK_NEXT_DROP); |
| 200 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 201 | p0->error = |
| 202 | error_node->errors[IP4_ERROR_UNICAST_SOURCE_CHECK_FAILS]; |
| 203 | p1->error = |
| 204 | error_node->errors[IP4_ERROR_UNICAST_SOURCE_CHECK_FAILS]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | |
| 206 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, |
| 207 | to_next, n_left_to_next, |
| 208 | pi0, pi1, next0, next1); |
| 209 | } |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 210 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 211 | while (n_left_from > 0 && n_left_to_next > 0) |
| 212 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 213 | vlib_buffer_t *p0; |
| 214 | ip4_header_t *ip0; |
| 215 | ip4_fib_mtrie_t *mtrie0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | ip4_fib_mtrie_leaf_t leaf0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 217 | ip4_source_check_config_t *c0; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 218 | u32 pi0, next0, pass0, lb_index0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 219 | const load_balance_t *lb0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 220 | |
| 221 | pi0 = from[0]; |
| 222 | to_next[0] = pi0; |
| 223 | from += 1; |
| 224 | to_next += 1; |
| 225 | n_left_from -= 1; |
| 226 | n_left_to_next -= 1; |
| 227 | |
| 228 | p0 = vlib_get_buffer (vm, pi0); |
| 229 | ip0 = vlib_buffer_get_current (p0); |
| 230 | |
Damjan Marion | 7d98a12 | 2018-07-19 20:42:08 +0200 | [diff] [blame] | 231 | c0 = vnet_feature_next_with_data (&next0, p0, sizeof (c0[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 232 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 233 | mtrie0 = &ip4_fib_get (c0->fib_index)->mtrie; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 234 | |
Neale Ranns | 04a75e3 | 2017-03-23 06:46:01 -0700 | [diff] [blame] | 235 | leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, &ip0->src_address); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 236 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 237 | leaf0 = |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 238 | ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 2); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 239 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 240 | leaf0 = |
| 241 | ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 3); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 242 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 243 | lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 244 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 245 | lb0 = load_balance_get (lb_index0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 246 | |
| 247 | /* Pass multicast. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 248 | pass0 = ip4_address_is_multicast (&ip0->src_address) |
| 249 | || ip0->src_address.as_u32 == clib_host_to_net_u32 (0xFFFFFFFF); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 250 | |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 251 | if (IP4_SOURCE_CHECK_REACHABLE_VIA_RX == source_check_type) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 252 | { |
| 253 | pass0 |= fib_urpf_check (lb0->lb_urpf, |
| 254 | vnet_buffer (p0)->sw_if_index |
| 255 | [VLIB_RX]); |
| 256 | } |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 257 | else |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 258 | { |
| 259 | pass0 |= fib_urpf_check_size (lb0->lb_urpf); |
| 260 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | |
| 262 | next0 = (pass0 ? next0 : IP4_SOURCE_CHECK_NEXT_DROP); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 263 | p0->error = |
| 264 | error_node->errors[IP4_ERROR_UNICAST_SOURCE_CHECK_FAILS]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 265 | |
| 266 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 267 | to_next, n_left_to_next, |
| 268 | pi0, next0); |
| 269 | } |
| 270 | |
| 271 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 272 | } |
| 273 | |
| 274 | return frame->n_vectors; |
| 275 | } |
| 276 | |
Filip Tehlar | 26ea14e | 2019-03-11 05:30:21 -0700 | [diff] [blame] | 277 | VLIB_NODE_FN (ip4_check_source_reachable_via_any) (vlib_main_t * vm, |
| 278 | vlib_node_runtime_t * node, |
| 279 | vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 280 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 281 | return ip4_source_check_inline (vm, node, frame, |
| 282 | IP4_SOURCE_CHECK_REACHABLE_VIA_ANY); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Filip Tehlar | 26ea14e | 2019-03-11 05:30:21 -0700 | [diff] [blame] | 285 | VLIB_NODE_FN (ip4_check_source_reachable_via_rx) (vlib_main_t * vm, |
| 286 | vlib_node_runtime_t * node, |
| 287 | vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 288 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 289 | return ip4_source_check_inline (vm, node, frame, |
| 290 | IP4_SOURCE_CHECK_REACHABLE_VIA_RX); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 293 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 294 | VLIB_REGISTER_NODE (ip4_check_source_reachable_via_any) = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 295 | .name = "ip4-source-check-via-any", |
| 296 | .vector_size = sizeof (u32), |
| 297 | |
| 298 | .n_next_nodes = IP4_SOURCE_CHECK_N_NEXT, |
| 299 | .next_nodes = { |
Vijayabhaskar Katamreddy | ce07412 | 2017-11-15 13:50:26 -0800 | [diff] [blame] | 300 | [IP4_SOURCE_CHECK_NEXT_DROP] = "ip4-drop", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 301 | }, |
| 302 | |
| 303 | .format_buffer = format_ip4_header, |
| 304 | .format_trace = format_ip4_source_check_trace, |
| 305 | }; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 306 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 307 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 308 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 309 | VLIB_REGISTER_NODE (ip4_check_source_reachable_via_rx) = { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 310 | .name = "ip4-source-check-via-rx", |
| 311 | .vector_size = sizeof (u32), |
| 312 | |
| 313 | .n_next_nodes = IP4_SOURCE_CHECK_N_NEXT, |
| 314 | .next_nodes = { |
Vijayabhaskar Katamreddy | ce07412 | 2017-11-15 13:50:26 -0800 | [diff] [blame] | 315 | [IP4_SOURCE_CHECK_NEXT_DROP] = "ip4-drop", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 316 | }, |
| 317 | |
| 318 | .format_buffer = format_ip4_header, |
| 319 | .format_trace = format_ip4_source_check_trace, |
| 320 | }; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 321 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 322 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 323 | static clib_error_t * |
| 324 | set_ip_source_check (vlib_main_t * vm, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 325 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 326 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 327 | unformat_input_t _line_input, *line_input = &_line_input; |
| 328 | vnet_main_t *vnm = vnet_get_main (); |
| 329 | ip4_main_t *im = &ip4_main; |
| 330 | clib_error_t *error = 0; |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 331 | u32 sw_if_index, is_del; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 332 | ip4_source_check_config_t config; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 333 | char *feature_name = "ip4-source-check-via-rx"; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 334 | |
| 335 | sw_if_index = ~0; |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 336 | is_del = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 337 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 338 | if (!unformat_user (input, unformat_line_input, line_input)) |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 339 | return 0; |
| 340 | |
| 341 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 342 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 343 | if (unformat_user |
| 344 | (line_input, unformat_vnet_sw_interface, vnm, &sw_if_index)) |
| 345 | ; |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 346 | else if (unformat (line_input, "del")) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 347 | is_del = 1; |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 348 | else if (unformat (line_input, "loose")) |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 349 | feature_name = "ip4-source-check-via-any"; |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 350 | else |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 351 | { |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 352 | error = unformat_parse_error (line_input); |
| 353 | goto done; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | if (~0 == sw_if_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 358 | { |
| 359 | error = clib_error_return (0, "unknown interface `%U'", |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 360 | format_unformat_error, line_input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 361 | goto done; |
| 362 | } |
| 363 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 364 | config.fib_index = im->fib_index_by_sw_if_index[sw_if_index]; |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 365 | vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, |
| 366 | is_del == 0, &config, sizeof (config)); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 367 | done: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 368 | unformat_free (line_input); |
| 369 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 370 | return error; |
| 371 | } |
| 372 | |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 373 | /*? |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 374 | * This command adds the 'ip4-source-check-via-rx' graph node for |
| 375 | * a given interface. By adding the IPv4 source check graph node to |
| 376 | * an interface, the code verifies that the source address of incoming |
| 377 | * unicast packets are reachable over the incoming interface. Two flavours |
| 378 | * are supported (the default is strict): |
| 379 | * - loose: accept ingress packet if there is a route to reach the source |
| 380 | * - strict: accept ingress packet if it arrived on an interface which |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 381 | * the route to the source uses. i.e. an interface that the source |
| 382 | * is reachable via. |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 383 | * |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 384 | * @cliexpar |
| 385 | * @parblock |
| 386 | * Example of graph node before range checking is enabled: |
| 387 | * @cliexstart{show vlib graph ip4-source-check-via-rx} |
| 388 | * Name Next Previous |
Vijayabhaskar Katamreddy | ce07412 | 2017-11-15 13:50:26 -0800 | [diff] [blame] | 389 | * ip4-source-check-via-rx ip4-drop [0] |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 390 | * @cliexend |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 391 | * |
| 392 | * Example of how to enable unicast source checking on an interface: |
| 393 | * @cliexcmd{set interface ip source-check GigabitEthernet2/0/0 loose} |
| 394 | * |
| 395 | * Example of graph node after range checking is enabled: |
| 396 | * @cliexstart{show vlib graph ip4-source-check-via-rx} |
| 397 | * Name Next Previous |
Vijayabhaskar Katamreddy | ce07412 | 2017-11-15 13:50:26 -0800 | [diff] [blame] | 398 | * ip4-source-check-via-rx ip4-drop [0] ip4-input-no-checksum |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 399 | * ip4-source-and-port-range- ip4-input |
| 400 | * @cliexend |
| 401 | * |
| 402 | * Example of how to display the feature enabed on an interface: |
| 403 | * @cliexstart{show ip interface features GigabitEthernet2/0/0} |
| 404 | * IP feature paths configured on GigabitEthernet2/0/0... |
| 405 | * |
| 406 | * ipv4 unicast: |
| 407 | * ip4-source-check-via-rx |
| 408 | * ip4-lookup |
| 409 | * |
| 410 | * ipv4 multicast: |
| 411 | * ip4-lookup-multicast |
| 412 | * |
| 413 | * ipv4 multicast: |
| 414 | * interface-output |
| 415 | * |
| 416 | * ipv6 unicast: |
| 417 | * ip6-lookup |
| 418 | * |
| 419 | * ipv6 multicast: |
| 420 | * ip6-lookup |
| 421 | * |
| 422 | * ipv6 multicast: |
| 423 | * interface-output |
| 424 | * @cliexend |
| 425 | * |
| 426 | * Example of how to disable unicast source checking on an interface: |
| 427 | * @cliexcmd{set interface ip source-check GigabitEthernet2/0/0 del} |
| 428 | * @endparblock |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 429 | ?*/ |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 430 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 431 | VLIB_CLI_COMMAND (set_interface_ip_source_check_command, static) = { |
| 432 | .path = "set interface ip source-check", |
| 433 | .function = set_ip_source_check, |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 434 | .short_help = "set interface ip source-check <interface> [strict|loose] [del]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 435 | }; |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 436 | /* *INDENT-ON* */ |
| 437 | |
| 438 | static clib_error_t * |
| 439 | ip_source_check_accept (vlib_main_t * vm, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 440 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 441 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 442 | unformat_input_t _line_input, *line_input = &_line_input; |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 443 | fib_prefix_t pfx = { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 444 | .fp_proto = FIB_PROTOCOL_IP4, |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 445 | }; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 446 | clib_error_t *error = NULL; |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 447 | u32 table_id, is_add, fib_index; |
| 448 | |
| 449 | is_add = 1; |
| 450 | table_id = ~0; |
| 451 | |
| 452 | /* Get a line of input. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 453 | if (!unformat_user (input, unformat_line_input, line_input)) |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 454 | return 0; |
| 455 | |
| 456 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 457 | { |
| 458 | if (unformat (line_input, "table %d", &table_id)) |
| 459 | ; |
| 460 | else if (unformat (line_input, "del")) |
| 461 | is_add = 0; |
| 462 | else if (unformat (line_input, "add")) |
| 463 | is_add = 1; |
| 464 | else if (unformat (line_input, "%U/%d", |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 465 | unformat_ip4_address, &pfx.fp_addr.ip4, &pfx.fp_len)) |
| 466 | pfx.fp_proto = FIB_PROTOCOL_IP4; |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 467 | else |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 468 | { |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 469 | error = unformat_parse_error (line_input); |
| 470 | goto done; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 471 | } |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | if (~0 != table_id) |
| 475 | { |
Neale Ranns | 107e7d4 | 2017-04-11 09:55:19 -0700 | [diff] [blame] | 476 | fib_index = fib_table_find (pfx.fp_proto, table_id); |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 477 | if (~0 == fib_index) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 478 | { |
| 479 | error = clib_error_return (0, "Nonexistent table id %d", table_id); |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 480 | goto done; |
| 481 | } |
| 482 | } |
| 483 | else |
| 484 | { |
| 485 | fib_index = 0; |
| 486 | } |
| 487 | |
| 488 | if (is_add) |
| 489 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 490 | fib_table_entry_special_add (fib_index, |
| 491 | &pfx, |
| 492 | FIB_SOURCE_URPF_EXEMPT, |
Neale Ranns | a055830 | 2017-04-13 00:44:52 -0700 | [diff] [blame] | 493 | FIB_ENTRY_FLAG_DROP); |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 494 | } |
| 495 | else |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 496 | { |
| 497 | fib_table_entry_special_remove (fib_index, |
| 498 | &pfx, FIB_SOURCE_URPF_EXEMPT); |
| 499 | } |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 500 | |
| 501 | done: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 502 | unformat_free (line_input); |
| 503 | |
| 504 | return error; |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 505 | } |
| 506 | |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 507 | /*? |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 508 | * Add an exemption for a prefix to pass the Unicast Reverse Path |
| 509 | * Forwarding (uRPF) loose check. This is for testing purposes only. |
| 510 | * If the '<em>table</em>' is not enter it is defaulted to 0. Default |
| 511 | * is to '<em>add</em>'. VPP always performs a loose uRPF check for |
| 512 | * for-us traffic. |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 513 | * |
| 514 | * @cliexpar |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 515 | * Example of how to add a uRPF exception to a FIB table to pass the |
| 516 | * loose RPF tests: |
| 517 | * @cliexcmd{ip urpf-accept table 7 add} |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 518 | ?*/ |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 519 | /* *INDENT-OFF* */ |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 520 | VLIB_CLI_COMMAND (ip_source_check_accept_command, static) = { |
| 521 | .path = "ip urpf-accept", |
| 522 | .function = ip_source_check_accept, |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 523 | .short_help = "ip urpf-accept [table <table-id>] [add|del]", |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 524 | }; |
| 525 | /* *INDENT-ON* */ |
| 526 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 527 | |
Filip Tehlar | 26ea14e | 2019-03-11 05:30:21 -0700 | [diff] [blame] | 528 | #ifndef CLIB_MARCH_VARIANT |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 529 | /* Dummy init function to get us linked in. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 530 | clib_error_t * |
| 531 | ip4_source_check_init (vlib_main_t * vm) |
| 532 | { |
| 533 | return 0; |
| 534 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 535 | |
| 536 | VLIB_INIT_FUNCTION (ip4_source_check_init); |
Filip Tehlar | 26ea14e | 2019-03-11 05:30:21 -0700 | [diff] [blame] | 537 | #endif /* CLIB_MARCH_VARIANT */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 538 | |
| 539 | /* |
| 540 | * fd.io coding-style-patch-verification: ON |
| 541 | * |
| 542 | * Local Variables: |
| 543 | * eval: (c-set-style "gnu") |
| 544 | * End: |
| 545 | */ |