2 * Copyright (c) 2015 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 * ip/ip4_cli.c: ip4 commands
18 * Copyright (c) 2008 Eliot Dresselhaus
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 #include <vnet/ip/ip.h>
42 int ip4_address_compare (ip4_address_t * a1, ip4_address_t * a2)
43 { return clib_net_to_host_u32 (a1->data_u32) - clib_net_to_host_u32 (a2->data_u32); }
45 int ip6_address_compare (ip6_address_t * a1, ip6_address_t * a2)
48 for (i = 0; i < ARRAY_LEN (a1->as_u16); i++)
50 int cmp = clib_net_to_host_u16 (a1->as_u16[i]) - clib_net_to_host_u16 (a2->as_u16[i]);
57 VLIB_CLI_COMMAND (set_interface_ip_command, static) = {
58 .path = "set interface ip",
59 .short_help = "IP4/IP6 commands",
62 void ip_del_all_interface_addresses (vlib_main_t *vm, u32 sw_if_index)
64 ip4_main_t * im4 = &ip4_main;
65 ip4_address_t * ip4_addrs = 0;
67 ip6_main_t * im6 = &ip6_main;
68 ip6_address_t * ip6_addrs = 0;
70 ip_interface_address_t * ia;
73 foreach_ip_interface_address (&im4->lookup_main, ia, sw_if_index,
74 0 /* honor unnumbered */,
76 ip4_address_t * x = (ip4_address_t *)
77 ip_interface_address_get_address (&im4->lookup_main, ia);
78 vec_add1 (ip4_addrs, x[0]);
79 vec_add1 (ip4_masks, ia->address_length);
82 foreach_ip_interface_address (&im6->lookup_main, ia, sw_if_index,
83 0 /* honor unnumbered */,
85 ip6_address_t * x = (ip6_address_t *)
86 ip_interface_address_get_address (&im6->lookup_main, ia);
87 vec_add1 (ip6_addrs, x[0]);
88 vec_add1 (ip6_masks, ia->address_length);
91 for (i = 0; i < vec_len (ip4_addrs); i++)
92 ip4_add_del_interface_address (vm, sw_if_index, &ip4_addrs[i],
93 ip4_masks[i], 1 /* is_del */);
94 for (i = 0; i < vec_len (ip6_addrs); i++)
95 ip6_add_del_interface_address (vm, sw_if_index, &ip6_addrs[i],
96 ip6_masks[i], 1 /* is_del */);
100 vec_free (ip6_addrs);
101 vec_free (ip6_masks);
104 static clib_error_t *
105 ip_address_delete_cleanup (vnet_main_t * vnm, u32 hw_if_index, u32 is_create)
107 vlib_main_t * vm = vlib_get_main();
108 vnet_hw_interface_t * hw;
113 hw = vnet_get_hw_interface (vnm, hw_if_index);
115 ip_del_all_interface_addresses (vm, hw->sw_if_index);
119 VNET_HW_INTERFACE_ADD_DEL_FUNCTION (ip_address_delete_cleanup);
121 static clib_error_t *
122 add_del_ip_address (vlib_main_t * vm,
123 unformat_input_t * input,
124 vlib_cli_command_t * cmd)
126 vnet_main_t * vnm = vnet_get_main();
129 clib_error_t * error = 0;
130 u32 sw_if_index, length, is_del;
135 if (unformat (input, "del"))
138 if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
140 error = clib_error_return (0, "unknown interface `%U'",
141 format_unformat_error, input);
145 if (is_del && unformat (input, "all"))
146 ip_del_all_interface_addresses (vm, sw_if_index);
147 else if (unformat (input, "%U/%d", unformat_ip4_address, &a4, &length))
148 error = ip4_add_del_interface_address (vm, sw_if_index, &a4, length,
150 else if (unformat (input, "%U/%d", unformat_ip6_address, &a6, &length))
151 error = ip6_add_del_interface_address (vm, sw_if_index, &a6, length,
155 error = clib_error_return (0, "expected IP4/IP6 address/length `%U'",
156 format_unformat_error, input);
165 * To set an interface ip address, use "set interface ip address":
168 * @cliexstart{set interface ip address}
170 * vpp# set interface ip address GigabitEthernet2/0/0 db01::1/64
171 * Note that the debug CLI does not enforce classful mask-width / addressing constraints.
172 * Interfaces may have multiple ip4 and ip6 addresses.
173 * There is no concept of primary vs. secondary interface addresses; they're just addresses.
174 * To delete a specific interface ip address, use "set interface ip address del":
175 * vpp# set interface ip address del GigabitEthernet2/0/0 6.0.0.2/24
176 * To delete all interfaces addresses (ip4+ip6), use "set interface ip address del <intfc> all"
177 * vpp# set interface ip address del GigabitEthernet2/0/0 all
180 VLIB_CLI_COMMAND (set_interface_ip_address_command, static) = {
181 .path = "set interface ip address",
182 .function = add_del_ip_address,
183 .short_help = "Add/delete IP4/IP6 address for interface",
186 /* Dummy init function to get us linked in. */
187 static clib_error_t * ip4_cli_init (vlib_main_t * vm)
190 VLIB_INIT_FUNCTION (ip4_cli_init);