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>
24 nat66_enable_command_fn (vlib_main_t *vm, unformat_input_t *input,
25 vlib_cli_command_t *cmd)
27 nat66_main_t *nm = &nat66_main;
28 unformat_input_t _line_input, *line_input = &_line_input;
29 clib_error_t *error = 0;
33 return clib_error_return (0, "nat66 already enabled");
35 /* Get a line of input. */
36 if (!unformat_user (input, unformat_line_input, line_input))
38 if (nat66_plugin_enable (outside_vrf) != 0)
39 return clib_error_return (0, "nat66 enable failed");
43 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
45 if (unformat (line_input, "outside-vrf %u", &outside_vrf))
49 error = clib_error_return (0, "unknown input '%U'",
50 format_unformat_error, line_input);
55 if (nat66_plugin_enable (outside_vrf) != 0)
56 error = clib_error_return (0, "nat66 enable failed");
58 unformat_free (line_input);
63 nat66_disable_command_fn (vlib_main_t *vm, unformat_input_t *input,
64 vlib_cli_command_t *cmd)
66 nat66_main_t *nm = &nat66_main;
67 clib_error_t *error = 0;
70 return clib_error_return (0, "nat66 already disabled");
72 if (nat66_plugin_disable () != 0)
73 error = clib_error_return (0, "nat66 disable failed");
79 nat66_interface_feature_command_fn (vlib_main_t * vm,
80 unformat_input_t * input,
81 vlib_cli_command_t * cmd)
83 unformat_input_t _line_input, *line_input = &_line_input;
84 vnet_main_t *vnm = vnet_get_main ();
85 nat66_main_t *nm = &nat66_main;
86 clib_error_t *error = 0;
88 u32 *inside_sw_if_indices = 0;
89 u32 *outside_sw_if_indices = 0;
94 return clib_error_return (0, "nat66 disabled");
96 /* Get a line of input. */
97 if (!unformat_user (input, unformat_line_input, line_input))
100 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
102 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
104 vec_add1 (inside_sw_if_indices, sw_if_index);
105 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
107 vec_add1 (outside_sw_if_indices, sw_if_index);
108 else if (unformat (line_input, "del"))
112 error = clib_error_return (0, "unknown input '%U'",
113 format_unformat_error, line_input);
118 if (vec_len (inside_sw_if_indices))
120 for (i = 0; i < vec_len (inside_sw_if_indices); i++)
122 sw_if_index = inside_sw_if_indices[i];
123 rv = nat66_interface_add_del (sw_if_index, 1, is_add);
126 case VNET_API_ERROR_NO_SUCH_ENTRY:
128 clib_error_return (0, "%U NAT66 feature not enabled.",
129 format_vnet_sw_interface_name, vnm,
130 vnet_get_sw_interface (vnm, sw_if_index));
132 case VNET_API_ERROR_VALUE_EXIST:
134 clib_error_return (0, "%U NAT66 feature already enabled.",
135 format_vnet_sw_interface_name, vnm,
136 vnet_get_sw_interface (vnm, sw_if_index));
138 case VNET_API_ERROR_INVALID_VALUE:
139 case VNET_API_ERROR_INVALID_VALUE_2:
141 clib_error_return (0,
142 "%U NAT66 feature enable/disable failed.",
143 format_vnet_sw_interface_name, vnm,
144 vnet_get_sw_interface (vnm, sw_if_index));
153 if (vec_len (outside_sw_if_indices))
155 for (i = 0; i < vec_len (outside_sw_if_indices); i++)
157 sw_if_index = outside_sw_if_indices[i];
158 rv = nat66_interface_add_del (sw_if_index, 0, is_add);
161 case VNET_API_ERROR_NO_SUCH_ENTRY:
163 clib_error_return (0, "%U NAT66 feature not enabled.",
164 format_vnet_sw_interface_name, vnm,
165 vnet_get_sw_interface (vnm, sw_if_index));
167 case VNET_API_ERROR_VALUE_EXIST:
169 clib_error_return (0, "%U NAT66 feature already enabled.",
170 format_vnet_sw_interface_name, vnm,
171 vnet_get_sw_interface (vnm, sw_if_index));
173 case VNET_API_ERROR_INVALID_VALUE:
174 case VNET_API_ERROR_INVALID_VALUE_2:
176 clib_error_return (0,
177 "%U NAT66 feature enable/disable failed.",
178 format_vnet_sw_interface_name, vnm,
179 vnet_get_sw_interface (vnm, sw_if_index));
189 unformat_free (line_input);
190 vec_free (inside_sw_if_indices);
191 vec_free (outside_sw_if_indices);
197 nat66_cli_interface_walk (nat66_interface_t * i, void *ctx)
199 vlib_main_t *vm = ctx;
200 vnet_main_t *vnm = vnet_get_main ();
201 vlib_cli_output (vm, " %U %s", format_vnet_sw_interface_name, vnm,
202 vnet_get_sw_interface (vnm, i->sw_if_index),
203 nat66_interface_is_inside (i) ? "in" : "out");
207 static clib_error_t *
208 nat66_show_interfaces_command_fn (vlib_main_t * vm, unformat_input_t * input,
209 vlib_cli_command_t * cmd)
211 nat66_main_t *nm = &nat66_main;
213 return clib_error_return (0, "nat66 disabled");
214 vlib_cli_output (vm, "NAT66 interfaces:");
215 nat66_interfaces_walk (nat66_cli_interface_walk, vm);
219 static clib_error_t *
220 nat66_add_del_static_mapping_command_fn (vlib_main_t * vm,
221 unformat_input_t * input,
222 vlib_cli_command_t * cmd)
224 nat66_main_t *nm = &nat66_main;
225 unformat_input_t _line_input, *line_input = &_line_input;
226 clib_error_t *error = 0;
228 ip6_address_t l_addr, e_addr;
233 return clib_error_return (0, "nat66 disabled");
235 /* Get a line of input. */
236 if (!unformat_user (input, unformat_line_input, line_input))
239 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
241 if (unformat (line_input, "local %U external %U",
242 unformat_ip6_address, &l_addr,
243 unformat_ip6_address, &e_addr))
245 else if (unformat (line_input, "vrf %u", &vrf_id))
247 else if (unformat (line_input, "del"))
251 error = clib_error_return (0, "unknown input: '%U'",
252 format_unformat_error, line_input);
257 rv = nat66_static_mapping_add_del (&l_addr, &e_addr, vrf_id, is_add);
261 case VNET_API_ERROR_NO_SUCH_ENTRY:
262 error = clib_error_return (0, "NAT66 static mapping entry not exist.");
264 case VNET_API_ERROR_VALUE_EXIST:
265 error = clib_error_return (0, "NAT66 static mapping entry exist.");
272 unformat_free (line_input);
278 nat66_cli_static_mapping_walk (nat66_static_mapping_t * sm, void *ctx)
280 nat66_main_t *nm = &nat66_main;
281 vlib_main_t *vm = ctx;
285 fib = fib_table_get (sm->fib_index, FIB_PROTOCOL_IP6);
289 vlib_get_combined_counter (&nm->session_counters, sm - nm->sm, &vc);
291 vlib_cli_output (vm, " local %U external %U vrf %d",
292 format_ip6_address, &sm->l_addr,
293 format_ip6_address, &sm->e_addr, fib->ft_table_id);
294 vlib_cli_output (vm, " total pkts %lld, total bytes %lld", vc.packets,
300 static clib_error_t *
301 nat66_show_static_mappings_command_fn (vlib_main_t * vm,
302 unformat_input_t * input,
303 vlib_cli_command_t * cmd)
305 nat66_main_t *nm = &nat66_main;
307 return clib_error_return (0, "nat66 disabled");
308 vlib_cli_output (vm, "NAT66 static mappings:");
309 nat66_static_mappings_walk (nat66_cli_static_mapping_walk, vm);
315 * @cliexstart{nat66 enable}
316 * Enable NAT66 plugin
317 * To enable NAT66 plugin
319 * To enable NAT66 plugin with outside-vrf id 10
320 * vpp# nat66 enable outside-vrf 10
323 VLIB_CLI_COMMAND (nat66_enable_command, static) = {
324 .path = "nat66 enable",
325 .short_help = "nat66 enable [outside-vrf <vrf-id>]",
326 .function = nat66_enable_command_fn,
331 * @cliexstart{nat66 disable}
332 * Disable NAT66 plugin
333 * To disable NAT66 plugin
337 VLIB_CLI_COMMAND (nat66_disable_command, static) = {
338 .path = "nat66 disable",
339 .short_help = "nat66 disable",
340 .function = nat66_disable_command_fn,
345 * @cliexstart{set interface nat66}
346 * Enable/disable NAT66 feature on the interface.
347 * To enable NAT66 feature with local (IPv6) network interface
348 * GigabitEthernet0/8/0 and external (IPv4) network interface
349 * GigabitEthernet0/a/0 use:
350 * vpp# set interface nat66 in GigabitEthernet0/8/0 out GigabitEthernet0/a/0
353 VLIB_CLI_COMMAND (set_interface_nat66_command, static) = {
354 .path = "set interface nat66",
355 .short_help = "set interface nat66 in|out <intfc> [del]",
356 .function = nat66_interface_feature_command_fn,
361 * @cliexstart{show nat66 interfaces}
362 * Show interfaces with NAT66 feature.
363 * To show interfaces with NAT66 feature use:
364 * vpp# show nat66 interfaces
366 * GigabitEthernet0/8/0 in
367 * GigabitEthernet0/a/0 out
370 VLIB_CLI_COMMAND (show_nat66_interfaces_command, static) = {
371 .path = "show nat66 interfaces",
372 .short_help = "show nat66 interfaces",
373 .function = nat66_show_interfaces_command_fn,
378 * @cliexstart{nat66 add static mapping}
379 * Add/delete NAT66 static mapping entry.
380 * To add NAT66 static mapping entry use:
381 * vpp# nat66 add static mapping local fd01:1::4 external 2001:db8:c000:223::
382 * vpp# nat66 add static mapping local fd01:1::2 external 2001:db8:c000:221:: vrf 10
385 VLIB_CLI_COMMAND (show_nat66_add_del_static_mapping_command, static) = {
386 .path = "nat66 add static mapping",
387 .short_help = "nat66 add static mapping local <ip6-addr> external <ip6-addr>"
388 " [vfr <table-id>] [del]",
389 .function = nat66_add_del_static_mapping_command_fn,
394 * @cliexstart{show nat66 static mappings}
395 * Show NAT66 static mappings.
396 * To show NAT66 static mappings use:
397 * vpp# show nat66 static mappings
398 * NAT66 static mappings:
399 * local fd01:1::4 external 2001:db8:c000:223:: vrf 0
400 * local fd01:1::2 external 2001:db8:c000:221:: vrf 10
403 VLIB_CLI_COMMAND (show_nat66_static_mappings_command, static) = {
404 .path = "show nat66 static mappings",
405 .short_help = "show nat66 static mappings",
406 .function = nat66_show_static_mappings_command_fn,
410 * fd.io coding-style-patch-verification: ON
413 * eval: (c-set-style "gnu")