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> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 23 | |
| 24 | #include <vnet/ipsec/ipsec.h> |
| 25 | |
| 26 | static clib_error_t * |
| 27 | set_interface_spd_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 28 | unformat_input_t * input, |
| 29 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 30 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 31 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 32 | ipsec_main_t *im = &ipsec_main; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 33 | u32 sw_if_index = (u32) ~ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 34 | u32 spd_id; |
| 35 | int is_add = 1; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 36 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 37 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 38 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 39 | return 0; |
| 40 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 41 | if (unformat |
| 42 | (line_input, "%U %u", unformat_vnet_sw_interface, im->vnet_main, |
| 43 | &sw_if_index, &spd_id)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | ; |
| 45 | else if (unformat (line_input, "del")) |
| 46 | is_add = 0; |
| 47 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 48 | { |
| 49 | error = clib_error_return (0, "parse error: '%U'", |
| 50 | format_unformat_error, line_input); |
| 51 | goto done; |
| 52 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 54 | ipsec_set_interface_spd (vm, sw_if_index, spd_id, is_add); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 56 | done: |
| 57 | unformat_free (line_input); |
| 58 | |
| 59 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 62 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | VLIB_CLI_COMMAND (set_interface_spd_command, static) = { |
| 64 | .path = "set interface ipsec spd", |
| 65 | .short_help = |
| 66 | "set interface ipsec spd <int> <id>", |
| 67 | .function = set_interface_spd_command_fn, |
| 68 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 69 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | |
| 71 | static clib_error_t * |
| 72 | ipsec_sa_add_del_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 73 | unformat_input_t * input, |
| 74 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 76 | unformat_input_t _line_input, *line_input = &_line_input; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 77 | ip46_address_t tun_src = { }, tun_dst = |
| 78 | { |
| 79 | }; |
| 80 | ipsec_crypto_alg_t crypto_alg; |
| 81 | ipsec_integ_alg_t integ_alg; |
| 82 | ipsec_protocol_t proto; |
| 83 | ipsec_sa_flags_t flags; |
| 84 | clib_error_t *error; |
Kingwel Xie | d3d1205 | 2019-03-07 06:34:30 -0500 | [diff] [blame] | 85 | ipsec_key_t ck = { 0 }; |
| 86 | ipsec_key_t ik = { 0 }; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 87 | int is_add, rv; |
| 88 | u32 id, spi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 90 | error = NULL; |
| 91 | is_add = 0; |
| 92 | flags = IPSEC_SA_FLAG_NONE; |
| 93 | proto = IPSEC_PROTOCOL_ESP; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 95 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | return 0; |
| 97 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 98 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 99 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 100 | if (unformat (line_input, "add %u", &id)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 101 | is_add = 1; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 102 | else if (unformat (line_input, "del %u", &id)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 103 | is_add = 0; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 104 | else if (unformat (line_input, "spi %u", &spi)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 105 | ; |
| 106 | else if (unformat (line_input, "esp")) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 107 | proto = IPSEC_PROTOCOL_ESP; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 108 | else if (unformat (line_input, "ah")) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 109 | proto = IPSEC_PROTOCOL_AH; |
| 110 | else if (unformat (line_input, "crypto-key %U", |
| 111 | unformat_ipsec_key, &ck)) |
| 112 | ; |
| 113 | else if (unformat (line_input, "crypto-alg %U", |
| 114 | unformat_ipsec_crypto_alg, &crypto_alg)) |
| 115 | ; |
| 116 | else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik)) |
| 117 | ; |
| 118 | else if (unformat (line_input, "integ-alg %U", |
| 119 | unformat_ipsec_integ_alg, &integ_alg)) |
| 120 | ; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 121 | else if (unformat (line_input, "tunnel-src %U", |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 122 | unformat_ip46_address, &tun_src, IP46_TYPE_ANY)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 123 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 124 | flags |= IPSEC_SA_FLAG_IS_TUNNEL; |
| 125 | if (!ip46_address_is_ip4 (&tun_src)) |
| 126 | flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 127 | } |
| 128 | else if (unformat (line_input, "tunnel-dst %U", |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 129 | unformat_ip46_address, &tun_dst, IP46_TYPE_ANY)) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 130 | ; |
Radu Nicolau | 717de09 | 2018-08-03 10:37:24 +0100 | [diff] [blame] | 131 | else if (unformat (line_input, "udp-encap")) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 132 | flags |= IPSEC_SA_FLAG_UDP_ENCAP; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 133 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 134 | { |
| 135 | error = clib_error_return (0, "parse error: '%U'", |
| 136 | format_unformat_error, line_input); |
| 137 | goto done; |
| 138 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 139 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 141 | if (is_add) |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 142 | rv = ipsec_sa_add (id, spi, proto, crypto_alg, |
| 143 | &ck, integ_alg, &ik, flags, |
| 144 | 0, &tun_src, &tun_dst, NULL); |
| 145 | else |
| 146 | rv = ipsec_sa_del (id); |
Sergio Gonzalez Monroy | d04b60b | 2017-01-20 15:35:23 +0000 | [diff] [blame] | 147 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 148 | if (rv) |
| 149 | clib_error_return (0, "failed"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 151 | done: |
| 152 | unformat_free (line_input); |
| 153 | |
| 154 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 157 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 158 | VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = { |
| 159 | .path = "ipsec sa", |
| 160 | .short_help = |
| 161 | "ipsec sa [add|del]", |
| 162 | .function = ipsec_sa_add_del_command_fn, |
| 163 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 164 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 165 | |
| 166 | static clib_error_t * |
| 167 | ipsec_spd_add_del_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 168 | unformat_input_t * input, |
| 169 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 171 | unformat_input_t _line_input, *line_input = &_line_input; |
Damjan Marion | 3f54b18 | 2016-08-16 11:27:02 +0200 | [diff] [blame] | 172 | u32 spd_id = ~0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 173 | int is_add = ~0; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 174 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 176 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 177 | return 0; |
| 178 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 179 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 180 | { |
| 181 | if (unformat (line_input, "add")) |
| 182 | is_add = 1; |
| 183 | else if (unformat (line_input, "del")) |
| 184 | is_add = 0; |
| 185 | else if (unformat (line_input, "%u", &spd_id)) |
| 186 | ; |
| 187 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 188 | { |
| 189 | error = clib_error_return (0, "parse error: '%U'", |
| 190 | format_unformat_error, line_input); |
| 191 | goto done; |
| 192 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 193 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 194 | |
Damjan Marion | 3f54b18 | 2016-08-16 11:27:02 +0200 | [diff] [blame] | 195 | if (spd_id == ~0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 196 | { |
| 197 | error = clib_error_return (0, "please specify SPD ID"); |
| 198 | goto done; |
| 199 | } |
Damjan Marion | 3f54b18 | 2016-08-16 11:27:02 +0200 | [diff] [blame] | 200 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 201 | ipsec_add_del_spd (vm, spd_id, is_add); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 203 | done: |
| 204 | unformat_free (line_input); |
| 205 | |
| 206 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 209 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 210 | VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = { |
| 211 | .path = "ipsec spd", |
| 212 | .short_help = |
| 213 | "ipsec spd [add|del] <id>", |
| 214 | .function = ipsec_spd_add_del_command_fn, |
| 215 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 216 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 217 | |
| 218 | |
| 219 | static clib_error_t * |
| 220 | ipsec_policy_add_del_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 221 | unformat_input_t * input, |
| 222 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 223 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 224 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 225 | ipsec_policy_t p; |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 226 | int rv, is_add = 0; |
| 227 | u32 tmp, tmp2, stat_index; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 228 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 229 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 230 | clib_memset (&p, 0, sizeof (p)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 231 | p.lport.stop = p.rport.stop = ~0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 232 | p.laddr.stop.ip4.as_u32 = p.raddr.stop.ip4.as_u32 = (u32) ~ 0; |
| 233 | p.laddr.stop.ip6.as_u64[0] = p.laddr.stop.ip6.as_u64[1] = (u64) ~ 0; |
| 234 | p.raddr.stop.ip6.as_u64[0] = p.raddr.stop.ip6.as_u64[1] = (u64) ~ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 235 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 236 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 237 | return 0; |
| 238 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 239 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 240 | { |
| 241 | if (unformat (line_input, "add")) |
| 242 | is_add = 1; |
| 243 | else if (unformat (line_input, "del")) |
| 244 | is_add = 0; |
| 245 | else if (unformat (line_input, "spd %u", &p.id)) |
| 246 | ; |
| 247 | else if (unformat (line_input, "inbound")) |
| 248 | p.is_outbound = 0; |
| 249 | else if (unformat (line_input, "outbound")) |
| 250 | p.is_outbound = 1; |
| 251 | else if (unformat (line_input, "priority %d", &p.priority)) |
| 252 | ; |
| 253 | else if (unformat (line_input, "protocol %u", &tmp)) |
| 254 | p.protocol = (u8) tmp; |
| 255 | else |
| 256 | if (unformat |
| 257 | (line_input, "action %U", unformat_ipsec_policy_action, |
| 258 | &p.policy)) |
| 259 | { |
| 260 | if (p.policy == IPSEC_POLICY_ACTION_RESOLVE) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 261 | { |
| 262 | error = clib_error_return (0, "unsupported action: 'resolve'"); |
| 263 | goto done; |
| 264 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 265 | } |
| 266 | else if (unformat (line_input, "sa %u", &p.sa_id)) |
| 267 | ; |
| 268 | else if (unformat (line_input, "local-ip-range %U - %U", |
| 269 | unformat_ip4_address, &p.laddr.start.ip4, |
| 270 | unformat_ip4_address, &p.laddr.stop.ip4)) |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 271 | ; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 272 | else if (unformat (line_input, "remote-ip-range %U - %U", |
| 273 | unformat_ip4_address, &p.raddr.start.ip4, |
| 274 | unformat_ip4_address, &p.raddr.stop.ip4)) |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 275 | ; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 276 | else if (unformat (line_input, "local-ip-range %U - %U", |
| 277 | unformat_ip6_address, &p.laddr.start.ip6, |
| 278 | unformat_ip6_address, &p.laddr.stop.ip6)) |
| 279 | { |
| 280 | p.is_ipv6 = 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 281 | } |
| 282 | else if (unformat (line_input, "remote-ip-range %U - %U", |
| 283 | unformat_ip6_address, &p.raddr.start.ip6, |
| 284 | unformat_ip6_address, &p.raddr.stop.ip6)) |
| 285 | { |
| 286 | p.is_ipv6 = 1; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 287 | } |
| 288 | else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2)) |
| 289 | { |
| 290 | p.lport.start = tmp; |
| 291 | p.lport.stop = tmp2; |
| 292 | } |
| 293 | else |
| 294 | if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2)) |
| 295 | { |
| 296 | p.rport.start = tmp; |
| 297 | p.rport.stop = tmp2; |
| 298 | } |
| 299 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 300 | { |
| 301 | error = clib_error_return (0, "parse error: '%U'", |
| 302 | format_unformat_error, line_input); |
| 303 | goto done; |
| 304 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 305 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 306 | |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 307 | /* Check if SA is for IPv6/AH which is not supported. Return error if TRUE. */ |
| 308 | if (p.sa_id) |
| 309 | { |
| 310 | uword *p1; |
| 311 | ipsec_main_t *im = &ipsec_main; |
| 312 | ipsec_sa_t *sa = 0; |
| 313 | p1 = hash_get (im->sa_index_by_sa_id, p.sa_id); |
Klement Sekera | c2fc57e | 2018-06-28 14:20:12 +0200 | [diff] [blame] | 314 | if (!p1) |
| 315 | { |
| 316 | error = |
| 317 | clib_error_return (0, "SA with index %u not found", p.sa_id); |
| 318 | goto done; |
| 319 | } |
“mukeshyadav1984” | 430ac93 | 2017-11-23 02:39:33 -0800 | [diff] [blame] | 320 | sa = pool_elt_at_index (im->sad, p1[0]); |
| 321 | if (sa && sa->protocol == IPSEC_PROTOCOL_AH && is_add && p.is_ipv6) |
| 322 | { |
| 323 | error = clib_error_return (0, "AH not supported for IPV6: '%U'", |
| 324 | format_unformat_error, line_input); |
| 325 | goto done; |
| 326 | } |
| 327 | } |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 328 | rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index); |
| 329 | |
| 330 | if (!rv) |
| 331 | vlib_cli_output (vm, "policy-index:%d", stat_index); |
| 332 | else |
| 333 | vlib_cli_output (vm, "error:%d", rv); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 334 | |
| 335 | done: |
| 336 | unformat_free (line_input); |
| 337 | |
| 338 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 341 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 342 | VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = { |
| 343 | .path = "ipsec policy", |
| 344 | .short_help = |
| 345 | "ipsec policy [add|del] spd <id> priority <n> ", |
| 346 | .function = ipsec_policy_add_del_command_fn, |
| 347 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 348 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 349 | |
| 350 | static clib_error_t * |
| 351 | set_ipsec_sa_key_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 352 | unformat_input_t * input, |
| 353 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 354 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 355 | unformat_input_t _line_input, *line_input = &_line_input; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 356 | clib_error_t *error = NULL; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 357 | ipsec_key_t ck, ik; |
| 358 | u32 id; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 359 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 360 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 361 | return 0; |
| 362 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 363 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 364 | { |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 365 | if (unformat (line_input, "%u", &id)) |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 366 | ; |
| 367 | else |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 368 | if (unformat (line_input, "crypto-key %U", unformat_ipsec_key, &ck)) |
| 369 | ; |
| 370 | else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik)) |
| 371 | ; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 372 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 373 | { |
| 374 | error = clib_error_return (0, "parse error: '%U'", |
| 375 | format_unformat_error, line_input); |
| 376 | goto done; |
| 377 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 378 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 379 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 380 | ipsec_set_sa_key (id, &ck, &ik); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 381 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 382 | done: |
| 383 | unformat_free (line_input); |
| 384 | |
| 385 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 388 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 389 | VLIB_CLI_COMMAND (set_ipsec_sa_key_command, static) = { |
| 390 | .path = "set ipsec sa", |
| 391 | .short_help = |
| 392 | "set ipsec sa <id> crypto-key <key> integ-key <key>", |
| 393 | .function = set_ipsec_sa_key_command_fn, |
| 394 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 395 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 396 | |
| 397 | static clib_error_t * |
| 398 | show_ipsec_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 399 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 400 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 401 | ipsec_main_t *im = &ipsec_main; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 402 | u32 spd_id, sw_if_index, sai; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 403 | vnet_hw_interface_t *hi; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 404 | ipsec_tunnel_if_t *t; |
Klement Sekera | ea84130 | 2018-05-11 12:59:05 +0200 | [diff] [blame] | 405 | u8 *protocol = NULL; |
| 406 | u8 *policy = NULL; |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 407 | u32 i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 408 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 409 | /* *INDENT-OFF* */ |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 410 | pool_foreach_index (sai, im->sad, ({ |
| 411 | vlib_cli_output(vm, "%U", format_ipsec_sa, sai); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 412 | })); |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 413 | pool_foreach_index (i, im->spds, ({ |
| 414 | vlib_cli_output(vm, "%U", format_ipsec_spd, i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 415 | })); |
| 416 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 417 | vlib_cli_output (vm, "SPD Bindings:"); |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 418 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 419 | hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({ |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 420 | vlib_cli_output (vm, " %d -> %U", spd_id, |
| 421 | format_vnet_sw_if_index_name, im->vnet_main, |
| 422 | sw_if_index); |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 423 | })); |
| 424 | /* *INDENT-ON* */ |
| 425 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 426 | vlib_cli_output (vm, "tunnel interfaces"); |
| 427 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 428 | pool_foreach (t, im->tunnel_interfaces, ({ |
Matus Fabian | 694265d | 2016-08-10 01:55:36 -0700 | [diff] [blame] | 429 | if (t->hw_if_index == ~0) |
| 430 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 431 | hi = vnet_get_hw_interface (im->vnet_main, t->hw_if_index); |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 432 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 433 | vlib_cli_output(vm, " %s", hi->name); |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 434 | |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 435 | vlib_cli_output(vm, " out-bound sa"); |
| 436 | vlib_cli_output(vm, " %U", format_ipsec_sa, t->output_sa_index); |
| 437 | |
| 438 | vlib_cli_output(vm, " in-bound sa"); |
| 439 | vlib_cli_output(vm, " %U", format_ipsec_sa, t->input_sa_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 440 | })); |
Klement Sekera | ea84130 | 2018-05-11 12:59:05 +0200 | [diff] [blame] | 441 | vec_free(policy); |
| 442 | vec_free(protocol); |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 443 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 444 | return 0; |
| 445 | } |
| 446 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 447 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 448 | VLIB_CLI_COMMAND (show_ipsec_command, static) = { |
| 449 | .path = "show ipsec", |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 450 | .short_help = "show ipsec [backends]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 451 | .function = show_ipsec_command_fn, |
| 452 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 453 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 454 | |
| 455 | static clib_error_t * |
Klement Sekera | b4d3053 | 2018-11-08 13:00:02 +0100 | [diff] [blame] | 456 | ipsec_show_backends_command_fn (vlib_main_t * vm, |
| 457 | unformat_input_t * input, |
| 458 | vlib_cli_command_t * cmd) |
| 459 | { |
| 460 | ipsec_main_t *im = &ipsec_main; |
| 461 | u32 verbose = 0; |
| 462 | |
| 463 | (void) unformat (input, "verbose %u", &verbose); |
| 464 | |
| 465 | vlib_cli_output (vm, "IPsec AH backends available:"); |
| 466 | u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active"); |
| 467 | ipsec_ah_backend_t *ab; |
| 468 | /* *INDENT-OFF* */ |
| 469 | pool_foreach (ab, im->ah_backends, { |
| 470 | s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends, |
| 471 | ab - im->ah_backends == im->ah_current_backend ? "yes" : "no"); |
| 472 | if (verbose) { |
| 473 | vlib_node_t *n; |
| 474 | n = vlib_get_node (vm, ab->ah4_encrypt_node_index); |
| 475 | s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index); |
| 476 | n = vlib_get_node (vm, ab->ah4_decrypt_node_index); |
| 477 | s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index); |
| 478 | n = vlib_get_node (vm, ab->ah6_encrypt_node_index); |
| 479 | s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index); |
| 480 | n = vlib_get_node (vm, ab->ah6_decrypt_node_index); |
| 481 | s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index); |
| 482 | } |
| 483 | }); |
| 484 | /* *INDENT-ON* */ |
| 485 | vlib_cli_output (vm, "%v", s); |
| 486 | _vec_len (s) = 0; |
| 487 | vlib_cli_output (vm, "IPsec ESP backends available:"); |
| 488 | s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active"); |
| 489 | ipsec_esp_backend_t *eb; |
| 490 | /* *INDENT-OFF* */ |
| 491 | pool_foreach (eb, im->esp_backends, { |
| 492 | s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends, |
| 493 | eb - im->esp_backends == im->esp_current_backend ? "yes" |
| 494 | : "no"); |
| 495 | if (verbose) { |
| 496 | vlib_node_t *n; |
| 497 | n = vlib_get_node (vm, eb->esp4_encrypt_node_index); |
| 498 | s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index); |
| 499 | n = vlib_get_node (vm, eb->esp4_decrypt_node_index); |
| 500 | s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index); |
| 501 | n = vlib_get_node (vm, eb->esp6_encrypt_node_index); |
| 502 | s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index); |
| 503 | n = vlib_get_node (vm, eb->esp6_decrypt_node_index); |
| 504 | s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index); |
| 505 | } |
| 506 | }); |
| 507 | /* *INDENT-ON* */ |
| 508 | vlib_cli_output (vm, "%v", s); |
| 509 | |
| 510 | vec_free (s); |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | /* *INDENT-OFF* */ |
| 515 | VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = { |
| 516 | .path = "show ipsec backends", |
| 517 | .short_help = "show ipsec backends", |
| 518 | .function = ipsec_show_backends_command_fn, |
| 519 | }; |
| 520 | /* *INDENT-ON* */ |
| 521 | |
| 522 | static clib_error_t * |
| 523 | ipsec_select_backend_command_fn (vlib_main_t * vm, |
| 524 | unformat_input_t * input, |
| 525 | vlib_cli_command_t * cmd) |
| 526 | { |
| 527 | u32 backend_index; |
| 528 | ipsec_main_t *im = &ipsec_main; |
| 529 | |
| 530 | if (pool_elts (im->sad) > 0) |
| 531 | { |
| 532 | return clib_error_return (0, |
| 533 | "Cannot change IPsec backend, while %u SA entries are configured", |
| 534 | pool_elts (im->sad)); |
| 535 | } |
| 536 | |
| 537 | unformat_input_t _line_input, *line_input = &_line_input; |
| 538 | /* Get a line of input. */ |
| 539 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 540 | return 0; |
| 541 | |
| 542 | if (unformat (line_input, "ah")) |
| 543 | { |
| 544 | if (unformat (line_input, "%u", &backend_index)) |
| 545 | { |
| 546 | if (ipsec_select_ah_backend (im, backend_index) < 0) |
| 547 | { |
| 548 | return clib_error_return (0, "Invalid AH backend index `%u'", |
| 549 | backend_index); |
| 550 | } |
| 551 | } |
| 552 | else |
| 553 | { |
| 554 | return clib_error_return (0, "Invalid backend index `%U'", |
| 555 | format_unformat_error, line_input); |
| 556 | } |
| 557 | } |
| 558 | else if (unformat (line_input, "esp")) |
| 559 | { |
| 560 | if (unformat (line_input, "%u", &backend_index)) |
| 561 | { |
| 562 | if (ipsec_select_esp_backend (im, backend_index) < 0) |
| 563 | { |
| 564 | return clib_error_return (0, "Invalid ESP backend index `%u'", |
| 565 | backend_index); |
| 566 | } |
| 567 | } |
| 568 | else |
| 569 | { |
| 570 | return clib_error_return (0, "Invalid backend index `%U'", |
| 571 | format_unformat_error, line_input); |
| 572 | } |
| 573 | } |
| 574 | else |
| 575 | { |
| 576 | return clib_error_return (0, "Unknown input `%U'", |
| 577 | format_unformat_error, line_input); |
| 578 | } |
| 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | /* *INDENT-OFF* */ |
| 584 | VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = { |
| 585 | .path = "ipsec select backend", |
| 586 | .short_help = "ipsec select backend <ah|esp> <backend index>", |
| 587 | .function = ipsec_select_backend_command_fn, |
| 588 | }; |
| 589 | |
| 590 | /* *INDENT-ON* */ |
| 591 | |
| 592 | static clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 593 | clear_ipsec_counters_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 594 | unformat_input_t * input, |
| 595 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 596 | { |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 597 | vlib_clear_combined_counters (&ipsec_spd_policy_counters); |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 598 | vlib_clear_combined_counters (&ipsec_sa_counters); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 599 | |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 600 | return (NULL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 603 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 604 | VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = { |
| 605 | .path = "clear ipsec counters", |
| 606 | .short_help = "clear ipsec counters", |
| 607 | .function = clear_ipsec_counters_command_fn, |
| 608 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 609 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 610 | |
| 611 | static clib_error_t * |
| 612 | create_ipsec_tunnel_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 613 | unformat_input_t * input, |
| 614 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 615 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 616 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 617 | ipsec_add_del_tunnel_args_t a; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 618 | int rv; |
| 619 | u32 num_m_args = 0; |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 620 | u8 ipv4_set = 0; |
| 621 | u8 ipv6_set = 0; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 622 | clib_error_t *error = NULL; |
Kingwel Xie | d3d1205 | 2019-03-07 06:34:30 -0500 | [diff] [blame] | 623 | ipsec_key_t rck = { 0 }; |
| 624 | ipsec_key_t lck = { 0 }; |
| 625 | ipsec_key_t lik = { 0 }; |
| 626 | ipsec_key_t rik = { 0 }; |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 627 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 628 | clib_memset (&a, 0, sizeof (a)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 629 | a.is_add = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 630 | |
| 631 | /* Get a line of input. */ |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 632 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 633 | return 0; |
| 634 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 635 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 636 | { |
| 637 | if (unformat |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 638 | (line_input, "local-ip %U", unformat_ip46_address, &a.local_ip, |
| 639 | IP46_TYPE_ANY)) |
| 640 | { |
| 641 | ip46_address_is_ip4 (&a.local_ip) ? (ipv4_set = 1) : (ipv6_set = 1); |
| 642 | num_m_args++; |
| 643 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 644 | else |
| 645 | if (unformat |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 646 | (line_input, "remote-ip %U", unformat_ip46_address, &a.remote_ip, |
| 647 | IP46_TYPE_ANY)) |
| 648 | { |
| 649 | ip46_address_is_ip4 (&a.remote_ip) ? (ipv4_set = 1) : (ipv6_set = |
| 650 | 1); |
| 651 | num_m_args++; |
| 652 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 653 | else if (unformat (line_input, "local-spi %u", &a.local_spi)) |
| 654 | num_m_args++; |
| 655 | else if (unformat (line_input, "remote-spi %u", &a.remote_spi)) |
| 656 | num_m_args++; |
Matthew Smith | 8e1039a | 2018-04-12 07:32:56 -0500 | [diff] [blame] | 657 | else if (unformat (line_input, "instance %u", &a.show_instance)) |
| 658 | a.renumber = 1; |
Radu Nicolau | 717de09 | 2018-08-03 10:37:24 +0100 | [diff] [blame] | 659 | else if (unformat (line_input, "udp-encap")) |
| 660 | a.udp_encap = 1; |
Kingwel Xie | 2baf942 | 2019-02-04 02:07:06 -0800 | [diff] [blame] | 661 | else if (unformat (line_input, "use-esn")) |
| 662 | a.esn = 1; |
| 663 | else if (unformat (line_input, "use-anti-replay")) |
| 664 | a.anti_replay = 1; |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 665 | else if (unformat (line_input, "tx-table %u", &a.tx_table_id)) |
| 666 | ; |
Neale Ranns | fd06084 | 2019-03-04 13:44:42 +0000 | [diff] [blame] | 667 | else |
| 668 | if (unformat |
| 669 | (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck)) |
| 670 | ; |
| 671 | else |
| 672 | if (unformat |
| 673 | (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck)) |
| 674 | ; |
| 675 | else if (unformat (line_input, "crypto-alg %U", |
| 676 | unformat_ipsec_crypto_alg, &a.crypto_alg)) |
| 677 | ; |
| 678 | else |
| 679 | if (unformat |
| 680 | (line_input, "local-integ-key %U", unformat_ipsec_key, &lik)) |
| 681 | ; |
| 682 | else |
| 683 | if (unformat |
| 684 | (line_input, "rmote-integ-key %U", unformat_ipsec_key, &rik)) |
| 685 | ; |
| 686 | else if (unformat (line_input, "integ-alg %U", |
| 687 | unformat_ipsec_integ_alg, &a.integ_alg)) |
| 688 | ; |
Kingwel Xie | 2baf942 | 2019-02-04 02:07:06 -0800 | [diff] [blame] | 689 | else if (unformat (line_input, "del")) |
| 690 | a.is_add = 0; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 691 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 692 | { |
| 693 | error = clib_error_return (0, "unknown input `%U'", |
| 694 | format_unformat_error, line_input); |
| 695 | goto done; |
| 696 | } |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 697 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 698 | |
| 699 | if (num_m_args < 4) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 700 | { |
| 701 | error = clib_error_return (0, "mandatory argument(s) missing"); |
| 702 | goto done; |
| 703 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 704 | |
Kingwel Xie | 72b3bce | 2019-02-12 06:45:08 -0500 | [diff] [blame] | 705 | if (ipv6_set) |
| 706 | return clib_error_return (0, "currently only IPv4 supported"); |
| 707 | |
| 708 | if (ipv4_set && ipv6_set) |
| 709 | return clib_error_return (0, "both IPv4 and IPv6 addresses specified"); |
| 710 | |
Neale Ranns | fd06084 | 2019-03-04 13:44:42 +0000 | [diff] [blame] | 711 | clib_memcpy (a.local_crypto_key, lck.data, lck.len); |
| 712 | a.local_crypto_key_len = lck.len; |
| 713 | clib_memcpy (a.remote_crypto_key, rck.data, rck.len); |
| 714 | a.remote_crypto_key_len = rck.len; |
| 715 | |
Kingwel Xie | d3d1205 | 2019-03-07 06:34:30 -0500 | [diff] [blame] | 716 | clib_memcpy (a.local_integ_key, lik.data, lik.len); |
Neale Ranns | fd06084 | 2019-03-04 13:44:42 +0000 | [diff] [blame] | 717 | a.local_integ_key_len = lck.len; |
Kingwel Xie | d3d1205 | 2019-03-07 06:34:30 -0500 | [diff] [blame] | 718 | clib_memcpy (a.remote_integ_key, rik.data, rik.len); |
Neale Ranns | fd06084 | 2019-03-04 13:44:42 +0000 | [diff] [blame] | 719 | a.remote_integ_key_len = rck.len; |
| 720 | |
Matthew Smith | 2838a23 | 2016-06-21 16:05:09 -0500 | [diff] [blame] | 721 | rv = ipsec_add_del_tunnel_if (&a); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 722 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 723 | switch (rv) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 724 | { |
| 725 | case 0: |
| 726 | break; |
| 727 | case VNET_API_ERROR_INVALID_VALUE: |
| 728 | if (a.is_add) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 729 | error = clib_error_return (0, |
| 730 | "IPSec tunnel interface already exists..."); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 731 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 732 | error = clib_error_return (0, "IPSec tunnel interface not exists..."); |
| 733 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 734 | default: |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 735 | error = clib_error_return (0, "ipsec_register_interface returned %d", |
| 736 | rv); |
| 737 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 740 | done: |
| 741 | unformat_free (line_input); |
| 742 | |
| 743 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 746 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 747 | VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = { |
| 748 | .path = "create ipsec tunnel", |
Pierre Pfister | 4c422f9 | 2018-12-10 11:19:08 +0100 | [diff] [blame] | 749 | .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> " |
Kingwel Xie | 2baf942 | 2019-02-04 02:07:06 -0800 | [diff] [blame] | 750 | "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] | 751 | "[tx-table <table-id>]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 752 | .function = create_ipsec_tunnel_command_fn, |
| 753 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 754 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 755 | |
| 756 | static clib_error_t * |
| 757 | set_interface_key_command_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 758 | unformat_input_t * input, |
| 759 | vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 760 | { |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 761 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 762 | ipsec_main_t *im = &ipsec_main; |
| 763 | ipsec_if_set_key_type_t type = IPSEC_IF_SET_KEY_TYPE_NONE; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 764 | u32 hw_if_index = (u32) ~ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 765 | u32 alg; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 766 | u8 *key = 0; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 767 | clib_error_t *error = NULL; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 768 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 769 | if (!unformat_user (input, unformat_line_input, line_input)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 770 | return 0; |
| 771 | |
| 772 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 773 | { |
| 774 | if (unformat (line_input, "%U", |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 775 | unformat_vnet_hw_interface, im->vnet_main, &hw_if_index)) |
| 776 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 777 | else |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 778 | if (unformat |
| 779 | (line_input, "local crypto %U", unformat_ipsec_crypto_alg, &alg)) |
| 780 | type = IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO; |
| 781 | else |
| 782 | if (unformat |
| 783 | (line_input, "remote crypto %U", unformat_ipsec_crypto_alg, &alg)) |
| 784 | type = IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO; |
| 785 | else |
| 786 | if (unformat |
| 787 | (line_input, "local integ %U", unformat_ipsec_integ_alg, &alg)) |
| 788 | type = IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG; |
| 789 | else |
| 790 | if (unformat |
| 791 | (line_input, "remote integ %U", unformat_ipsec_integ_alg, &alg)) |
| 792 | type = IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG; |
| 793 | else if (unformat (line_input, "%U", unformat_hex_string, &key)) |
| 794 | ; |
| 795 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 796 | { |
| 797 | error = clib_error_return (0, "parse error: '%U'", |
| 798 | format_unformat_error, line_input); |
| 799 | goto done; |
| 800 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 803 | if (type == IPSEC_IF_SET_KEY_TYPE_NONE) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 804 | { |
| 805 | error = clib_error_return (0, "unknown key type"); |
| 806 | goto done; |
| 807 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 808 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 809 | if (alg > 0 && vec_len (key) == 0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 810 | { |
| 811 | error = clib_error_return (0, "key is not specified"); |
| 812 | goto done; |
| 813 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 814 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 815 | if (hw_if_index == (u32) ~ 0) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 816 | { |
| 817 | error = clib_error_return (0, "interface not specified"); |
| 818 | goto done; |
| 819 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 820 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 821 | ipsec_set_interface_key (im->vnet_main, hw_if_index, type, alg, key); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 822 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 823 | done: |
| 824 | vec_free (key); |
| 825 | unformat_free (line_input); |
| 826 | |
| 827 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 830 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 831 | VLIB_CLI_COMMAND (set_interface_key_command, static) = { |
| 832 | .path = "set interface ipsec key", |
| 833 | .short_help = |
| 834 | "set interface ipsec key <int> <local|remote> <crypto|integ> <key type> <key>", |
| 835 | .function = set_interface_key_command_fn, |
| 836 | }; |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 837 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 838 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 839 | clib_error_t * |
| 840 | ipsec_cli_init (vlib_main_t * vm) |
| 841 | { |
| 842 | return 0; |
| 843 | } |
| 844 | |
| 845 | VLIB_INIT_FUNCTION (ipsec_cli_init); |
| 846 | |
Keith Burns (alagalah) | 166a9d4 | 2016-08-06 11:00:56 -0700 | [diff] [blame] | 847 | |
| 848 | /* |
| 849 | * fd.io coding-style-patch-verification: ON |
| 850 | * |
| 851 | * Local Variables: |
| 852 | * eval: (c-set-style "gnu") |
| 853 | * End: |
| 854 | */ |