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_interface_feature_command_fn (vlib_main_t * vm,
25 unformat_input_t * input,
26 vlib_cli_command_t * cmd)
28 unformat_input_t _line_input, *line_input = &_line_input;
29 vnet_main_t *vnm = vnet_get_main ();
30 clib_error_t *error = 0;
32 u32 *inside_sw_if_indices = 0;
33 u32 *outside_sw_if_indices = 0;
37 /* Get a line of input. */
38 if (!unformat_user (input, unformat_line_input, line_input))
41 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
43 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
45 vec_add1 (inside_sw_if_indices, sw_if_index);
46 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
48 vec_add1 (outside_sw_if_indices, sw_if_index);
49 else if (unformat (line_input, "del"))
53 error = clib_error_return (0, "unknown input '%U'",
54 format_unformat_error, line_input);
59 if (vec_len (inside_sw_if_indices))
61 for (i = 0; i < vec_len (inside_sw_if_indices); i++)
63 sw_if_index = inside_sw_if_indices[i];
64 rv = nat66_interface_add_del (sw_if_index, 1, is_add);
67 case VNET_API_ERROR_NO_SUCH_ENTRY:
69 clib_error_return (0, "%U NAT66 feature not enabled.",
70 format_vnet_sw_interface_name, vnm,
71 vnet_get_sw_interface (vnm, sw_if_index));
73 case VNET_API_ERROR_VALUE_EXIST:
75 clib_error_return (0, "%U NAT66 feature already enabled.",
76 format_vnet_sw_interface_name, vnm,
77 vnet_get_sw_interface (vnm, sw_if_index));
79 case VNET_API_ERROR_INVALID_VALUE:
80 case VNET_API_ERROR_INVALID_VALUE_2:
83 "%U NAT66 feature enable/disable failed.",
84 format_vnet_sw_interface_name, vnm,
85 vnet_get_sw_interface (vnm, sw_if_index));
94 if (vec_len (outside_sw_if_indices))
96 for (i = 0; i < vec_len (outside_sw_if_indices); i++)
98 sw_if_index = outside_sw_if_indices[i];
99 rv = nat66_interface_add_del (sw_if_index, 0, is_add);
102 case VNET_API_ERROR_NO_SUCH_ENTRY:
104 clib_error_return (0, "%U NAT66 feature not enabled.",
105 format_vnet_sw_interface_name, vnm,
106 vnet_get_sw_interface (vnm, sw_if_index));
108 case VNET_API_ERROR_VALUE_EXIST:
110 clib_error_return (0, "%U NAT66 feature already enabled.",
111 format_vnet_sw_interface_name, vnm,
112 vnet_get_sw_interface (vnm, sw_if_index));
114 case VNET_API_ERROR_INVALID_VALUE:
115 case VNET_API_ERROR_INVALID_VALUE_2:
117 clib_error_return (0,
118 "%U NAT66 feature enable/disable failed.",
119 format_vnet_sw_interface_name, vnm,
120 vnet_get_sw_interface (vnm, sw_if_index));
130 unformat_free (line_input);
131 vec_free (inside_sw_if_indices);
132 vec_free (outside_sw_if_indices);
138 nat66_cli_interface_walk (nat66_interface_t * i, void *ctx)
140 vlib_main_t *vm = ctx;
141 vnet_main_t *vnm = vnet_get_main ();
143 vlib_cli_output (vm, " %U %s", format_vnet_sw_interface_name, vnm,
144 vnet_get_sw_interface (vnm, i->sw_if_index),
145 nat66_interface_is_inside (i) ? "in" : "out");
149 static clib_error_t *
150 nat66_show_interfaces_command_fn (vlib_main_t * vm, unformat_input_t * input,
151 vlib_cli_command_t * cmd)
153 vlib_cli_output (vm, "NAT66 interfaces:");
154 nat66_interfaces_walk (nat66_cli_interface_walk, vm);
159 static clib_error_t *
160 nat66_add_del_static_mapping_command_fn (vlib_main_t * vm,
161 unformat_input_t * input,
162 vlib_cli_command_t * cmd)
164 unformat_input_t _line_input, *line_input = &_line_input;
165 clib_error_t *error = 0;
167 ip6_address_t l_addr, e_addr;
171 /* Get a line of input. */
172 if (!unformat_user (input, unformat_line_input, line_input))
175 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
177 if (unformat (line_input, "local %U external %U",
178 unformat_ip6_address, &l_addr,
179 unformat_ip6_address, &e_addr))
181 else if (unformat (line_input, "vrf %u", &vrf_id))
183 else if (unformat (line_input, "del"))
187 error = clib_error_return (0, "unknown input: '%U'",
188 format_unformat_error, line_input);
193 rv = nat66_static_mapping_add_del (&l_addr, &e_addr, vrf_id, is_add);
197 case VNET_API_ERROR_NO_SUCH_ENTRY:
198 error = clib_error_return (0, "NAT66 static mapping entry not exist.");
200 case VNET_API_ERROR_VALUE_EXIST:
201 error = clib_error_return (0, "NAT66 static mapping entry exist.");
208 unformat_free (line_input);
214 nat66_cli_static_mapping_walk (nat66_static_mapping_t * sm, void *ctx)
216 nat66_main_t *nm = &nat66_main;
217 vlib_main_t *vm = ctx;
221 fib = fib_table_get (sm->fib_index, FIB_PROTOCOL_IP6);
225 vlib_get_combined_counter (&nm->session_counters, sm - nm->sm, &vc);
227 vlib_cli_output (vm, " local %U external %U vrf %d",
228 format_ip6_address, &sm->l_addr,
229 format_ip6_address, &sm->e_addr, fib->ft_table_id);
230 vlib_cli_output (vm, " total pkts %lld, total bytes %lld", vc.packets,
236 static clib_error_t *
237 nat66_show_static_mappings_command_fn (vlib_main_t * vm,
238 unformat_input_t * input,
239 vlib_cli_command_t * cmd)
241 vlib_cli_output (vm, "NAT66 static mappings:");
242 nat66_static_mappings_walk (nat66_cli_static_mapping_walk, vm);
249 * @cliexstart{set interface nat66}
250 * Enable/disable NAT66 feature on the interface.
251 * To enable NAT66 feature with local (IPv6) network interface
252 * GigabitEthernet0/8/0 and external (IPv4) network interface
253 * GigabitEthernet0/a/0 use:
254 * vpp# set interface nat66 in GigabitEthernet0/8/0 out GigabitEthernet0/a/0
257 VLIB_CLI_COMMAND (set_interface_nat66_command, static) = {
258 .path = "set interface nat66",
259 .short_help = "set interface nat66 in|out <intfc> [del]",
260 .function = nat66_interface_feature_command_fn,
265 * @cliexstart{show nat66 interfaces}
266 * Show interfaces with NAT66 feature.
267 * To show interfaces with NAT66 feature use:
268 * vpp# show nat66 interfaces
270 * GigabitEthernet0/8/0 in
271 * GigabitEthernet0/a/0 out
274 VLIB_CLI_COMMAND (show_nat66_interfaces_command, static) = {
275 .path = "show nat66 interfaces",
276 .short_help = "show nat66 interfaces",
277 .function = nat66_show_interfaces_command_fn,
282 * @cliexstart{nat66 add static mapping}
283 * Add/delete NAT66 static mapping entry.
284 * To add NAT66 static mapping entry use:
285 * vpp# nat66 add static mapping local fd01:1::4 external 2001:db8:c000:223::
286 * vpp# nat66 add static mapping local fd01:1::2 external 2001:db8:c000:221:: vrf 10
289 VLIB_CLI_COMMAND (show_nat66_add_del_static_mapping_command, static) = {
290 .path = "nat66 add static mapping",
291 .short_help = "nat66 add static mapping local <ip6-addr> external <ip6-addr>"
292 " [vfr <table-id>] [del]",
293 .function = nat66_add_del_static_mapping_command_fn,
298 * @cliexstart{show nat66 static mappings}
299 * Show NAT66 static mappings.
300 * To show NAT66 static mappings use:
301 * vpp# show nat66 static mappings
302 * NAT66 static mappings:
303 * local fd01:1::4 external 2001:db8:c000:223:: vrf 0
304 * local fd01:1::2 external 2001:db8:c000:221:: vrf 10
307 VLIB_CLI_COMMAND (show_nat66_static_mappings_command, static) = {
308 .path = "show nat66 static mappings",
309 .short_help = "show nat66 static mappings",
310 .function = nat66_show_static_mappings_command_fn,
314 * fd.io coding-style-patch-verification: ON
317 * eval: (c-set-style "gnu")