blob: 25c512759207cbea0c98dcb00aca0f4a085c0c54 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Ranns0bfe5d82016-08-25 15:29:12 +010041#include <vnet/fib/ip4_fib.h>
Neale Ranns3ee44042016-10-03 13:05:48 +010042#include <vnet/fib/fib_urpf_list.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010043#include <vnet/dpo/load_balance.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070044
Billy McFall0683c9c2016-10-13 08:27:31 -040045/**
46 * @file
47 * @brief IPv4 Unicast Source Check.
48 *
49 * This file contains the IPv4 interface unicast source check.
50 */
51
52
Dave Barachd7cb1b52016-12-09 09:52:16 -050053typedef struct
54{
Ed Warnickecb9cada2015-12-08 15:45:58 -070055 u8 packet_data[64];
Dave Barachd7cb1b52016-12-09 09:52:16 -050056 index_t urpf;
Ed Warnickecb9cada2015-12-08 15:45:58 -070057} ip4_source_check_trace_t;
58
Dave Barachd7cb1b52016-12-09 09:52:16 -050059static u8 *
60format_ip4_source_check_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070061{
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 Barachd7cb1b52016-12-09 09:52:16 -050064 ip4_source_check_trace_t *t = va_arg (*va, ip4_source_check_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070065
66 s = format (s, "%U",
Dave Barachd7cb1b52016-12-09 09:52:16 -050067 format_ip4_header, t->packet_data, sizeof (t->packet_data));
Ed Warnickecb9cada2015-12-08 15:45:58 -070068
69 return s;
70}
71
Dave Barachd7cb1b52016-12-09 09:52:16 -050072typedef enum
73{
Ed Warnickecb9cada2015-12-08 15:45:58 -070074 IP4_SOURCE_CHECK_NEXT_DROP,
75 IP4_SOURCE_CHECK_N_NEXT,
76} ip4_source_check_next_t;
77
Dave Barachd7cb1b52016-12-09 09:52:16 -050078typedef enum
79{
Ed Warnickecb9cada2015-12-08 15:45:58 -070080 IP4_SOURCE_CHECK_REACHABLE_VIA_RX,
81 IP4_SOURCE_CHECK_REACHABLE_VIA_ANY,
82} ip4_source_check_type_t;
83
Dave Barachd7cb1b52016-12-09 09:52:16 -050084typedef union
85{
Neale Ranns3ee44042016-10-03 13:05:48 +010086 u32 fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -070087} ip4_source_check_config_t;
88
89always_inline uword
90ip4_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 Barachd7cb1b52016-12-09 09:52:16 -050095 u32 n_left_from, *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -070096 u32 next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -050097 vlib_node_runtime_t *error_node =
98 vlib_node_get_runtime (vm, ip4_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070099
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 Barachd7cb1b52016-12-09 09:52:16 -0500113 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114
115 while (n_left_from >= 4 && n_left_to_next >= 2)
116 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500117 vlib_buffer_t *p0, *p1;
118 ip4_header_t *ip0, *ip1;
119 ip4_fib_mtrie_t *mtrie0, *mtrie1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120 ip4_fib_mtrie_leaf_t leaf0, leaf1;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500121 ip4_source_check_config_t *c0, *c1;
122 const load_balance_t *lb0, *lb1;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100123 u32 pi0, next0, pass0, lb_index0;
124 u32 pi1, next1, pass1, lb_index1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125
126 /* Prefetch next iteration. */
127 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500128 vlib_buffer_t *p2, *p3;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129
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
Dave Barachd7cb1b52016-12-09 09:52:16 -0500153 c0 =
154 vnet_feature_next_with_data (vnet_buffer (p0)->sw_if_index
155 [VLIB_RX], &next0, p0,
156 sizeof (c0[0]));
157 c1 =
158 vnet_feature_next_with_data (vnet_buffer (p1)->sw_if_index
159 [VLIB_RX], &next1, p1,
160 sizeof (c1[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100162 mtrie0 = &ip4_fib_get (c0->fib_index)->mtrie;
163 mtrie1 = &ip4_fib_get (c1->fib_index)->mtrie;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164
Neale Ranns04a75e32017-03-23 06:46:01 -0700165 leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, &ip0->src_address);
166 leaf1 = ip4_fib_mtrie_lookup_step_one (mtrie1, &ip1->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167
Dave Barachd7cb1b52016-12-09 09:52:16 -0500168 leaf0 =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500169 ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 2);
170 leaf1 =
171 ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172
Dave Barachd7cb1b52016-12-09 09:52:16 -0500173 leaf0 =
174 ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 3);
175 leaf1 =
176 ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100178 lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
179 lb_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180
Dave Barachd7cb1b52016-12-09 09:52:16 -0500181 lb0 = load_balance_get (lb_index0);
182 lb1 = load_balance_get (lb_index1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183
184 /* Pass multicast. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500185 pass0 = ip4_address_is_multicast (&ip0->src_address)
186 || ip0->src_address.as_u32 == clib_host_to_net_u32 (0xFFFFFFFF);
187 pass1 = ip4_address_is_multicast (&ip1->src_address)
188 || ip1->src_address.as_u32 == clib_host_to_net_u32 (0xFFFFFFFF);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189
Neale Ranns3ee44042016-10-03 13:05:48 +0100190 if (IP4_SOURCE_CHECK_REACHABLE_VIA_RX == source_check_type)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500191 {
192 pass0 |= fib_urpf_check (lb0->lb_urpf,
193 vnet_buffer (p0)->sw_if_index
194 [VLIB_RX]);
195 pass1 |=
196 fib_urpf_check (lb1->lb_urpf,
197 vnet_buffer (p1)->sw_if_index[VLIB_RX]);
198 }
Neale Ranns3ee44042016-10-03 13:05:48 +0100199 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500200 {
201 pass0 |= fib_urpf_check_size (lb0->lb_urpf);
202 pass1 |= fib_urpf_check_size (lb1->lb_urpf);
203 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204 next0 = (pass0 ? next0 : IP4_SOURCE_CHECK_NEXT_DROP);
205 next1 = (pass1 ? next1 : IP4_SOURCE_CHECK_NEXT_DROP);
206
Dave Barachd7cb1b52016-12-09 09:52:16 -0500207 p0->error =
208 error_node->errors[IP4_ERROR_UNICAST_SOURCE_CHECK_FAILS];
209 p1->error =
210 error_node->errors[IP4_ERROR_UNICAST_SOURCE_CHECK_FAILS];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211
212 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
213 to_next, n_left_to_next,
214 pi0, pi1, next0, next1);
215 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500216
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217 while (n_left_from > 0 && n_left_to_next > 0)
218 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500219 vlib_buffer_t *p0;
220 ip4_header_t *ip0;
221 ip4_fib_mtrie_t *mtrie0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222 ip4_fib_mtrie_leaf_t leaf0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500223 ip4_source_check_config_t *c0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100224 u32 pi0, next0, pass0, lb_index0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500225 const load_balance_t *lb0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226
227 pi0 = from[0];
228 to_next[0] = pi0;
229 from += 1;
230 to_next += 1;
231 n_left_from -= 1;
232 n_left_to_next -= 1;
233
234 p0 = vlib_get_buffer (vm, pi0);
235 ip0 = vlib_buffer_get_current (p0);
236
Dave Barachd7cb1b52016-12-09 09:52:16 -0500237 c0 =
238 vnet_feature_next_with_data (vnet_buffer (p0)->sw_if_index
239 [VLIB_RX], &next0, p0,
240 sizeof (c0[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100242 mtrie0 = &ip4_fib_get (c0->fib_index)->mtrie;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243
Neale Ranns04a75e32017-03-23 06:46:01 -0700244 leaf0 = ip4_fib_mtrie_lookup_step_one (mtrie0, &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245
Dave Barachd7cb1b52016-12-09 09:52:16 -0500246 leaf0 =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500247 ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248
Dave Barachd7cb1b52016-12-09 09:52:16 -0500249 leaf0 =
250 ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100252 lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253
Dave Barachd7cb1b52016-12-09 09:52:16 -0500254 lb0 = load_balance_get (lb_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255
256 /* Pass multicast. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500257 pass0 = ip4_address_is_multicast (&ip0->src_address)
258 || ip0->src_address.as_u32 == clib_host_to_net_u32 (0xFFFFFFFF);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259
Neale Ranns3ee44042016-10-03 13:05:48 +0100260 if (IP4_SOURCE_CHECK_REACHABLE_VIA_RX == source_check_type)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500261 {
262 pass0 |= fib_urpf_check (lb0->lb_urpf,
263 vnet_buffer (p0)->sw_if_index
264 [VLIB_RX]);
265 }
Neale Ranns3ee44042016-10-03 13:05:48 +0100266 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500267 {
268 pass0 |= fib_urpf_check_size (lb0->lb_urpf);
269 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270
271 next0 = (pass0 ? next0 : IP4_SOURCE_CHECK_NEXT_DROP);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500272 p0->error =
273 error_node->errors[IP4_ERROR_UNICAST_SOURCE_CHECK_FAILS];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274
275 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
276 to_next, n_left_to_next,
277 pi0, next0);
278 }
279
280 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
281 }
282
283 return frame->n_vectors;
284}
285
286static uword
287ip4_source_check_reachable_via_any (vlib_main_t * vm,
288 vlib_node_runtime_t * node,
289 vlib_frame_t * frame)
290{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500291 return ip4_source_check_inline (vm, node, frame,
292 IP4_SOURCE_CHECK_REACHABLE_VIA_ANY);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293}
294
295static uword
296ip4_source_check_reachable_via_rx (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500297 vlib_node_runtime_t * node,
298 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500300 return ip4_source_check_inline (vm, node, frame,
301 IP4_SOURCE_CHECK_REACHABLE_VIA_RX);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302}
303
Dave Barachd7cb1b52016-12-09 09:52:16 -0500304/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700305VLIB_REGISTER_NODE (ip4_check_source_reachable_via_any) = {
306 .function = ip4_source_check_reachable_via_any,
307 .name = "ip4-source-check-via-any",
308 .vector_size = sizeof (u32),
309
310 .n_next_nodes = IP4_SOURCE_CHECK_N_NEXT,
311 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800312 [IP4_SOURCE_CHECK_NEXT_DROP] = "ip4-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313 },
314
315 .format_buffer = format_ip4_header,
316 .format_trace = format_ip4_source_check_trace,
317};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500318/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319
Damjan Marion1c80e832016-05-11 23:07:18 +0200320VLIB_NODE_FUNCTION_MULTIARCH (ip4_check_source_reachable_via_any,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500321 ip4_source_check_reachable_via_any);
Damjan Marion1c80e832016-05-11 23:07:18 +0200322
Dave Barachd7cb1b52016-12-09 09:52:16 -0500323/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324VLIB_REGISTER_NODE (ip4_check_source_reachable_via_rx) = {
325 .function = ip4_source_check_reachable_via_rx,
326 .name = "ip4-source-check-via-rx",
327 .vector_size = sizeof (u32),
328
329 .n_next_nodes = IP4_SOURCE_CHECK_N_NEXT,
330 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800331 [IP4_SOURCE_CHECK_NEXT_DROP] = "ip4-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332 },
333
334 .format_buffer = format_ip4_header,
335 .format_trace = format_ip4_source_check_trace,
336};
Dave Barachd7cb1b52016-12-09 09:52:16 -0500337/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338
Damjan Marion1c80e832016-05-11 23:07:18 +0200339VLIB_NODE_FUNCTION_MULTIARCH (ip4_check_source_reachable_via_rx,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500340 ip4_source_check_reachable_via_rx);
Damjan Marion1c80e832016-05-11 23:07:18 +0200341
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342static clib_error_t *
343set_ip_source_check (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500344 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500346 unformat_input_t _line_input, *line_input = &_line_input;
347 vnet_main_t *vnm = vnet_get_main ();
348 ip4_main_t *im = &ip4_main;
349 clib_error_t *error = 0;
Damjan Marion8b3191e2016-11-09 19:54:20 +0100350 u32 sw_if_index, is_del;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351 ip4_source_check_config_t config;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500352 char *feature_name = "ip4-source-check-via-rx";
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353
354 sw_if_index = ~0;
Neale Ranns3ee44042016-10-03 13:05:48 +0100355 is_del = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356
Dave Barachd7cb1b52016-12-09 09:52:16 -0500357 if (!unformat_user (input, unformat_line_input, line_input))
Neale Ranns3ee44042016-10-03 13:05:48 +0100358 return 0;
359
360 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
361 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500362 if (unformat_user
363 (line_input, unformat_vnet_sw_interface, vnm, &sw_if_index))
364 ;
Neale Ranns3ee44042016-10-03 13:05:48 +0100365 else if (unformat (line_input, "del"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500366 is_del = 1;
Neale Ranns3ee44042016-10-03 13:05:48 +0100367 else if (unformat (line_input, "loose"))
Damjan Marion8b3191e2016-11-09 19:54:20 +0100368 feature_name = "ip4-source-check-via-any";
Neale Ranns3ee44042016-10-03 13:05:48 +0100369 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500370 {
Neale Ranns3ee44042016-10-03 13:05:48 +0100371 error = unformat_parse_error (line_input);
372 goto done;
373 }
374 }
375
376 if (~0 == sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700377 {
378 error = clib_error_return (0, "unknown interface `%U'",
Neale Ranns3ee44042016-10-03 13:05:48 +0100379 format_unformat_error, line_input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380 goto done;
381 }
382
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383 config.fib_index = im->fib_index_by_sw_if_index[sw_if_index];
Damjan Marion8b3191e2016-11-09 19:54:20 +0100384 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
385 is_del == 0, &config, sizeof (config));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500386done:
Billy McFalla9a20e72017-02-15 11:39:12 -0500387 unformat_free (line_input);
388
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389 return error;
390}
391
Neale Ranns3ee44042016-10-03 13:05:48 +0100392/*?
Billy McFall0683c9c2016-10-13 08:27:31 -0400393 * This command adds the 'ip4-source-check-via-rx' graph node for
394 * a given interface. By adding the IPv4 source check graph node to
395 * an interface, the code verifies that the source address of incoming
396 * unicast packets are reachable over the incoming interface. Two flavours
397 * are supported (the default is strict):
398 * - loose: accept ingress packet if there is a route to reach the source
399 * - strict: accept ingress packet if it arrived on an interface which
Neale Ranns3ee44042016-10-03 13:05:48 +0100400 * the route to the source uses. i.e. an interface that the source
401 * is reachable via.
Neale Ranns3ee44042016-10-03 13:05:48 +0100402 *
Billy McFall0683c9c2016-10-13 08:27:31 -0400403 * @cliexpar
404 * @parblock
405 * Example of graph node before range checking is enabled:
406 * @cliexstart{show vlib graph ip4-source-check-via-rx}
407 * Name Next Previous
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800408 * ip4-source-check-via-rx ip4-drop [0]
Neale Ranns3ee44042016-10-03 13:05:48 +0100409 * @cliexend
Billy McFall0683c9c2016-10-13 08:27:31 -0400410 *
411 * Example of how to enable unicast source checking on an interface:
412 * @cliexcmd{set interface ip source-check GigabitEthernet2/0/0 loose}
413 *
414 * Example of graph node after range checking is enabled:
415 * @cliexstart{show vlib graph ip4-source-check-via-rx}
416 * Name Next Previous
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -0800417 * ip4-source-check-via-rx ip4-drop [0] ip4-input-no-checksum
Billy McFall0683c9c2016-10-13 08:27:31 -0400418 * ip4-source-and-port-range- ip4-input
419 * @cliexend
420 *
421 * Example of how to display the feature enabed on an interface:
422 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
423 * IP feature paths configured on GigabitEthernet2/0/0...
424 *
425 * ipv4 unicast:
426 * ip4-source-check-via-rx
427 * ip4-lookup
428 *
429 * ipv4 multicast:
430 * ip4-lookup-multicast
431 *
432 * ipv4 multicast:
433 * interface-output
434 *
435 * ipv6 unicast:
436 * ip6-lookup
437 *
438 * ipv6 multicast:
439 * ip6-lookup
440 *
441 * ipv6 multicast:
442 * interface-output
443 * @cliexend
444 *
445 * Example of how to disable unicast source checking on an interface:
446 * @cliexcmd{set interface ip source-check GigabitEthernet2/0/0 del}
447 * @endparblock
Neale Ranns3ee44042016-10-03 13:05:48 +0100448?*/
Billy McFall0683c9c2016-10-13 08:27:31 -0400449/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450VLIB_CLI_COMMAND (set_interface_ip_source_check_command, static) = {
451 .path = "set interface ip source-check",
452 .function = set_ip_source_check,
Billy McFall0683c9c2016-10-13 08:27:31 -0400453 .short_help = "set interface ip source-check <interface> [strict|loose] [del]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700454};
Neale Ranns3ee44042016-10-03 13:05:48 +0100455/* *INDENT-ON* */
456
457static clib_error_t *
458ip_source_check_accept (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500459 unformat_input_t * input, vlib_cli_command_t * cmd)
Neale Ranns3ee44042016-10-03 13:05:48 +0100460{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500461 unformat_input_t _line_input, *line_input = &_line_input;
Neale Ranns3ee44042016-10-03 13:05:48 +0100462 fib_prefix_t pfx = {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500463 .fp_proto = FIB_PROTOCOL_IP4,
Neale Ranns3ee44042016-10-03 13:05:48 +0100464 };
Dave Barachd7cb1b52016-12-09 09:52:16 -0500465 clib_error_t *error = NULL;
Neale Ranns3ee44042016-10-03 13:05:48 +0100466 u32 table_id, is_add, fib_index;
467
468 is_add = 1;
469 table_id = ~0;
470
471 /* Get a line of input. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500472 if (!unformat_user (input, unformat_line_input, line_input))
Neale Ranns3ee44042016-10-03 13:05:48 +0100473 return 0;
474
475 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
476 {
477 if (unformat (line_input, "table %d", &table_id))
478 ;
479 else if (unformat (line_input, "del"))
480 is_add = 0;
481 else if (unformat (line_input, "add"))
482 is_add = 1;
483 else if (unformat (line_input, "%U/%d",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500484 unformat_ip4_address, &pfx.fp_addr.ip4, &pfx.fp_len))
485 pfx.fp_proto = FIB_PROTOCOL_IP4;
Neale Ranns3ee44042016-10-03 13:05:48 +0100486 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500487 {
Neale Ranns3ee44042016-10-03 13:05:48 +0100488 error = unformat_parse_error (line_input);
489 goto done;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500490 }
Neale Ranns3ee44042016-10-03 13:05:48 +0100491 }
492
493 if (~0 != table_id)
494 {
Neale Ranns107e7d42017-04-11 09:55:19 -0700495 fib_index = fib_table_find (pfx.fp_proto, table_id);
Neale Ranns3ee44042016-10-03 13:05:48 +0100496 if (~0 == fib_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500497 {
498 error = clib_error_return (0, "Nonexistent table id %d", table_id);
Neale Ranns3ee44042016-10-03 13:05:48 +0100499 goto done;
500 }
501 }
502 else
503 {
504 fib_index = 0;
505 }
506
507 if (is_add)
508 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500509 fib_table_entry_special_add (fib_index,
510 &pfx,
511 FIB_SOURCE_URPF_EXEMPT,
Neale Rannsa0558302017-04-13 00:44:52 -0700512 FIB_ENTRY_FLAG_DROP);
Neale Ranns3ee44042016-10-03 13:05:48 +0100513 }
514 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500515 {
516 fib_table_entry_special_remove (fib_index,
517 &pfx, FIB_SOURCE_URPF_EXEMPT);
518 }
Neale Ranns3ee44042016-10-03 13:05:48 +0100519
520done:
Billy McFalla9a20e72017-02-15 11:39:12 -0500521 unformat_free (line_input);
522
523 return error;
Neale Ranns3ee44042016-10-03 13:05:48 +0100524}
525
Neale Ranns3ee44042016-10-03 13:05:48 +0100526/*?
Billy McFall0683c9c2016-10-13 08:27:31 -0400527 * Add an exemption for a prefix to pass the Unicast Reverse Path
528 * Forwarding (uRPF) loose check. This is for testing purposes only.
529 * If the '<em>table</em>' is not enter it is defaulted to 0. Default
530 * is to '<em>add</em>'. VPP always performs a loose uRPF check for
531 * for-us traffic.
Neale Ranns3ee44042016-10-03 13:05:48 +0100532 *
533 * @cliexpar
Billy McFall0683c9c2016-10-13 08:27:31 -0400534 * Example of how to add a uRPF exception to a FIB table to pass the
535 * loose RPF tests:
536 * @cliexcmd{ip urpf-accept table 7 add}
Neale Ranns3ee44042016-10-03 13:05:48 +0100537?*/
Billy McFall0683c9c2016-10-13 08:27:31 -0400538/* *INDENT-OFF* */
Neale Ranns3ee44042016-10-03 13:05:48 +0100539VLIB_CLI_COMMAND (ip_source_check_accept_command, static) = {
540 .path = "ip urpf-accept",
541 .function = ip_source_check_accept,
Billy McFall0683c9c2016-10-13 08:27:31 -0400542 .short_help = "ip urpf-accept [table <table-id>] [add|del]",
Neale Ranns3ee44042016-10-03 13:05:48 +0100543};
544/* *INDENT-ON* */
545
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546
547/* Dummy init function to get us linked in. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500548clib_error_t *
549ip4_source_check_init (vlib_main_t * vm)
550{
551 return 0;
552}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553
554VLIB_INIT_FUNCTION (ip4_source_check_init);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500555
556/*
557 * fd.io coding-style-patch-verification: ON
558 *
559 * Local Variables:
560 * eval: (c-set-style "gnu")
561 * End:
562 */