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>
20 static const char *urpf_feat_arcs[N_AF][VLIB_N_DIR] =
23 [VLIB_RX] = "ip4-unicast",
24 [VLIB_TX] = "ip4-output",
27 [VLIB_RX] = "ip6-unicast",
28 [VLIB_TX] = "ip6-output",
32 static const char *urpf_feats[N_AF][VLIB_N_DIR][URPF_N_MODES] =
36 [URPF_MODE_STRICT] = "ip4-rx-urpf-strict",
37 [URPF_MODE_LOOSE] = "ip4-rx-urpf-loose",
40 [URPF_MODE_STRICT] = "ip4-tx-urpf-strict",
41 [URPF_MODE_LOOSE] = "ip4-tx-urpf-loose",
46 [URPF_MODE_STRICT] = "ip6-rx-urpf-strict",
47 [URPF_MODE_LOOSE] = "ip6-rx-urpf-loose",
50 [URPF_MODE_STRICT] = "ip6-tx-urpf-strict",
51 [URPF_MODE_LOOSE] = "ip6-tx-urpf-loose",
57 * Per-af, per-direction, per-interface uRPF configs
60 urpf_data_t *urpf_cfgs[N_AF][VLIB_N_DIR];
63 format_urpf_mode (u8 * s, va_list * a)
65 urpf_mode_t mode = va_arg (*a, int);
71 return (format (s, "%s", b));
76 return (format (s, "unknown"));
80 unformat_urpf_mode (unformat_input_t * input, va_list * args)
82 urpf_mode_t *mode = va_arg (*args, urpf_mode_t *);
87 else if (unformat (input, b)) \
89 *mode = URPF_MODE_##a; \
98 urpf_update (urpf_mode_t mode, u32 sw_if_index, ip_address_family_t af,
99 vlib_dir_t dir, u32 table_id)
101 fib_protocol_t proto;
105 proto = ip_address_family_to_fib_proto (af);
106 fib_index = fib_table_find (proto, table_id);
107 if (fib_index == (~0))
108 return VNET_API_ERROR_INVALID_VALUE;
112 bool is_ip4 = (AF_IP4 == af);
113 u32 *fib_index_by_sw_if_index = is_ip4 ?
114 ip4_main.fib_index_by_sw_if_index :
115 ip6_main.fib_index_by_sw_if_index;
117 fib_index = fib_index_by_sw_if_index[sw_if_index];
120 urpf_mode_t off = URPF_MODE_OFF;
121 urpf_data_t empty = { .fib_index = 0, .mode = off };
122 vec_validate_init_empty (urpf_cfgs[af][dir], sw_if_index, empty);
123 old = urpf_cfgs[af][dir][sw_if_index];
125 urpf_data_t data = { .fib_index = fib_index,
127 .fib_index_is_custom = (table_id != ~0) };
128 urpf_cfgs[af][dir][sw_if_index] = data;
129 if (data.mode != old.mode || data.fib_index != old.fib_index)
131 if (URPF_MODE_OFF != old.mode)
132 /* disable what we have */
133 vnet_feature_enable_disable (urpf_feat_arcs[af][dir],
134 urpf_feats[af][dir][old.mode],
135 sw_if_index, 0, 0, 0);
137 if (URPF_MODE_OFF != data.mode)
138 /* enable what's new */
139 vnet_feature_enable_disable (urpf_feat_arcs[af][dir],
140 urpf_feats[af][dir][data.mode],
141 sw_if_index, 1, 0, 0);
143 /* else - no change to existing config */
148 urpf_table_bind_v4 (ip4_main_t *im, uword opaque, u32 sw_if_index,
149 u32 new_fib_index, u32 old_fib_index)
152 urpf_data_t empty = { .fib_index = 0, .mode = URPF_MODE_OFF };
153 FOREACH_VLIB_DIR (dir)
155 vec_validate_init_empty (urpf_cfgs[AF_IP4][dir], sw_if_index, empty);
156 if (!urpf_cfgs[AF_IP4][dir][sw_if_index].fib_index_is_custom)
158 urpf_cfgs[AF_IP4][dir][sw_if_index].fib_index = new_fib_index;
164 urpf_table_bind_v6 (ip6_main_t *im, uword opaque, u32 sw_if_index,
165 u32 new_fib_index, u32 old_fib_index)
168 urpf_data_t empty = { .fib_index = 0, .mode = URPF_MODE_OFF };
169 FOREACH_VLIB_DIR (dir)
171 vec_validate_init_empty (urpf_cfgs[AF_IP6][dir], sw_if_index, empty);
172 if (!urpf_cfgs[AF_IP6][dir][sw_if_index].fib_index_is_custom)
174 urpf_cfgs[AF_IP6][dir][sw_if_index].fib_index = new_fib_index;
179 static clib_error_t *
180 urpf_init (vlib_main_t *vm)
182 ip4_table_bind_callback_t cb4 = {
183 .function = urpf_table_bind_v4,
185 vec_add1 (ip4_main.table_bind_callbacks, cb4);
187 ip6_table_bind_callback_t cb6 = {
188 .function = urpf_table_bind_v6,
190 vec_add1 (ip6_main.table_bind_callbacks, cb6);
194 VLIB_INIT_FUNCTION (urpf_init);
196 static clib_error_t *
197 urpf_cli_update (vlib_main_t * vm,
198 unformat_input_t * input, vlib_cli_command_t * cmd)
200 unformat_input_t _line_input, *line_input = &_line_input;
201 vnet_main_t *vnm = vnet_get_main ();
202 clib_error_t *error = NULL;
203 ip_address_family_t af;
212 mode = URPF_MODE_STRICT;
215 if (!unformat_user (input, unformat_line_input, line_input))
218 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
220 if (unformat (line_input, "%U",
221 unformat_vnet_sw_interface, vnm, &sw_if_index))
223 else if (unformat (line_input, "%U", unformat_urpf_mode, &mode))
225 else if (unformat (line_input, "table %d", &table_id))
227 else if (unformat (line_input, "%U", unformat_ip_address_family, &af))
229 else if (unformat (line_input, "%U", unformat_vlib_rx_tx, &dir))
233 error = unformat_parse_error (line_input);
238 if (~0 == sw_if_index)
240 error = clib_error_return (0, "unknown interface `%U'",
241 format_unformat_error, line_input);
246 rv = urpf_update (mode, sw_if_index, af, dir, table_id);
249 error = clib_error_return (0, "unknown table id");
253 unformat_free (line_input);
259 * This command configures uRPF on an interface.
260 * Two flavours are supported (the default is strict):
261 * - loose: accept ingress packet if there is a route to reach the source
262 * - strict: accept ingress packet if it arrived on an interface which
263 * the route to the source uses. i.e. an interface that the source
268 * Example of graph node before range checking is enabled:
269 * @cliexstart{show vlib graph ip4-rx-urpf-strict}
271 * ip4-rx-urpf-strict ip4-drop [0]
274 * Example of how to enable unicast source checking on an interface:
275 * @cliexcmd{set urpf ip4 rx GigabitEthernet2/0/0 loose}
277 * Example of graph node after range checking is enabled:
278 * @cliexstart{show vlib graph ip4-rx-urpf-loose}
280 * ip4-rx-urpf-loose ip4-drop [0] ip4-input-no-checksum
281 * ip4-source-and-port-range- ip4-input
284 * Example of how to display the feature enabled on an interface:
285 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
286 * IP feature paths configured on GigabitEthernet2/0/0...
293 * ip4-lookup-multicast
308 * Example of how to disable unicast source checking on an interface:
309 * @cliexcmd{set urpf ip4 off GigabitEthernet2/0/0}
312 VLIB_CLI_COMMAND (set_interface_ip_source_check_command, static) = {
314 .function = urpf_cli_update,
315 .short_help = "set urpf [ip4|ip6] [rx|tx] [off|strict|loose] "
316 "<INTERFACE> [table <table>]",
319 static clib_error_t *
320 urpf_cli_accept (vlib_main_t * vm,
321 unformat_input_t * input, vlib_cli_command_t * cmd)
323 unformat_input_t _line_input, *line_input = &_line_input;
324 clib_error_t *error = NULL;
327 u32 table_id, is_add, fib_index;
332 /* Get a line of input. */
333 if (!unformat_user (input, unformat_line_input, line_input))
336 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
338 if (unformat (line_input, "table %d", &table_id))
340 else if (unformat (line_input, "del"))
342 else if (unformat (line_input, "add"))
344 else if (unformat (line_input, "%U", unformat_ip_prefix, &pfx))
348 error = unformat_parse_error (line_input);
353 ip_prefix_to_fib_prefix (&pfx, &fpfx);
355 fib_index = fib_table_find (fpfx.fp_proto, table_id);
359 error = clib_error_return (0, "Nonexistent table id %d", table_id);
364 fib_table_entry_special_add (fib_index,
366 FIB_SOURCE_URPF_EXEMPT, FIB_ENTRY_FLAG_DROP);
368 fib_table_entry_special_remove (fib_index, &fpfx, FIB_SOURCE_URPF_EXEMPT);
371 unformat_free (line_input);
377 * Add an exemption for a prefix to pass the Unicast Reverse Path
378 * Forwarding (uRPF) loose check. This is for testing purposes only.
379 * If the '<em>table</em>' is not enter it is defaulted to 0. Default
380 * is to '<em>add</em>'. VPP always performs a loose uRPF check for
384 * Example of how to add a uRPF exception to a FIB table to pass the
386 * @cliexcmd{set urpf-accept table 7 10.0.0.0/8 add}
388 VLIB_CLI_COMMAND (urpf_accept_command, static) = {
389 .path = "set urpf-accept",
390 .function = urpf_cli_accept,
391 .short_help = "urpf-accept [table <table-id>] [add|del] <PREFIX>",
395 * fd.io coding-style-patch-verification: ON
398 * eval: (c-set-style "gnu")