Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 1 | /* |
| 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: |
| 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 | #include "ipip.h" |
| 17 | #include <vppinfra/error.h> |
| 18 | #include <vnet/vnet.h> |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 19 | #include <vnet/fib/fib_table.h> |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 20 | |
| 21 | static clib_error_t *create_ipip_tunnel_command_fn(vlib_main_t *vm, |
| 22 | unformat_input_t *input, |
| 23 | vlib_cli_command_t *cmd) { |
| 24 | unformat_input_t _line_input, *line_input = &_line_input; |
| 25 | ip46_address_t src = ip46_address_initializer, dst = ip46_address_initializer; |
| 26 | u32 instance = ~0; |
| 27 | u32 fib_index = 0; |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 28 | u32 table_id = 0; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 29 | int rv; |
| 30 | u32 num_m_args = 0; |
| 31 | u32 sw_if_index; |
| 32 | clib_error_t *error = NULL; |
| 33 | bool ip4_set = false, ip6_set = false; |
| 34 | |
| 35 | /* Get a line of input. */ |
| 36 | if (!unformat_user(input, unformat_line_input, line_input)) |
| 37 | return 0; |
| 38 | |
| 39 | while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) { |
| 40 | if (unformat(line_input, "instance %d", &instance)) |
| 41 | ; |
| 42 | else if (unformat(line_input, "src %U", unformat_ip4_address, &src.ip4)) { |
| 43 | num_m_args++; |
| 44 | ip4_set = true; |
| 45 | } else if (unformat(line_input, "dst %U", unformat_ip4_address, &dst.ip4)) { |
| 46 | num_m_args++; |
| 47 | ip4_set = true; |
| 48 | } else if (unformat(line_input, "src %U", unformat_ip6_address, &src.ip6)) { |
| 49 | num_m_args++; |
| 50 | ip6_set = true; |
| 51 | } else if (unformat(line_input, "dst %U", unformat_ip6_address, &dst.ip6)) { |
| 52 | num_m_args++; |
| 53 | ip6_set = true; |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 54 | } else if (unformat(line_input, "outer-table-id %d", &table_id)) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 55 | ; |
| 56 | else { |
| 57 | error = clib_error_return(0, "unknown input `%U'", format_unformat_error, |
| 58 | line_input); |
| 59 | goto done; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if (num_m_args < 2) { |
| 64 | error = clib_error_return(0, "mandatory argument(s) missing"); |
| 65 | goto done; |
| 66 | } |
| 67 | if (ip4_set && ip6_set) { |
| 68 | error = clib_error_return(0, "source and destination must be of same address family"); |
| 69 | goto done; |
| 70 | } |
| 71 | |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 72 | fib_index = fib_table_find(fib_ip_proto(ip6_set), table_id); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 73 | |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 74 | if (~0 == fib_index) |
| 75 | { |
| 76 | rv = VNET_API_ERROR_NO_SUCH_FIB; |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | rv = ipip_add_tunnel(ip6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4, |
| 81 | instance, |
| 82 | &src, |
| 83 | &dst, |
| 84 | fib_index, |
| 85 | 0, |
| 86 | &sw_if_index); |
| 87 | } |
| 88 | |
| 89 | switch (rv) { |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 90 | case 0: |
| 91 | vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), |
| 92 | sw_if_index); |
| 93 | break; |
| 94 | case VNET_API_ERROR_IF_ALREADY_EXISTS: |
| 95 | error = clib_error_return(0, "IPIP tunnel already exists..."); |
| 96 | goto done; |
| 97 | case VNET_API_ERROR_NO_SUCH_FIB: |
| 98 | error = clib_error_return(0, "outer fib ID %d doesn't exist\n", fib_index); |
| 99 | goto done; |
| 100 | case VNET_API_ERROR_NO_SUCH_ENTRY: |
| 101 | error = clib_error_return(0, "IPIP tunnel doesn't exist"); |
| 102 | goto done; |
| 103 | case VNET_API_ERROR_INSTANCE_IN_USE: |
| 104 | error = clib_error_return(0, "Instance is in use"); |
| 105 | goto done; |
| 106 | default: |
| 107 | error = clib_error_return(0, "vnet_ipip_add_del_tunnel returned %d", rv); |
| 108 | goto done; |
| 109 | } |
| 110 | |
| 111 | done: |
| 112 | unformat_free(line_input); |
| 113 | |
| 114 | return error; |
| 115 | } |
| 116 | |
| 117 | static clib_error_t *delete_ipip_tunnel_command_fn(vlib_main_t *vm, |
| 118 | unformat_input_t *input, |
| 119 | vlib_cli_command_t *cmd) { |
| 120 | unformat_input_t _line_input, *line_input = &_line_input; |
| 121 | int rv; |
| 122 | u32 num_m_args = 0; |
| 123 | u32 sw_if_index = ~0; |
| 124 | clib_error_t *error = NULL; |
| 125 | |
| 126 | /* Get a line of input. */ |
| 127 | if (!unformat_user(input, unformat_line_input, line_input)) |
| 128 | return 0; |
| 129 | |
| 130 | while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) { |
| 131 | if (unformat(line_input, "sw_if_index %d", &sw_if_index)) |
| 132 | num_m_args++; |
| 133 | else { |
| 134 | error = clib_error_return(0, "unknown input `%U'", format_unformat_error, |
| 135 | line_input); |
| 136 | goto done; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if (num_m_args < 1) { |
| 141 | error = clib_error_return(0, "mandatory argument(s) missing"); |
| 142 | goto done; |
| 143 | } |
| 144 | |
| 145 | rv = ipip_del_tunnel(sw_if_index); |
| 146 | printf("RV %d\n", rv); |
| 147 | |
| 148 | done: |
| 149 | unformat_free(line_input); |
| 150 | |
| 151 | return error; |
| 152 | } |
| 153 | |
| 154 | /* *INDENT-OFF* */ |
| 155 | VLIB_CLI_COMMAND(create_ipip_tunnel_command, static) = { |
| 156 | .path = "create ipip tunnel", |
| 157 | .short_help = "create ipip tunnel src <addr> dst <addr> [instance <n>] " |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 158 | "[outer-table-id <ID>]", |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 159 | .function = create_ipip_tunnel_command_fn, |
| 160 | }; |
| 161 | VLIB_CLI_COMMAND(delete_ipip_tunnel_command, static) = { |
| 162 | .path = "delete ipip tunnel", |
Ignas Bacius | 3d93ad9 | 2019-10-10 16:14:47 +0300 | [diff] [blame] | 163 | .short_help = "delete ipip tunnel sw_if_index <sw_if_index>", |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 164 | .function = delete_ipip_tunnel_command_fn, |
| 165 | }; |
| 166 | /* *INDENT-ON* */ |
| 167 | |
| 168 | static u8 *format_ipip_tunnel(u8 *s, va_list *args) { |
| 169 | ipip_tunnel_t *t = va_arg(*args, ipip_tunnel_t *); |
| 170 | |
| 171 | ip46_type_t type = (t->transport == IPIP_TRANSPORT_IP4) ? IP46_TYPE_IP4 : IP46_TYPE_IP6; |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 172 | u32 table_id; |
| 173 | |
| 174 | table_id = fib_table_get_table_id(t->fib_index, |
| 175 | fib_proto_from_ip46(type)); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 176 | switch (t->mode) { |
| 177 | case IPIP_MODE_6RD: |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 178 | s = format(s, "[%d] 6rd src %U ip6-pfx %U/%d table-ID %d sw-if-idx %d ", |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 179 | t->dev_instance, |
| 180 | format_ip46_address, &t->tunnel_src, type, |
| 181 | format_ip6_address, &t->sixrd.ip6_prefix, t->sixrd.ip6_prefix_len, |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 182 | table_id, t->sw_if_index); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 183 | break; |
| 184 | case IPIP_MODE_P2P: |
| 185 | default: |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 186 | s = format(s, "[%d] instance %d src %U dst %U table-ID %d sw-if-idx %d ", |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 187 | t->dev_instance, t->user_instance, |
| 188 | format_ip46_address, &t->tunnel_src, type, |
| 189 | format_ip46_address, &t->tunnel_dst, type, |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 190 | table_id, t->sw_if_index); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 191 | break; |
| 192 | } |
| 193 | |
| 194 | return s; |
| 195 | } |
| 196 | |
| 197 | static clib_error_t *show_ipip_tunnel_command_fn(vlib_main_t *vm, |
| 198 | unformat_input_t *input, |
| 199 | vlib_cli_command_t *cmd) { |
| 200 | ipip_main_t *gm = &ipip_main; |
| 201 | ipip_tunnel_t *t; |
| 202 | u32 ti = ~0; |
| 203 | |
| 204 | if (pool_elts(gm->tunnels) == 0) |
| 205 | vlib_cli_output(vm, "No IPIP tunnels configured..."); |
| 206 | |
| 207 | while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) { |
| 208 | if (unformat(input, "%d", &ti)) |
| 209 | ; |
| 210 | else |
| 211 | break; |
| 212 | } |
| 213 | |
| 214 | if (ti == ~0) { |
| 215 | /* *INDENT-OFF* */ |
| 216 | pool_foreach(t, gm->tunnels, |
| 217 | ({vlib_cli_output(vm, "%U", format_ipip_tunnel, t); })); |
| 218 | /* *INDENT-ON* */ |
| 219 | } else { |
| 220 | t = pool_elt_at_index(gm->tunnels, ti); |
| 221 | if (t) |
| 222 | vlib_cli_output(vm, "%U", format_ipip_tunnel, t); |
| 223 | } |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | /* *INDENT-OFF* */ |
| 228 | VLIB_CLI_COMMAND(show_ipip_tunnel_command, static) = { |
| 229 | .path = "show ipip tunnel", |
| 230 | .function = show_ipip_tunnel_command_fn, |
| 231 | }; |
| 232 | /* *INDENT-ON* */ |
| 233 | |
| 234 | static clib_error_t *create_sixrd_tunnel_command_fn(vlib_main_t *vm, |
| 235 | unformat_input_t *input, |
| 236 | vlib_cli_command_t *cmd) { |
| 237 | unformat_input_t _line_input, *line_input = &_line_input; |
| 238 | ip4_address_t ip4_prefix; |
| 239 | ip6_address_t ip6_prefix; |
| 240 | ip4_address_t ip4_src; |
| 241 | u32 ip6_prefix_len = 0, ip4_prefix_len = 0, sixrd_tunnel_index; |
| 242 | u32 num_m_args = 0; |
| 243 | /* Optional arguments */ |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 244 | u32 ip4_table_id = 0, ip4_fib_index; |
| 245 | u32 ip6_table_id = 0, ip6_fib_index; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 246 | clib_error_t *error = 0; |
| 247 | bool security_check = false; |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 248 | int rv; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 249 | |
| 250 | /* Get a line of input. */ |
| 251 | if (!unformat_user(input, unformat_line_input, line_input)) |
| 252 | return 0; |
| 253 | while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) { |
| 254 | if (unformat(line_input, "security-check")) |
| 255 | security_check = true; |
| 256 | else if (unformat(line_input, "ip6-pfx %U/%d", unformat_ip6_address, |
| 257 | &ip6_prefix, &ip6_prefix_len)) |
| 258 | num_m_args++; |
| 259 | else if (unformat(line_input, "ip4-pfx %U/%d", unformat_ip4_address, |
| 260 | &ip4_prefix, &ip4_prefix_len)) |
| 261 | num_m_args++; |
| 262 | else if (unformat(line_input, "ip4-src %U", unformat_ip4_address, &ip4_src)) |
| 263 | num_m_args++; |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 264 | else if (unformat(line_input, "ip4-table-id %d", &ip4_table_id)) |
| 265 | ; |
| 266 | else if (unformat(line_input, "ip6-table-id %d", &ip6_table_id)) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 267 | ; |
| 268 | else { |
| 269 | error = clib_error_return(0, "unknown input `%U'", format_unformat_error, |
| 270 | line_input); |
| 271 | goto done; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if (num_m_args < 3) { |
| 276 | error = clib_error_return(0, "mandatory argument(s) missing"); |
| 277 | goto done; |
| 278 | } |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 279 | ip4_fib_index = fib_table_find(FIB_PROTOCOL_IP4, ip4_table_id); |
| 280 | ip6_fib_index = fib_table_find(FIB_PROTOCOL_IP6, ip6_table_id); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 281 | |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 282 | if (~0 == ip4_fib_index) |
| 283 | { |
| 284 | error = clib_error_return(0, "No such IP4 table %d", ip4_table_id); |
| 285 | rv = VNET_API_ERROR_NO_SUCH_FIB; |
| 286 | } |
| 287 | else if (~0 == ip6_fib_index) |
| 288 | { |
| 289 | error = clib_error_return(0, "No such IP6 table %d", ip6_table_id); |
| 290 | rv = VNET_API_ERROR_NO_SUCH_FIB; |
| 291 | } |
| 292 | else |
| 293 | { |
| 294 | rv = sixrd_add_tunnel(&ip6_prefix, ip6_prefix_len, &ip4_prefix, |
| 295 | ip4_prefix_len, &ip4_src, security_check, |
| 296 | ip4_fib_index, ip6_fib_index, |
| 297 | &sixrd_tunnel_index); |
| 298 | |
| 299 | if (rv) |
| 300 | error = clib_error_return(0, "adding tunnel failed %d", rv); |
| 301 | } |
| 302 | |
| 303 | done: |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 304 | unformat_free(line_input); |
| 305 | |
| 306 | return error; |
| 307 | } |
| 308 | |
| 309 | static clib_error_t *delete_sixrd_tunnel_command_fn(vlib_main_t *vm, |
| 310 | unformat_input_t *input, |
| 311 | vlib_cli_command_t *cmd) { |
| 312 | unformat_input_t _line_input, *line_input = &_line_input; |
| 313 | u32 num_m_args = 0; |
| 314 | /* Optional arguments */ |
| 315 | clib_error_t *error = 0; |
| 316 | u32 sw_if_index = ~0; |
| 317 | |
| 318 | /* Get a line of input. */ |
| 319 | if (!unformat_user(input, unformat_line_input, line_input)) |
| 320 | return 0; |
| 321 | while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) { |
| 322 | if (unformat(line_input, "sw_if_index %d", &sw_if_index)) |
| 323 | num_m_args++; |
| 324 | else { |
| 325 | error = clib_error_return(0, "unknown input `%U'", format_unformat_error, |
| 326 | line_input); |
| 327 | goto done; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | if (num_m_args < 1) { |
| 332 | error = clib_error_return(0, "mandatory argument(s) missing"); |
| 333 | goto done; |
| 334 | } |
| 335 | int rv = sixrd_del_tunnel(sw_if_index); |
| 336 | printf("RV %d\n", rv); |
| 337 | |
| 338 | done: |
| 339 | unformat_free(line_input); |
| 340 | |
| 341 | return error; |
| 342 | } |
| 343 | |
| 344 | /* *INDENT-OFF* */ |
| 345 | VLIB_CLI_COMMAND(create_sixrd_tunnel_command, static) = { |
| 346 | .path = "create 6rd tunnel", |
| 347 | .short_help = "create 6rd tunnel ip6-pfx <ip6-pfx> ip4-pfx <ip4-pfx> " |
Ignas Bacius | 3d93ad9 | 2019-10-10 16:14:47 +0300 | [diff] [blame] | 348 | "ip4-src <ip4-addr> ip4-table-id <ID> ip6-table-id <ID> " |
BenoƮt Ganne | 1b52ca9 | 2019-04-19 10:12:42 +0200 | [diff] [blame] | 349 | "[security-check]", |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 350 | .function = create_sixrd_tunnel_command_fn, |
| 351 | }; |
| 352 | VLIB_CLI_COMMAND(delete_sixrd_tunnel_command, static) = { |
| 353 | .path = "delete 6rd tunnel", |
Ignas Bacius | 3d93ad9 | 2019-10-10 16:14:47 +0300 | [diff] [blame] | 354 | .short_help = "delete 6rd tunnel sw_if_index <sw_if_index>", |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 355 | .function = delete_sixrd_tunnel_command_fn, |
| 356 | }; |
| 357 | /* *INDENT-ON* */ |