2 * Copyright (c) 2020 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 #include <urpf/urpf.h>
18 #include <vnet/fib/fib_table.h>
21 static const char *urpf_feat_arcs[N_AF][VLIB_N_DIR] =
24 [VLIB_RX] = "ip4-unicast",
25 [VLIB_TX] = "ip4-output",
28 [VLIB_RX] = "ip6-unicast",
29 [VLIB_TX] = "ip6-output",
33 static const char *urpf_feats[N_AF][VLIB_N_DIR][URPF_N_MODES] =
37 [URPF_MODE_STRICT] = "ip4-rx-urpf-strict",
38 [URPF_MODE_LOOSE] = "ip4-rx-urpf-loose",
41 [URPF_MODE_STRICT] = "ip4-tx-urpf-strict",
42 [URPF_MODE_LOOSE] = "ip4-tx-urpf-loose",
47 [URPF_MODE_STRICT] = "ip6-rx-urpf-strict",
48 [URPF_MODE_LOOSE] = "ip6-rx-urpf-loose",
51 [URPF_MODE_STRICT] = "ip6-tx-urpf-strict",
52 [URPF_MODE_LOOSE] = "ip6-tx-urpf-loose",
59 * Per-af, per-direction, per-interface uRPF configs
61 static urpf_mode_t *urpf_cfgs[N_AF][VLIB_N_DIR];
64 format_urpf_mode (u8 * s, va_list * a)
66 urpf_mode_t mode = va_arg (*a, int);
72 return (format (s, "%s", b));
77 return (format (s, "unknown"));
81 unformat_urpf_mode (unformat_input_t * input, va_list * args)
83 urpf_mode_t *mode = va_arg (*args, urpf_mode_t *);
88 else if (unformat (input, b)) \
90 *mode = URPF_MODE_##a; \
99 urpf_update (urpf_mode_t mode,
100 u32 sw_if_index, ip_address_family_t af, vlib_dir_t dir)
104 vec_validate_init_empty (urpf_cfgs[af][dir], sw_if_index, URPF_MODE_OFF);
105 old = urpf_cfgs[af][dir][sw_if_index];
109 if (URPF_MODE_OFF != old)
110 /* disable what we have */
111 vnet_feature_enable_disable (urpf_feat_arcs[af][dir],
112 urpf_feats[af][dir][old],
113 sw_if_index, 0, 0, 0);
115 if (URPF_MODE_OFF != mode)
116 /* enable what's new */
117 vnet_feature_enable_disable (urpf_feat_arcs[af][dir],
118 urpf_feats[af][dir][mode],
119 sw_if_index, 1, 0, 0);
121 /* else - no change to existing config */
123 urpf_cfgs[af][dir][sw_if_index] = mode;
126 static clib_error_t *
127 urpf_cli_update (vlib_main_t * vm,
128 unformat_input_t * input, vlib_cli_command_t * cmd)
130 unformat_input_t _line_input, *line_input = &_line_input;
131 vnet_main_t *vnm = vnet_get_main ();
132 clib_error_t *error = NULL;
133 ip_address_family_t af;
141 mode = URPF_MODE_STRICT;
143 if (!unformat_user (input, unformat_line_input, line_input))
146 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
148 if (unformat (line_input, "%U",
149 unformat_vnet_sw_interface, vnm, &sw_if_index))
151 else if (unformat (line_input, "%U", unformat_urpf_mode, &mode))
153 else if (unformat (line_input, "%U", unformat_ip_address_family, &af))
155 else if (unformat (line_input, "%U", unformat_vlib_rx_tx, &dir))
159 error = unformat_parse_error (line_input);
164 if (~0 == sw_if_index)
166 error = clib_error_return (0, "unknown interface `%U'",
167 format_unformat_error, line_input);
171 urpf_update (mode, sw_if_index, af, dir);
173 unformat_free (line_input);
179 * This command configures uRPF on an interface.
180 * Two flavours are supported (the default is strict):
181 * - loose: accept ingress packet if there is a route to reach the source
182 * - strict: accept ingress packet if it arrived on an interface which
183 * the route to the source uses. i.e. an interface that the source
188 * Example of graph node before range checking is enabled:
189 * @cliexstart{show vlib graph ip4-rx-urpf-strict}
191 * ip4-rx-urpf-strict ip4-drop [0]
194 * Example of how to enable unicast source checking on an interface:
195 * @cliexcmd{set urpf ip4 rx GigabitEthernet2/0/0 loose}
197 * Example of graph node after range checking is enabled:
198 * @cliexstart{show vlib graph ip4-rx-urpf-loose}
200 * ip4-rx-urpf-loose ip4-drop [0] ip4-input-no-checksum
201 * ip4-source-and-port-range- ip4-input
204 * Example of how to display the feature enabed on an interface:
205 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
206 * IP feature paths configured on GigabitEthernet2/0/0...
213 * ip4-lookup-multicast
228 * Example of how to disable unicast source checking on an interface:
229 * @cliexcmd{set urpf ip4 off GigabitEthernet2/0/0}
233 VLIB_CLI_COMMAND (set_interface_ip_source_check_command, static) = {
235 .function = urpf_cli_update,
236 .short_help = "set urpf [ip4|ip6] [rx|tx] [off|strict|loose] <INTERFACE>",
240 static clib_error_t *
241 urpf_cli_accept (vlib_main_t * vm,
242 unformat_input_t * input, vlib_cli_command_t * cmd)
244 unformat_input_t _line_input, *line_input = &_line_input;
245 clib_error_t *error = NULL;
248 u32 table_id, is_add, fib_index;
253 /* Get a line of input. */
254 if (!unformat_user (input, unformat_line_input, line_input))
257 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
259 if (unformat (line_input, "table %d", &table_id))
261 else if (unformat (line_input, "del"))
263 else if (unformat (line_input, "add"))
265 else if (unformat (line_input, "%U", unformat_ip_prefix, &pfx))
269 error = unformat_parse_error (line_input);
274 ip_prefix_to_fib_prefix (&pfx, &fpfx);
276 fib_index = fib_table_find (fpfx.fp_proto, table_id);
280 error = clib_error_return (0, "Nonexistent table id %d", table_id);
285 fib_table_entry_special_add (fib_index,
287 FIB_SOURCE_URPF_EXEMPT, FIB_ENTRY_FLAG_DROP);
289 fib_table_entry_special_remove (fib_index, &fpfx, FIB_SOURCE_URPF_EXEMPT);
292 unformat_free (line_input);
298 * Add an exemption for a prefix to pass the Unicast Reverse Path
299 * Forwarding (uRPF) loose check. This is for testing purposes only.
300 * If the '<em>table</em>' is not enter it is defaulted to 0. Default
301 * is to '<em>add</em>'. VPP always performs a loose uRPF check for
305 * Example of how to add a uRPF exception to a FIB table to pass the
307 * @cliexcmd{set urpf-accept table 7 10.0.0.0/8 add}
310 VLIB_CLI_COMMAND (urpf_accept_command, static) = {
311 .path = "set urpf-accept",
312 .function = urpf_cli_accept,
313 .short_help = "urpf-accept [table <table-id>] [add|del] <PREFIX>",
318 * fd.io coding-style-patch-verification: ON
321 * eval: (c-set-style "gnu")