blob: 0bc7aeae9961b1b3999c7574e1908a2e03114131 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Pfister4c422f92018-12-10 11:19:08 +010022#include <vnet/fib/fib.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070023
24#include <vnet/ipsec/ipsec.h>
Neale Rannsc87b66c2019-02-07 07:26:12 -080025#include <vnet/ipsec/ipsec_tun.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070026
27static clib_error_t *
28set_interface_spd_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070029 unformat_input_t * input,
30 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -070031{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070032 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -070033 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070034 u32 sw_if_index = (u32) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070035 u32 spd_id;
36 int is_add = 1;
Billy McFalla9a20e72017-02-15 11:39:12 -050037 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -070038
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070039 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -070040 return 0;
41
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070042 if (unformat
43 (line_input, "%U %u", unformat_vnet_sw_interface, im->vnet_main,
44 &sw_if_index, &spd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -070045 ;
46 else if (unformat (line_input, "del"))
47 is_add = 0;
48 else
Billy McFalla9a20e72017-02-15 11:39:12 -050049 {
50 error = clib_error_return (0, "parse error: '%U'",
51 format_unformat_error, line_input);
52 goto done;
53 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070054
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070055 ipsec_set_interface_spd (vm, sw_if_index, spd_id, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -070056
Billy McFalla9a20e72017-02-15 11:39:12 -050057done:
58 unformat_free (line_input);
59
60 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -070061}
62
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070063/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070064VLIB_CLI_COMMAND (set_interface_spd_command, static) = {
65 .path = "set interface ipsec spd",
66 .short_help =
67 "set interface ipsec spd <int> <id>",
68 .function = set_interface_spd_command_fn,
69};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070070/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070071
72static clib_error_t *
73ipsec_sa_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070074 unformat_input_t * input,
75 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -070076{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070077 unformat_input_t _line_input, *line_input = &_line_input;
Neale Ranns8d7c5022019-02-06 01:41:05 -080078 ip46_address_t tun_src = { }, tun_dst =
79 {
80 };
81 ipsec_crypto_alg_t crypto_alg;
82 ipsec_integ_alg_t integ_alg;
83 ipsec_protocol_t proto;
84 ipsec_sa_flags_t flags;
85 clib_error_t *error;
Kingwel Xied3d12052019-03-07 06:34:30 -050086 ipsec_key_t ck = { 0 };
87 ipsec_key_t ik = { 0 };
Neale Ranns80f6fd52019-04-16 02:41:34 +000088 u32 id, spi, salt;
Neale Ranns8d7c5022019-02-06 01:41:05 -080089 int is_add, rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -070090
Neale Ranns8d7c5022019-02-06 01:41:05 -080091 error = NULL;
92 is_add = 0;
93 flags = IPSEC_SA_FLAG_NONE;
94 proto = IPSEC_PROTOCOL_ESP;
Neale Rannse6be7022019-06-04 15:37:34 +000095 integ_alg = IPSEC_INTEG_ALG_NONE;
96 crypto_alg = IPSEC_CRYPTO_ALG_NONE;
Ed Warnickecb9cada2015-12-08 15:45:58 -070097
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070098 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -070099 return 0;
100
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700101 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
102 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800103 if (unformat (line_input, "add %u", &id))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700104 is_add = 1;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800105 else if (unformat (line_input, "del %u", &id))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700106 is_add = 0;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800107 else if (unformat (line_input, "spi %u", &spi))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700108 ;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800109 else if (unformat (line_input, "salt 0x%x", &salt))
Neale Ranns80f6fd52019-04-16 02:41:34 +0000110 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700111 else if (unformat (line_input, "esp"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800112 proto = IPSEC_PROTOCOL_ESP;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700113 else if (unformat (line_input, "ah"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800114 proto = IPSEC_PROTOCOL_AH;
115 else if (unformat (line_input, "crypto-key %U",
116 unformat_ipsec_key, &ck))
117 ;
118 else if (unformat (line_input, "crypto-alg %U",
119 unformat_ipsec_crypto_alg, &crypto_alg))
120 ;
121 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
122 ;
123 else if (unformat (line_input, "integ-alg %U",
124 unformat_ipsec_integ_alg, &integ_alg))
125 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700126 else if (unformat (line_input, "tunnel-src %U",
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500127 unformat_ip46_address, &tun_src, IP46_TYPE_ANY))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700128 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800129 flags |= IPSEC_SA_FLAG_IS_TUNNEL;
130 if (!ip46_address_is_ip4 (&tun_src))
131 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700132 }
133 else if (unformat (line_input, "tunnel-dst %U",
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500134 unformat_ip46_address, &tun_dst, IP46_TYPE_ANY))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800135 ;
Radu Nicolau717de092018-08-03 10:37:24 +0100136 else if (unformat (line_input, "udp-encap"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800137 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700138 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500139 {
140 error = clib_error_return (0, "parse error: '%U'",
141 format_unformat_error, line_input);
142 goto done;
143 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700144 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000146 if (is_add)
Neale Ranns495d7ff2019-07-12 09:15:26 +0000147 rv = ipsec_sa_add_and_lock (id, spi, proto, crypto_alg,
148 &ck, integ_alg, &ik, flags,
149 0, clib_host_to_net_u32 (salt),
150 &tun_src, &tun_dst, NULL);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800151 else
Neale Ranns495d7ff2019-07-12 09:15:26 +0000152 rv = ipsec_sa_unlock_id (id);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000153
Neale Ranns8d7c5022019-02-06 01:41:05 -0800154 if (rv)
Neale Rannse6be7022019-06-04 15:37:34 +0000155 error = clib_error_return (0, "failed");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156
Billy McFalla9a20e72017-02-15 11:39:12 -0500157done:
158 unformat_free (line_input);
159
160 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161}
162
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700163/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
165 .path = "ipsec sa",
166 .short_help =
167 "ipsec sa [add|del]",
168 .function = ipsec_sa_add_del_command_fn,
169};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700170/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171
172static clib_error_t *
173ipsec_spd_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700174 unformat_input_t * input,
175 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700177 unformat_input_t _line_input, *line_input = &_line_input;
Damjan Marion3f54b182016-08-16 11:27:02 +0200178 u32 spd_id = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 int is_add = ~0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500180 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700182 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183 return 0;
184
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700185 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
186 {
187 if (unformat (line_input, "add"))
188 is_add = 1;
189 else if (unformat (line_input, "del"))
190 is_add = 0;
191 else if (unformat (line_input, "%u", &spd_id))
192 ;
193 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500194 {
195 error = clib_error_return (0, "parse error: '%U'",
196 format_unformat_error, line_input);
197 goto done;
198 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700199 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200
Damjan Marion3f54b182016-08-16 11:27:02 +0200201 if (spd_id == ~0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500202 {
203 error = clib_error_return (0, "please specify SPD ID");
204 goto done;
205 }
Damjan Marion3f54b182016-08-16 11:27:02 +0200206
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700207 ipsec_add_del_spd (vm, spd_id, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208
Billy McFalla9a20e72017-02-15 11:39:12 -0500209done:
210 unformat_free (line_input);
211
212 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213}
214
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700215/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
217 .path = "ipsec spd",
218 .short_help =
219 "ipsec spd [add|del] <id>",
220 .function = ipsec_spd_add_del_command_fn,
221};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700222/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223
224
225static clib_error_t *
226ipsec_policy_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700227 unformat_input_t * input,
228 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700230 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231 ipsec_policy_t p;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800232 int rv, is_add = 0;
233 u32 tmp, tmp2, stat_index;
Billy McFalla9a20e72017-02-15 11:39:12 -0500234 clib_error_t *error = NULL;
Neale Ranns9f231d42019-03-19 10:06:00 +0000235 u32 is_outbound;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236
Dave Barachb7b92992018-10-17 10:38:51 -0400237 clib_memset (&p, 0, sizeof (p));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238 p.lport.stop = p.rport.stop = ~0;
Neale Ranns9f231d42019-03-19 10:06:00 +0000239 is_outbound = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700241 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242 return 0;
243
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700244 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
245 {
246 if (unformat (line_input, "add"))
247 is_add = 1;
248 else if (unformat (line_input, "del"))
249 is_add = 0;
250 else if (unformat (line_input, "spd %u", &p.id))
251 ;
252 else if (unformat (line_input, "inbound"))
Neale Ranns9f231d42019-03-19 10:06:00 +0000253 is_outbound = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700254 else if (unformat (line_input, "outbound"))
Neale Ranns9f231d42019-03-19 10:06:00 +0000255 is_outbound = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700256 else if (unformat (line_input, "priority %d", &p.priority))
257 ;
258 else if (unformat (line_input, "protocol %u", &tmp))
259 p.protocol = (u8) tmp;
260 else
261 if (unformat
262 (line_input, "action %U", unformat_ipsec_policy_action,
263 &p.policy))
264 {
265 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
Billy McFalla9a20e72017-02-15 11:39:12 -0500266 {
267 error = clib_error_return (0, "unsupported action: 'resolve'");
268 goto done;
269 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700270 }
271 else if (unformat (line_input, "sa %u", &p.sa_id))
272 ;
273 else if (unformat (line_input, "local-ip-range %U - %U",
274 unformat_ip4_address, &p.laddr.start.ip4,
275 unformat_ip4_address, &p.laddr.stop.ip4))
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800276 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700277 else if (unformat (line_input, "remote-ip-range %U - %U",
278 unformat_ip4_address, &p.raddr.start.ip4,
279 unformat_ip4_address, &p.raddr.stop.ip4))
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800280 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700281 else if (unformat (line_input, "local-ip-range %U - %U",
282 unformat_ip6_address, &p.laddr.start.ip6,
283 unformat_ip6_address, &p.laddr.stop.ip6))
284 {
285 p.is_ipv6 = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700286 }
287 else if (unformat (line_input, "remote-ip-range %U - %U",
288 unformat_ip6_address, &p.raddr.start.ip6,
289 unformat_ip6_address, &p.raddr.stop.ip6))
290 {
291 p.is_ipv6 = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700292 }
293 else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
294 {
295 p.lport.start = tmp;
296 p.lport.stop = tmp2;
297 }
298 else
299 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
300 {
301 p.rport.start = tmp;
302 p.rport.stop = tmp2;
303 }
304 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500305 {
306 error = clib_error_return (0, "parse error: '%U'",
307 format_unformat_error, line_input);
308 goto done;
309 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700310 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311
Neale Ranns9f231d42019-03-19 10:06:00 +0000312 rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
313
314 if (rv)
315 {
316 error = clib_error_return (0, "unsupported policy type for:",
317 " outboud:%s %s action:%U",
318 (is_outbound ? "yes" : "no"),
319 (p.is_ipv6 ? "IPv4" : "IPv6"),
320 format_ipsec_policy_action, p.policy);
321 goto done;
322 }
323
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800324 rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
325
326 if (!rv)
327 vlib_cli_output (vm, "policy-index:%d", stat_index);
328 else
329 vlib_cli_output (vm, "error:%d", rv);
Billy McFalla9a20e72017-02-15 11:39:12 -0500330
331done:
332 unformat_free (line_input);
333
334 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335}
336
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700337/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
339 .path = "ipsec policy",
340 .short_help =
341 "ipsec policy [add|del] spd <id> priority <n> ",
342 .function = ipsec_policy_add_del_command_fn,
343};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700344/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345
Neale Rannsb294f102019-04-03 13:17:50 +0000346static void
Neale Ranns670027a2019-08-27 12:47:17 +0000347ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im, u8 detail)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348{
Neale Rannsb294f102019-04-03 13:17:50 +0000349 u32 sai;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700351 /* *INDENT-OFF* */
Neale Ranns8d7c5022019-02-06 01:41:05 -0800352 pool_foreach_index (sai, im->sad, ({
Neale Ranns670027a2019-08-27 12:47:17 +0000353 vlib_cli_output(vm, "%U", format_ipsec_sa, sai,
354 (detail ? IPSEC_FORMAT_DETAIL : IPSEC_FORMAT_BRIEF));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355 }));
Neale Rannsb294f102019-04-03 13:17:50 +0000356 /* *INDENT-ON* */
357}
358
359static void
360ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im)
361{
362 u32 spdi;
363
364 /* *INDENT-OFF* */
365 pool_foreach_index (spdi, im->spds, ({
366 vlib_cli_output(vm, "%U", format_ipsec_spd, spdi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367 }));
Neale Rannsb294f102019-04-03 13:17:50 +0000368 /* *INDENT-ON* */
369}
370
371static void
372ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im)
373{
374 u32 spd_id, sw_if_index;
Neale Rannsd83c4a82019-06-14 06:48:27 -0700375 ipsec_spd_t *spd;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700376
Neale Ranns311124e2019-01-24 04:52:25 -0800377 vlib_cli_output (vm, "SPD Bindings:");
Neale Ranns8d7c5022019-02-06 01:41:05 -0800378
Neale Rannsb294f102019-04-03 13:17:50 +0000379 /* *INDENT-OFF* */
Neale Ranns311124e2019-01-24 04:52:25 -0800380 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
Neale Rannsd83c4a82019-06-14 06:48:27 -0700381 spd = pool_elt_at_index (im->spds, spd_id);
382 vlib_cli_output (vm, " %d -> %U", spd->id,
Neale Ranns8d7c5022019-02-06 01:41:05 -0800383 format_vnet_sw_if_index_name, im->vnet_main,
384 sw_if_index);
Neale Ranns311124e2019-01-24 04:52:25 -0800385 }));
386 /* *INDENT-ON* */
Neale Rannsb294f102019-04-03 13:17:50 +0000387}
Neale Ranns311124e2019-01-24 04:52:25 -0800388
Neale Rannsb294f102019-04-03 13:17:50 +0000389static void
390ipsec_tunnel_show_all (vlib_main_t * vm, ipsec_main_t * im)
391{
392 u32 ti;
393
394 vlib_cli_output (vm, "Tunnel interfaces");
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700395 /* *INDENT-OFF* */
Neale Rannsb294f102019-04-03 13:17:50 +0000396 pool_foreach_index (ti, im->tunnel_interfaces, ({
397 vlib_cli_output(vm, " %U", format_ipsec_tunnel, ti);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700399 /* *INDENT-ON* */
Neale Rannsb294f102019-04-03 13:17:50 +0000400}
401
402static clib_error_t *
403show_ipsec_command_fn (vlib_main_t * vm,
404 unformat_input_t * input, vlib_cli_command_t * cmd)
405{
406 ipsec_main_t *im = &ipsec_main;
407
Neale Ranns670027a2019-08-27 12:47:17 +0000408 ipsec_sa_show_all (vm, im, 0);
Neale Rannsb294f102019-04-03 13:17:50 +0000409 ipsec_spd_show_all (vm, im);
410 ipsec_spd_bindings_show_all (vm, im);
411 ipsec_tunnel_show_all (vm, im);
412
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413 return 0;
414}
415
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700416/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417VLIB_CLI_COMMAND (show_ipsec_command, static) = {
Neale Rannsb294f102019-04-03 13:17:50 +0000418 .path = "show ipsec all",
419 .short_help = "show ipsec all",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420 .function = show_ipsec_command_fn,
421};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700422/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423
424static clib_error_t *
Neale Rannsb294f102019-04-03 13:17:50 +0000425show_ipsec_sa_command_fn (vlib_main_t * vm,
426 unformat_input_t * input, vlib_cli_command_t * cmd)
427{
428 ipsec_main_t *im = &ipsec_main;
429 u32 sai = ~0;
Neale Ranns670027a2019-08-27 12:47:17 +0000430 u8 detail = 0;
Neale Rannsb294f102019-04-03 13:17:50 +0000431
432 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
433 {
434 if (unformat (input, "%u", &sai))
435 ;
Neale Ranns670027a2019-08-27 12:47:17 +0000436 if (unformat (input, "detail"))
437 detail = 1;
Neale Rannsb294f102019-04-03 13:17:50 +0000438 else
439 break;
440 }
441
442 if (~0 == sai)
Neale Ranns670027a2019-08-27 12:47:17 +0000443 ipsec_sa_show_all (vm, im, detail);
Neale Rannsb294f102019-04-03 13:17:50 +0000444 else
445 vlib_cli_output (vm, "%U", format_ipsec_sa, sai, IPSEC_FORMAT_DETAIL);
446
447 return 0;
448}
449
Neale Rannsc87b66c2019-02-07 07:26:12 -0800450static clib_error_t *
451clear_ipsec_sa_command_fn (vlib_main_t * vm,
452 unformat_input_t * input, vlib_cli_command_t * cmd)
453{
454 ipsec_main_t *im = &ipsec_main;
455 u32 sai = ~0;
456
457 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
458 {
459 if (unformat (input, "%u", &sai))
460 ;
461 else
462 break;
463 }
464
465 if (~0 == sai)
466 {
467 /* *INDENT-OFF* */
468 pool_foreach_index (sai, im->sad, ({
469 ipsec_sa_clear(sai);
470 }));
471 /* *INDENT-ON* */
472 }
473 else
474 {
475 if (pool_is_free_index (im->sad, sai))
476 return clib_error_return (0, "unknown SA index: %d", sai);
477 else
478 ipsec_sa_clear (sai);
479 }
480
481 return 0;
482}
483
Neale Rannsb294f102019-04-03 13:17:50 +0000484/* *INDENT-OFF* */
485VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
486 .path = "show ipsec sa",
487 .short_help = "show ipsec sa [index]",
488 .function = show_ipsec_sa_command_fn,
489};
Neale Rannsc87b66c2019-02-07 07:26:12 -0800490
491VLIB_CLI_COMMAND (clear_ipsec_sa_command, static) = {
492 .path = "clear ipsec sa",
493 .short_help = "clear ipsec sa [index]",
494 .function = clear_ipsec_sa_command_fn,
495};
Neale Rannsb294f102019-04-03 13:17:50 +0000496/* *INDENT-ON* */
497
498static clib_error_t *
499show_ipsec_spd_command_fn (vlib_main_t * vm,
500 unformat_input_t * input, vlib_cli_command_t * cmd)
501{
502 ipsec_main_t *im = &ipsec_main;
503 u8 show_bindings = 0;
504 u32 spdi = ~0;
505
506 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
507 {
508 if (unformat (input, "%u", &spdi))
509 ;
510 else if (unformat (input, "bindings"))
511 show_bindings = 1;
512 else
513 break;
514 }
515
516 if (show_bindings)
517 ipsec_spd_bindings_show_all (vm, im);
518 else if (~0 != spdi)
519 vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
520 else
521 ipsec_spd_show_all (vm, im);
522
523 return 0;
524}
525
526/* *INDENT-OFF* */
527VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
528 .path = "show ipsec spd",
529 .short_help = "show ipsec spd [index]",
530 .function = show_ipsec_spd_command_fn,
531};
532/* *INDENT-ON* */
533
534static clib_error_t *
535show_ipsec_tunnel_command_fn (vlib_main_t * vm,
536 unformat_input_t * input,
537 vlib_cli_command_t * cmd)
538{
539 ipsec_main_t *im = &ipsec_main;
540 u32 ti = ~0;
541
542 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
543 {
544 if (unformat (input, "%u", &ti))
545 ;
546 else
547 break;
548 }
549
550 if (~0 != ti)
551 vlib_cli_output (vm, "%U", format_ipsec_tunnel, ti);
552 else
553 ipsec_tunnel_show_all (vm, im);
554
555 return 0;
556}
557
558/* *INDENT-OFF* */
559VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
560 .path = "show ipsec tunnel",
561 .short_help = "show ipsec tunnel [index]",
562 .function = show_ipsec_tunnel_command_fn,
563};
564/* *INDENT-ON* */
565
566static clib_error_t *
Klement Sekerab4d30532018-11-08 13:00:02 +0100567ipsec_show_backends_command_fn (vlib_main_t * vm,
568 unformat_input_t * input,
569 vlib_cli_command_t * cmd)
570{
571 ipsec_main_t *im = &ipsec_main;
572 u32 verbose = 0;
573
574 (void) unformat (input, "verbose %u", &verbose);
575
576 vlib_cli_output (vm, "IPsec AH backends available:");
577 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
578 ipsec_ah_backend_t *ab;
579 /* *INDENT-OFF* */
580 pool_foreach (ab, im->ah_backends, {
581 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
582 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
583 if (verbose) {
584 vlib_node_t *n;
585 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
586 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
587 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
588 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
589 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
590 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
591 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
592 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
593 }
594 });
595 /* *INDENT-ON* */
596 vlib_cli_output (vm, "%v", s);
597 _vec_len (s) = 0;
598 vlib_cli_output (vm, "IPsec ESP backends available:");
599 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
600 ipsec_esp_backend_t *eb;
601 /* *INDENT-OFF* */
602 pool_foreach (eb, im->esp_backends, {
603 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
604 eb - im->esp_backends == im->esp_current_backend ? "yes"
605 : "no");
606 if (verbose) {
607 vlib_node_t *n;
608 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
609 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
610 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
611 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
612 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
613 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
614 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
615 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
616 }
617 });
618 /* *INDENT-ON* */
619 vlib_cli_output (vm, "%v", s);
620
621 vec_free (s);
622 return 0;
623}
624
625/* *INDENT-OFF* */
626VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
627 .path = "show ipsec backends",
628 .short_help = "show ipsec backends",
629 .function = ipsec_show_backends_command_fn,
630};
631/* *INDENT-ON* */
632
633static clib_error_t *
634ipsec_select_backend_command_fn (vlib_main_t * vm,
635 unformat_input_t * input,
636 vlib_cli_command_t * cmd)
637{
Klement Sekerab4d30532018-11-08 13:00:02 +0100638 unformat_input_t _line_input, *line_input = &_line_input;
Neale Rannse8915fc2019-04-23 20:57:55 -0400639 ipsec_main_t *im = &ipsec_main;
640 clib_error_t *error;
641 u32 backend_index;
642
643 error = ipsec_rsc_in_use (im);
644
645 if (error)
646 return error;
647
Klement Sekerab4d30532018-11-08 13:00:02 +0100648 /* Get a line of input. */
649 if (!unformat_user (input, unformat_line_input, line_input))
650 return 0;
651
652 if (unformat (line_input, "ah"))
653 {
654 if (unformat (line_input, "%u", &backend_index))
655 {
656 if (ipsec_select_ah_backend (im, backend_index) < 0)
657 {
658 return clib_error_return (0, "Invalid AH backend index `%u'",
659 backend_index);
660 }
661 }
662 else
663 {
664 return clib_error_return (0, "Invalid backend index `%U'",
665 format_unformat_error, line_input);
666 }
667 }
668 else if (unformat (line_input, "esp"))
669 {
670 if (unformat (line_input, "%u", &backend_index))
671 {
672 if (ipsec_select_esp_backend (im, backend_index) < 0)
673 {
674 return clib_error_return (0, "Invalid ESP backend index `%u'",
675 backend_index);
676 }
677 }
678 else
679 {
680 return clib_error_return (0, "Invalid backend index `%U'",
681 format_unformat_error, line_input);
682 }
683 }
684 else
685 {
686 return clib_error_return (0, "Unknown input `%U'",
687 format_unformat_error, line_input);
688 }
689
690 return 0;
691}
692
693/* *INDENT-OFF* */
694VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
695 .path = "ipsec select backend",
696 .short_help = "ipsec select backend <ah|esp> <backend index>",
697 .function = ipsec_select_backend_command_fn,
698};
699
700/* *INDENT-ON* */
701
702static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700703clear_ipsec_counters_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700704 unformat_input_t * input,
705 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700706{
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800707 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
Neale Rannseba31ec2019-02-17 18:04:27 +0000708 vlib_clear_combined_counters (&ipsec_sa_counters);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700709
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800710 return (NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711}
712
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700713/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
715 .path = "clear ipsec counters",
716 .short_help = "clear ipsec counters",
717 .function = clear_ipsec_counters_command_fn,
718};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700719/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720
721static clib_error_t *
722create_ipsec_tunnel_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700723 unformat_input_t * input,
724 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700725{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700726 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700727 ipsec_add_del_tunnel_args_t a;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728 int rv;
729 u32 num_m_args = 0;
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500730 u8 ipv4_set = 0;
731 u8 ipv6_set = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500732 clib_error_t *error = NULL;
Kingwel Xied3d12052019-03-07 06:34:30 -0500733 ipsec_key_t rck = { 0 };
734 ipsec_key_t lck = { 0 };
735 ipsec_key_t lik = { 0 };
736 ipsec_key_t rik = { 0 };
Matthew Smith2838a232016-06-21 16:05:09 -0500737
Dave Barachb7b92992018-10-17 10:38:51 -0400738 clib_memset (&a, 0, sizeof (a));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700739 a.is_add = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700740
741 /* Get a line of input. */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700742 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700743 return 0;
744
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700745 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
746 {
747 if (unformat
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500748 (line_input, "local-ip %U", unformat_ip46_address, &a.local_ip,
749 IP46_TYPE_ANY))
750 {
751 ip46_address_is_ip4 (&a.local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
752 num_m_args++;
753 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700754 else
755 if (unformat
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500756 (line_input, "remote-ip %U", unformat_ip46_address, &a.remote_ip,
757 IP46_TYPE_ANY))
758 {
759 ip46_address_is_ip4 (&a.remote_ip) ? (ipv4_set = 1) : (ipv6_set =
760 1);
761 num_m_args++;
762 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700763 else if (unformat (line_input, "local-spi %u", &a.local_spi))
764 num_m_args++;
765 else if (unformat (line_input, "remote-spi %u", &a.remote_spi))
766 num_m_args++;
Matthew Smith8e1039a2018-04-12 07:32:56 -0500767 else if (unformat (line_input, "instance %u", &a.show_instance))
768 a.renumber = 1;
Neale Ranns47feb112019-04-11 15:14:07 +0000769 else if (unformat (line_input, "salt 0x%x", &a.salt))
770 ;
Radu Nicolau717de092018-08-03 10:37:24 +0100771 else if (unformat (line_input, "udp-encap"))
772 a.udp_encap = 1;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800773 else if (unformat (line_input, "use-esn"))
774 a.esn = 1;
775 else if (unformat (line_input, "use-anti-replay"))
776 a.anti_replay = 1;
Pierre Pfister4c422f92018-12-10 11:19:08 +0100777 else if (unformat (line_input, "tx-table %u", &a.tx_table_id))
778 ;
Neale Rannsfd060842019-03-04 13:44:42 +0000779 else
780 if (unformat
781 (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
782 ;
783 else
784 if (unformat
785 (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
786 ;
787 else if (unformat (line_input, "crypto-alg %U",
788 unformat_ipsec_crypto_alg, &a.crypto_alg))
789 ;
790 else
791 if (unformat
792 (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
793 ;
794 else
795 if (unformat
Simon Zhange8e950a2019-04-23 23:04:07 +0800796 (line_input, "remote-integ-key %U", unformat_ipsec_key, &rik))
Neale Rannsfd060842019-03-04 13:44:42 +0000797 ;
798 else if (unformat (line_input, "integ-alg %U",
799 unformat_ipsec_integ_alg, &a.integ_alg))
800 ;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800801 else if (unformat (line_input, "del"))
802 a.is_add = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700803 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500804 {
805 error = clib_error_return (0, "unknown input `%U'",
806 format_unformat_error, line_input);
807 goto done;
808 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700809 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700810
811 if (num_m_args < 4)
Billy McFalla9a20e72017-02-15 11:39:12 -0500812 {
813 error = clib_error_return (0, "mandatory argument(s) missing");
814 goto done;
815 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700816
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500817 if (ipv4_set && ipv6_set)
818 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
819
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400820 a.is_ip6 = ipv6_set;
821
Neale Rannsfd060842019-03-04 13:44:42 +0000822 clib_memcpy (a.local_crypto_key, lck.data, lck.len);
823 a.local_crypto_key_len = lck.len;
824 clib_memcpy (a.remote_crypto_key, rck.data, rck.len);
825 a.remote_crypto_key_len = rck.len;
826
Kingwel Xied3d12052019-03-07 06:34:30 -0500827 clib_memcpy (a.local_integ_key, lik.data, lik.len);
Neale Rannsfd060842019-03-04 13:44:42 +0000828 a.local_integ_key_len = lck.len;
Kingwel Xied3d12052019-03-07 06:34:30 -0500829 clib_memcpy (a.remote_integ_key, rik.data, rik.len);
Neale Rannsfd060842019-03-04 13:44:42 +0000830 a.remote_integ_key_len = rck.len;
831
Matthew Smith2838a232016-06-21 16:05:09 -0500832 rv = ipsec_add_del_tunnel_if (&a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700833
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700834 switch (rv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700835 {
836 case 0:
837 break;
838 case VNET_API_ERROR_INVALID_VALUE:
839 if (a.is_add)
Billy McFalla9a20e72017-02-15 11:39:12 -0500840 error = clib_error_return (0,
841 "IPSec tunnel interface already exists...");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700842 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500843 error = clib_error_return (0, "IPSec tunnel interface not exists...");
844 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700845 default:
Billy McFalla9a20e72017-02-15 11:39:12 -0500846 error = clib_error_return (0, "ipsec_register_interface returned %d",
847 rv);
848 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700849 }
850
Billy McFalla9a20e72017-02-15 11:39:12 -0500851done:
852 unformat_free (line_input);
853
854 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700855}
856
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700857/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700858VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
859 .path = "create ipsec tunnel",
Pierre Pfister4c422f92018-12-10 11:19:08 +0100860 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
Kingwel Xie2baf9422019-02-04 02:07:06 -0800861 "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
Pierre Pfister4c422f92018-12-10 11:19:08 +0100862 "[tx-table <table-id>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700863 .function = create_ipsec_tunnel_command_fn,
864};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700865/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700866
Neale Rannsc87b66c2019-02-07 07:26:12 -0800867static clib_error_t *
868ipsec_tun_protect_cmd (vlib_main_t * vm,
869 unformat_input_t * input, vlib_cli_command_t * cmd)
870{
871 unformat_input_t _line_input, *line_input = &_line_input;
872 u32 sw_if_index, is_del, sa_in, sa_out, *sa_ins = NULL;
873 vnet_main_t *vnm;
874
875 is_del = 0;
876 sw_if_index = ~0;
877 vnm = vnet_get_main ();
878
879 if (!unformat_user (input, unformat_line_input, line_input))
880 return 0;
881
882 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
883 {
884 if (unformat (line_input, "del"))
885 is_del = 1;
886 else if (unformat (line_input, "add"))
887 is_del = 0;
888 else if (unformat (line_input, "sa-in %d", &sa_in))
889 vec_add1 (sa_ins, sa_in);
890 else if (unformat (line_input, "sa-out %d", &sa_out))
891 ;
892 else if (unformat (line_input, "%U",
893 unformat_vnet_sw_interface, vnm, &sw_if_index))
894 ;
895 else
896 return (clib_error_return (0, "unknown input '%U'",
897 format_unformat_error, line_input));
898 }
899
900 if (!is_del)
901 ipsec_tun_protect_update (sw_if_index, sa_out, sa_ins);
902
903 unformat_free (line_input);
904 return NULL;
905}
906
907/**
908 * Protect tunnel with IPSEC
909 */
910/* *INDENT-OFF* */
911VLIB_CLI_COMMAND (ipsec_tun_protect_cmd_node, static) =
912{
913 .path = "ipsec tunnel protect",
914 .function = ipsec_tun_protect_cmd,
915 .short_help = "ipsec tunnel protect <interface> input-sa <SA> output-sa <SA>",
916 // this is not MP safe
917};
918/* *INDENT-ON* */
919
920static walk_rc_t
921ipsec_tun_protect_show_one (index_t itpi, void *ctx)
922{
923 vlib_cli_output (ctx, "%U", format_ipsec_tun_protect, itpi);
924
925 return (WALK_CONTINUE);
926}
927
928static clib_error_t *
929ipsec_tun_protect_show (vlib_main_t * vm,
930 unformat_input_t * input, vlib_cli_command_t * cmd)
931{
932 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
933
934 return NULL;
935}
936
937/**
938 * show IPSEC tunnel protection
939 */
940/* *INDENT-OFF* */
941VLIB_CLI_COMMAND (ipsec_tun_protect_show_node, static) =
942{
943 .path = "show ipsec protect",
944 .function = ipsec_tun_protect_show,
945 .short_help = "show ipsec protect",
946};
947/* *INDENT-ON* */
948
Neale Ranns41afb332019-07-16 06:19:35 -0700949static clib_error_t *
950ipsec_tun_protect_hash_show (vlib_main_t * vm,
951 unformat_input_t * input,
952 vlib_cli_command_t * cmd)
953{
954 ipsec_main_t *im = &ipsec_main;
955
956 {
957 ipsec_tun_lkup_result_t value;
958 ipsec4_tunnel_key_t key;
959
960 vlib_cli_output (vm, "IPv4:");
961
962 /* *INDENT-OFF* */
963 hash_foreach(key.as_u64, value.as_u64, im->tun4_protect_by_key,
964 ({
965 vlib_cli_output (vm, " %U", format_ipsec4_tunnel_key, &key);
966 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
967 }));
968 /* *INDENT-ON* */
969 }
970
971 {
972 ipsec_tun_lkup_result_t value;
973 ipsec6_tunnel_key_t *key;
974
975 vlib_cli_output (vm, "IPv6:");
976
977 /* *INDENT-OFF* */
978 hash_foreach_mem(key, value.as_u64, im->tun6_protect_by_key,
979 ({
980 vlib_cli_output (vm, " %U", format_ipsec6_tunnel_key, key);
981 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
982 }));
983 /* *INDENT-ON* */
984 }
985
986 return NULL;
987}
988
989/**
990 * show IPSEC tunnel protection hash tables
991 */
992/* *INDENT-OFF* */
993VLIB_CLI_COMMAND (ipsec_tun_protect_hash_show_node, static) =
994{
995 .path = "show ipsec protect-hash",
996 .function = ipsec_tun_protect_hash_show,
997 .short_help = "show ipsec protect-hash",
998};
999/* *INDENT-ON* */
1000
Ed Warnickecb9cada2015-12-08 15:45:58 -07001001clib_error_t *
1002ipsec_cli_init (vlib_main_t * vm)
1003{
1004 return 0;
1005}
1006
1007VLIB_INIT_FUNCTION (ipsec_cli_init);
1008
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001009
1010/*
1011 * fd.io coding-style-patch-verification: ON
1012 *
1013 * Local Variables:
1014 * eval: (c-set-style "gnu")
1015 * End:
1016 */