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