blob: 5385a0f15c88c1cb3e34e4aedcf13b98ea0984e9 [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>
Neale Ranns12989b52019-09-26 16:20:19 +000023#include <vnet/ipip/ipip.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070024
25#include <vnet/ipsec/ipsec.h>
Neale Rannsc87b66c2019-02-07 07:26:12 -080026#include <vnet/ipsec/ipsec_tun.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070027
28static clib_error_t *
29set_interface_spd_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070030 unformat_input_t * input,
31 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -070032{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070033 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -070034 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070035 u32 sw_if_index = (u32) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070036 u32 spd_id;
37 int is_add = 1;
Billy McFalla9a20e72017-02-15 11:39:12 -050038 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -070039
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070040 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -070041 return 0;
42
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070043 if (unformat
44 (line_input, "%U %u", unformat_vnet_sw_interface, im->vnet_main,
45 &sw_if_index, &spd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -070046 ;
47 else if (unformat (line_input, "del"))
48 is_add = 0;
49 else
Billy McFalla9a20e72017-02-15 11:39:12 -050050 {
51 error = clib_error_return (0, "parse error: '%U'",
52 format_unformat_error, line_input);
53 goto done;
54 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070055
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070056 ipsec_set_interface_spd (vm, sw_if_index, spd_id, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -070057
Billy McFalla9a20e72017-02-15 11:39:12 -050058done:
59 unformat_free (line_input);
60
61 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -070062}
63
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070064/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070065VLIB_CLI_COMMAND (set_interface_spd_command, static) = {
66 .path = "set interface ipsec spd",
67 .short_help =
68 "set interface ipsec spd <int> <id>",
69 .function = set_interface_spd_command_fn,
70};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070071/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070072
73static clib_error_t *
74ipsec_sa_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070075 unformat_input_t * input,
76 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -070077{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070078 unformat_input_t _line_input, *line_input = &_line_input;
Neale Ranns8d7c5022019-02-06 01:41:05 -080079 ip46_address_t tun_src = { }, tun_dst =
80 {
81 };
82 ipsec_crypto_alg_t crypto_alg;
83 ipsec_integ_alg_t integ_alg;
84 ipsec_protocol_t proto;
85 ipsec_sa_flags_t flags;
86 clib_error_t *error;
Kingwel Xied3d12052019-03-07 06:34:30 -050087 ipsec_key_t ck = { 0 };
88 ipsec_key_t ik = { 0 };
Neale Ranns80f6fd52019-04-16 02:41:34 +000089 u32 id, spi, salt;
Neale Ranns8d7c5022019-02-06 01:41:05 -080090 int is_add, rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -070091
Neale Ranns8d7c5022019-02-06 01:41:05 -080092 error = NULL;
93 is_add = 0;
94 flags = IPSEC_SA_FLAG_NONE;
95 proto = IPSEC_PROTOCOL_ESP;
Neale Rannse6be7022019-06-04 15:37:34 +000096 integ_alg = IPSEC_INTEG_ALG_NONE;
97 crypto_alg = IPSEC_CRYPTO_ALG_NONE;
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070099 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100 return 0;
101
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700102 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
103 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800104 if (unformat (line_input, "add %u", &id))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700105 is_add = 1;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800106 else if (unformat (line_input, "del %u", &id))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700107 is_add = 0;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800108 else if (unformat (line_input, "spi %u", &spi))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700109 ;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800110 else if (unformat (line_input, "salt 0x%x", &salt))
Neale Ranns80f6fd52019-04-16 02:41:34 +0000111 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700112 else if (unformat (line_input, "esp"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800113 proto = IPSEC_PROTOCOL_ESP;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700114 else if (unformat (line_input, "ah"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800115 proto = IPSEC_PROTOCOL_AH;
116 else if (unformat (line_input, "crypto-key %U",
117 unformat_ipsec_key, &ck))
118 ;
119 else if (unformat (line_input, "crypto-alg %U",
120 unformat_ipsec_crypto_alg, &crypto_alg))
121 ;
122 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
123 ;
124 else if (unformat (line_input, "integ-alg %U",
125 unformat_ipsec_integ_alg, &integ_alg))
126 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700127 else if (unformat (line_input, "tunnel-src %U",
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500128 unformat_ip46_address, &tun_src, IP46_TYPE_ANY))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700129 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800130 flags |= IPSEC_SA_FLAG_IS_TUNNEL;
131 if (!ip46_address_is_ip4 (&tun_src))
132 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700133 }
134 else if (unformat (line_input, "tunnel-dst %U",
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500135 unformat_ip46_address, &tun_dst, IP46_TYPE_ANY))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800136 ;
Radu Nicolau717de092018-08-03 10:37:24 +0100137 else if (unformat (line_input, "udp-encap"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800138 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700139 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500140 {
141 error = clib_error_return (0, "parse error: '%U'",
142 format_unformat_error, line_input);
143 goto done;
144 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700145 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000147 if (is_add)
Neale Ranns495d7ff2019-07-12 09:15:26 +0000148 rv = ipsec_sa_add_and_lock (id, spi, proto, crypto_alg,
149 &ck, integ_alg, &ik, flags,
150 0, clib_host_to_net_u32 (salt),
151 &tun_src, &tun_dst, NULL);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800152 else
Neale Ranns495d7ff2019-07-12 09:15:26 +0000153 rv = ipsec_sa_unlock_id (id);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000154
Neale Ranns8d7c5022019-02-06 01:41:05 -0800155 if (rv)
Neale Rannse6be7022019-06-04 15:37:34 +0000156 error = clib_error_return (0, "failed");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157
Billy McFalla9a20e72017-02-15 11:39:12 -0500158done:
159 unformat_free (line_input);
160
161 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162}
163
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700164/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
166 .path = "ipsec sa",
167 .short_help =
168 "ipsec sa [add|del]",
169 .function = ipsec_sa_add_del_command_fn,
170};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700171/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172
173static clib_error_t *
174ipsec_spd_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700175 unformat_input_t * input,
176 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700178 unformat_input_t _line_input, *line_input = &_line_input;
Damjan Marion3f54b182016-08-16 11:27:02 +0200179 u32 spd_id = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180 int is_add = ~0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500181 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700183 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184 return 0;
185
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700186 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
187 {
188 if (unformat (line_input, "add"))
189 is_add = 1;
190 else if (unformat (line_input, "del"))
191 is_add = 0;
192 else if (unformat (line_input, "%u", &spd_id))
193 ;
194 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500195 {
196 error = clib_error_return (0, "parse error: '%U'",
197 format_unformat_error, line_input);
198 goto done;
199 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700200 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201
Damjan Marion3f54b182016-08-16 11:27:02 +0200202 if (spd_id == ~0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500203 {
204 error = clib_error_return (0, "please specify SPD ID");
205 goto done;
206 }
Damjan Marion3f54b182016-08-16 11:27:02 +0200207
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700208 ipsec_add_del_spd (vm, spd_id, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209
Billy McFalla9a20e72017-02-15 11:39:12 -0500210done:
211 unformat_free (line_input);
212
213 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214}
215
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700216/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
218 .path = "ipsec spd",
219 .short_help =
220 "ipsec spd [add|del] <id>",
221 .function = ipsec_spd_add_del_command_fn,
222};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700223/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224
225
226static clib_error_t *
227ipsec_policy_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700228 unformat_input_t * input,
229 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700231 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232 ipsec_policy_t p;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800233 int rv, is_add = 0;
234 u32 tmp, tmp2, stat_index;
Billy McFalla9a20e72017-02-15 11:39:12 -0500235 clib_error_t *error = NULL;
Neale Ranns9f231d42019-03-19 10:06:00 +0000236 u32 is_outbound;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237
Dave Barachb7b92992018-10-17 10:38:51 -0400238 clib_memset (&p, 0, sizeof (p));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239 p.lport.stop = p.rport.stop = ~0;
Neale Ranns9f231d42019-03-19 10:06:00 +0000240 is_outbound = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700242 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243 return 0;
244
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700245 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
246 {
247 if (unformat (line_input, "add"))
248 is_add = 1;
249 else if (unformat (line_input, "del"))
250 is_add = 0;
251 else if (unformat (line_input, "spd %u", &p.id))
252 ;
253 else if (unformat (line_input, "inbound"))
Neale Ranns9f231d42019-03-19 10:06:00 +0000254 is_outbound = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700255 else if (unformat (line_input, "outbound"))
Neale Ranns9f231d42019-03-19 10:06:00 +0000256 is_outbound = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700257 else if (unformat (line_input, "priority %d", &p.priority))
258 ;
259 else if (unformat (line_input, "protocol %u", &tmp))
260 p.protocol = (u8) tmp;
261 else
262 if (unformat
263 (line_input, "action %U", unformat_ipsec_policy_action,
264 &p.policy))
265 {
266 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
Billy McFalla9a20e72017-02-15 11:39:12 -0500267 {
268 error = clib_error_return (0, "unsupported action: 'resolve'");
269 goto done;
270 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700271 }
272 else if (unformat (line_input, "sa %u", &p.sa_id))
273 ;
274 else if (unformat (line_input, "local-ip-range %U - %U",
275 unformat_ip4_address, &p.laddr.start.ip4,
276 unformat_ip4_address, &p.laddr.stop.ip4))
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800277 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700278 else if (unformat (line_input, "remote-ip-range %U - %U",
279 unformat_ip4_address, &p.raddr.start.ip4,
280 unformat_ip4_address, &p.raddr.stop.ip4))
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800281 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700282 else if (unformat (line_input, "local-ip-range %U - %U",
283 unformat_ip6_address, &p.laddr.start.ip6,
284 unformat_ip6_address, &p.laddr.stop.ip6))
285 {
286 p.is_ipv6 = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700287 }
288 else if (unformat (line_input, "remote-ip-range %U - %U",
289 unformat_ip6_address, &p.raddr.start.ip6,
290 unformat_ip6_address, &p.raddr.stop.ip6))
291 {
292 p.is_ipv6 = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700293 }
294 else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
295 {
296 p.lport.start = tmp;
297 p.lport.stop = tmp2;
298 }
299 else
300 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
301 {
302 p.rport.start = tmp;
303 p.rport.stop = tmp2;
304 }
305 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500306 {
307 error = clib_error_return (0, "parse error: '%U'",
308 format_unformat_error, line_input);
309 goto done;
310 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700311 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312
Neale Ranns9f231d42019-03-19 10:06:00 +0000313 rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
314
315 if (rv)
316 {
317 error = clib_error_return (0, "unsupported policy type for:",
318 " outboud:%s %s action:%U",
319 (is_outbound ? "yes" : "no"),
320 (p.is_ipv6 ? "IPv4" : "IPv6"),
321 format_ipsec_policy_action, p.policy);
322 goto done;
323 }
324
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800325 rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
326
327 if (!rv)
328 vlib_cli_output (vm, "policy-index:%d", stat_index);
329 else
330 vlib_cli_output (vm, "error:%d", rv);
Billy McFalla9a20e72017-02-15 11:39:12 -0500331
332done:
333 unformat_free (line_input);
334
335 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336}
337
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700338/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700339VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
340 .path = "ipsec policy",
341 .short_help =
342 "ipsec policy [add|del] spd <id> priority <n> ",
343 .function = ipsec_policy_add_del_command_fn,
344};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700345/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346
Neale Rannsb294f102019-04-03 13:17:50 +0000347static void
Neale Ranns670027a2019-08-27 12:47:17 +0000348ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im, u8 detail)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349{
Neale Rannsb294f102019-04-03 13:17:50 +0000350 u32 sai;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700352 /* *INDENT-OFF* */
Neale Ranns8d7c5022019-02-06 01:41:05 -0800353 pool_foreach_index (sai, im->sad, ({
Neale Ranns670027a2019-08-27 12:47:17 +0000354 vlib_cli_output(vm, "%U", format_ipsec_sa, sai,
355 (detail ? IPSEC_FORMAT_DETAIL : IPSEC_FORMAT_BRIEF));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356 }));
Neale Rannsb294f102019-04-03 13:17:50 +0000357 /* *INDENT-ON* */
358}
359
360static void
361ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im)
362{
363 u32 spdi;
364
365 /* *INDENT-OFF* */
366 pool_foreach_index (spdi, im->spds, ({
367 vlib_cli_output(vm, "%U", format_ipsec_spd, spdi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368 }));
Neale Rannsb294f102019-04-03 13:17:50 +0000369 /* *INDENT-ON* */
370}
371
372static void
373ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im)
374{
375 u32 spd_id, sw_if_index;
Neale Rannsd83c4a82019-06-14 06:48:27 -0700376 ipsec_spd_t *spd;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700377
Neale Ranns311124e2019-01-24 04:52:25 -0800378 vlib_cli_output (vm, "SPD Bindings:");
Neale Ranns8d7c5022019-02-06 01:41:05 -0800379
Neale Rannsb294f102019-04-03 13:17:50 +0000380 /* *INDENT-OFF* */
Neale Ranns311124e2019-01-24 04:52:25 -0800381 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
Neale Rannsd83c4a82019-06-14 06:48:27 -0700382 spd = pool_elt_at_index (im->spds, spd_id);
383 vlib_cli_output (vm, " %d -> %U", spd->id,
Neale Ranns8d7c5022019-02-06 01:41:05 -0800384 format_vnet_sw_if_index_name, im->vnet_main,
385 sw_if_index);
Neale Ranns311124e2019-01-24 04:52:25 -0800386 }));
387 /* *INDENT-ON* */
Neale Rannsb294f102019-04-03 13:17:50 +0000388}
Neale Ranns311124e2019-01-24 04:52:25 -0800389
Neale Ranns12989b52019-09-26 16:20:19 +0000390static walk_rc_t
391ipsec_tun_protect_show_one (index_t itpi, void *ctx)
Neale Rannsb294f102019-04-03 13:17:50 +0000392{
Neale Ranns12989b52019-09-26 16:20:19 +0000393 vlib_cli_output (ctx, "%U", format_ipsec_tun_protect, itpi);
Neale Rannsb294f102019-04-03 13:17:50 +0000394
Neale Ranns12989b52019-09-26 16:20:19 +0000395 return (WALK_CONTINUE);
396}
397
398static void
399ipsec_tunnel_show_all (vlib_main_t * vm)
400{
401 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000402}
403
404static clib_error_t *
405show_ipsec_command_fn (vlib_main_t * vm,
406 unformat_input_t * input, vlib_cli_command_t * cmd)
407{
408 ipsec_main_t *im = &ipsec_main;
409
Neale Ranns670027a2019-08-27 12:47:17 +0000410 ipsec_sa_show_all (vm, im, 0);
Neale Rannsb294f102019-04-03 13:17:50 +0000411 ipsec_spd_show_all (vm, im);
412 ipsec_spd_bindings_show_all (vm, im);
Neale Ranns12989b52019-09-26 16:20:19 +0000413 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000414
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415 return 0;
416}
417
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700418/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419VLIB_CLI_COMMAND (show_ipsec_command, static) = {
Neale Rannsb294f102019-04-03 13:17:50 +0000420 .path = "show ipsec all",
421 .short_help = "show ipsec all",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422 .function = show_ipsec_command_fn,
423};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700424/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425
426static clib_error_t *
Neale Rannsb294f102019-04-03 13:17:50 +0000427show_ipsec_sa_command_fn (vlib_main_t * vm,
428 unformat_input_t * input, vlib_cli_command_t * cmd)
429{
430 ipsec_main_t *im = &ipsec_main;
431 u32 sai = ~0;
Neale Ranns670027a2019-08-27 12:47:17 +0000432 u8 detail = 0;
Neale Rannsb294f102019-04-03 13:17:50 +0000433
434 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
435 {
436 if (unformat (input, "%u", &sai))
437 ;
Neale Ranns670027a2019-08-27 12:47:17 +0000438 if (unformat (input, "detail"))
439 detail = 1;
Neale Rannsb294f102019-04-03 13:17:50 +0000440 else
441 break;
442 }
443
444 if (~0 == sai)
Neale Ranns670027a2019-08-27 12:47:17 +0000445 ipsec_sa_show_all (vm, im, detail);
Neale Rannsb294f102019-04-03 13:17:50 +0000446 else
Christian E. Hopps01d61e72019-09-27 14:43:22 -0400447 vlib_cli_output (vm, "%U", format_ipsec_sa, sai,
448 IPSEC_FORMAT_DETAIL | IPSEC_FORMAT_INSECURE);
Neale Rannsb294f102019-04-03 13:17:50 +0000449
450 return 0;
451}
452
Neale Rannsc87b66c2019-02-07 07:26:12 -0800453static clib_error_t *
454clear_ipsec_sa_command_fn (vlib_main_t * vm,
455 unformat_input_t * input, vlib_cli_command_t * cmd)
456{
457 ipsec_main_t *im = &ipsec_main;
458 u32 sai = ~0;
459
460 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
461 {
462 if (unformat (input, "%u", &sai))
463 ;
464 else
465 break;
466 }
467
468 if (~0 == sai)
469 {
470 /* *INDENT-OFF* */
471 pool_foreach_index (sai, im->sad, ({
472 ipsec_sa_clear(sai);
473 }));
474 /* *INDENT-ON* */
475 }
476 else
477 {
478 if (pool_is_free_index (im->sad, sai))
479 return clib_error_return (0, "unknown SA index: %d", sai);
480 else
481 ipsec_sa_clear (sai);
482 }
483
484 return 0;
485}
486
Neale Rannsb294f102019-04-03 13:17:50 +0000487/* *INDENT-OFF* */
488VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
489 .path = "show ipsec sa",
490 .short_help = "show ipsec sa [index]",
491 .function = show_ipsec_sa_command_fn,
492};
Neale Rannsc87b66c2019-02-07 07:26:12 -0800493
494VLIB_CLI_COMMAND (clear_ipsec_sa_command, static) = {
495 .path = "clear ipsec sa",
496 .short_help = "clear ipsec sa [index]",
497 .function = clear_ipsec_sa_command_fn,
498};
Neale Rannsb294f102019-04-03 13:17:50 +0000499/* *INDENT-ON* */
500
501static clib_error_t *
502show_ipsec_spd_command_fn (vlib_main_t * vm,
503 unformat_input_t * input, vlib_cli_command_t * cmd)
504{
505 ipsec_main_t *im = &ipsec_main;
506 u8 show_bindings = 0;
507 u32 spdi = ~0;
508
509 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
510 {
511 if (unformat (input, "%u", &spdi))
512 ;
513 else if (unformat (input, "bindings"))
514 show_bindings = 1;
515 else
516 break;
517 }
518
519 if (show_bindings)
520 ipsec_spd_bindings_show_all (vm, im);
521 else if (~0 != spdi)
522 vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
523 else
524 ipsec_spd_show_all (vm, im);
525
526 return 0;
527}
528
529/* *INDENT-OFF* */
530VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
531 .path = "show ipsec spd",
532 .short_help = "show ipsec spd [index]",
533 .function = show_ipsec_spd_command_fn,
534};
535/* *INDENT-ON* */
536
537static clib_error_t *
538show_ipsec_tunnel_command_fn (vlib_main_t * vm,
539 unformat_input_t * input,
540 vlib_cli_command_t * cmd)
541{
Neale Ranns12989b52019-09-26 16:20:19 +0000542 ipsec_tunnel_show_all (vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000543
544 return 0;
545}
546
547/* *INDENT-OFF* */
548VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
549 .path = "show ipsec tunnel",
Neale Ranns12989b52019-09-26 16:20:19 +0000550 .short_help = "show ipsec tunnel",
Neale Rannsb294f102019-04-03 13:17:50 +0000551 .function = show_ipsec_tunnel_command_fn,
552};
553/* *INDENT-ON* */
554
555static clib_error_t *
Klement Sekerab4d30532018-11-08 13:00:02 +0100556ipsec_show_backends_command_fn (vlib_main_t * vm,
557 unformat_input_t * input,
558 vlib_cli_command_t * cmd)
559{
560 ipsec_main_t *im = &ipsec_main;
561 u32 verbose = 0;
562
563 (void) unformat (input, "verbose %u", &verbose);
564
565 vlib_cli_output (vm, "IPsec AH backends available:");
566 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
567 ipsec_ah_backend_t *ab;
568 /* *INDENT-OFF* */
569 pool_foreach (ab, im->ah_backends, {
570 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
571 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
572 if (verbose) {
573 vlib_node_t *n;
574 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
575 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
576 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
577 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
578 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
579 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
580 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
581 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
582 }
583 });
584 /* *INDENT-ON* */
585 vlib_cli_output (vm, "%v", s);
586 _vec_len (s) = 0;
587 vlib_cli_output (vm, "IPsec ESP backends available:");
588 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
589 ipsec_esp_backend_t *eb;
590 /* *INDENT-OFF* */
591 pool_foreach (eb, im->esp_backends, {
592 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
593 eb - im->esp_backends == im->esp_current_backend ? "yes"
594 : "no");
595 if (verbose) {
596 vlib_node_t *n;
597 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
598 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
599 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
600 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
601 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
602 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
603 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
604 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
605 }
606 });
607 /* *INDENT-ON* */
608 vlib_cli_output (vm, "%v", s);
609
610 vec_free (s);
611 return 0;
612}
613
614/* *INDENT-OFF* */
615VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
616 .path = "show ipsec backends",
617 .short_help = "show ipsec backends",
618 .function = ipsec_show_backends_command_fn,
619};
620/* *INDENT-ON* */
621
622static clib_error_t *
623ipsec_select_backend_command_fn (vlib_main_t * vm,
624 unformat_input_t * input,
625 vlib_cli_command_t * cmd)
626{
Klement Sekerab4d30532018-11-08 13:00:02 +0100627 unformat_input_t _line_input, *line_input = &_line_input;
Neale Rannse8915fc2019-04-23 20:57:55 -0400628 ipsec_main_t *im = &ipsec_main;
629 clib_error_t *error;
630 u32 backend_index;
631
632 error = ipsec_rsc_in_use (im);
633
634 if (error)
635 return error;
636
Klement Sekerab4d30532018-11-08 13:00:02 +0100637 /* Get a line of input. */
638 if (!unformat_user (input, unformat_line_input, line_input))
639 return 0;
640
641 if (unformat (line_input, "ah"))
642 {
643 if (unformat (line_input, "%u", &backend_index))
644 {
645 if (ipsec_select_ah_backend (im, backend_index) < 0)
646 {
647 return clib_error_return (0, "Invalid AH backend index `%u'",
648 backend_index);
649 }
650 }
651 else
652 {
653 return clib_error_return (0, "Invalid backend index `%U'",
654 format_unformat_error, line_input);
655 }
656 }
657 else if (unformat (line_input, "esp"))
658 {
659 if (unformat (line_input, "%u", &backend_index))
660 {
661 if (ipsec_select_esp_backend (im, backend_index) < 0)
662 {
663 return clib_error_return (0, "Invalid ESP backend index `%u'",
664 backend_index);
665 }
666 }
667 else
668 {
669 return clib_error_return (0, "Invalid backend index `%U'",
670 format_unformat_error, line_input);
671 }
672 }
673 else
674 {
675 return clib_error_return (0, "Unknown input `%U'",
676 format_unformat_error, line_input);
677 }
678
679 return 0;
680}
681
682/* *INDENT-OFF* */
683VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
684 .path = "ipsec select backend",
685 .short_help = "ipsec select backend <ah|esp> <backend index>",
686 .function = ipsec_select_backend_command_fn,
687};
688
689/* *INDENT-ON* */
690
691static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692clear_ipsec_counters_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700693 unformat_input_t * input,
694 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700695{
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800696 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
Neale Rannseba31ec2019-02-17 18:04:27 +0000697 vlib_clear_combined_counters (&ipsec_sa_counters);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700698
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800699 return (NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700}
701
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700702/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700703VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
704 .path = "clear ipsec counters",
705 .short_help = "clear ipsec counters",
706 .function = clear_ipsec_counters_command_fn,
707};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700708/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700709
Neale Ranns12989b52019-09-26 16:20:19 +0000710static u32
711ipsec_tun_mk_local_sa_id (u32 ti)
712{
713 return (0x80000000 | ti);
714}
715
716static u32
717ipsec_tun_mk_remote_sa_id (u32 ti)
718{
719 return (0xc0000000 | ti);
720}
721
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722static clib_error_t *
723create_ipsec_tunnel_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700724 unformat_input_t * input,
725 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700727 unformat_input_t _line_input, *line_input = &_line_input;
Neale Ranns12989b52019-09-26 16:20:19 +0000728 ip46_address_t local_ip = ip46_address_initializer;
729 ip46_address_t remote_ip = ip46_address_initializer;
730 ipsec_crypto_alg_t crypto_alg;
731 ipsec_integ_alg_t integ_alg;
732 ipsec_sa_flags_t flags;
733 u32 local_spi, remote_spi, salt, table_id, fib_index;
734 u32 instance = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700735 int rv;
736 u32 num_m_args = 0;
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500737 u8 ipv4_set = 0;
738 u8 ipv6_set = 0;
Neale Ranns12989b52019-09-26 16:20:19 +0000739 u8 is_add = 1;
Billy McFalla9a20e72017-02-15 11:39:12 -0500740 clib_error_t *error = NULL;
Kingwel Xied3d12052019-03-07 06:34:30 -0500741 ipsec_key_t rck = { 0 };
742 ipsec_key_t lck = { 0 };
743 ipsec_key_t lik = { 0 };
744 ipsec_key_t rik = { 0 };
Matthew Smith2838a232016-06-21 16:05:09 -0500745
Neale Ranns12989b52019-09-26 16:20:19 +0000746 table_id = 0;
747 flags = IPSEC_SA_FLAG_NONE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748
749 /* Get a line of input. */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700750 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751 return 0;
752
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700753 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
754 {
755 if (unformat
Neale Ranns12989b52019-09-26 16:20:19 +0000756 (line_input, "local-ip %U", unformat_ip46_address, &local_ip,
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500757 IP46_TYPE_ANY))
758 {
Neale Ranns12989b52019-09-26 16:20:19 +0000759 ip46_address_is_ip4 (&local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500760 num_m_args++;
761 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700762 else
763 if (unformat
Neale Ranns12989b52019-09-26 16:20:19 +0000764 (line_input, "remote-ip %U", unformat_ip46_address, &remote_ip,
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500765 IP46_TYPE_ANY))
766 {
Neale Ranns12989b52019-09-26 16:20:19 +0000767 ip46_address_is_ip4 (&remote_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500768 num_m_args++;
769 }
Neale Ranns12989b52019-09-26 16:20:19 +0000770 else if (unformat (line_input, "local-spi %u", &local_spi))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700771 num_m_args++;
Neale Ranns12989b52019-09-26 16:20:19 +0000772 else if (unformat (line_input, "remote-spi %u", &remote_spi))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700773 num_m_args++;
Neale Ranns12989b52019-09-26 16:20:19 +0000774 else if (unformat (line_input, "salt 0x%x", &salt))
Neale Ranns47feb112019-04-11 15:14:07 +0000775 ;
Radu Nicolau717de092018-08-03 10:37:24 +0100776 else if (unformat (line_input, "udp-encap"))
Neale Ranns12989b52019-09-26 16:20:19 +0000777 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800778 else if (unformat (line_input, "use-esn"))
Neale Ranns12989b52019-09-26 16:20:19 +0000779 flags |= IPSEC_SA_FLAG_USE_ESN;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800780 else if (unformat (line_input, "use-anti-replay"))
Neale Ranns12989b52019-09-26 16:20:19 +0000781 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
782 else if (unformat (line_input, "instance %u", &instance))
783 ;
784 else if (unformat (line_input, "tx-table %u", &table_id))
Pierre Pfister4c422f92018-12-10 11:19:08 +0100785 ;
Neale Rannsfd060842019-03-04 13:44:42 +0000786 else
787 if (unformat
788 (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
789 ;
790 else
791 if (unformat
792 (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
793 ;
794 else if (unformat (line_input, "crypto-alg %U",
Neale Ranns12989b52019-09-26 16:20:19 +0000795 unformat_ipsec_crypto_alg, &crypto_alg))
Neale Rannsfd060842019-03-04 13:44:42 +0000796 ;
797 else
798 if (unformat
799 (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
800 ;
801 else
802 if (unformat
Simon Zhange8e950a2019-04-23 23:04:07 +0800803 (line_input, "remote-integ-key %U", unformat_ipsec_key, &rik))
Neale Rannsfd060842019-03-04 13:44:42 +0000804 ;
805 else if (unformat (line_input, "integ-alg %U",
Neale Ranns12989b52019-09-26 16:20:19 +0000806 unformat_ipsec_integ_alg, &integ_alg))
Neale Rannsfd060842019-03-04 13:44:42 +0000807 ;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800808 else if (unformat (line_input, "del"))
Neale Ranns12989b52019-09-26 16:20:19 +0000809 is_add = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700810 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500811 {
812 error = clib_error_return (0, "unknown input `%U'",
813 format_unformat_error, line_input);
814 goto done;
815 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700816 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700817
818 if (num_m_args < 4)
Billy McFalla9a20e72017-02-15 11:39:12 -0500819 {
820 error = clib_error_return (0, "mandatory argument(s) missing");
821 goto done;
822 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700823
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500824 if (ipv4_set && ipv6_set)
825 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
826
Neale Ranns12989b52019-09-26 16:20:19 +0000827 fib_index = fib_table_find (fib_ip_proto (ipv6_set), table_id);
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400828
Neale Ranns12989b52019-09-26 16:20:19 +0000829 if (~0 == fib_index)
830 {
831 rv = VNET_API_ERROR_NO_SUCH_FIB;
832 goto done;
833 }
Neale Rannsfd060842019-03-04 13:44:42 +0000834
Neale Ranns12989b52019-09-26 16:20:19 +0000835 if (is_add)
836 {
837 // remote = input, local = output
838 u32 sw_if_index;
Neale Rannsfd060842019-03-04 13:44:42 +0000839
Neale Ranns12989b52019-09-26 16:20:19 +0000840 /* create an ip-ip tunnel, then the two SA, then bind them */
841 rv =
842 ipip_add_tunnel (ipv6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4,
843 instance, &local_ip, &remote_ip, fib_index, 0,
844 &sw_if_index);
845 rv |=
846 ipsec_sa_add_and_lock (ipsec_tun_mk_local_sa_id (sw_if_index),
847 local_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
848 &lck, integ_alg, &lik, flags, table_id,
849 clib_host_to_net_u32 (salt), &local_ip,
850 &remote_ip, NULL);
851 rv |=
852 ipsec_sa_add_and_lock (ipsec_tun_mk_remote_sa_id (sw_if_index),
853 remote_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
854 &rck, integ_alg, &rik,
855 (flags | IPSEC_SA_FLAG_IS_INBOUND), table_id,
856 clib_host_to_net_u32 (salt), &remote_ip,
857 &local_ip, NULL);
858 rv |=
859 ipsec_tun_protect_update_one (sw_if_index,
860 ipsec_tun_mk_local_sa_id (sw_if_index),
861 ipsec_tun_mk_remote_sa_id
862 (sw_if_index));
863 }
864 else
865 rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700866
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700867 switch (rv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700868 {
869 case 0:
870 break;
871 case VNET_API_ERROR_INVALID_VALUE:
Neale Rannsd14fccd2019-11-15 15:03:27 +0000872 error = clib_error_return (0,
873 "IPSec tunnel interface already exists...");
Billy McFalla9a20e72017-02-15 11:39:12 -0500874 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700875 default:
Billy McFalla9a20e72017-02-15 11:39:12 -0500876 error = clib_error_return (0, "ipsec_register_interface returned %d",
877 rv);
878 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700879 }
880
Billy McFalla9a20e72017-02-15 11:39:12 -0500881done:
882 unformat_free (line_input);
883
884 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700885}
886
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700887/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700888VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
889 .path = "create ipsec tunnel",
Pierre Pfister4c422f92018-12-10 11:19:08 +0100890 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
Kingwel Xie2baf9422019-02-04 02:07:06 -0800891 "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
Pierre Pfister4c422f92018-12-10 11:19:08 +0100892 "[tx-table <table-id>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700893 .function = create_ipsec_tunnel_command_fn,
894};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700895/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700896
Neale Rannsc87b66c2019-02-07 07:26:12 -0800897static clib_error_t *
898ipsec_tun_protect_cmd (vlib_main_t * vm,
899 unformat_input_t * input, vlib_cli_command_t * cmd)
900{
901 unformat_input_t _line_input, *line_input = &_line_input;
902 u32 sw_if_index, is_del, sa_in, sa_out, *sa_ins = NULL;
903 vnet_main_t *vnm;
904
905 is_del = 0;
906 sw_if_index = ~0;
907 vnm = vnet_get_main ();
908
909 if (!unformat_user (input, unformat_line_input, line_input))
910 return 0;
911
912 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
913 {
914 if (unformat (line_input, "del"))
915 is_del = 1;
916 else if (unformat (line_input, "add"))
917 is_del = 0;
918 else if (unformat (line_input, "sa-in %d", &sa_in))
919 vec_add1 (sa_ins, sa_in);
920 else if (unformat (line_input, "sa-out %d", &sa_out))
921 ;
922 else if (unformat (line_input, "%U",
923 unformat_vnet_sw_interface, vnm, &sw_if_index))
924 ;
925 else
926 return (clib_error_return (0, "unknown input '%U'",
927 format_unformat_error, line_input));
928 }
929
930 if (!is_del)
931 ipsec_tun_protect_update (sw_if_index, sa_out, sa_ins);
932
933 unformat_free (line_input);
934 return NULL;
935}
936
937/**
938 * Protect tunnel with IPSEC
939 */
940/* *INDENT-OFF* */
941VLIB_CLI_COMMAND (ipsec_tun_protect_cmd_node, static) =
942{
943 .path = "ipsec tunnel protect",
944 .function = ipsec_tun_protect_cmd,
945 .short_help = "ipsec tunnel protect <interface> input-sa <SA> output-sa <SA>",
946 // this is not MP safe
947};
948/* *INDENT-ON* */
949
Neale Rannsc87b66c2019-02-07 07:26:12 -0800950
951static clib_error_t *
952ipsec_tun_protect_show (vlib_main_t * vm,
953 unformat_input_t * input, vlib_cli_command_t * cmd)
954{
955 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
956
957 return NULL;
958}
959
960/**
961 * show IPSEC tunnel protection
962 */
963/* *INDENT-OFF* */
964VLIB_CLI_COMMAND (ipsec_tun_protect_show_node, static) =
965{
966 .path = "show ipsec protect",
967 .function = ipsec_tun_protect_show,
968 .short_help = "show ipsec protect",
969};
970/* *INDENT-ON* */
971
Neale Ranns41afb332019-07-16 06:19:35 -0700972static clib_error_t *
973ipsec_tun_protect_hash_show (vlib_main_t * vm,
974 unformat_input_t * input,
975 vlib_cli_command_t * cmd)
976{
977 ipsec_main_t *im = &ipsec_main;
978
979 {
980 ipsec_tun_lkup_result_t value;
981 ipsec4_tunnel_key_t key;
982
983 vlib_cli_output (vm, "IPv4:");
984
985 /* *INDENT-OFF* */
986 hash_foreach(key.as_u64, value.as_u64, im->tun4_protect_by_key,
987 ({
988 vlib_cli_output (vm, " %U", format_ipsec4_tunnel_key, &key);
989 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
990 }));
991 /* *INDENT-ON* */
992 }
993
994 {
995 ipsec_tun_lkup_result_t value;
996 ipsec6_tunnel_key_t *key;
997
998 vlib_cli_output (vm, "IPv6:");
999
1000 /* *INDENT-OFF* */
1001 hash_foreach_mem(key, value.as_u64, im->tun6_protect_by_key,
1002 ({
1003 vlib_cli_output (vm, " %U", format_ipsec6_tunnel_key, key);
1004 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
1005 }));
1006 /* *INDENT-ON* */
1007 }
1008
1009 return NULL;
1010}
1011
1012/**
1013 * show IPSEC tunnel protection hash tables
1014 */
1015/* *INDENT-OFF* */
1016VLIB_CLI_COMMAND (ipsec_tun_protect_hash_show_node, static) =
1017{
1018 .path = "show ipsec protect-hash",
1019 .function = ipsec_tun_protect_hash_show,
1020 .short_help = "show ipsec protect-hash",
1021};
1022/* *INDENT-ON* */
1023
Ed Warnickecb9cada2015-12-08 15:45:58 -07001024clib_error_t *
1025ipsec_cli_init (vlib_main_t * vm)
1026{
1027 return 0;
1028}
1029
1030VLIB_INIT_FUNCTION (ipsec_cli_init);
1031
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001032
1033/*
1034 * fd.io coding-style-patch-verification: ON
1035 *
1036 * Local Variables:
1037 * eval: (c-set-style "gnu")
1038 * End:
1039 */