blob: 7779e79f067a81607d11e6948eebaedf69c61fcb [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 Ranns8dc75c02019-12-10 01:08:19 +000092 salt = 0;
Neale Ranns8d7c5022019-02-06 01:41:05 -080093 error = NULL;
94 is_add = 0;
95 flags = IPSEC_SA_FLAG_NONE;
96 proto = IPSEC_PROTOCOL_ESP;
Neale Rannse6be7022019-06-04 15:37:34 +000097 integ_alg = IPSEC_INTEG_ALG_NONE;
98 crypto_alg = IPSEC_CRYPTO_ALG_NONE;
Ed Warnickecb9cada2015-12-08 15:45:58 -070099
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700100 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101 return 0;
102
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700103 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
104 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800105 if (unformat (line_input, "add %u", &id))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700106 is_add = 1;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800107 else if (unformat (line_input, "del %u", &id))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700108 is_add = 0;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800109 else if (unformat (line_input, "spi %u", &spi))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700110 ;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800111 else if (unformat (line_input, "salt 0x%x", &salt))
Neale Ranns80f6fd52019-04-16 02:41:34 +0000112 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700113 else if (unformat (line_input, "esp"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800114 proto = IPSEC_PROTOCOL_ESP;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700115 else if (unformat (line_input, "ah"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800116 proto = IPSEC_PROTOCOL_AH;
117 else if (unformat (line_input, "crypto-key %U",
118 unformat_ipsec_key, &ck))
119 ;
120 else if (unformat (line_input, "crypto-alg %U",
121 unformat_ipsec_crypto_alg, &crypto_alg))
122 ;
123 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
124 ;
125 else if (unformat (line_input, "integ-alg %U",
126 unformat_ipsec_integ_alg, &integ_alg))
127 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700128 else if (unformat (line_input, "tunnel-src %U",
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500129 unformat_ip46_address, &tun_src, IP46_TYPE_ANY))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700130 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800131 flags |= IPSEC_SA_FLAG_IS_TUNNEL;
132 if (!ip46_address_is_ip4 (&tun_src))
133 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700134 }
135 else if (unformat (line_input, "tunnel-dst %U",
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500136 unformat_ip46_address, &tun_dst, IP46_TYPE_ANY))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800137 ;
Radu Nicolau717de092018-08-03 10:37:24 +0100138 else if (unformat (line_input, "udp-encap"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800139 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700140 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500141 {
142 error = clib_error_return (0, "parse error: '%U'",
143 format_unformat_error, line_input);
144 goto done;
145 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700146 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000148 if (is_add)
Neale Ranns495d7ff2019-07-12 09:15:26 +0000149 rv = ipsec_sa_add_and_lock (id, spi, proto, crypto_alg,
150 &ck, integ_alg, &ik, flags,
151 0, clib_host_to_net_u32 (salt),
Filip Tehlare5d34912020-03-02 15:17:37 +0000152 &tun_src, &tun_dst, NULL,
153 IPSEC_UDP_PORT_NONE);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800154 else
Neale Ranns495d7ff2019-07-12 09:15:26 +0000155 rv = ipsec_sa_unlock_id (id);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000156
Neale Ranns8d7c5022019-02-06 01:41:05 -0800157 if (rv)
Neale Rannse6be7022019-06-04 15:37:34 +0000158 error = clib_error_return (0, "failed");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159
Billy McFalla9a20e72017-02-15 11:39:12 -0500160done:
161 unformat_free (line_input);
162
163 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164}
165
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700166/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
168 .path = "ipsec sa",
169 .short_help =
170 "ipsec sa [add|del]",
171 .function = ipsec_sa_add_del_command_fn,
172};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700173/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174
175static clib_error_t *
176ipsec_spd_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700177 unformat_input_t * input,
178 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700180 unformat_input_t _line_input, *line_input = &_line_input;
Damjan Marion3f54b182016-08-16 11:27:02 +0200181 u32 spd_id = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182 int is_add = ~0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500183 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700185 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186 return 0;
187
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700188 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
189 {
190 if (unformat (line_input, "add"))
191 is_add = 1;
192 else if (unformat (line_input, "del"))
193 is_add = 0;
194 else if (unformat (line_input, "%u", &spd_id))
195 ;
196 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500197 {
198 error = clib_error_return (0, "parse error: '%U'",
199 format_unformat_error, line_input);
200 goto done;
201 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700202 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203
Damjan Marion3f54b182016-08-16 11:27:02 +0200204 if (spd_id == ~0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500205 {
206 error = clib_error_return (0, "please specify SPD ID");
207 goto done;
208 }
Damjan Marion3f54b182016-08-16 11:27:02 +0200209
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700210 ipsec_add_del_spd (vm, spd_id, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211
Billy McFalla9a20e72017-02-15 11:39:12 -0500212done:
213 unformat_free (line_input);
214
215 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216}
217
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700218/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
220 .path = "ipsec spd",
221 .short_help =
222 "ipsec spd [add|del] <id>",
223 .function = ipsec_spd_add_del_command_fn,
224};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700225/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226
227
228static clib_error_t *
229ipsec_policy_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700230 unformat_input_t * input,
231 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700233 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234 ipsec_policy_t p;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800235 int rv, is_add = 0;
236 u32 tmp, tmp2, stat_index;
Billy McFalla9a20e72017-02-15 11:39:12 -0500237 clib_error_t *error = NULL;
Neale Ranns9f231d42019-03-19 10:06:00 +0000238 u32 is_outbound;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239
Dave Barachb7b92992018-10-17 10:38:51 -0400240 clib_memset (&p, 0, sizeof (p));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 p.lport.stop = p.rport.stop = ~0;
Neale Ranns9f231d42019-03-19 10:06:00 +0000242 is_outbound = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700244 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245 return 0;
246
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700247 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
248 {
249 if (unformat (line_input, "add"))
250 is_add = 1;
251 else if (unformat (line_input, "del"))
252 is_add = 0;
253 else if (unformat (line_input, "spd %u", &p.id))
254 ;
255 else if (unformat (line_input, "inbound"))
Neale Ranns9f231d42019-03-19 10:06:00 +0000256 is_outbound = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700257 else if (unformat (line_input, "outbound"))
Neale Ranns9f231d42019-03-19 10:06:00 +0000258 is_outbound = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700259 else if (unformat (line_input, "priority %d", &p.priority))
260 ;
261 else if (unformat (line_input, "protocol %u", &tmp))
262 p.protocol = (u8) tmp;
263 else
264 if (unformat
265 (line_input, "action %U", unformat_ipsec_policy_action,
266 &p.policy))
267 {
268 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
Billy McFalla9a20e72017-02-15 11:39:12 -0500269 {
270 error = clib_error_return (0, "unsupported action: 'resolve'");
271 goto done;
272 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700273 }
274 else if (unformat (line_input, "sa %u", &p.sa_id))
275 ;
276 else if (unformat (line_input, "local-ip-range %U - %U",
277 unformat_ip4_address, &p.laddr.start.ip4,
278 unformat_ip4_address, &p.laddr.stop.ip4))
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800279 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700280 else if (unformat (line_input, "remote-ip-range %U - %U",
281 unformat_ip4_address, &p.raddr.start.ip4,
282 unformat_ip4_address, &p.raddr.stop.ip4))
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800283 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700284 else if (unformat (line_input, "local-ip-range %U - %U",
285 unformat_ip6_address, &p.laddr.start.ip6,
286 unformat_ip6_address, &p.laddr.stop.ip6))
287 {
288 p.is_ipv6 = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700289 }
290 else if (unformat (line_input, "remote-ip-range %U - %U",
291 unformat_ip6_address, &p.raddr.start.ip6,
292 unformat_ip6_address, &p.raddr.stop.ip6))
293 {
294 p.is_ipv6 = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700295 }
296 else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
297 {
298 p.lport.start = tmp;
299 p.lport.stop = tmp2;
300 }
301 else
302 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
303 {
304 p.rport.start = tmp;
305 p.rport.stop = tmp2;
306 }
307 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500308 {
309 error = clib_error_return (0, "parse error: '%U'",
310 format_unformat_error, line_input);
311 goto done;
312 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700313 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314
Neale Ranns9f231d42019-03-19 10:06:00 +0000315 rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
316
317 if (rv)
318 {
319 error = clib_error_return (0, "unsupported policy type for:",
320 " outboud:%s %s action:%U",
321 (is_outbound ? "yes" : "no"),
322 (p.is_ipv6 ? "IPv4" : "IPv6"),
323 format_ipsec_policy_action, p.policy);
324 goto done;
325 }
326
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800327 rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
328
329 if (!rv)
330 vlib_cli_output (vm, "policy-index:%d", stat_index);
331 else
332 vlib_cli_output (vm, "error:%d", rv);
Billy McFalla9a20e72017-02-15 11:39:12 -0500333
334done:
335 unformat_free (line_input);
336
337 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338}
339
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700340/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
342 .path = "ipsec policy",
343 .short_help =
344 "ipsec policy [add|del] spd <id> priority <n> ",
345 .function = ipsec_policy_add_del_command_fn,
346};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700347/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348
Neale Rannsb294f102019-04-03 13:17:50 +0000349static void
Neale Ranns670027a2019-08-27 12:47:17 +0000350ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im, u8 detail)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351{
Neale Rannsb294f102019-04-03 13:17:50 +0000352 u32 sai;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700354 /* *INDENT-OFF* */
Neale Ranns8d7c5022019-02-06 01:41:05 -0800355 pool_foreach_index (sai, im->sad, ({
Neale Ranns670027a2019-08-27 12:47:17 +0000356 vlib_cli_output(vm, "%U", format_ipsec_sa, sai,
357 (detail ? IPSEC_FORMAT_DETAIL : IPSEC_FORMAT_BRIEF));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358 }));
Neale Rannsb294f102019-04-03 13:17:50 +0000359 /* *INDENT-ON* */
360}
361
362static void
363ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im)
364{
365 u32 spdi;
366
367 /* *INDENT-OFF* */
368 pool_foreach_index (spdi, im->spds, ({
369 vlib_cli_output(vm, "%U", format_ipsec_spd, spdi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370 }));
Neale Rannsb294f102019-04-03 13:17:50 +0000371 /* *INDENT-ON* */
372}
373
374static void
375ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im)
376{
377 u32 spd_id, sw_if_index;
Neale Rannsd83c4a82019-06-14 06:48:27 -0700378 ipsec_spd_t *spd;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379
Neale Ranns311124e2019-01-24 04:52:25 -0800380 vlib_cli_output (vm, "SPD Bindings:");
Neale Ranns8d7c5022019-02-06 01:41:05 -0800381
Neale Rannsb294f102019-04-03 13:17:50 +0000382 /* *INDENT-OFF* */
Neale Ranns311124e2019-01-24 04:52:25 -0800383 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
Neale Rannsd83c4a82019-06-14 06:48:27 -0700384 spd = pool_elt_at_index (im->spds, spd_id);
385 vlib_cli_output (vm, " %d -> %U", spd->id,
Neale Ranns8d7c5022019-02-06 01:41:05 -0800386 format_vnet_sw_if_index_name, im->vnet_main,
387 sw_if_index);
Neale Ranns311124e2019-01-24 04:52:25 -0800388 }));
389 /* *INDENT-ON* */
Neale Rannsb294f102019-04-03 13:17:50 +0000390}
Neale Ranns311124e2019-01-24 04:52:25 -0800391
Neale Ranns12989b52019-09-26 16:20:19 +0000392static walk_rc_t
393ipsec_tun_protect_show_one (index_t itpi, void *ctx)
Neale Rannsb294f102019-04-03 13:17:50 +0000394{
Neale Ranns28287212019-12-16 00:53:11 +0000395 vlib_cli_output (ctx, "%U", format_ipsec_tun_protect_index, itpi);
Neale Rannsb294f102019-04-03 13:17:50 +0000396
Neale Ranns12989b52019-09-26 16:20:19 +0000397 return (WALK_CONTINUE);
398}
399
400static void
401ipsec_tunnel_show_all (vlib_main_t * vm)
402{
403 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000404}
405
406static clib_error_t *
407show_ipsec_command_fn (vlib_main_t * vm,
408 unformat_input_t * input, vlib_cli_command_t * cmd)
409{
410 ipsec_main_t *im = &ipsec_main;
411
Neale Ranns670027a2019-08-27 12:47:17 +0000412 ipsec_sa_show_all (vm, im, 0);
Neale Rannsb294f102019-04-03 13:17:50 +0000413 ipsec_spd_show_all (vm, im);
414 ipsec_spd_bindings_show_all (vm, im);
Neale Ranns12989b52019-09-26 16:20:19 +0000415 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000416
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417 return 0;
418}
419
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700420/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421VLIB_CLI_COMMAND (show_ipsec_command, static) = {
Neale Rannsb294f102019-04-03 13:17:50 +0000422 .path = "show ipsec all",
423 .short_help = "show ipsec all",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424 .function = show_ipsec_command_fn,
425};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700426/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427
428static clib_error_t *
Neale Rannsb294f102019-04-03 13:17:50 +0000429show_ipsec_sa_command_fn (vlib_main_t * vm,
430 unformat_input_t * input, vlib_cli_command_t * cmd)
431{
432 ipsec_main_t *im = &ipsec_main;
433 u32 sai = ~0;
Neale Ranns670027a2019-08-27 12:47:17 +0000434 u8 detail = 0;
Neale Rannsb294f102019-04-03 13:17:50 +0000435
436 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
437 {
438 if (unformat (input, "%u", &sai))
439 ;
Neale Ranns670027a2019-08-27 12:47:17 +0000440 if (unformat (input, "detail"))
441 detail = 1;
Neale Rannsb294f102019-04-03 13:17:50 +0000442 else
443 break;
444 }
445
446 if (~0 == sai)
Neale Ranns670027a2019-08-27 12:47:17 +0000447 ipsec_sa_show_all (vm, im, detail);
Neale Rannsb294f102019-04-03 13:17:50 +0000448 else
Christian E. Hopps01d61e72019-09-27 14:43:22 -0400449 vlib_cli_output (vm, "%U", format_ipsec_sa, sai,
450 IPSEC_FORMAT_DETAIL | IPSEC_FORMAT_INSECURE);
Neale Rannsb294f102019-04-03 13:17:50 +0000451
452 return 0;
453}
454
Neale Rannsc87b66c2019-02-07 07:26:12 -0800455static clib_error_t *
456clear_ipsec_sa_command_fn (vlib_main_t * vm,
457 unformat_input_t * input, vlib_cli_command_t * cmd)
458{
459 ipsec_main_t *im = &ipsec_main;
460 u32 sai = ~0;
461
462 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
463 {
464 if (unformat (input, "%u", &sai))
465 ;
466 else
467 break;
468 }
469
470 if (~0 == sai)
471 {
472 /* *INDENT-OFF* */
473 pool_foreach_index (sai, im->sad, ({
474 ipsec_sa_clear(sai);
475 }));
476 /* *INDENT-ON* */
477 }
478 else
479 {
480 if (pool_is_free_index (im->sad, sai))
481 return clib_error_return (0, "unknown SA index: %d", sai);
482 else
483 ipsec_sa_clear (sai);
484 }
485
486 return 0;
487}
488
Neale Rannsb294f102019-04-03 13:17:50 +0000489/* *INDENT-OFF* */
490VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
491 .path = "show ipsec sa",
492 .short_help = "show ipsec sa [index]",
493 .function = show_ipsec_sa_command_fn,
494};
Neale Rannsc87b66c2019-02-07 07:26:12 -0800495
496VLIB_CLI_COMMAND (clear_ipsec_sa_command, static) = {
497 .path = "clear ipsec sa",
498 .short_help = "clear ipsec sa [index]",
499 .function = clear_ipsec_sa_command_fn,
500};
Neale Rannsb294f102019-04-03 13:17:50 +0000501/* *INDENT-ON* */
502
503static clib_error_t *
504show_ipsec_spd_command_fn (vlib_main_t * vm,
505 unformat_input_t * input, vlib_cli_command_t * cmd)
506{
507 ipsec_main_t *im = &ipsec_main;
508 u8 show_bindings = 0;
509 u32 spdi = ~0;
510
511 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
512 {
513 if (unformat (input, "%u", &spdi))
514 ;
515 else if (unformat (input, "bindings"))
516 show_bindings = 1;
517 else
518 break;
519 }
520
521 if (show_bindings)
522 ipsec_spd_bindings_show_all (vm, im);
523 else if (~0 != spdi)
524 vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
525 else
526 ipsec_spd_show_all (vm, im);
527
528 return 0;
529}
530
531/* *INDENT-OFF* */
532VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
533 .path = "show ipsec spd",
534 .short_help = "show ipsec spd [index]",
535 .function = show_ipsec_spd_command_fn,
536};
537/* *INDENT-ON* */
538
539static clib_error_t *
540show_ipsec_tunnel_command_fn (vlib_main_t * vm,
541 unformat_input_t * input,
542 vlib_cli_command_t * cmd)
543{
Neale Ranns12989b52019-09-26 16:20:19 +0000544 ipsec_tunnel_show_all (vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000545
546 return 0;
547}
548
549/* *INDENT-OFF* */
550VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
551 .path = "show ipsec tunnel",
Neale Ranns12989b52019-09-26 16:20:19 +0000552 .short_help = "show ipsec tunnel",
Neale Rannsb294f102019-04-03 13:17:50 +0000553 .function = show_ipsec_tunnel_command_fn,
554};
555/* *INDENT-ON* */
556
557static clib_error_t *
Klement Sekerab4d30532018-11-08 13:00:02 +0100558ipsec_show_backends_command_fn (vlib_main_t * vm,
559 unformat_input_t * input,
560 vlib_cli_command_t * cmd)
561{
562 ipsec_main_t *im = &ipsec_main;
563 u32 verbose = 0;
564
565 (void) unformat (input, "verbose %u", &verbose);
566
567 vlib_cli_output (vm, "IPsec AH backends available:");
568 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
569 ipsec_ah_backend_t *ab;
570 /* *INDENT-OFF* */
571 pool_foreach (ab, im->ah_backends, {
572 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
573 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
574 if (verbose) {
575 vlib_node_t *n;
576 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
577 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
578 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
579 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
580 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
581 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
582 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
583 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
584 }
585 });
586 /* *INDENT-ON* */
587 vlib_cli_output (vm, "%v", s);
588 _vec_len (s) = 0;
589 vlib_cli_output (vm, "IPsec ESP backends available:");
590 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
591 ipsec_esp_backend_t *eb;
592 /* *INDENT-OFF* */
593 pool_foreach (eb, im->esp_backends, {
594 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
595 eb - im->esp_backends == im->esp_current_backend ? "yes"
596 : "no");
597 if (verbose) {
598 vlib_node_t *n;
599 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
600 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
601 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
602 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
603 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
604 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
605 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
606 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
607 }
608 });
609 /* *INDENT-ON* */
610 vlib_cli_output (vm, "%v", s);
611
612 vec_free (s);
613 return 0;
614}
615
616/* *INDENT-OFF* */
617VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
618 .path = "show ipsec backends",
619 .short_help = "show ipsec backends",
620 .function = ipsec_show_backends_command_fn,
621};
622/* *INDENT-ON* */
623
624static clib_error_t *
625ipsec_select_backend_command_fn (vlib_main_t * vm,
626 unformat_input_t * input,
627 vlib_cli_command_t * cmd)
628{
Klement Sekerab4d30532018-11-08 13:00:02 +0100629 unformat_input_t _line_input, *line_input = &_line_input;
Neale Rannse8915fc2019-04-23 20:57:55 -0400630 ipsec_main_t *im = &ipsec_main;
631 clib_error_t *error;
632 u32 backend_index;
633
634 error = ipsec_rsc_in_use (im);
635
636 if (error)
637 return error;
638
Klement Sekerab4d30532018-11-08 13:00:02 +0100639 /* Get a line of input. */
640 if (!unformat_user (input, unformat_line_input, line_input))
641 return 0;
642
643 if (unformat (line_input, "ah"))
644 {
645 if (unformat (line_input, "%u", &backend_index))
646 {
647 if (ipsec_select_ah_backend (im, backend_index) < 0)
648 {
649 return clib_error_return (0, "Invalid AH backend index `%u'",
650 backend_index);
651 }
652 }
653 else
654 {
655 return clib_error_return (0, "Invalid backend index `%U'",
656 format_unformat_error, line_input);
657 }
658 }
659 else if (unformat (line_input, "esp"))
660 {
661 if (unformat (line_input, "%u", &backend_index))
662 {
663 if (ipsec_select_esp_backend (im, backend_index) < 0)
664 {
665 return clib_error_return (0, "Invalid ESP backend index `%u'",
666 backend_index);
667 }
668 }
669 else
670 {
671 return clib_error_return (0, "Invalid backend index `%U'",
672 format_unformat_error, line_input);
673 }
674 }
675 else
676 {
677 return clib_error_return (0, "Unknown input `%U'",
678 format_unformat_error, line_input);
679 }
680
681 return 0;
682}
683
684/* *INDENT-OFF* */
685VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
686 .path = "ipsec select backend",
687 .short_help = "ipsec select backend <ah|esp> <backend index>",
688 .function = ipsec_select_backend_command_fn,
689};
690
691/* *INDENT-ON* */
692
693static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694clear_ipsec_counters_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700695 unformat_input_t * input,
696 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700697{
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800698 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
Neale Rannseba31ec2019-02-17 18:04:27 +0000699 vlib_clear_combined_counters (&ipsec_sa_counters);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800701 return (NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700702}
703
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700704/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
706 .path = "clear ipsec counters",
707 .short_help = "clear ipsec counters",
708 .function = clear_ipsec_counters_command_fn,
709};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700710/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711
Neale Ranns12989b52019-09-26 16:20:19 +0000712static u32
713ipsec_tun_mk_local_sa_id (u32 ti)
714{
715 return (0x80000000 | ti);
716}
717
718static u32
719ipsec_tun_mk_remote_sa_id (u32 ti)
720{
721 return (0xc0000000 | ti);
722}
723
Ed Warnickecb9cada2015-12-08 15:45:58 -0700724static clib_error_t *
725create_ipsec_tunnel_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700726 unformat_input_t * input,
727 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700729 unformat_input_t _line_input, *line_input = &_line_input;
Neale Ranns12989b52019-09-26 16:20:19 +0000730 ip46_address_t local_ip = ip46_address_initializer;
731 ip46_address_t remote_ip = ip46_address_initializer;
Neale Ranns28287212019-12-16 00:53:11 +0000732 ip_address_t nh = IP_ADDRESS_V4_ALL_0S;
Damjan Marion68449852020-03-16 17:53:38 +0100733 ipsec_crypto_alg_t crypto_alg = IPSEC_CRYPTO_ALG_NONE;
734 ipsec_integ_alg_t integ_alg = IPSEC_INTEG_ALG_NONE;
Neale Ranns12989b52019-09-26 16:20:19 +0000735 ipsec_sa_flags_t flags;
736 u32 local_spi, remote_spi, salt, table_id, fib_index;
737 u32 instance = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700738 int rv;
739 u32 num_m_args = 0;
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500740 u8 ipv4_set = 0;
741 u8 ipv6_set = 0;
Neale Ranns12989b52019-09-26 16:20:19 +0000742 u8 is_add = 1;
Billy McFalla9a20e72017-02-15 11:39:12 -0500743 clib_error_t *error = NULL;
Kingwel Xied3d12052019-03-07 06:34:30 -0500744 ipsec_key_t rck = { 0 };
745 ipsec_key_t lck = { 0 };
746 ipsec_key_t lik = { 0 };
747 ipsec_key_t rik = { 0 };
Matthew Smith2838a232016-06-21 16:05:09 -0500748
Neale Ranns12989b52019-09-26 16:20:19 +0000749 table_id = 0;
750 flags = IPSEC_SA_FLAG_NONE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751
752 /* Get a line of input. */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700753 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700754 return 0;
755
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700756 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
757 {
758 if (unformat
Neale Ranns12989b52019-09-26 16:20:19 +0000759 (line_input, "local-ip %U", unformat_ip46_address, &local_ip,
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500760 IP46_TYPE_ANY))
761 {
Neale Ranns12989b52019-09-26 16:20:19 +0000762 ip46_address_is_ip4 (&local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500763 num_m_args++;
764 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700765 else
766 if (unformat
Neale Ranns12989b52019-09-26 16:20:19 +0000767 (line_input, "remote-ip %U", unformat_ip46_address, &remote_ip,
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500768 IP46_TYPE_ANY))
769 {
Neale Ranns12989b52019-09-26 16:20:19 +0000770 ip46_address_is_ip4 (&remote_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500771 num_m_args++;
772 }
Neale Ranns12989b52019-09-26 16:20:19 +0000773 else if (unformat (line_input, "local-spi %u", &local_spi))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700774 num_m_args++;
Neale Ranns12989b52019-09-26 16:20:19 +0000775 else if (unformat (line_input, "remote-spi %u", &remote_spi))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700776 num_m_args++;
Neale Ranns12989b52019-09-26 16:20:19 +0000777 else if (unformat (line_input, "salt 0x%x", &salt))
Neale Ranns47feb112019-04-11 15:14:07 +0000778 ;
Radu Nicolau717de092018-08-03 10:37:24 +0100779 else if (unformat (line_input, "udp-encap"))
Neale Ranns12989b52019-09-26 16:20:19 +0000780 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800781 else if (unformat (line_input, "use-esn"))
Neale Ranns12989b52019-09-26 16:20:19 +0000782 flags |= IPSEC_SA_FLAG_USE_ESN;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800783 else if (unformat (line_input, "use-anti-replay"))
Neale Ranns12989b52019-09-26 16:20:19 +0000784 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
785 else if (unformat (line_input, "instance %u", &instance))
786 ;
787 else if (unformat (line_input, "tx-table %u", &table_id))
Pierre Pfister4c422f92018-12-10 11:19:08 +0100788 ;
Neale Rannsfd060842019-03-04 13:44:42 +0000789 else
790 if (unformat
791 (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
792 ;
793 else
794 if (unformat
795 (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
796 ;
797 else if (unformat (line_input, "crypto-alg %U",
Neale Ranns12989b52019-09-26 16:20:19 +0000798 unformat_ipsec_crypto_alg, &crypto_alg))
Neale Rannsfd060842019-03-04 13:44:42 +0000799 ;
800 else
801 if (unformat
802 (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
803 ;
804 else
805 if (unformat
Simon Zhange8e950a2019-04-23 23:04:07 +0800806 (line_input, "remote-integ-key %U", unformat_ipsec_key, &rik))
Neale Rannsfd060842019-03-04 13:44:42 +0000807 ;
808 else if (unformat (line_input, "integ-alg %U",
Neale Ranns12989b52019-09-26 16:20:19 +0000809 unformat_ipsec_integ_alg, &integ_alg))
Neale Rannsfd060842019-03-04 13:44:42 +0000810 ;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800811 else if (unformat (line_input, "del"))
Neale Ranns12989b52019-09-26 16:20:19 +0000812 is_add = 0;
Neale Ranns28287212019-12-16 00:53:11 +0000813 else if (unformat (line_input, "nh &U", unformat_ip_address, &nh))
814 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700815 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500816 {
817 error = clib_error_return (0, "unknown input `%U'",
818 format_unformat_error, line_input);
819 goto done;
820 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700821 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700822
823 if (num_m_args < 4)
Billy McFalla9a20e72017-02-15 11:39:12 -0500824 {
825 error = clib_error_return (0, "mandatory argument(s) missing");
826 goto done;
827 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700828
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500829 if (ipv4_set && ipv6_set)
830 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
831
Neale Ranns12989b52019-09-26 16:20:19 +0000832 fib_index = fib_table_find (fib_ip_proto (ipv6_set), table_id);
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400833
Neale Ranns12989b52019-09-26 16:20:19 +0000834 if (~0 == fib_index)
835 {
836 rv = VNET_API_ERROR_NO_SUCH_FIB;
837 goto done;
838 }
Neale Rannsfd060842019-03-04 13:44:42 +0000839
Neale Ranns12989b52019-09-26 16:20:19 +0000840 if (is_add)
841 {
842 // remote = input, local = output
843 u32 sw_if_index;
Neale Rannsfd060842019-03-04 13:44:42 +0000844
Neale Ranns12989b52019-09-26 16:20:19 +0000845 /* create an ip-ip tunnel, then the two SA, then bind them */
846 rv =
847 ipip_add_tunnel (ipv6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4,
Neale Ranns95346962019-11-25 13:04:44 +0000848 instance, &local_ip, &remote_ip, fib_index,
Neale Ranns59ff9182019-12-29 23:55:18 +0000849 TUNNEL_ENCAP_DECAP_FLAG_NONE, IP_DSCP_CS0,
Neale Ranns14053c92019-12-29 23:55:18 +0000850 TUNNEL_MODE_P2P, &sw_if_index);
Neale Ranns12989b52019-09-26 16:20:19 +0000851 rv |=
852 ipsec_sa_add_and_lock (ipsec_tun_mk_local_sa_id (sw_if_index),
853 local_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
854 &lck, integ_alg, &lik, flags, table_id,
855 clib_host_to_net_u32 (salt), &local_ip,
Filip Tehlare5d34912020-03-02 15:17:37 +0000856 &remote_ip, NULL, IPSEC_UDP_PORT_NONE);
Neale Ranns12989b52019-09-26 16:20:19 +0000857 rv |=
858 ipsec_sa_add_and_lock (ipsec_tun_mk_remote_sa_id (sw_if_index),
859 remote_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
860 &rck, integ_alg, &rik,
861 (flags | IPSEC_SA_FLAG_IS_INBOUND), table_id,
862 clib_host_to_net_u32 (salt), &remote_ip,
Filip Tehlare5d34912020-03-02 15:17:37 +0000863 &local_ip, NULL, IPSEC_UDP_PORT_NONE);
Neale Ranns12989b52019-09-26 16:20:19 +0000864 rv |=
Neale Ranns28287212019-12-16 00:53:11 +0000865 ipsec_tun_protect_update_one (sw_if_index, &nh,
Neale Ranns12989b52019-09-26 16:20:19 +0000866 ipsec_tun_mk_local_sa_id (sw_if_index),
867 ipsec_tun_mk_remote_sa_id
868 (sw_if_index));
869 }
870 else
871 rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700872
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700873 switch (rv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700874 {
875 case 0:
876 break;
877 case VNET_API_ERROR_INVALID_VALUE:
Neale Rannsd14fccd2019-11-15 15:03:27 +0000878 error = clib_error_return (0,
879 "IPSec tunnel interface already exists...");
Billy McFalla9a20e72017-02-15 11:39:12 -0500880 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700881 default:
Billy McFalla9a20e72017-02-15 11:39:12 -0500882 error = clib_error_return (0, "ipsec_register_interface returned %d",
883 rv);
884 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700885 }
886
Billy McFalla9a20e72017-02-15 11:39:12 -0500887done:
888 unformat_free (line_input);
889
890 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700891}
892
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700893/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700894VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
895 .path = "create ipsec tunnel",
Pierre Pfister4c422f92018-12-10 11:19:08 +0100896 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
Kingwel Xie2baf9422019-02-04 02:07:06 -0800897 "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
Pierre Pfister4c422f92018-12-10 11:19:08 +0100898 "[tx-table <table-id>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700899 .function = create_ipsec_tunnel_command_fn,
900};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700901/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700902
Neale Rannsc87b66c2019-02-07 07:26:12 -0800903static clib_error_t *
904ipsec_tun_protect_cmd (vlib_main_t * vm,
905 unformat_input_t * input, vlib_cli_command_t * cmd)
906{
907 unformat_input_t _line_input, *line_input = &_line_input;
908 u32 sw_if_index, is_del, sa_in, sa_out, *sa_ins = NULL;
Neale Ranns28287212019-12-16 00:53:11 +0000909 ip_address_t peer = { };
Neale Rannsc87b66c2019-02-07 07:26:12 -0800910 vnet_main_t *vnm;
911
912 is_del = 0;
913 sw_if_index = ~0;
914 vnm = vnet_get_main ();
915
916 if (!unformat_user (input, unformat_line_input, line_input))
917 return 0;
918
919 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
920 {
921 if (unformat (line_input, "del"))
922 is_del = 1;
923 else if (unformat (line_input, "add"))
924 is_del = 0;
925 else if (unformat (line_input, "sa-in %d", &sa_in))
926 vec_add1 (sa_ins, sa_in);
927 else if (unformat (line_input, "sa-out %d", &sa_out))
928 ;
929 else if (unformat (line_input, "%U",
930 unformat_vnet_sw_interface, vnm, &sw_if_index))
931 ;
Neale Ranns28287212019-12-16 00:53:11 +0000932 else if (unformat (line_input, "%U", unformat_ip_address, &peer))
933 ;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800934 else
935 return (clib_error_return (0, "unknown input '%U'",
936 format_unformat_error, line_input));
937 }
938
939 if (!is_del)
Neale Ranns28287212019-12-16 00:53:11 +0000940 ipsec_tun_protect_update (sw_if_index, &peer, sa_out, sa_ins);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800941
942 unformat_free (line_input);
943 return NULL;
944}
945
946/**
947 * Protect tunnel with IPSEC
948 */
949/* *INDENT-OFF* */
950VLIB_CLI_COMMAND (ipsec_tun_protect_cmd_node, static) =
951{
952 .path = "ipsec tunnel protect",
953 .function = ipsec_tun_protect_cmd,
954 .short_help = "ipsec tunnel protect <interface> input-sa <SA> output-sa <SA>",
955 // this is not MP safe
956};
957/* *INDENT-ON* */
958
Neale Rannsc87b66c2019-02-07 07:26:12 -0800959
960static clib_error_t *
961ipsec_tun_protect_show (vlib_main_t * vm,
962 unformat_input_t * input, vlib_cli_command_t * cmd)
963{
964 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
965
966 return NULL;
967}
968
969/**
970 * show IPSEC tunnel protection
971 */
972/* *INDENT-OFF* */
973VLIB_CLI_COMMAND (ipsec_tun_protect_show_node, static) =
974{
975 .path = "show ipsec protect",
976 .function = ipsec_tun_protect_show,
977 .short_help = "show ipsec protect",
978};
979/* *INDENT-ON* */
980
Neale Ranns41afb332019-07-16 06:19:35 -0700981static clib_error_t *
982ipsec_tun_protect_hash_show (vlib_main_t * vm,
983 unformat_input_t * input,
984 vlib_cli_command_t * cmd)
985{
986 ipsec_main_t *im = &ipsec_main;
987
988 {
989 ipsec_tun_lkup_result_t value;
990 ipsec4_tunnel_key_t key;
991
992 vlib_cli_output (vm, "IPv4:");
993
994 /* *INDENT-OFF* */
995 hash_foreach(key.as_u64, value.as_u64, im->tun4_protect_by_key,
996 ({
997 vlib_cli_output (vm, " %U", format_ipsec4_tunnel_key, &key);
998 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
999 }));
1000 /* *INDENT-ON* */
1001 }
1002
1003 {
1004 ipsec_tun_lkup_result_t value;
1005 ipsec6_tunnel_key_t *key;
1006
1007 vlib_cli_output (vm, "IPv6:");
1008
1009 /* *INDENT-OFF* */
1010 hash_foreach_mem(key, value.as_u64, im->tun6_protect_by_key,
1011 ({
1012 vlib_cli_output (vm, " %U", format_ipsec6_tunnel_key, key);
1013 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
1014 }));
1015 /* *INDENT-ON* */
1016 }
1017
1018 return NULL;
1019}
1020
1021/**
1022 * show IPSEC tunnel protection hash tables
1023 */
1024/* *INDENT-OFF* */
1025VLIB_CLI_COMMAND (ipsec_tun_protect_hash_show_node, static) =
1026{
1027 .path = "show ipsec protect-hash",
1028 .function = ipsec_tun_protect_hash_show,
1029 .short_help = "show ipsec protect-hash",
1030};
1031/* *INDENT-ON* */
1032
Ed Warnickecb9cada2015-12-08 15:45:58 -07001033clib_error_t *
1034ipsec_cli_init (vlib_main_t * vm)
1035{
1036 return 0;
1037}
1038
1039VLIB_INIT_FUNCTION (ipsec_cli_init);
1040
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001041
1042/*
1043 * fd.io coding-style-patch-verification: ON
1044 *
1045 * Local Variables:
1046 * eval: (c-set-style "gnu")
1047 * End:
1048 */