2 * Copyright (c) 2018 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.
20 #include <nat/nat66/nat66.h>
21 #include <vnet/fib/fib_table.h>
23 #define NAT66_EXPECTED_ARGUMENT "expected required argument(s)"
24 #define NAT66_PLUGIN_DISABLED "error plugin disabled"
26 #define CHECK_ENABLED() \
29 if (PREDICT_FALSE (!nat66_main.enabled)) \
31 return clib_error_return (0, NAT66_PLUGIN_DISABLED); \
37 nat66_enable_disable_command_fn (vlib_main_t *vm, unformat_input_t *input,
38 vlib_cli_command_t *cmd)
40 nat66_main_t *nm = &nat66_main;
41 unformat_input_t _line_input, *line_input = &_line_input;
42 clib_error_t *error = 0;
45 u8 enable_set = 0, enable = 0;
47 if (!unformat_user (input, unformat_line_input, line_input))
48 return clib_error_return (0, NAT66_EXPECTED_ARGUMENT);
50 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
52 if (unformat (line_input, "outside-vrf %u", &outside_vrf))
57 if (unformat (line_input, "disable"))
59 else if (unformat (line_input, "enable"))
64 error = clib_error_return (0, "unknown input '%U'",
65 format_unformat_error, line_input);
72 error = clib_error_return (0, "expected enable | disable");
80 error = clib_error_return (0, "already enabled");
84 if (nat66_plugin_enable (outside_vrf) != 0)
85 error = clib_error_return (0, "enable failed");
91 error = clib_error_return (0, "already disabled");
95 if (nat66_plugin_disable () != 0)
96 error = clib_error_return (0, "disable failed");
100 unformat_free (line_input);
104 static clib_error_t *
105 nat66_interface_feature_command_fn (vlib_main_t * vm,
106 unformat_input_t * input,
107 vlib_cli_command_t * cmd)
109 unformat_input_t _line_input, *line_input = &_line_input;
110 vnet_main_t *vnm = vnet_get_main ();
111 clib_error_t *error = 0;
113 u32 *inside_sw_if_indices = 0;
114 u32 *outside_sw_if_indices = 0;
120 if (!unformat_user (input, unformat_line_input, line_input))
121 return clib_error_return (0, NAT66_EXPECTED_ARGUMENT);
123 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
125 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
127 vec_add1 (inside_sw_if_indices, sw_if_index);
128 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
130 vec_add1 (outside_sw_if_indices, sw_if_index);
131 else if (unformat (line_input, "del"))
135 error = clib_error_return (0, "unknown input '%U'",
136 format_unformat_error, line_input);
141 if (vec_len (inside_sw_if_indices))
143 for (i = 0; i < vec_len (inside_sw_if_indices); i++)
145 sw_if_index = inside_sw_if_indices[i];
146 rv = nat66_interface_add_del (sw_if_index, 1, is_add);
149 case VNET_API_ERROR_NO_SUCH_ENTRY:
151 clib_error_return (0, "%U NAT66 feature not enabled.",
152 format_vnet_sw_interface_name, vnm,
153 vnet_get_sw_interface (vnm, sw_if_index));
155 case VNET_API_ERROR_VALUE_EXIST:
157 clib_error_return (0, "%U NAT66 feature already enabled.",
158 format_vnet_sw_interface_name, vnm,
159 vnet_get_sw_interface (vnm, sw_if_index));
161 case VNET_API_ERROR_INVALID_VALUE:
162 case VNET_API_ERROR_INVALID_VALUE_2:
164 clib_error_return (0,
165 "%U NAT66 feature enable/disable failed.",
166 format_vnet_sw_interface_name, vnm,
167 vnet_get_sw_interface (vnm, sw_if_index));
176 if (vec_len (outside_sw_if_indices))
178 for (i = 0; i < vec_len (outside_sw_if_indices); i++)
180 sw_if_index = outside_sw_if_indices[i];
181 rv = nat66_interface_add_del (sw_if_index, 0, is_add);
184 case VNET_API_ERROR_NO_SUCH_ENTRY:
186 clib_error_return (0, "%U NAT66 feature not enabled.",
187 format_vnet_sw_interface_name, vnm,
188 vnet_get_sw_interface (vnm, sw_if_index));
190 case VNET_API_ERROR_VALUE_EXIST:
192 clib_error_return (0, "%U NAT66 feature already enabled.",
193 format_vnet_sw_interface_name, vnm,
194 vnet_get_sw_interface (vnm, sw_if_index));
196 case VNET_API_ERROR_INVALID_VALUE:
197 case VNET_API_ERROR_INVALID_VALUE_2:
199 clib_error_return (0,
200 "%U NAT66 feature enable/disable failed.",
201 format_vnet_sw_interface_name, vnm,
202 vnet_get_sw_interface (vnm, sw_if_index));
212 unformat_free (line_input);
213 vec_free (inside_sw_if_indices);
214 vec_free (outside_sw_if_indices);
220 nat66_cli_interface_walk (nat66_interface_t * i, void *ctx)
222 vlib_main_t *vm = ctx;
223 vnet_main_t *vnm = vnet_get_main ();
224 vlib_cli_output (vm, " %U %s", format_vnet_sw_interface_name, vnm,
225 vnet_get_sw_interface (vnm, i->sw_if_index),
226 nat66_interface_is_inside (i) ? "in" : "out");
230 static clib_error_t *
231 nat66_show_interfaces_command_fn (vlib_main_t * vm, unformat_input_t * input,
232 vlib_cli_command_t * cmd)
235 vlib_cli_output (vm, "NAT66 interfaces:");
236 nat66_interfaces_walk (nat66_cli_interface_walk, vm);
240 static clib_error_t *
241 nat66_add_del_static_mapping_command_fn (vlib_main_t * vm,
242 unformat_input_t * input,
243 vlib_cli_command_t * cmd)
245 unformat_input_t _line_input, *line_input = &_line_input;
246 clib_error_t *error = 0;
247 ip6_address_t l_addr, e_addr;
254 if (!unformat_user (input, unformat_line_input, line_input))
255 return clib_error_return (0, NAT66_EXPECTED_ARGUMENT);
257 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
259 if (unformat (line_input, "local %U external %U",
260 unformat_ip6_address, &l_addr,
261 unformat_ip6_address, &e_addr))
263 else if (unformat (line_input, "vrf %u", &vrf_id))
265 else if (unformat (line_input, "del"))
269 error = clib_error_return (0, "unknown input: '%U'",
270 format_unformat_error, line_input);
275 rv = nat66_static_mapping_add_del (&l_addr, &e_addr, vrf_id, is_add);
279 case VNET_API_ERROR_NO_SUCH_ENTRY:
280 error = clib_error_return (0, "NAT66 static mapping entry not exist.");
282 case VNET_API_ERROR_VALUE_EXIST:
283 error = clib_error_return (0, "NAT66 static mapping entry exist.");
290 unformat_free (line_input);
296 nat66_cli_static_mapping_walk (nat66_static_mapping_t * sm, void *ctx)
298 nat66_main_t *nm = &nat66_main;
299 vlib_main_t *vm = ctx;
303 fib = fib_table_get (sm->fib_index, FIB_PROTOCOL_IP6);
307 vlib_get_combined_counter (&nm->session_counters, sm - nm->sm, &vc);
309 vlib_cli_output (vm, " local %U external %U vrf %d",
310 format_ip6_address, &sm->l_addr,
311 format_ip6_address, &sm->e_addr, fib->ft_table_id);
312 vlib_cli_output (vm, " total pkts %lld, total bytes %lld", vc.packets,
318 static clib_error_t *
319 nat66_show_static_mappings_command_fn (vlib_main_t * vm,
320 unformat_input_t * input,
321 vlib_cli_command_t * cmd)
324 vlib_cli_output (vm, "NAT66 static mappings:");
325 nat66_static_mappings_walk (nat66_cli_static_mapping_walk, vm);
332 * To enable NAT66 plugin
334 * To disable NAT66 plugin
336 * To enable NAT66 plugin with outside-vrf id 10
337 * vpp# nat66 enable outside-vrf 10
340 VLIB_CLI_COMMAND (nat66_enable_disable_command, static) = {
342 .short_help = "nat66 <enable [outside-vrf <vrf-id>]>|disable",
343 .function = nat66_enable_disable_command_fn,
348 * @cliexstart{set interface nat66}
349 * Enable/disable NAT66 feature on the interface.
350 * To enable NAT66 feature with local (IPv6) network interface
351 * GigabitEthernet0/8/0 and external (IPv4) network interface
352 * GigabitEthernet0/a/0 use:
353 * vpp# set interface nat66 in GigabitEthernet0/8/0 out GigabitEthernet0/a/0
356 VLIB_CLI_COMMAND (set_interface_nat66_command, static) = {
357 .path = "set interface nat66",
358 .short_help = "set interface nat66 in|out <intfc> [del]",
359 .function = nat66_interface_feature_command_fn,
364 * @cliexstart{show nat66 interfaces}
365 * Show interfaces with NAT66 feature.
366 * To show interfaces with NAT66 feature use:
367 * vpp# show nat66 interfaces
369 * GigabitEthernet0/8/0 in
370 * GigabitEthernet0/a/0 out
373 VLIB_CLI_COMMAND (show_nat66_interfaces_command, static) = {
374 .path = "show nat66 interfaces",
375 .short_help = "show nat66 interfaces",
376 .function = nat66_show_interfaces_command_fn,
381 * @cliexstart{nat66 add static mapping}
382 * Add/delete NAT66 static mapping entry.
383 * To add NAT66 static mapping entry use:
384 * vpp# nat66 add static mapping local fd01:1::4 external 2001:db8:c000:223::
385 * vpp# nat66 add static mapping local fd01:1::2 external 2001:db8:c000:221:: vrf 10
388 VLIB_CLI_COMMAND (show_nat66_add_del_static_mapping_command, static) = {
389 .path = "nat66 add static mapping",
390 .short_help = "nat66 add static mapping local <ip6-addr> external <ip6-addr>"
391 " [vfr <table-id>] [del]",
392 .function = nat66_add_del_static_mapping_command_fn,
397 * @cliexstart{show nat66 static mappings}
398 * Show NAT66 static mappings.
399 * To show NAT66 static mappings use:
400 * vpp# show nat66 static mappings
401 * NAT66 static mappings:
402 * local fd01:1::4 external 2001:db8:c000:223:: vrf 0
403 * local fd01:1::2 external 2001:db8:c000:221:: vrf 10
406 VLIB_CLI_COMMAND (show_nat66_static_mappings_command, static) = {
407 .path = "show nat66 static mappings",
408 .short_help = "show nat66 static mappings",
409 .function = nat66_show_static_mappings_command_fn,
413 * fd.io coding-style-patch-verification: ON
416 * eval: (c-set-style "gnu")