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