Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 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. |
| 14 | */ |
| 15 | /* |
| 16 | * ip/ip4_cli.c: ip4 commands |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 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: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 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. |
| 38 | */ |
| 39 | |
| 40 | #include <vnet/ip/ip.h> |
| 41 | |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 42 | /** |
| 43 | * @file |
| 44 | * @brief Set IP Address. |
| 45 | * |
| 46 | * Configure an IPv4 or IPv6 address for on an interface. |
| 47 | */ |
| 48 | |
| 49 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 50 | int |
| 51 | ip4_address_compare (ip4_address_t * a1, ip4_address_t * a2) |
| 52 | { |
| 53 | return clib_net_to_host_u32 (a1->data_u32) - |
| 54 | clib_net_to_host_u32 (a2->data_u32); |
| 55 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 57 | int |
| 58 | ip6_address_compare (ip6_address_t * a1, ip6_address_t * a2) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | { |
| 60 | int i; |
| 61 | for (i = 0; i < ARRAY_LEN (a1->as_u16); i++) |
| 62 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 63 | int cmp = |
| 64 | clib_net_to_host_u16 (a1->as_u16[i]) - |
| 65 | clib_net_to_host_u16 (a2->as_u16[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | if (cmp != 0) |
| 67 | return cmp; |
| 68 | } |
| 69 | return 0; |
| 70 | } |
| 71 | |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 72 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | VLIB_CLI_COMMAND (set_interface_ip_command, static) = { |
| 74 | .path = "set interface ip", |
| 75 | .short_help = "IP4/IP6 commands", |
| 76 | }; |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 77 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 79 | void |
| 80 | ip_del_all_interface_addresses (vlib_main_t * vm, u32 sw_if_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 82 | ip4_main_t *im4 = &ip4_main; |
| 83 | ip4_address_t *ip4_addrs = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | u32 *ip4_masks = 0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 85 | ip6_main_t *im6 = &ip6_main; |
| 86 | ip6_address_t *ip6_addrs = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | u32 *ip6_masks = 0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 88 | ip_interface_address_t *ia; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | int i; |
| 90 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 91 | /* *INDENT-OFF* */ |
| 92 | foreach_ip_interface_address (&im4->lookup_main, ia, sw_if_index, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | 0 /* honor unnumbered */, |
| 94 | ({ |
| 95 | ip4_address_t * x = (ip4_address_t *) |
| 96 | ip_interface_address_get_address (&im4->lookup_main, ia); |
| 97 | vec_add1 (ip4_addrs, x[0]); |
| 98 | vec_add1 (ip4_masks, ia->address_length); |
| 99 | })); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 100 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 102 | /* *INDENT-OFF* */ |
| 103 | foreach_ip_interface_address (&im6->lookup_main, ia, sw_if_index, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | 0 /* honor unnumbered */, |
| 105 | ({ |
| 106 | ip6_address_t * x = (ip6_address_t *) |
| 107 | ip_interface_address_get_address (&im6->lookup_main, ia); |
| 108 | vec_add1 (ip6_addrs, x[0]); |
| 109 | vec_add1 (ip6_masks, ia->address_length); |
| 110 | })); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 111 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | |
| 113 | for (i = 0; i < vec_len (ip4_addrs); i++) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 114 | ip4_add_del_interface_address (vm, sw_if_index, &ip4_addrs[i], |
| 115 | ip4_masks[i], 1 /* is_del */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | for (i = 0; i < vec_len (ip6_addrs); i++) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 117 | ip6_add_del_interface_address (vm, sw_if_index, &ip6_addrs[i], |
| 118 | ip6_masks[i], 1 /* is_del */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 119 | |
| 120 | vec_free (ip4_addrs); |
| 121 | vec_free (ip4_masks); |
| 122 | vec_free (ip6_addrs); |
| 123 | vec_free (ip6_masks); |
| 124 | } |
| 125 | |
| 126 | static clib_error_t * |
Dave Barach | b8dca74 | 2016-07-01 12:38:11 -0400 | [diff] [blame] | 127 | ip_address_delete_cleanup (vnet_main_t * vnm, u32 hw_if_index, u32 is_create) |
| 128 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 129 | vlib_main_t *vm = vlib_get_main (); |
| 130 | vnet_hw_interface_t *hw; |
Dave Barach | b8dca74 | 2016-07-01 12:38:11 -0400 | [diff] [blame] | 131 | |
| 132 | if (is_create) |
| 133 | return 0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 134 | |
Dave Barach | b8dca74 | 2016-07-01 12:38:11 -0400 | [diff] [blame] | 135 | hw = vnet_get_hw_interface (vnm, hw_if_index); |
| 136 | |
| 137 | ip_del_all_interface_addresses (vm, hw->sw_if_index); |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | VNET_HW_INTERFACE_ADD_DEL_FUNCTION (ip_address_delete_cleanup); |
| 142 | |
| 143 | static clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | add_del_ip_address (vlib_main_t * vm, |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 145 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 147 | vnet_main_t *vnm = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 148 | ip4_address_t a4; |
| 149 | ip6_address_t a6; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 150 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 151 | u32 sw_if_index, length, is_del; |
| 152 | |
| 153 | sw_if_index = ~0; |
| 154 | is_del = 0; |
| 155 | |
| 156 | if (unformat (input, "del")) |
| 157 | is_del = 1; |
| 158 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 159 | if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 160 | { |
| 161 | error = clib_error_return (0, "unknown interface `%U'", |
| 162 | format_unformat_error, input); |
| 163 | goto done; |
| 164 | } |
| 165 | |
| 166 | if (is_del && unformat (input, "all")) |
| 167 | ip_del_all_interface_addresses (vm, sw_if_index); |
| 168 | else if (unformat (input, "%U/%d", unformat_ip4_address, &a4, &length)) |
| 169 | error = ip4_add_del_interface_address (vm, sw_if_index, &a4, length, |
| 170 | is_del); |
| 171 | else if (unformat (input, "%U/%d", unformat_ip6_address, &a6, &length)) |
| 172 | error = ip6_add_del_interface_address (vm, sw_if_index, &a6, length, |
| 173 | is_del); |
| 174 | else |
| 175 | { |
| 176 | error = clib_error_return (0, "expected IP4/IP6 address/length `%U'", |
| 177 | format_unformat_error, input); |
| 178 | goto done; |
| 179 | } |
| 180 | |
| 181 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 182 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 183 | return error; |
| 184 | } |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 185 | |
Keith Burns (alagalah) | 6ef7bb9 | 2016-09-10 14:55:04 -0700 | [diff] [blame] | 186 | /*? |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 187 | * Add an IP Address to an interface or remove and IP Address from an interface. |
| 188 | * The IP Address can be an IPv4 or an IPv6 address. Interfaces may have multiple |
| 189 | * IPv4 and IPv6 addresses. There is no concept of primary vs. secondary |
| 190 | * interface addresses; they're just addresses. |
| 191 | * |
| 192 | * To display the addresses associated with a given interface, use the command |
| 193 | * '<em>show interface address <interface></em>'. |
| 194 | * |
| 195 | * Note that the debug CLI does not enforce classful mask-width / addressing |
| 196 | * constraints. |
Keith Burns (alagalah) | 6ef7bb9 | 2016-09-10 14:55:04 -0700 | [diff] [blame] | 197 | * |
| 198 | * @cliexpar |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 199 | * @parblock |
| 200 | * An example of how to add an IPv4 address to an interface: |
| 201 | * @cliexcmd{set interface ip address GigabitEthernet2/0/0 172.16.2.12/24} |
| 202 | * |
| 203 | * An example of how to add an IPv6 address to an interface: |
| 204 | * @cliexcmd{set interface ip address GigabitEthernet2/0/0 @::a:1:1:0:7/126} |
| 205 | * |
| 206 | * To delete a specific interface ip address: |
| 207 | * @cliexcmd{set interface ip address GigabitEthernet2/0/0 172.16.2.12/24 del} |
| 208 | * |
| 209 | * To delete all interfaces addresses (IPv4 and IPv6): |
| 210 | * @cliexcmd{set interface ip address GigabitEthernet2/0/0 del all} |
| 211 | * @endparblock |
Keith Burns (alagalah) | 6ef7bb9 | 2016-09-10 14:55:04 -0700 | [diff] [blame] | 212 | ?*/ |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 213 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | VLIB_CLI_COMMAND (set_interface_ip_address_command, static) = { |
| 215 | .path = "set interface ip address", |
| 216 | .function = add_del_ip_address, |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 217 | .short_help = "set interface ip address <interface> [<ip-addr>/<mask> [del]] | [del all]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 218 | }; |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 219 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 220 | |
| 221 | /* Dummy init function to get us linked in. */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 222 | static clib_error_t * |
| 223 | ip4_cli_init (vlib_main_t * vm) |
| 224 | { |
| 225 | return 0; |
| 226 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 227 | |
| 228 | VLIB_INIT_FUNCTION (ip4_cli_init); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 229 | |
| 230 | /* |
| 231 | * fd.io coding-style-patch-verification: ON |
| 232 | * |
| 233 | * Local Variables: |
| 234 | * eval: (c-set-style "gnu") |
| 235 | * End: |
| 236 | */ |