Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * decap.c : IPSec tunnel support |
| 3 | * |
| 4 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <vnet/vnet.h> |
| 19 | #include <vnet/api_errno.h> |
| 20 | #include <vnet/ip/ip.h> |
| 21 | #include <vnet/interface.h> |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 22 | #include <vnet/fib/fib.h> |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 23 | #include <vnet/ipip/ipip.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 24 | |
| 25 | #include <vnet/ipsec/ipsec.h> |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 26 | #include <vnet/ipsec/ipsec_tun.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 27 | |
| 28 | static clib_error_t * |
| 29 | set_interface_spd_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 30 | unformat_input_t * input, |
| 31 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 32 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 33 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 34 | ipsec_main_t *im = &ipsec_main; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 35 | u32 sw_if_index = (u32) ~ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 36 | u32 spd_id; |
| 37 | int is_add = 1; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 38 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 39 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 40 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 41 | return 0; |
| 42 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 43 | if (unformat |
| 44 | (line_input, "%U %u", unformat_vnet_sw_interface, im->vnet_main, |
| 45 | &sw_if_index, &spd_id)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | ; |
| 47 | else if (unformat (line_input, "del")) |
| 48 | is_add = 0; |
| 49 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 50 | { |
| 51 | error = clib_error_return (0, "parse error: '%U'", |
| 52 | format_unformat_error, line_input); |
| 53 | goto done; |
| 54 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 56 | ipsec_set_interface_spd (vm, sw_if_index, spd_id, is_add); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 58 | done: |
| 59 | unformat_free (line_input); |
| 60 | |
| 61 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 64 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | VLIB_CLI_COMMAND (set_interface_spd_command, static) = { |
| 66 | .path = "set interface ipsec spd", |
| 67 | .short_help = |
| 68 | "set interface ipsec spd <int> <id>", |
| 69 | .function = set_interface_spd_command_fn, |
| 70 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 71 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | |
| 73 | static clib_error_t * |
| 74 | ipsec_sa_add_del_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 75 | unformat_input_t * input, |
| 76 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 78 | unformat_input_t _line_input, *line_input = &_line_input; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 79 | ip46_address_t tun_src = { }, tun_dst = |
| 80 | { |
| 81 | }; |
| 82 | ipsec_crypto_alg_t crypto_alg; |
| 83 | ipsec_integ_alg_t integ_alg; |
| 84 | ipsec_protocol_t proto; |
| 85 | ipsec_sa_flags_t flags; |
| 86 | clib_error_t *error; |
Kingwel Xie | d3d1205 | 2019-03-07 06:34:30 -0500 | [diff] [blame] | 87 | ipsec_key_t ck = { 0 }; |
| 88 | ipsec_key_t ik = { 0 }; |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 89 | u32 id, spi, salt, sai; |
| 90 | u16 udp_src, udp_dst; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 91 | int is_add, rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 92 | |
Neale Ranns | 8dc75c0 | 2019-12-10 01:08:19 +0000 | [diff] [blame] | 93 | salt = 0; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 94 | error = NULL; |
| 95 | is_add = 0; |
| 96 | flags = IPSEC_SA_FLAG_NONE; |
| 97 | proto = IPSEC_PROTOCOL_ESP; |
Neale Ranns | e6be702 | 2019-06-04 15:37:34 +0000 | [diff] [blame] | 98 | integ_alg = IPSEC_INTEG_ALG_NONE; |
| 99 | crypto_alg = IPSEC_CRYPTO_ALG_NONE; |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 100 | udp_src = udp_dst = IPSEC_UDP_PORT_NONE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 102 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | return 0; |
| 104 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 105 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 106 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 107 | if (unformat (line_input, "add %u", &id)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 108 | is_add = 1; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 109 | else if (unformat (line_input, "del %u", &id)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 110 | is_add = 0; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 111 | else if (unformat (line_input, "spi %u", &spi)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 112 | ; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 113 | else if (unformat (line_input, "salt 0x%x", &salt)) |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 114 | ; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 115 | else if (unformat (line_input, "esp")) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 116 | proto = IPSEC_PROTOCOL_ESP; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 117 | else if (unformat (line_input, "ah")) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 118 | proto = IPSEC_PROTOCOL_AH; |
| 119 | else if (unformat (line_input, "crypto-key %U", |
| 120 | unformat_ipsec_key, &ck)) |
| 121 | ; |
| 122 | else if (unformat (line_input, "crypto-alg %U", |
| 123 | unformat_ipsec_crypto_alg, &crypto_alg)) |
| 124 | ; |
| 125 | else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik)) |
| 126 | ; |
| 127 | else if (unformat (line_input, "integ-alg %U", |
| 128 | unformat_ipsec_integ_alg, &integ_alg)) |
| 129 | ; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 130 | else if (unformat (line_input, "tunnel-src %U", |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 131 | unformat_ip46_address, &tun_src, IP46_TYPE_ANY)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 132 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 133 | flags |= IPSEC_SA_FLAG_IS_TUNNEL; |
| 134 | if (!ip46_address_is_ip4 (&tun_src)) |
| 135 | flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 136 | } |
| 137 | else if (unformat (line_input, "tunnel-dst %U", |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 138 | unformat_ip46_address, &tun_dst, IP46_TYPE_ANY)) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 139 | ; |
Radu Nicolau | 717de09 | 2018-08-03 10:37:24 +0100 | [diff] [blame] | 140 | else if (unformat (line_input, "udp-encap")) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 141 | flags |= IPSEC_SA_FLAG_UDP_ENCAP; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 142 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 143 | { |
| 144 | error = clib_error_return (0, "parse error: '%U'", |
| 145 | format_unformat_error, line_input); |
| 146 | goto done; |
| 147 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 148 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 150 | if (is_add) |
Neale Ranns | 495d7ff | 2019-07-12 09:15:26 +0000 | [diff] [blame] | 151 | rv = ipsec_sa_add_and_lock (id, spi, proto, crypto_alg, |
| 152 | &ck, integ_alg, &ik, flags, |
| 153 | 0, clib_host_to_net_u32 (salt), |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 154 | &tun_src, &tun_dst, &sai, udp_src, udp_dst); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 155 | else |
Neale Ranns | 495d7ff | 2019-07-12 09:15:26 +0000 | [diff] [blame] | 156 | rv = ipsec_sa_unlock_id (id); |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 157 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 158 | if (rv) |
Neale Ranns | e6be702 | 2019-06-04 15:37:34 +0000 | [diff] [blame] | 159 | error = clib_error_return (0, "failed"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 160 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 161 | done: |
| 162 | unformat_free (line_input); |
| 163 | |
| 164 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 167 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = { |
| 169 | .path = "ipsec sa", |
| 170 | .short_help = |
| 171 | "ipsec sa [add|del]", |
| 172 | .function = ipsec_sa_add_del_command_fn, |
| 173 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 174 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | |
| 176 | static clib_error_t * |
| 177 | ipsec_spd_add_del_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 178 | unformat_input_t * input, |
| 179 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 181 | unformat_input_t _line_input, *line_input = &_line_input; |
Damjan Marion | 3f54b18 | 2016-08-16 11:27:02 +0200 | [diff] [blame] | 182 | u32 spd_id = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 183 | int is_add = ~0; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 184 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 185 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 186 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | return 0; |
| 188 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 189 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 190 | { |
| 191 | if (unformat (line_input, "add")) |
| 192 | is_add = 1; |
| 193 | else if (unformat (line_input, "del")) |
| 194 | is_add = 0; |
| 195 | else if (unformat (line_input, "%u", &spd_id)) |
| 196 | ; |
| 197 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 198 | { |
| 199 | error = clib_error_return (0, "parse error: '%U'", |
| 200 | format_unformat_error, line_input); |
| 201 | goto done; |
| 202 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 203 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 204 | |
Damjan Marion | 3f54b18 | 2016-08-16 11:27:02 +0200 | [diff] [blame] | 205 | if (spd_id == ~0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 206 | { |
| 207 | error = clib_error_return (0, "please specify SPD ID"); |
| 208 | goto done; |
| 209 | } |
Damjan Marion | 3f54b18 | 2016-08-16 11:27:02 +0200 | [diff] [blame] | 210 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 211 | ipsec_add_del_spd (vm, spd_id, is_add); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 213 | done: |
| 214 | unformat_free (line_input); |
| 215 | |
| 216 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 219 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 220 | VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = { |
| 221 | .path = "ipsec spd", |
| 222 | .short_help = |
| 223 | "ipsec spd [add|del] <id>", |
| 224 | .function = ipsec_spd_add_del_command_fn, |
| 225 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 226 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 227 | |
| 228 | |
| 229 | static clib_error_t * |
| 230 | ipsec_policy_add_del_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 231 | unformat_input_t * input, |
| 232 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 233 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 234 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 235 | ipsec_policy_t p; |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 236 | int rv, is_add = 0; |
| 237 | u32 tmp, tmp2, stat_index; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 238 | clib_error_t *error = NULL; |
Neale Ranns | 9f231d4 | 2019-03-19 10:06:00 +0000 | [diff] [blame] | 239 | u32 is_outbound; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 240 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 241 | clib_memset (&p, 0, sizeof (p)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 242 | p.lport.stop = p.rport.stop = ~0; |
Neale Ranns | 9f231d4 | 2019-03-19 10:06:00 +0000 | [diff] [blame] | 243 | is_outbound = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 244 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 245 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 246 | return 0; |
| 247 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 248 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 249 | { |
| 250 | if (unformat (line_input, "add")) |
| 251 | is_add = 1; |
| 252 | else if (unformat (line_input, "del")) |
| 253 | is_add = 0; |
| 254 | else if (unformat (line_input, "spd %u", &p.id)) |
| 255 | ; |
| 256 | else if (unformat (line_input, "inbound")) |
Neale Ranns | 9f231d4 | 2019-03-19 10:06:00 +0000 | [diff] [blame] | 257 | is_outbound = 0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 258 | else if (unformat (line_input, "outbound")) |
Neale Ranns | 9f231d4 | 2019-03-19 10:06:00 +0000 | [diff] [blame] | 259 | is_outbound = 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 260 | else if (unformat (line_input, "priority %d", &p.priority)) |
| 261 | ; |
| 262 | else if (unformat (line_input, "protocol %u", &tmp)) |
| 263 | p.protocol = (u8) tmp; |
| 264 | else |
| 265 | if (unformat |
| 266 | (line_input, "action %U", unformat_ipsec_policy_action, |
| 267 | &p.policy)) |
| 268 | { |
| 269 | if (p.policy == IPSEC_POLICY_ACTION_RESOLVE) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 270 | { |
| 271 | error = clib_error_return (0, "unsupported action: 'resolve'"); |
| 272 | goto done; |
| 273 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 274 | } |
| 275 | else if (unformat (line_input, "sa %u", &p.sa_id)) |
| 276 | ; |
| 277 | else if (unformat (line_input, "local-ip-range %U - %U", |
| 278 | unformat_ip4_address, &p.laddr.start.ip4, |
| 279 | unformat_ip4_address, &p.laddr.stop.ip4)) |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 280 | ; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 281 | else if (unformat (line_input, "remote-ip-range %U - %U", |
| 282 | unformat_ip4_address, &p.raddr.start.ip4, |
| 283 | unformat_ip4_address, &p.raddr.stop.ip4)) |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 284 | ; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 285 | else if (unformat (line_input, "local-ip-range %U - %U", |
| 286 | unformat_ip6_address, &p.laddr.start.ip6, |
| 287 | unformat_ip6_address, &p.laddr.stop.ip6)) |
| 288 | { |
| 289 | p.is_ipv6 = 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 290 | } |
| 291 | else if (unformat (line_input, "remote-ip-range %U - %U", |
| 292 | unformat_ip6_address, &p.raddr.start.ip6, |
| 293 | unformat_ip6_address, &p.raddr.stop.ip6)) |
| 294 | { |
| 295 | p.is_ipv6 = 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 296 | } |
| 297 | else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2)) |
| 298 | { |
| 299 | p.lport.start = tmp; |
| 300 | p.lport.stop = tmp2; |
| 301 | } |
| 302 | else |
| 303 | if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2)) |
| 304 | { |
| 305 | p.rport.start = tmp; |
| 306 | p.rport.stop = tmp2; |
| 307 | } |
| 308 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 309 | { |
| 310 | error = clib_error_return (0, "parse error: '%U'", |
| 311 | format_unformat_error, line_input); |
| 312 | goto done; |
| 313 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 314 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 315 | |
Neale Ranns | 9f231d4 | 2019-03-19 10:06:00 +0000 | [diff] [blame] | 316 | rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type); |
| 317 | |
| 318 | if (rv) |
| 319 | { |
| 320 | error = clib_error_return (0, "unsupported policy type for:", |
| 321 | " outboud:%s %s action:%U", |
| 322 | (is_outbound ? "yes" : "no"), |
| 323 | (p.is_ipv6 ? "IPv4" : "IPv6"), |
| 324 | format_ipsec_policy_action, p.policy); |
| 325 | goto done; |
| 326 | } |
| 327 | |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 328 | rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index); |
| 329 | |
| 330 | if (!rv) |
| 331 | vlib_cli_output (vm, "policy-index:%d", stat_index); |
| 332 | else |
| 333 | vlib_cli_output (vm, "error:%d", rv); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 334 | |
| 335 | done: |
| 336 | unformat_free (line_input); |
| 337 | |
| 338 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 341 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 342 | VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = { |
| 343 | .path = "ipsec policy", |
| 344 | .short_help = |
| 345 | "ipsec policy [add|del] spd <id> priority <n> ", |
| 346 | .function = ipsec_policy_add_del_command_fn, |
| 347 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 348 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 349 | |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 350 | static void |
Neale Ranns | 670027a | 2019-08-27 12:47:17 +0000 | [diff] [blame] | 351 | ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im, u8 detail) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 352 | { |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 353 | u32 sai; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 354 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 355 | /* *INDENT-OFF* */ |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 356 | pool_foreach_index (sai, im->sad, ({ |
Neale Ranns | 670027a | 2019-08-27 12:47:17 +0000 | [diff] [blame] | 357 | vlib_cli_output(vm, "%U", format_ipsec_sa, sai, |
| 358 | (detail ? IPSEC_FORMAT_DETAIL : IPSEC_FORMAT_BRIEF)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 359 | })); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 360 | /* *INDENT-ON* */ |
| 361 | } |
| 362 | |
| 363 | static void |
| 364 | ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im) |
| 365 | { |
| 366 | u32 spdi; |
| 367 | |
| 368 | /* *INDENT-OFF* */ |
| 369 | pool_foreach_index (spdi, im->spds, ({ |
| 370 | vlib_cli_output(vm, "%U", format_ipsec_spd, spdi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 371 | })); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 372 | /* *INDENT-ON* */ |
| 373 | } |
| 374 | |
| 375 | static void |
| 376 | ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im) |
| 377 | { |
| 378 | u32 spd_id, sw_if_index; |
Neale Ranns | d83c4a8 | 2019-06-14 06:48:27 -0700 | [diff] [blame] | 379 | ipsec_spd_t *spd; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 380 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 381 | vlib_cli_output (vm, "SPD Bindings:"); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 382 | |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 383 | /* *INDENT-OFF* */ |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 384 | hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({ |
Neale Ranns | d83c4a8 | 2019-06-14 06:48:27 -0700 | [diff] [blame] | 385 | spd = pool_elt_at_index (im->spds, spd_id); |
| 386 | vlib_cli_output (vm, " %d -> %U", spd->id, |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 387 | format_vnet_sw_if_index_name, im->vnet_main, |
| 388 | sw_if_index); |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 389 | })); |
| 390 | /* *INDENT-ON* */ |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 391 | } |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 392 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 393 | static walk_rc_t |
| 394 | ipsec_tun_protect_show_one (index_t itpi, void *ctx) |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 395 | { |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 396 | vlib_cli_output (ctx, "%U", format_ipsec_tun_protect_index, itpi); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 397 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 398 | return (WALK_CONTINUE); |
| 399 | } |
| 400 | |
| 401 | static void |
| 402 | ipsec_tunnel_show_all (vlib_main_t * vm) |
| 403 | { |
| 404 | ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | static clib_error_t * |
| 408 | show_ipsec_command_fn (vlib_main_t * vm, |
| 409 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 410 | { |
| 411 | ipsec_main_t *im = &ipsec_main; |
| 412 | |
Neale Ranns | 670027a | 2019-08-27 12:47:17 +0000 | [diff] [blame] | 413 | ipsec_sa_show_all (vm, im, 0); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 414 | ipsec_spd_show_all (vm, im); |
| 415 | ipsec_spd_bindings_show_all (vm, im); |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 416 | ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 417 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 418 | vlib_cli_output (vm, "IPSec async mode: %s", |
| 419 | (im->async_mode ? "on" : "off")); |
| 420 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 421 | return 0; |
| 422 | } |
| 423 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 424 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 425 | VLIB_CLI_COMMAND (show_ipsec_command, static) = { |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 426 | .path = "show ipsec all", |
| 427 | .short_help = "show ipsec all", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 428 | .function = show_ipsec_command_fn, |
| 429 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 430 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 431 | |
| 432 | static clib_error_t * |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 433 | show_ipsec_sa_command_fn (vlib_main_t * vm, |
| 434 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 435 | { |
| 436 | ipsec_main_t *im = &ipsec_main; |
| 437 | u32 sai = ~0; |
Neale Ranns | 670027a | 2019-08-27 12:47:17 +0000 | [diff] [blame] | 438 | u8 detail = 0; |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 439 | |
| 440 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 441 | { |
| 442 | if (unformat (input, "%u", &sai)) |
| 443 | ; |
Neale Ranns | 670027a | 2019-08-27 12:47:17 +0000 | [diff] [blame] | 444 | if (unformat (input, "detail")) |
| 445 | detail = 1; |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 446 | else |
| 447 | break; |
| 448 | } |
| 449 | |
| 450 | if (~0 == sai) |
Neale Ranns | 670027a | 2019-08-27 12:47:17 +0000 | [diff] [blame] | 451 | ipsec_sa_show_all (vm, im, detail); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 452 | else |
Christian E. Hopps | 01d61e7 | 2019-09-27 14:43:22 -0400 | [diff] [blame] | 453 | vlib_cli_output (vm, "%U", format_ipsec_sa, sai, |
| 454 | IPSEC_FORMAT_DETAIL | IPSEC_FORMAT_INSECURE); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 459 | static clib_error_t * |
| 460 | clear_ipsec_sa_command_fn (vlib_main_t * vm, |
| 461 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 462 | { |
| 463 | ipsec_main_t *im = &ipsec_main; |
| 464 | u32 sai = ~0; |
| 465 | |
| 466 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 467 | { |
| 468 | if (unformat (input, "%u", &sai)) |
| 469 | ; |
| 470 | else |
| 471 | break; |
| 472 | } |
| 473 | |
| 474 | if (~0 == sai) |
| 475 | { |
| 476 | /* *INDENT-OFF* */ |
| 477 | pool_foreach_index (sai, im->sad, ({ |
| 478 | ipsec_sa_clear(sai); |
| 479 | })); |
| 480 | /* *INDENT-ON* */ |
| 481 | } |
| 482 | else |
| 483 | { |
| 484 | if (pool_is_free_index (im->sad, sai)) |
| 485 | return clib_error_return (0, "unknown SA index: %d", sai); |
| 486 | else |
| 487 | ipsec_sa_clear (sai); |
| 488 | } |
| 489 | |
| 490 | return 0; |
| 491 | } |
| 492 | |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 493 | /* *INDENT-OFF* */ |
| 494 | VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = { |
| 495 | .path = "show ipsec sa", |
| 496 | .short_help = "show ipsec sa [index]", |
| 497 | .function = show_ipsec_sa_command_fn, |
| 498 | }; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 499 | |
| 500 | VLIB_CLI_COMMAND (clear_ipsec_sa_command, static) = { |
| 501 | .path = "clear ipsec sa", |
| 502 | .short_help = "clear ipsec sa [index]", |
| 503 | .function = clear_ipsec_sa_command_fn, |
| 504 | }; |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 505 | /* *INDENT-ON* */ |
| 506 | |
| 507 | static clib_error_t * |
| 508 | show_ipsec_spd_command_fn (vlib_main_t * vm, |
| 509 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 510 | { |
| 511 | ipsec_main_t *im = &ipsec_main; |
| 512 | u8 show_bindings = 0; |
| 513 | u32 spdi = ~0; |
| 514 | |
| 515 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 516 | { |
| 517 | if (unformat (input, "%u", &spdi)) |
| 518 | ; |
| 519 | else if (unformat (input, "bindings")) |
| 520 | show_bindings = 1; |
| 521 | else |
| 522 | break; |
| 523 | } |
| 524 | |
| 525 | if (show_bindings) |
| 526 | ipsec_spd_bindings_show_all (vm, im); |
| 527 | else if (~0 != spdi) |
| 528 | vlib_cli_output (vm, "%U", format_ipsec_spd, spdi); |
| 529 | else |
| 530 | ipsec_spd_show_all (vm, im); |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | /* *INDENT-OFF* */ |
| 536 | VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = { |
| 537 | .path = "show ipsec spd", |
| 538 | .short_help = "show ipsec spd [index]", |
| 539 | .function = show_ipsec_spd_command_fn, |
| 540 | }; |
| 541 | /* *INDENT-ON* */ |
| 542 | |
| 543 | static clib_error_t * |
| 544 | show_ipsec_tunnel_command_fn (vlib_main_t * vm, |
| 545 | unformat_input_t * input, |
| 546 | vlib_cli_command_t * cmd) |
| 547 | { |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 548 | ipsec_tunnel_show_all (vm); |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | /* *INDENT-OFF* */ |
| 554 | VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = { |
| 555 | .path = "show ipsec tunnel", |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 556 | .short_help = "show ipsec tunnel", |
Neale Ranns | b294f10 | 2019-04-03 13:17:50 +0000 | [diff] [blame] | 557 | .function = show_ipsec_tunnel_command_fn, |
| 558 | }; |
| 559 | /* *INDENT-ON* */ |
| 560 | |
| 561 | static clib_error_t * |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 562 | ipsec_show_backends_command_fn (vlib_main_t * vm, |
| 563 | unformat_input_t * input, |
| 564 | vlib_cli_command_t * cmd) |
| 565 | { |
| 566 | ipsec_main_t *im = &ipsec_main; |
| 567 | u32 verbose = 0; |
| 568 | |
| 569 | (void) unformat (input, "verbose %u", &verbose); |
| 570 | |
| 571 | vlib_cli_output (vm, "IPsec AH backends available:"); |
| 572 | u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active"); |
| 573 | ipsec_ah_backend_t *ab; |
| 574 | /* *INDENT-OFF* */ |
| 575 | pool_foreach (ab, im->ah_backends, { |
| 576 | s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends, |
| 577 | ab - im->ah_backends == im->ah_current_backend ? "yes" : "no"); |
| 578 | if (verbose) { |
| 579 | vlib_node_t *n; |
| 580 | n = vlib_get_node (vm, ab->ah4_encrypt_node_index); |
| 581 | s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index); |
| 582 | n = vlib_get_node (vm, ab->ah4_decrypt_node_index); |
| 583 | s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index); |
| 584 | n = vlib_get_node (vm, ab->ah6_encrypt_node_index); |
| 585 | s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index); |
| 586 | n = vlib_get_node (vm, ab->ah6_decrypt_node_index); |
| 587 | s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index); |
| 588 | } |
| 589 | }); |
| 590 | /* *INDENT-ON* */ |
| 591 | vlib_cli_output (vm, "%v", s); |
| 592 | _vec_len (s) = 0; |
| 593 | vlib_cli_output (vm, "IPsec ESP backends available:"); |
| 594 | s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active"); |
| 595 | ipsec_esp_backend_t *eb; |
| 596 | /* *INDENT-OFF* */ |
| 597 | pool_foreach (eb, im->esp_backends, { |
| 598 | s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends, |
| 599 | eb - im->esp_backends == im->esp_current_backend ? "yes" |
| 600 | : "no"); |
| 601 | if (verbose) { |
| 602 | vlib_node_t *n; |
| 603 | n = vlib_get_node (vm, eb->esp4_encrypt_node_index); |
| 604 | s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index); |
| 605 | n = vlib_get_node (vm, eb->esp4_decrypt_node_index); |
| 606 | s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index); |
| 607 | n = vlib_get_node (vm, eb->esp6_encrypt_node_index); |
| 608 | s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index); |
| 609 | n = vlib_get_node (vm, eb->esp6_decrypt_node_index); |
| 610 | s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index); |
| 611 | } |
| 612 | }); |
| 613 | /* *INDENT-ON* */ |
| 614 | vlib_cli_output (vm, "%v", s); |
| 615 | |
| 616 | vec_free (s); |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | /* *INDENT-OFF* */ |
| 621 | VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = { |
| 622 | .path = "show ipsec backends", |
| 623 | .short_help = "show ipsec backends", |
| 624 | .function = ipsec_show_backends_command_fn, |
| 625 | }; |
| 626 | /* *INDENT-ON* */ |
| 627 | |
| 628 | static clib_error_t * |
| 629 | ipsec_select_backend_command_fn (vlib_main_t * vm, |
| 630 | unformat_input_t * input, |
| 631 | vlib_cli_command_t * cmd) |
| 632 | { |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 633 | unformat_input_t _line_input, *line_input = &_line_input; |
Neale Ranns | e8915fc | 2019-04-23 20:57:55 -0400 | [diff] [blame] | 634 | ipsec_main_t *im = &ipsec_main; |
| 635 | clib_error_t *error; |
| 636 | u32 backend_index; |
| 637 | |
| 638 | error = ipsec_rsc_in_use (im); |
| 639 | |
| 640 | if (error) |
| 641 | return error; |
| 642 | |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 643 | /* Get a line of input. */ |
| 644 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 645 | return 0; |
| 646 | |
| 647 | if (unformat (line_input, "ah")) |
| 648 | { |
| 649 | if (unformat (line_input, "%u", &backend_index)) |
| 650 | { |
| 651 | if (ipsec_select_ah_backend (im, backend_index) < 0) |
| 652 | { |
| 653 | return clib_error_return (0, "Invalid AH backend index `%u'", |
| 654 | backend_index); |
| 655 | } |
| 656 | } |
| 657 | else |
| 658 | { |
| 659 | return clib_error_return (0, "Invalid backend index `%U'", |
| 660 | format_unformat_error, line_input); |
| 661 | } |
| 662 | } |
| 663 | else if (unformat (line_input, "esp")) |
| 664 | { |
| 665 | if (unformat (line_input, "%u", &backend_index)) |
| 666 | { |
| 667 | if (ipsec_select_esp_backend (im, backend_index) < 0) |
| 668 | { |
| 669 | return clib_error_return (0, "Invalid ESP backend index `%u'", |
| 670 | backend_index); |
| 671 | } |
| 672 | } |
| 673 | else |
| 674 | { |
| 675 | return clib_error_return (0, "Invalid backend index `%U'", |
| 676 | format_unformat_error, line_input); |
| 677 | } |
| 678 | } |
| 679 | else |
| 680 | { |
| 681 | return clib_error_return (0, "Unknown input `%U'", |
| 682 | format_unformat_error, line_input); |
| 683 | } |
| 684 | |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | /* *INDENT-OFF* */ |
| 689 | VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = { |
| 690 | .path = "ipsec select backend", |
| 691 | .short_help = "ipsec select backend <ah|esp> <backend index>", |
| 692 | .function = ipsec_select_backend_command_fn, |
| 693 | }; |
| 694 | |
| 695 | /* *INDENT-ON* */ |
| 696 | |
| 697 | static clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 698 | clear_ipsec_counters_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 699 | unformat_input_t * input, |
| 700 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 701 | { |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 702 | vlib_clear_combined_counters (&ipsec_spd_policy_counters); |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 703 | vlib_clear_combined_counters (&ipsec_sa_counters); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 704 | |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 705 | return (NULL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 708 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 709 | VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = { |
| 710 | .path = "clear ipsec counters", |
| 711 | .short_help = "clear ipsec counters", |
| 712 | .function = clear_ipsec_counters_command_fn, |
| 713 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 714 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 715 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 716 | static u32 |
| 717 | ipsec_tun_mk_local_sa_id (u32 ti) |
| 718 | { |
| 719 | return (0x80000000 | ti); |
| 720 | } |
| 721 | |
| 722 | static u32 |
| 723 | ipsec_tun_mk_remote_sa_id (u32 ti) |
| 724 | { |
| 725 | return (0xc0000000 | ti); |
| 726 | } |
| 727 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 728 | static clib_error_t * |
| 729 | create_ipsec_tunnel_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 730 | unformat_input_t * input, |
| 731 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 732 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 733 | unformat_input_t _line_input, *line_input = &_line_input; |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 734 | ip46_address_t local_ip = ip46_address_initializer; |
| 735 | ip46_address_t remote_ip = ip46_address_initializer; |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 736 | ip_address_t nh = IP_ADDRESS_V4_ALL_0S; |
Damjan Marion | 6844985 | 2020-03-16 17:53:38 +0100 | [diff] [blame] | 737 | ipsec_crypto_alg_t crypto_alg = IPSEC_CRYPTO_ALG_NONE; |
| 738 | ipsec_integ_alg_t integ_alg = IPSEC_INTEG_ALG_NONE; |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 739 | ipsec_sa_flags_t flags; |
| 740 | u32 local_spi, remote_spi, salt, table_id, fib_index; |
| 741 | u32 instance = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 742 | int rv; |
| 743 | u32 num_m_args = 0; |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 744 | u8 ipv4_set = 0; |
| 745 | u8 ipv6_set = 0; |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 746 | u8 is_add = 1; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 747 | clib_error_t *error = NULL; |
Kingwel Xie | d3d1205 | 2019-03-07 06:34:30 -0500 | [diff] [blame] | 748 | ipsec_key_t rck = { 0 }; |
| 749 | ipsec_key_t lck = { 0 }; |
| 750 | ipsec_key_t lik = { 0 }; |
| 751 | ipsec_key_t rik = { 0 }; |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 752 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 753 | table_id = 0; |
| 754 | flags = IPSEC_SA_FLAG_NONE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 755 | |
| 756 | /* Get a line of input. */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 757 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 758 | return 0; |
| 759 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 760 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 761 | { |
| 762 | if (unformat |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 763 | (line_input, "local-ip %U", unformat_ip46_address, &local_ip, |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 764 | IP46_TYPE_ANY)) |
| 765 | { |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 766 | ip46_address_is_ip4 (&local_ip) ? (ipv4_set = 1) : (ipv6_set = 1); |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 767 | num_m_args++; |
| 768 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 769 | else |
| 770 | if (unformat |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 771 | (line_input, "remote-ip %U", unformat_ip46_address, &remote_ip, |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 772 | IP46_TYPE_ANY)) |
| 773 | { |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 774 | ip46_address_is_ip4 (&remote_ip) ? (ipv4_set = 1) : (ipv6_set = 1); |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 775 | num_m_args++; |
| 776 | } |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 777 | else if (unformat (line_input, "local-spi %u", &local_spi)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 778 | num_m_args++; |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 779 | else if (unformat (line_input, "remote-spi %u", &remote_spi)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 780 | num_m_args++; |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 781 | else if (unformat (line_input, "salt 0x%x", &salt)) |
Neale Ranns | 47feb11 | 2019-04-11 15:14:07 +0000 | [diff] [blame] | 782 | ; |
Radu Nicolau | 717de09 | 2018-08-03 10:37:24 +0100 | [diff] [blame] | 783 | else if (unformat (line_input, "udp-encap")) |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 784 | flags |= IPSEC_SA_FLAG_UDP_ENCAP; |
Kingwel Xie | 2baf942 | 2019-02-04 02:07:06 -0800 | [diff] [blame] | 785 | else if (unformat (line_input, "use-esn")) |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 786 | flags |= IPSEC_SA_FLAG_USE_ESN; |
Kingwel Xie | 2baf942 | 2019-02-04 02:07:06 -0800 | [diff] [blame] | 787 | else if (unformat (line_input, "use-anti-replay")) |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 788 | flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY; |
| 789 | else if (unformat (line_input, "instance %u", &instance)) |
| 790 | ; |
| 791 | else if (unformat (line_input, "tx-table %u", &table_id)) |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 792 | ; |
Neale Ranns | fd06084 | 2019-03-04 13:44:42 +0000 | [diff] [blame] | 793 | else |
| 794 | if (unformat |
| 795 | (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck)) |
| 796 | ; |
| 797 | else |
| 798 | if (unformat |
| 799 | (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck)) |
| 800 | ; |
| 801 | else if (unformat (line_input, "crypto-alg %U", |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 802 | unformat_ipsec_crypto_alg, &crypto_alg)) |
Neale Ranns | fd06084 | 2019-03-04 13:44:42 +0000 | [diff] [blame] | 803 | ; |
| 804 | else |
| 805 | if (unformat |
| 806 | (line_input, "local-integ-key %U", unformat_ipsec_key, &lik)) |
| 807 | ; |
| 808 | else |
| 809 | if (unformat |
Simon Zhang | e8e950a | 2019-04-23 23:04:07 +0800 | [diff] [blame] | 810 | (line_input, "remote-integ-key %U", unformat_ipsec_key, &rik)) |
Neale Ranns | fd06084 | 2019-03-04 13:44:42 +0000 | [diff] [blame] | 811 | ; |
| 812 | else if (unformat (line_input, "integ-alg %U", |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 813 | unformat_ipsec_integ_alg, &integ_alg)) |
Neale Ranns | fd06084 | 2019-03-04 13:44:42 +0000 | [diff] [blame] | 814 | ; |
Kingwel Xie | 2baf942 | 2019-02-04 02:07:06 -0800 | [diff] [blame] | 815 | else if (unformat (line_input, "del")) |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 816 | is_add = 0; |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 817 | else if (unformat (line_input, "nh &U", unformat_ip_address, &nh)) |
| 818 | ; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 819 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 820 | { |
| 821 | error = clib_error_return (0, "unknown input `%U'", |
| 822 | format_unformat_error, line_input); |
| 823 | goto done; |
| 824 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 825 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 826 | |
| 827 | if (num_m_args < 4) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 828 | { |
| 829 | error = clib_error_return (0, "mandatory argument(s) missing"); |
| 830 | goto done; |
| 831 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 832 | |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 833 | if (ipv4_set && ipv6_set) |
| 834 | return clib_error_return (0, "both IPv4 and IPv6 addresses specified"); |
| 835 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 836 | fib_index = fib_table_find (fib_ip_proto (ipv6_set), table_id); |
Kingwel Xie | 1ba5bc8 | 2019-03-20 07:21:58 -0400 | [diff] [blame] | 837 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 838 | if (~0 == fib_index) |
| 839 | { |
| 840 | rv = VNET_API_ERROR_NO_SUCH_FIB; |
| 841 | goto done; |
| 842 | } |
Neale Ranns | fd06084 | 2019-03-04 13:44:42 +0000 | [diff] [blame] | 843 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 844 | if (is_add) |
| 845 | { |
| 846 | // remote = input, local = output |
| 847 | u32 sw_if_index; |
Neale Ranns | fd06084 | 2019-03-04 13:44:42 +0000 | [diff] [blame] | 848 | |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 849 | /* create an ip-ip tunnel, then the two SA, then bind them */ |
| 850 | rv = |
| 851 | ipip_add_tunnel (ipv6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4, |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 852 | instance, &local_ip, &remote_ip, fib_index, |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 853 | TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0, |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 854 | TUNNEL_MODE_P2P, &sw_if_index); |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 855 | rv |= |
| 856 | ipsec_sa_add_and_lock (ipsec_tun_mk_local_sa_id (sw_if_index), |
| 857 | local_spi, IPSEC_PROTOCOL_ESP, crypto_alg, |
| 858 | &lck, integ_alg, &lik, flags, table_id, |
| 859 | clib_host_to_net_u32 (salt), &local_ip, |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 860 | &remote_ip, NULL, IPSEC_UDP_PORT_NONE, |
| 861 | IPSEC_UDP_PORT_NONE); |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 862 | rv |= |
| 863 | ipsec_sa_add_and_lock (ipsec_tun_mk_remote_sa_id (sw_if_index), |
| 864 | remote_spi, IPSEC_PROTOCOL_ESP, crypto_alg, |
| 865 | &rck, integ_alg, &rik, |
| 866 | (flags | IPSEC_SA_FLAG_IS_INBOUND), table_id, |
| 867 | clib_host_to_net_u32 (salt), &remote_ip, |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 868 | &local_ip, NULL, IPSEC_UDP_PORT_NONE, |
| 869 | IPSEC_UDP_PORT_NONE); |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 870 | rv |= |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 871 | ipsec_tun_protect_update_one (sw_if_index, &nh, |
Neale Ranns | 12989b5 | 2019-09-26 16:20:19 +0000 | [diff] [blame] | 872 | ipsec_tun_mk_local_sa_id (sw_if_index), |
| 873 | ipsec_tun_mk_remote_sa_id |
| 874 | (sw_if_index)); |
| 875 | } |
| 876 | else |
| 877 | rv = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 878 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 879 | switch (rv) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 880 | { |
| 881 | case 0: |
| 882 | break; |
| 883 | case VNET_API_ERROR_INVALID_VALUE: |
Neale Ranns | d14fccd | 2019-11-15 15:03:27 +0000 | [diff] [blame] | 884 | error = clib_error_return (0, |
| 885 | "IPSec tunnel interface already exists..."); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 886 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 887 | default: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 888 | error = clib_error_return (0, "ipsec_register_interface returned %d", |
| 889 | rv); |
| 890 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 893 | done: |
| 894 | unformat_free (line_input); |
| 895 | |
| 896 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 897 | } |
| 898 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 899 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 900 | VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = { |
| 901 | .path = "create ipsec tunnel", |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 902 | .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> " |
Kingwel Xie | 2baf942 | 2019-02-04 02:07:06 -0800 | [diff] [blame] | 903 | "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] " |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 904 | "[tx-table <table-id>]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 905 | .function = create_ipsec_tunnel_command_fn, |
| 906 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 907 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 908 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 909 | static clib_error_t * |
| 910 | ipsec_tun_protect_cmd (vlib_main_t * vm, |
| 911 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 912 | { |
| 913 | unformat_input_t _line_input, *line_input = &_line_input; |
| 914 | u32 sw_if_index, is_del, sa_in, sa_out, *sa_ins = NULL; |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 915 | ip_address_t peer = { }; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 916 | vnet_main_t *vnm; |
| 917 | |
| 918 | is_del = 0; |
| 919 | sw_if_index = ~0; |
| 920 | vnm = vnet_get_main (); |
| 921 | |
| 922 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 923 | return 0; |
| 924 | |
| 925 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 926 | { |
| 927 | if (unformat (line_input, "del")) |
| 928 | is_del = 1; |
| 929 | else if (unformat (line_input, "add")) |
| 930 | is_del = 0; |
| 931 | else if (unformat (line_input, "sa-in %d", &sa_in)) |
| 932 | vec_add1 (sa_ins, sa_in); |
| 933 | else if (unformat (line_input, "sa-out %d", &sa_out)) |
| 934 | ; |
| 935 | else if (unformat (line_input, "%U", |
| 936 | unformat_vnet_sw_interface, vnm, &sw_if_index)) |
| 937 | ; |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 938 | else if (unformat (line_input, "%U", unformat_ip_address, &peer)) |
| 939 | ; |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 940 | else |
| 941 | return (clib_error_return (0, "unknown input '%U'", |
| 942 | format_unformat_error, line_input)); |
| 943 | } |
| 944 | |
| 945 | if (!is_del) |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 946 | ipsec_tun_protect_update (sw_if_index, &peer, sa_out, sa_ins); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 947 | |
| 948 | unformat_free (line_input); |
| 949 | return NULL; |
| 950 | } |
| 951 | |
| 952 | /** |
| 953 | * Protect tunnel with IPSEC |
| 954 | */ |
| 955 | /* *INDENT-OFF* */ |
| 956 | VLIB_CLI_COMMAND (ipsec_tun_protect_cmd_node, static) = |
| 957 | { |
| 958 | .path = "ipsec tunnel protect", |
| 959 | .function = ipsec_tun_protect_cmd, |
| 960 | .short_help = "ipsec tunnel protect <interface> input-sa <SA> output-sa <SA>", |
| 961 | // this is not MP safe |
| 962 | }; |
| 963 | /* *INDENT-ON* */ |
| 964 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 965 | |
| 966 | static clib_error_t * |
| 967 | ipsec_tun_protect_show (vlib_main_t * vm, |
| 968 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 969 | { |
| 970 | ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm); |
| 971 | |
| 972 | return NULL; |
| 973 | } |
| 974 | |
| 975 | /** |
| 976 | * show IPSEC tunnel protection |
| 977 | */ |
| 978 | /* *INDENT-OFF* */ |
| 979 | VLIB_CLI_COMMAND (ipsec_tun_protect_show_node, static) = |
| 980 | { |
| 981 | .path = "show ipsec protect", |
| 982 | .function = ipsec_tun_protect_show, |
| 983 | .short_help = "show ipsec protect", |
| 984 | }; |
| 985 | /* *INDENT-ON* */ |
| 986 | |
Neale Ranns | 41afb33 | 2019-07-16 06:19:35 -0700 | [diff] [blame] | 987 | static clib_error_t * |
| 988 | ipsec_tun_protect_hash_show (vlib_main_t * vm, |
| 989 | unformat_input_t * input, |
| 990 | vlib_cli_command_t * cmd) |
| 991 | { |
| 992 | ipsec_main_t *im = &ipsec_main; |
| 993 | |
| 994 | { |
| 995 | ipsec_tun_lkup_result_t value; |
| 996 | ipsec4_tunnel_key_t key; |
| 997 | |
| 998 | vlib_cli_output (vm, "IPv4:"); |
| 999 | |
| 1000 | /* *INDENT-OFF* */ |
| 1001 | hash_foreach(key.as_u64, value.as_u64, im->tun4_protect_by_key, |
| 1002 | ({ |
| 1003 | vlib_cli_output (vm, " %U", format_ipsec4_tunnel_key, &key); |
| 1004 | vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index); |
| 1005 | })); |
| 1006 | /* *INDENT-ON* */ |
| 1007 | } |
| 1008 | |
| 1009 | { |
| 1010 | ipsec_tun_lkup_result_t value; |
| 1011 | ipsec6_tunnel_key_t *key; |
| 1012 | |
| 1013 | vlib_cli_output (vm, "IPv6:"); |
| 1014 | |
| 1015 | /* *INDENT-OFF* */ |
| 1016 | hash_foreach_mem(key, value.as_u64, im->tun6_protect_by_key, |
| 1017 | ({ |
| 1018 | vlib_cli_output (vm, " %U", format_ipsec6_tunnel_key, key); |
| 1019 | vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index); |
| 1020 | })); |
| 1021 | /* *INDENT-ON* */ |
| 1022 | } |
| 1023 | |
| 1024 | return NULL; |
| 1025 | } |
| 1026 | |
| 1027 | /** |
| 1028 | * show IPSEC tunnel protection hash tables |
| 1029 | */ |
| 1030 | /* *INDENT-OFF* */ |
| 1031 | VLIB_CLI_COMMAND (ipsec_tun_protect_hash_show_node, static) = |
| 1032 | { |
| 1033 | .path = "show ipsec protect-hash", |
| 1034 | .function = ipsec_tun_protect_hash_show, |
| 1035 | .short_help = "show ipsec protect-hash", |
| 1036 | }; |
| 1037 | /* *INDENT-ON* */ |
| 1038 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1039 | clib_error_t * |
| 1040 | ipsec_cli_init (vlib_main_t * vm) |
| 1041 | { |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | VLIB_INIT_FUNCTION (ipsec_cli_init); |
| 1046 | |
Fan Zhang | f539578 | 2020-04-29 14:00:03 +0100 | [diff] [blame] | 1047 | static clib_error_t * |
| 1048 | set_async_mode_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 1049 | vlib_cli_command_t * cmd) |
| 1050 | { |
| 1051 | unformat_input_t _line_input, *line_input = &_line_input; |
| 1052 | int async_enable = 0; |
| 1053 | |
| 1054 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 1055 | return 0; |
| 1056 | |
| 1057 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 1058 | { |
| 1059 | if (unformat (line_input, "on")) |
| 1060 | async_enable = 1; |
| 1061 | else if (unformat (line_input, "off")) |
| 1062 | async_enable = 0; |
| 1063 | else |
| 1064 | return (clib_error_return (0, "unknown input '%U'", |
| 1065 | format_unformat_error, line_input)); |
| 1066 | } |
| 1067 | |
| 1068 | vnet_crypto_request_async_mode (async_enable); |
| 1069 | ipsec_set_async_mode (async_enable); |
| 1070 | |
| 1071 | unformat_free (line_input); |
| 1072 | return (NULL); |
| 1073 | } |
| 1074 | |
| 1075 | /* *INDENT-OFF* */ |
| 1076 | VLIB_CLI_COMMAND (set_async_mode_command, static) = { |
| 1077 | .path = "set ipsec async mode", |
| 1078 | .short_help = "set ipsec async mode on|off", |
| 1079 | .function = set_async_mode_command_fn, |
| 1080 | }; |
| 1081 | /* *INDENT-ON* */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 1082 | |
| 1083 | /* |
| 1084 | * fd.io coding-style-patch-verification: ON |
| 1085 | * |
| 1086 | * Local Variables: |
| 1087 | * eval: (c-set-style "gnu") |
| 1088 | * End: |
| 1089 | */ |