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
62 urpf_data_t *urpf_cfgs[N_AF][VLIB_N_DIR];
65 format_urpf_mode (u8 * s, va_list * a)
67 urpf_mode_t mode = va_arg (*a, int);
73 return (format (s, "%s", b));
78 return (format (s, "unknown"));
82 unformat_urpf_mode (unformat_input_t * input, va_list * args)
84 urpf_mode_t *mode = va_arg (*args, urpf_mode_t *);
89 else if (unformat (input, b)) \
91 *mode = URPF_MODE_##a; \
100 urpf_update (urpf_mode_t mode, u32 sw_if_index, ip_address_family_t af,
101 vlib_dir_t dir, u32 table_id)
103 fib_protocol_t proto;
107 proto = ip_address_family_to_fib_proto (af);
108 fib_index = fib_table_find (proto, table_id);
109 if (fib_index == (~0))
110 return VNET_API_ERROR_INVALID_VALUE;
114 bool is_ip4 = (AF_IP4 == af);
115 u32 *fib_index_by_sw_if_index = is_ip4 ?
116 ip4_main.fib_index_by_sw_if_index :
117 ip6_main.fib_index_by_sw_if_index;
119 fib_index = fib_index_by_sw_if_index[sw_if_index];
122 urpf_mode_t off = URPF_MODE_OFF;
123 urpf_data_t empty = { .fib_index = 0, .mode = off };
124 vec_validate_init_empty (urpf_cfgs[af][dir], sw_if_index, empty);
125 old = urpf_cfgs[af][dir][sw_if_index];
127 urpf_data_t data = { .fib_index = fib_index,
129 .fib_index_is_custom = (table_id != ~0) };
130 urpf_cfgs[af][dir][sw_if_index] = data;
131 if (data.mode != old.mode || data.fib_index != old.fib_index)
133 if (URPF_MODE_OFF != old.mode)
134 /* disable what we have */
135 vnet_feature_enable_disable (urpf_feat_arcs[af][dir],
136 urpf_feats[af][dir][old.mode],
137 sw_if_index, 0, 0, 0);
139 if (URPF_MODE_OFF != data.mode)
140 /* enable what's new */
141 vnet_feature_enable_disable (urpf_feat_arcs[af][dir],
142 urpf_feats[af][dir][data.mode],
143 sw_if_index, 1, 0, 0);
145 /* else - no change to existing config */
150 urpf_table_bind_v4 (ip4_main_t *im, uword opaque, u32 sw_if_index,
151 u32 new_fib_index, u32 old_fib_index)
154 urpf_data_t empty = { .fib_index = 0, .mode = URPF_MODE_OFF };
155 FOREACH_VLIB_DIR (dir)
157 vec_validate_init_empty (urpf_cfgs[AF_IP4][dir], sw_if_index, empty);
158 if (!urpf_cfgs[AF_IP4][dir][sw_if_index].fib_index_is_custom)
160 urpf_cfgs[AF_IP4][dir][sw_if_index].fib_index = new_fib_index;
166 urpf_table_bind_v6 (ip6_main_t *im, uword opaque, u32 sw_if_index,
167 u32 new_fib_index, u32 old_fib_index)
170 urpf_data_t empty = { .fib_index = 0, .mode = URPF_MODE_OFF };
171 FOREACH_VLIB_DIR (dir)
173 vec_validate_init_empty (urpf_cfgs[AF_IP6][dir], sw_if_index, empty);
174 if (!urpf_cfgs[AF_IP6][dir][sw_if_index].fib_index_is_custom)
176 urpf_cfgs[AF_IP6][dir][sw_if_index].fib_index = new_fib_index;
181 static clib_error_t *
182 urpf_init (vlib_main_t *vm)
184 ip4_table_bind_callback_t cb4 = {
185 .function = urpf_table_bind_v4,
187 vec_add1 (ip4_main.table_bind_callbacks, cb4);
189 ip6_table_bind_callback_t cb6 = {
190 .function = urpf_table_bind_v6,
192 vec_add1 (ip6_main.table_bind_callbacks, cb6);
196 VLIB_INIT_FUNCTION (urpf_init);
198 static clib_error_t *
199 urpf_cli_update (vlib_main_t * vm,
200 unformat_input_t * input, vlib_cli_command_t * cmd)
202 unformat_input_t _line_input, *line_input = &_line_input;
203 vnet_main_t *vnm = vnet_get_main ();
204 clib_error_t *error = NULL;
205 ip_address_family_t af;
214 mode = URPF_MODE_STRICT;
217 if (!unformat_user (input, unformat_line_input, line_input))
220 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
222 if (unformat (line_input, "%U",
223 unformat_vnet_sw_interface, vnm, &sw_if_index))
225 else if (unformat (line_input, "%U", unformat_urpf_mode, &mode))
227 else if (unformat (line_input, "table %d", &table_id))
229 else if (unformat (line_input, "%U", unformat_ip_address_family, &af))
231 else if (unformat (line_input, "%U", unformat_vlib_rx_tx, &dir))
235 error = unformat_parse_error (line_input);
240 if (~0 == sw_if_index)
242 error = clib_error_return (0, "unknown interface `%U'",
243 format_unformat_error, line_input);
248 rv = urpf_update (mode, sw_if_index, af, dir, table_id);
251 error = clib_error_return (0, "unknown table id");
255 unformat_free (line_input);
261 * This command configures uRPF on an interface.
262 * Two flavours are supported (the default is strict):
263 * - loose: accept ingress packet if there is a route to reach the source
264 * - strict: accept ingress packet if it arrived on an interface which
265 * the route to the source uses. i.e. an interface that the source
270 * Example of graph node before range checking is enabled:
271 * @cliexstart{show vlib graph ip4-rx-urpf-strict}
273 * ip4-rx-urpf-strict ip4-drop [0]
276 * Example of how to enable unicast source checking on an interface:
277 * @cliexcmd{set urpf ip4 rx GigabitEthernet2/0/0 loose}
279 * Example of graph node after range checking is enabled:
280 * @cliexstart{show vlib graph ip4-rx-urpf-loose}
282 * ip4-rx-urpf-loose ip4-drop [0] ip4-input-no-checksum
283 * ip4-source-and-port-range- ip4-input
286 * Example of how to display the feature enabled on an interface:
287 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
288 * IP feature paths configured on GigabitEthernet2/0/0...
295 * ip4-lookup-multicast
310 * Example of how to disable unicast source checking on an interface:
311 * @cliexcmd{set urpf ip4 off GigabitEthernet2/0/0}
315 VLIB_CLI_COMMAND (set_interface_ip_source_check_command, static) = {
317 .function = urpf_cli_update,
318 .short_help = "set urpf [ip4|ip6] [rx|tx] [off|strict|loose] "
319 "<INTERFACE> [table <table>]",
323 static clib_error_t *
324 urpf_cli_accept (vlib_main_t * vm,
325 unformat_input_t * input, vlib_cli_command_t * cmd)
327 unformat_input_t _line_input, *line_input = &_line_input;
328 clib_error_t *error = NULL;
331 u32 table_id, is_add, fib_index;
336 /* Get a line of input. */
337 if (!unformat_user (input, unformat_line_input, line_input))
340 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
342 if (unformat (line_input, "table %d", &table_id))
344 else if (unformat (line_input, "del"))
346 else if (unformat (line_input, "add"))
348 else if (unformat (line_input, "%U", unformat_ip_prefix, &pfx))
352 error = unformat_parse_error (line_input);
357 ip_prefix_to_fib_prefix (&pfx, &fpfx);
359 fib_index = fib_table_find (fpfx.fp_proto, table_id);
363 error = clib_error_return (0, "Nonexistent table id %d", table_id);
368 fib_table_entry_special_add (fib_index,
370 FIB_SOURCE_URPF_EXEMPT, FIB_ENTRY_FLAG_DROP);
372 fib_table_entry_special_remove (fib_index, &fpfx, FIB_SOURCE_URPF_EXEMPT);
375 unformat_free (line_input);
381 * Add an exemption for a prefix to pass the Unicast Reverse Path
382 * Forwarding (uRPF) loose check. This is for testing purposes only.
383 * If the '<em>table</em>' is not enter it is defaulted to 0. Default
384 * is to '<em>add</em>'. VPP always performs a loose uRPF check for
388 * Example of how to add a uRPF exception to a FIB table to pass the
390 * @cliexcmd{set urpf-accept table 7 10.0.0.0/8 add}
393 VLIB_CLI_COMMAND (urpf_accept_command, static) = {
394 .path = "set urpf-accept",
395 .function = urpf_cli_accept,
396 .short_help = "urpf-accept [table <table-id>] [add|del] <PREFIX>",
401 * fd.io coding-style-patch-verification: ON
404 * eval: (c-set-style "gnu")