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