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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * ip/ip4_source_check.c: IP v4 check source address (unicast RPF check)
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vnet/ip/ip.h>
41 #include <vnet/fib/ip4_fib.h>
42 #include <vnet/fib/fib_urpf_list.h>
43 #include <vnet/dpo/load_balance.h>
47 * @brief IPv4 Unicast Source Check.
49 * This file contains the IPv4 interface unicast source check.
56 } ip4_source_check_trace_t;
58 static u8 * format_ip4_source_check_trace (u8 * s, va_list * va)
60 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
61 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
62 ip4_source_check_trace_t * t = va_arg (*va, ip4_source_check_trace_t *);
66 t->packet_data, sizeof (t->packet_data));
72 IP4_SOURCE_CHECK_NEXT_DROP,
73 IP4_SOURCE_CHECK_N_NEXT,
74 } ip4_source_check_next_t;
77 IP4_SOURCE_CHECK_REACHABLE_VIA_RX,
78 IP4_SOURCE_CHECK_REACHABLE_VIA_ANY,
79 } ip4_source_check_type_t;
83 } ip4_source_check_config_t;
86 ip4_source_check_inline (vlib_main_t * vm,
87 vlib_node_runtime_t * node,
89 ip4_source_check_type_t source_check_type)
91 ip4_main_t * im = &ip4_main;
92 ip_lookup_main_t * lm = &im->lookup_main;
93 ip_config_main_t * cm = &lm->feature_config_mains[VNET_IP_RX_UNICAST_FEAT];
94 u32 n_left_from, * from, * to_next;
96 vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip4_input_node.index);
98 from = vlib_frame_vector_args (frame);
99 n_left_from = frame->n_vectors;
100 next_index = node->cached_next_index;
102 if (node->flags & VLIB_NODE_FLAG_TRACE)
103 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
105 sizeof (ip4_source_check_trace_t));
107 while (n_left_from > 0)
111 vlib_get_next_frame (vm, node, next_index,
112 to_next, n_left_to_next);
114 while (n_left_from >= 4 && n_left_to_next >= 2)
116 vlib_buffer_t * p0, * p1;
117 ip4_header_t * ip0, * ip1;
118 ip4_fib_mtrie_t * mtrie0, * mtrie1;
119 ip4_fib_mtrie_leaf_t leaf0, leaf1;
120 ip4_source_check_config_t * c0, * c1;
121 const load_balance_t * lb0, * lb1;
122 u32 pi0, next0, pass0, lb_index0;
123 u32 pi1, next1, pass1, lb_index1;
125 /* Prefetch next iteration. */
127 vlib_buffer_t * p2, * p3;
129 p2 = vlib_get_buffer (vm, from[2]);
130 p3 = vlib_get_buffer (vm, from[3]);
132 vlib_prefetch_buffer_header (p2, LOAD);
133 vlib_prefetch_buffer_header (p3, LOAD);
135 CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD);
136 CLIB_PREFETCH (p3->data, sizeof (ip1[0]), LOAD);
139 pi0 = to_next[0] = from[0];
140 pi1 = to_next[1] = from[1];
146 p0 = vlib_get_buffer (vm, pi0);
147 p1 = vlib_get_buffer (vm, pi1);
149 ip0 = vlib_buffer_get_current (p0);
150 ip1 = vlib_buffer_get_current (p1);
152 c0 = vnet_get_config_data (&cm->config_main,
153 &p0->current_config_index,
156 c1 = vnet_get_config_data (&cm->config_main,
157 &p1->current_config_index,
161 mtrie0 = &ip4_fib_get (c0->fib_index)->mtrie;
162 mtrie1 = &ip4_fib_get (c1->fib_index)->mtrie;
164 leaf0 = leaf1 = IP4_FIB_MTRIE_LEAF_ROOT;
166 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 0);
167 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 0);
169 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 1);
170 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 1);
172 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 2);
173 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 2);
175 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 3);
176 leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, &ip1->src_address, 3);
178 lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
179 lb_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1);
181 lb0 = load_balance_get(lb_index0);
182 lb1 = load_balance_get(lb_index1);
184 /* Pass multicast. */
185 pass0 = ip4_address_is_multicast (&ip0->src_address) || ip0->src_address.as_u32 == clib_host_to_net_u32(0xFFFFFFFF);
186 pass1 = ip4_address_is_multicast (&ip1->src_address) || ip1->src_address.as_u32 == clib_host_to_net_u32(0xFFFFFFFF);
188 if (IP4_SOURCE_CHECK_REACHABLE_VIA_RX == source_check_type)
190 pass0 |= fib_urpf_check(lb0->lb_urpf,
191 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
192 pass1 |= fib_urpf_check(lb1->lb_urpf,
193 vnet_buffer (p1)->sw_if_index[VLIB_RX]);
197 pass0 |= fib_urpf_check_size(lb0->lb_urpf);
198 pass1 |= fib_urpf_check_size(lb1->lb_urpf);
200 next0 = (pass0 ? next0 : IP4_SOURCE_CHECK_NEXT_DROP);
201 next1 = (pass1 ? next1 : IP4_SOURCE_CHECK_NEXT_DROP);
203 p0->error = error_node->errors[IP4_ERROR_UNICAST_SOURCE_CHECK_FAILS];
204 p1->error = error_node->errors[IP4_ERROR_UNICAST_SOURCE_CHECK_FAILS];
206 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
207 to_next, n_left_to_next,
208 pi0, pi1, next0, next1);
211 while (n_left_from > 0 && n_left_to_next > 0)
215 ip4_fib_mtrie_t * mtrie0;
216 ip4_fib_mtrie_leaf_t leaf0;
217 ip4_source_check_config_t * c0;
218 u32 pi0, next0, pass0, lb_index0;
219 const load_balance_t * lb0;
228 p0 = vlib_get_buffer (vm, pi0);
229 ip0 = vlib_buffer_get_current (p0);
231 c0 = vnet_get_config_data (&cm->config_main,
232 &p0->current_config_index,
236 mtrie0 = &ip4_fib_get (c0->fib_index)->mtrie;
238 leaf0 = IP4_FIB_MTRIE_LEAF_ROOT;
240 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 0);
242 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 1);
244 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 2);
246 leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, &ip0->src_address, 3);
248 lb_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0);
250 lb0 = load_balance_get(lb_index0);
252 /* Pass multicast. */
253 pass0 = ip4_address_is_multicast (&ip0->src_address) || ip0->src_address.as_u32 == clib_host_to_net_u32(0xFFFFFFFF);
255 if (IP4_SOURCE_CHECK_REACHABLE_VIA_RX == source_check_type)
257 pass0 |= fib_urpf_check(lb0->lb_urpf,
258 vnet_buffer (p0)->sw_if_index[VLIB_RX]);
262 pass0 |= fib_urpf_check_size(lb0->lb_urpf);
265 next0 = (pass0 ? next0 : IP4_SOURCE_CHECK_NEXT_DROP);
266 p0->error = error_node->errors[IP4_ERROR_UNICAST_SOURCE_CHECK_FAILS];
268 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
269 to_next, n_left_to_next,
273 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
276 return frame->n_vectors;
280 ip4_source_check_reachable_via_any (vlib_main_t * vm,
281 vlib_node_runtime_t * node,
282 vlib_frame_t * frame)
284 return ip4_source_check_inline (vm, node, frame, IP4_SOURCE_CHECK_REACHABLE_VIA_ANY);
288 ip4_source_check_reachable_via_rx (vlib_main_t * vm,
289 vlib_node_runtime_t * node,
290 vlib_frame_t * frame)
292 return ip4_source_check_inline (vm, node, frame, IP4_SOURCE_CHECK_REACHABLE_VIA_RX);
295 VLIB_REGISTER_NODE (ip4_check_source_reachable_via_any) = {
296 .function = ip4_source_check_reachable_via_any,
297 .name = "ip4-source-check-via-any",
298 .vector_size = sizeof (u32),
300 .n_next_nodes = IP4_SOURCE_CHECK_N_NEXT,
302 [IP4_SOURCE_CHECK_NEXT_DROP] = "error-drop",
305 .format_buffer = format_ip4_header,
306 .format_trace = format_ip4_source_check_trace,
309 VLIB_NODE_FUNCTION_MULTIARCH (ip4_check_source_reachable_via_any,
310 ip4_source_check_reachable_via_any)
312 VLIB_REGISTER_NODE (ip4_check_source_reachable_via_rx) = {
313 .function = ip4_source_check_reachable_via_rx,
314 .name = "ip4-source-check-via-rx",
315 .vector_size = sizeof (u32),
317 .n_next_nodes = IP4_SOURCE_CHECK_N_NEXT,
319 [IP4_SOURCE_CHECK_NEXT_DROP] = "error-drop",
322 .format_buffer = format_ip4_header,
323 .format_trace = format_ip4_source_check_trace,
326 VLIB_NODE_FUNCTION_MULTIARCH (ip4_check_source_reachable_via_rx,
327 ip4_source_check_reachable_via_rx)
329 static clib_error_t *
330 set_ip_source_check (vlib_main_t * vm,
331 unformat_input_t * input,
332 vlib_cli_command_t * cmd)
334 unformat_input_t _line_input, * line_input = &_line_input;
335 vnet_main_t * vnm = vnet_get_main();
336 ip4_main_t * im = &ip4_main;
337 ip_lookup_main_t * lm = &im->lookup_main;
338 ip_config_main_t * rx_cm = &lm->feature_config_mains[VNET_IP_RX_UNICAST_FEAT];
339 clib_error_t * error = 0;
340 u32 sw_if_index, is_del, ci;
341 ip4_source_check_config_t config;
346 feature_index = im->ip4_unicast_rx_feature_source_reachable_via_rx;
348 if (! unformat_user (input, unformat_line_input, line_input))
351 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
353 if (unformat_user (line_input, unformat_vnet_sw_interface, vnm, &sw_if_index))
355 else if (unformat (line_input, "del"))
357 else if (unformat (line_input, "strict"))
358 feature_index = im->ip4_unicast_rx_feature_source_reachable_via_rx;
359 else if (unformat (line_input, "loose"))
360 feature_index = im->ip4_unicast_rx_feature_source_reachable_via_any;
363 error = unformat_parse_error (line_input);
368 if (~0 == sw_if_index)
370 error = clib_error_return (0, "unknown interface `%U'",
371 format_unformat_error, line_input);
375 config.fib_index = im->fib_index_by_sw_if_index[sw_if_index];
376 ci = rx_cm->config_index_by_sw_if_index[sw_if_index];
378 ? vnet_config_del_feature
379 : vnet_config_add_feature)
380 (vm, &rx_cm->config_main,
385 rx_cm->config_index_by_sw_if_index[sw_if_index] = ci;
392 * This command adds the 'ip4-source-check-via-rx' graph node for
393 * a given interface. By adding the IPv4 source check graph node to
394 * an interface, the code verifies that the source address of incoming
395 * unicast packets are reachable over the incoming interface. Two flavours
396 * are supported (the default is strict):
397 * - loose: accept ingress packet if there is a route to reach the source
398 * - strict: accept ingress packet if it arrived on an interface which
399 * the route to the source uses. i.e. an interface that the source
404 * Example of graph node before range checking is enabled:
405 * @cliexstart{show vlib graph ip4-source-check-via-rx}
407 * ip4-source-check-via-rx error-drop [0]
410 * Example of how to enable unicast source checking on an interface:
411 * @cliexcmd{set interface ip source-check GigabitEthernet2/0/0 loose}
413 * Example of graph node after range checking is enabled:
414 * @cliexstart{show vlib graph ip4-source-check-via-rx}
416 * ip4-source-check-via-rx error-drop [0] ip4-input-no-checksum
417 * ip4-source-and-port-range- ip4-input
420 * Example of how to display the feature enabed on an interface:
421 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
422 * IP feature paths configured on GigabitEthernet2/0/0...
425 * ip4-source-check-via-rx
429 * ip4-lookup-multicast
444 * Example of how to disable unicast source checking on an interface:
445 * @cliexcmd{set interface ip source-check GigabitEthernet2/0/0 del}
449 VLIB_CLI_COMMAND (set_interface_ip_source_check_command, static) = {
450 .path = "set interface ip source-check",
451 .function = set_ip_source_check,
452 .short_help = "set interface ip source-check <interface> [strict|loose] [del]",
456 static clib_error_t *
457 ip_source_check_accept (vlib_main_t * vm,
458 unformat_input_t * input,
459 vlib_cli_command_t * cmd)
461 unformat_input_t _line_input, * line_input = &_line_input;
463 .fp_proto = FIB_PROTOCOL_IP4,
465 clib_error_t * error = NULL;
466 u32 table_id, is_add, fib_index;
471 /* Get a line of input. */
472 if (! unformat_user (input, unformat_line_input, line_input))
475 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
477 if (unformat (line_input, "table %d", &table_id))
479 else if (unformat (line_input, "del"))
481 else if (unformat (line_input, "add"))
483 else if (unformat (line_input, "%U/%d",
484 unformat_ip4_address,
487 pfx.fp_proto = FIB_PROTOCOL_IP4;
490 error = unformat_parse_error (line_input);
497 fib_index = fib_table_id_find_fib_index(pfx.fp_proto, table_id);
500 error = clib_error_return (0,
501 "Nonexistent table id %d",
513 fib_table_entry_special_add(fib_index,
515 FIB_SOURCE_URPF_EXEMPT,
521 fib_table_entry_special_remove(fib_index,
523 FIB_SOURCE_URPF_EXEMPT);
531 * Add an exemption for a prefix to pass the Unicast Reverse Path
532 * Forwarding (uRPF) loose check. This is for testing purposes only.
533 * If the '<em>table</em>' is not enter it is defaulted to 0. Default
534 * is to '<em>add</em>'. VPP always performs a loose uRPF check for
538 * Example of how to add a uRPF exception to a FIB table to pass the
540 * @cliexcmd{ip urpf-accept table 7 add}
543 VLIB_CLI_COMMAND (ip_source_check_accept_command, static) = {
544 .path = "ip urpf-accept",
545 .function = ip_source_check_accept,
546 .short_help = "ip urpf-accept [table <table-id>] [add|del]",
551 /* Dummy init function to get us linked in. */
552 clib_error_t * ip4_source_check_init (vlib_main_t * vm)
555 VLIB_INIT_FUNCTION (ip4_source_check_init);