blob: e4121605fdcff5db94dd655993cb56bb9b7b7049 [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),
152 &tun_src, &tun_dst, NULL);
Neale Ranns8d7c5022019-02-06 01:41:05 -0800153 else
Neale Ranns495d7ff2019-07-12 09:15:26 +0000154 rv = ipsec_sa_unlock_id (id);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000155
Neale Ranns8d7c5022019-02-06 01:41:05 -0800156 if (rv)
Neale Rannse6be7022019-06-04 15:37:34 +0000157 error = clib_error_return (0, "failed");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158
Billy McFalla9a20e72017-02-15 11:39:12 -0500159done:
160 unformat_free (line_input);
161
162 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163}
164
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700165/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
167 .path = "ipsec sa",
168 .short_help =
169 "ipsec sa [add|del]",
170 .function = ipsec_sa_add_del_command_fn,
171};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700172/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173
174static clib_error_t *
175ipsec_spd_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700176 unformat_input_t * input,
177 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700179 unformat_input_t _line_input, *line_input = &_line_input;
Damjan Marion3f54b182016-08-16 11:27:02 +0200180 u32 spd_id = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181 int is_add = ~0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500182 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700184 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185 return 0;
186
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700187 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
188 {
189 if (unformat (line_input, "add"))
190 is_add = 1;
191 else if (unformat (line_input, "del"))
192 is_add = 0;
193 else if (unformat (line_input, "%u", &spd_id))
194 ;
195 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500196 {
197 error = clib_error_return (0, "parse error: '%U'",
198 format_unformat_error, line_input);
199 goto done;
200 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700201 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202
Damjan Marion3f54b182016-08-16 11:27:02 +0200203 if (spd_id == ~0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500204 {
205 error = clib_error_return (0, "please specify SPD ID");
206 goto done;
207 }
Damjan Marion3f54b182016-08-16 11:27:02 +0200208
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700209 ipsec_add_del_spd (vm, spd_id, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210
Billy McFalla9a20e72017-02-15 11:39:12 -0500211done:
212 unformat_free (line_input);
213
214 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215}
216
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700217/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
219 .path = "ipsec spd",
220 .short_help =
221 "ipsec spd [add|del] <id>",
222 .function = ipsec_spd_add_del_command_fn,
223};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700224/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225
226
227static clib_error_t *
228ipsec_policy_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700229 unformat_input_t * input,
230 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700232 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233 ipsec_policy_t p;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800234 int rv, is_add = 0;
235 u32 tmp, tmp2, stat_index;
Billy McFalla9a20e72017-02-15 11:39:12 -0500236 clib_error_t *error = NULL;
Neale Ranns9f231d42019-03-19 10:06:00 +0000237 u32 is_outbound;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238
Dave Barachb7b92992018-10-17 10:38:51 -0400239 clib_memset (&p, 0, sizeof (p));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240 p.lport.stop = p.rport.stop = ~0;
Neale Ranns9f231d42019-03-19 10:06:00 +0000241 is_outbound = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700243 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244 return 0;
245
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700246 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
247 {
248 if (unformat (line_input, "add"))
249 is_add = 1;
250 else if (unformat (line_input, "del"))
251 is_add = 0;
252 else if (unformat (line_input, "spd %u", &p.id))
253 ;
254 else if (unformat (line_input, "inbound"))
Neale Ranns9f231d42019-03-19 10:06:00 +0000255 is_outbound = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700256 else if (unformat (line_input, "outbound"))
Neale Ranns9f231d42019-03-19 10:06:00 +0000257 is_outbound = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700258 else if (unformat (line_input, "priority %d", &p.priority))
259 ;
260 else if (unformat (line_input, "protocol %u", &tmp))
261 p.protocol = (u8) tmp;
262 else
263 if (unformat
264 (line_input, "action %U", unformat_ipsec_policy_action,
265 &p.policy))
266 {
267 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
Billy McFalla9a20e72017-02-15 11:39:12 -0500268 {
269 error = clib_error_return (0, "unsupported action: 'resolve'");
270 goto done;
271 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700272 }
273 else if (unformat (line_input, "sa %u", &p.sa_id))
274 ;
275 else if (unformat (line_input, "local-ip-range %U - %U",
276 unformat_ip4_address, &p.laddr.start.ip4,
277 unformat_ip4_address, &p.laddr.stop.ip4))
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800278 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700279 else if (unformat (line_input, "remote-ip-range %U - %U",
280 unformat_ip4_address, &p.raddr.start.ip4,
281 unformat_ip4_address, &p.raddr.stop.ip4))
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800282 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700283 else if (unformat (line_input, "local-ip-range %U - %U",
284 unformat_ip6_address, &p.laddr.start.ip6,
285 unformat_ip6_address, &p.laddr.stop.ip6))
286 {
287 p.is_ipv6 = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700288 }
289 else if (unformat (line_input, "remote-ip-range %U - %U",
290 unformat_ip6_address, &p.raddr.start.ip6,
291 unformat_ip6_address, &p.raddr.stop.ip6))
292 {
293 p.is_ipv6 = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700294 }
295 else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
296 {
297 p.lport.start = tmp;
298 p.lport.stop = tmp2;
299 }
300 else
301 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
302 {
303 p.rport.start = tmp;
304 p.rport.stop = tmp2;
305 }
306 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500307 {
308 error = clib_error_return (0, "parse error: '%U'",
309 format_unformat_error, line_input);
310 goto done;
311 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700312 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313
Neale Ranns9f231d42019-03-19 10:06:00 +0000314 rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
315
316 if (rv)
317 {
318 error = clib_error_return (0, "unsupported policy type for:",
319 " outboud:%s %s action:%U",
320 (is_outbound ? "yes" : "no"),
321 (p.is_ipv6 ? "IPv4" : "IPv6"),
322 format_ipsec_policy_action, p.policy);
323 goto done;
324 }
325
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800326 rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
327
328 if (!rv)
329 vlib_cli_output (vm, "policy-index:%d", stat_index);
330 else
331 vlib_cli_output (vm, "error:%d", rv);
Billy McFalla9a20e72017-02-15 11:39:12 -0500332
333done:
334 unformat_free (line_input);
335
336 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337}
338
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700339/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
341 .path = "ipsec policy",
342 .short_help =
343 "ipsec policy [add|del] spd <id> priority <n> ",
344 .function = ipsec_policy_add_del_command_fn,
345};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700346/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347
Neale Rannsb294f102019-04-03 13:17:50 +0000348static void
Neale Ranns670027a2019-08-27 12:47:17 +0000349ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im, u8 detail)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350{
Neale Rannsb294f102019-04-03 13:17:50 +0000351 u32 sai;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700353 /* *INDENT-OFF* */
Neale Ranns8d7c5022019-02-06 01:41:05 -0800354 pool_foreach_index (sai, im->sad, ({
Neale Ranns670027a2019-08-27 12:47:17 +0000355 vlib_cli_output(vm, "%U", format_ipsec_sa, sai,
356 (detail ? IPSEC_FORMAT_DETAIL : IPSEC_FORMAT_BRIEF));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357 }));
Neale Rannsb294f102019-04-03 13:17:50 +0000358 /* *INDENT-ON* */
359}
360
361static void
362ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im)
363{
364 u32 spdi;
365
366 /* *INDENT-OFF* */
367 pool_foreach_index (spdi, im->spds, ({
368 vlib_cli_output(vm, "%U", format_ipsec_spd, spdi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369 }));
Neale Rannsb294f102019-04-03 13:17:50 +0000370 /* *INDENT-ON* */
371}
372
373static void
374ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im)
375{
376 u32 spd_id, sw_if_index;
Neale Rannsd83c4a82019-06-14 06:48:27 -0700377 ipsec_spd_t *spd;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378
Neale Ranns311124e2019-01-24 04:52:25 -0800379 vlib_cli_output (vm, "SPD Bindings:");
Neale Ranns8d7c5022019-02-06 01:41:05 -0800380
Neale Rannsb294f102019-04-03 13:17:50 +0000381 /* *INDENT-OFF* */
Neale Ranns311124e2019-01-24 04:52:25 -0800382 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
Neale Rannsd83c4a82019-06-14 06:48:27 -0700383 spd = pool_elt_at_index (im->spds, spd_id);
384 vlib_cli_output (vm, " %d -> %U", spd->id,
Neale Ranns8d7c5022019-02-06 01:41:05 -0800385 format_vnet_sw_if_index_name, im->vnet_main,
386 sw_if_index);
Neale Ranns311124e2019-01-24 04:52:25 -0800387 }));
388 /* *INDENT-ON* */
Neale Rannsb294f102019-04-03 13:17:50 +0000389}
Neale Ranns311124e2019-01-24 04:52:25 -0800390
Neale Ranns12989b52019-09-26 16:20:19 +0000391static walk_rc_t
392ipsec_tun_protect_show_one (index_t itpi, void *ctx)
Neale Rannsb294f102019-04-03 13:17:50 +0000393{
Neale Ranns12989b52019-09-26 16:20:19 +0000394 vlib_cli_output (ctx, "%U", format_ipsec_tun_protect, itpi);
Neale Rannsb294f102019-04-03 13:17:50 +0000395
Neale Ranns12989b52019-09-26 16:20:19 +0000396 return (WALK_CONTINUE);
397}
398
399static void
400ipsec_tunnel_show_all (vlib_main_t * vm)
401{
402 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000403}
404
405static clib_error_t *
406show_ipsec_command_fn (vlib_main_t * vm,
407 unformat_input_t * input, vlib_cli_command_t * cmd)
408{
409 ipsec_main_t *im = &ipsec_main;
410
Neale Ranns670027a2019-08-27 12:47:17 +0000411 ipsec_sa_show_all (vm, im, 0);
Neale Rannsb294f102019-04-03 13:17:50 +0000412 ipsec_spd_show_all (vm, im);
413 ipsec_spd_bindings_show_all (vm, im);
Neale Ranns12989b52019-09-26 16:20:19 +0000414 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000415
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416 return 0;
417}
418
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700419/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420VLIB_CLI_COMMAND (show_ipsec_command, static) = {
Neale Rannsb294f102019-04-03 13:17:50 +0000421 .path = "show ipsec all",
422 .short_help = "show ipsec all",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423 .function = show_ipsec_command_fn,
424};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700425/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426
427static clib_error_t *
Neale Rannsb294f102019-04-03 13:17:50 +0000428show_ipsec_sa_command_fn (vlib_main_t * vm,
429 unformat_input_t * input, vlib_cli_command_t * cmd)
430{
431 ipsec_main_t *im = &ipsec_main;
432 u32 sai = ~0;
Neale Ranns670027a2019-08-27 12:47:17 +0000433 u8 detail = 0;
Neale Rannsb294f102019-04-03 13:17:50 +0000434
435 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
436 {
437 if (unformat (input, "%u", &sai))
438 ;
Neale Ranns670027a2019-08-27 12:47:17 +0000439 if (unformat (input, "detail"))
440 detail = 1;
Neale Rannsb294f102019-04-03 13:17:50 +0000441 else
442 break;
443 }
444
445 if (~0 == sai)
Neale Ranns670027a2019-08-27 12:47:17 +0000446 ipsec_sa_show_all (vm, im, detail);
Neale Rannsb294f102019-04-03 13:17:50 +0000447 else
Christian E. Hopps01d61e72019-09-27 14:43:22 -0400448 vlib_cli_output (vm, "%U", format_ipsec_sa, sai,
449 IPSEC_FORMAT_DETAIL | IPSEC_FORMAT_INSECURE);
Neale Rannsb294f102019-04-03 13:17:50 +0000450
451 return 0;
452}
453
Neale Rannsc87b66c2019-02-07 07:26:12 -0800454static clib_error_t *
455clear_ipsec_sa_command_fn (vlib_main_t * vm,
456 unformat_input_t * input, vlib_cli_command_t * cmd)
457{
458 ipsec_main_t *im = &ipsec_main;
459 u32 sai = ~0;
460
461 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
462 {
463 if (unformat (input, "%u", &sai))
464 ;
465 else
466 break;
467 }
468
469 if (~0 == sai)
470 {
471 /* *INDENT-OFF* */
472 pool_foreach_index (sai, im->sad, ({
473 ipsec_sa_clear(sai);
474 }));
475 /* *INDENT-ON* */
476 }
477 else
478 {
479 if (pool_is_free_index (im->sad, sai))
480 return clib_error_return (0, "unknown SA index: %d", sai);
481 else
482 ipsec_sa_clear (sai);
483 }
484
485 return 0;
486}
487
Neale Rannsb294f102019-04-03 13:17:50 +0000488/* *INDENT-OFF* */
489VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
490 .path = "show ipsec sa",
491 .short_help = "show ipsec sa [index]",
492 .function = show_ipsec_sa_command_fn,
493};
Neale Rannsc87b66c2019-02-07 07:26:12 -0800494
495VLIB_CLI_COMMAND (clear_ipsec_sa_command, static) = {
496 .path = "clear ipsec sa",
497 .short_help = "clear ipsec sa [index]",
498 .function = clear_ipsec_sa_command_fn,
499};
Neale Rannsb294f102019-04-03 13:17:50 +0000500/* *INDENT-ON* */
501
502static clib_error_t *
503show_ipsec_spd_command_fn (vlib_main_t * vm,
504 unformat_input_t * input, vlib_cli_command_t * cmd)
505{
506 ipsec_main_t *im = &ipsec_main;
507 u8 show_bindings = 0;
508 u32 spdi = ~0;
509
510 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
511 {
512 if (unformat (input, "%u", &spdi))
513 ;
514 else if (unformat (input, "bindings"))
515 show_bindings = 1;
516 else
517 break;
518 }
519
520 if (show_bindings)
521 ipsec_spd_bindings_show_all (vm, im);
522 else if (~0 != spdi)
523 vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
524 else
525 ipsec_spd_show_all (vm, im);
526
527 return 0;
528}
529
530/* *INDENT-OFF* */
531VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
532 .path = "show ipsec spd",
533 .short_help = "show ipsec spd [index]",
534 .function = show_ipsec_spd_command_fn,
535};
536/* *INDENT-ON* */
537
538static clib_error_t *
539show_ipsec_tunnel_command_fn (vlib_main_t * vm,
540 unformat_input_t * input,
541 vlib_cli_command_t * cmd)
542{
Neale Ranns12989b52019-09-26 16:20:19 +0000543 ipsec_tunnel_show_all (vm);
Neale Rannsb294f102019-04-03 13:17:50 +0000544
545 return 0;
546}
547
548/* *INDENT-OFF* */
549VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
550 .path = "show ipsec tunnel",
Neale Ranns12989b52019-09-26 16:20:19 +0000551 .short_help = "show ipsec tunnel",
Neale Rannsb294f102019-04-03 13:17:50 +0000552 .function = show_ipsec_tunnel_command_fn,
553};
554/* *INDENT-ON* */
555
556static clib_error_t *
Klement Sekerab4d30532018-11-08 13:00:02 +0100557ipsec_show_backends_command_fn (vlib_main_t * vm,
558 unformat_input_t * input,
559 vlib_cli_command_t * cmd)
560{
561 ipsec_main_t *im = &ipsec_main;
562 u32 verbose = 0;
563
564 (void) unformat (input, "verbose %u", &verbose);
565
566 vlib_cli_output (vm, "IPsec AH backends available:");
567 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
568 ipsec_ah_backend_t *ab;
569 /* *INDENT-OFF* */
570 pool_foreach (ab, im->ah_backends, {
571 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
572 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
573 if (verbose) {
574 vlib_node_t *n;
575 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
576 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
577 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
578 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
579 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
580 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
581 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
582 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
583 }
584 });
585 /* *INDENT-ON* */
586 vlib_cli_output (vm, "%v", s);
587 _vec_len (s) = 0;
588 vlib_cli_output (vm, "IPsec ESP backends available:");
589 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
590 ipsec_esp_backend_t *eb;
591 /* *INDENT-OFF* */
592 pool_foreach (eb, im->esp_backends, {
593 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
594 eb - im->esp_backends == im->esp_current_backend ? "yes"
595 : "no");
596 if (verbose) {
597 vlib_node_t *n;
598 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
599 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
600 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
601 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
602 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
603 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
604 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
605 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
606 }
607 });
608 /* *INDENT-ON* */
609 vlib_cli_output (vm, "%v", s);
610
611 vec_free (s);
612 return 0;
613}
614
615/* *INDENT-OFF* */
616VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
617 .path = "show ipsec backends",
618 .short_help = "show ipsec backends",
619 .function = ipsec_show_backends_command_fn,
620};
621/* *INDENT-ON* */
622
623static clib_error_t *
624ipsec_select_backend_command_fn (vlib_main_t * vm,
625 unformat_input_t * input,
626 vlib_cli_command_t * cmd)
627{
Klement Sekerab4d30532018-11-08 13:00:02 +0100628 unformat_input_t _line_input, *line_input = &_line_input;
Neale Rannse8915fc2019-04-23 20:57:55 -0400629 ipsec_main_t *im = &ipsec_main;
630 clib_error_t *error;
631 u32 backend_index;
632
633 error = ipsec_rsc_in_use (im);
634
635 if (error)
636 return error;
637
Klement Sekerab4d30532018-11-08 13:00:02 +0100638 /* Get a line of input. */
639 if (!unformat_user (input, unformat_line_input, line_input))
640 return 0;
641
642 if (unformat (line_input, "ah"))
643 {
644 if (unformat (line_input, "%u", &backend_index))
645 {
646 if (ipsec_select_ah_backend (im, backend_index) < 0)
647 {
648 return clib_error_return (0, "Invalid AH backend index `%u'",
649 backend_index);
650 }
651 }
652 else
653 {
654 return clib_error_return (0, "Invalid backend index `%U'",
655 format_unformat_error, line_input);
656 }
657 }
658 else if (unformat (line_input, "esp"))
659 {
660 if (unformat (line_input, "%u", &backend_index))
661 {
662 if (ipsec_select_esp_backend (im, backend_index) < 0)
663 {
664 return clib_error_return (0, "Invalid ESP backend index `%u'",
665 backend_index);
666 }
667 }
668 else
669 {
670 return clib_error_return (0, "Invalid backend index `%U'",
671 format_unformat_error, line_input);
672 }
673 }
674 else
675 {
676 return clib_error_return (0, "Unknown input `%U'",
677 format_unformat_error, line_input);
678 }
679
680 return 0;
681}
682
683/* *INDENT-OFF* */
684VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
685 .path = "ipsec select backend",
686 .short_help = "ipsec select backend <ah|esp> <backend index>",
687 .function = ipsec_select_backend_command_fn,
688};
689
690/* *INDENT-ON* */
691
692static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693clear_ipsec_counters_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700694 unformat_input_t * input,
695 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700696{
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800697 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
Neale Rannseba31ec2019-02-17 18:04:27 +0000698 vlib_clear_combined_counters (&ipsec_sa_counters);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800700 return (NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700701}
702
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700703/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700704VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
705 .path = "clear ipsec counters",
706 .short_help = "clear ipsec counters",
707 .function = clear_ipsec_counters_command_fn,
708};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700709/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710
Neale Ranns12989b52019-09-26 16:20:19 +0000711static u32
712ipsec_tun_mk_local_sa_id (u32 ti)
713{
714 return (0x80000000 | ti);
715}
716
717static u32
718ipsec_tun_mk_remote_sa_id (u32 ti)
719{
720 return (0xc0000000 | ti);
721}
722
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723static clib_error_t *
724create_ipsec_tunnel_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700725 unformat_input_t * input,
726 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700727{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700728 unformat_input_t _line_input, *line_input = &_line_input;
Neale Ranns12989b52019-09-26 16:20:19 +0000729 ip46_address_t local_ip = ip46_address_initializer;
730 ip46_address_t remote_ip = ip46_address_initializer;
731 ipsec_crypto_alg_t crypto_alg;
732 ipsec_integ_alg_t integ_alg;
733 ipsec_sa_flags_t flags;
734 u32 local_spi, remote_spi, salt, table_id, fib_index;
735 u32 instance = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700736 int rv;
737 u32 num_m_args = 0;
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500738 u8 ipv4_set = 0;
739 u8 ipv6_set = 0;
Neale Ranns12989b52019-09-26 16:20:19 +0000740 u8 is_add = 1;
Billy McFalla9a20e72017-02-15 11:39:12 -0500741 clib_error_t *error = NULL;
Kingwel Xied3d12052019-03-07 06:34:30 -0500742 ipsec_key_t rck = { 0 };
743 ipsec_key_t lck = { 0 };
744 ipsec_key_t lik = { 0 };
745 ipsec_key_t rik = { 0 };
Matthew Smith2838a232016-06-21 16:05:09 -0500746
Neale Ranns12989b52019-09-26 16:20:19 +0000747 table_id = 0;
748 flags = IPSEC_SA_FLAG_NONE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749
750 /* Get a line of input. */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700751 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700752 return 0;
753
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700754 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
755 {
756 if (unformat
Neale Ranns12989b52019-09-26 16:20:19 +0000757 (line_input, "local-ip %U", unformat_ip46_address, &local_ip,
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500758 IP46_TYPE_ANY))
759 {
Neale Ranns12989b52019-09-26 16:20:19 +0000760 ip46_address_is_ip4 (&local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500761 num_m_args++;
762 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700763 else
764 if (unformat
Neale Ranns12989b52019-09-26 16:20:19 +0000765 (line_input, "remote-ip %U", unformat_ip46_address, &remote_ip,
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500766 IP46_TYPE_ANY))
767 {
Neale Ranns12989b52019-09-26 16:20:19 +0000768 ip46_address_is_ip4 (&remote_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500769 num_m_args++;
770 }
Neale Ranns12989b52019-09-26 16:20:19 +0000771 else if (unformat (line_input, "local-spi %u", &local_spi))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700772 num_m_args++;
Neale Ranns12989b52019-09-26 16:20:19 +0000773 else if (unformat (line_input, "remote-spi %u", &remote_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, "salt 0x%x", &salt))
Neale Ranns47feb112019-04-11 15:14:07 +0000776 ;
Radu Nicolau717de092018-08-03 10:37:24 +0100777 else if (unformat (line_input, "udp-encap"))
Neale Ranns12989b52019-09-26 16:20:19 +0000778 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800779 else if (unformat (line_input, "use-esn"))
Neale Ranns12989b52019-09-26 16:20:19 +0000780 flags |= IPSEC_SA_FLAG_USE_ESN;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800781 else if (unformat (line_input, "use-anti-replay"))
Neale Ranns12989b52019-09-26 16:20:19 +0000782 flags |= IPSEC_SA_FLAG_USE_ANTI_REPLAY;
783 else if (unformat (line_input, "instance %u", &instance))
784 ;
785 else if (unformat (line_input, "tx-table %u", &table_id))
Pierre Pfister4c422f92018-12-10 11:19:08 +0100786 ;
Neale Rannsfd060842019-03-04 13:44:42 +0000787 else
788 if (unformat
789 (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
790 ;
791 else
792 if (unformat
793 (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
794 ;
795 else if (unformat (line_input, "crypto-alg %U",
Neale Ranns12989b52019-09-26 16:20:19 +0000796 unformat_ipsec_crypto_alg, &crypto_alg))
Neale Rannsfd060842019-03-04 13:44:42 +0000797 ;
798 else
799 if (unformat
800 (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
801 ;
802 else
803 if (unformat
Simon Zhange8e950a2019-04-23 23:04:07 +0800804 (line_input, "remote-integ-key %U", unformat_ipsec_key, &rik))
Neale Rannsfd060842019-03-04 13:44:42 +0000805 ;
806 else if (unformat (line_input, "integ-alg %U",
Neale Ranns12989b52019-09-26 16:20:19 +0000807 unformat_ipsec_integ_alg, &integ_alg))
Neale Rannsfd060842019-03-04 13:44:42 +0000808 ;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800809 else if (unformat (line_input, "del"))
Neale Ranns12989b52019-09-26 16:20:19 +0000810 is_add = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700811 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500812 {
813 error = clib_error_return (0, "unknown input `%U'",
814 format_unformat_error, line_input);
815 goto done;
816 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700817 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700818
819 if (num_m_args < 4)
Billy McFalla9a20e72017-02-15 11:39:12 -0500820 {
821 error = clib_error_return (0, "mandatory argument(s) missing");
822 goto done;
823 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700824
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500825 if (ipv4_set && ipv6_set)
826 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
827
Neale Ranns12989b52019-09-26 16:20:19 +0000828 fib_index = fib_table_find (fib_ip_proto (ipv6_set), table_id);
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400829
Neale Ranns12989b52019-09-26 16:20:19 +0000830 if (~0 == fib_index)
831 {
832 rv = VNET_API_ERROR_NO_SUCH_FIB;
833 goto done;
834 }
Neale Rannsfd060842019-03-04 13:44:42 +0000835
Neale Ranns12989b52019-09-26 16:20:19 +0000836 if (is_add)
837 {
838 // remote = input, local = output
839 u32 sw_if_index;
Neale Rannsfd060842019-03-04 13:44:42 +0000840
Neale Ranns12989b52019-09-26 16:20:19 +0000841 /* create an ip-ip tunnel, then the two SA, then bind them */
842 rv =
843 ipip_add_tunnel (ipv6_set ? IPIP_TRANSPORT_IP6 : IPIP_TRANSPORT_IP4,
Neale Ranns95346962019-11-25 13:04:44 +0000844 instance, &local_ip, &remote_ip, fib_index,
845 IPIP_TUNNEL_FLAG_NONE, IP_DSCP_CS0, &sw_if_index);
Neale Ranns12989b52019-09-26 16:20:19 +0000846 rv |=
847 ipsec_sa_add_and_lock (ipsec_tun_mk_local_sa_id (sw_if_index),
848 local_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
849 &lck, integ_alg, &lik, flags, table_id,
850 clib_host_to_net_u32 (salt), &local_ip,
851 &remote_ip, NULL);
852 rv |=
853 ipsec_sa_add_and_lock (ipsec_tun_mk_remote_sa_id (sw_if_index),
854 remote_spi, IPSEC_PROTOCOL_ESP, crypto_alg,
855 &rck, integ_alg, &rik,
856 (flags | IPSEC_SA_FLAG_IS_INBOUND), table_id,
857 clib_host_to_net_u32 (salt), &remote_ip,
858 &local_ip, NULL);
859 rv |=
860 ipsec_tun_protect_update_one (sw_if_index,
861 ipsec_tun_mk_local_sa_id (sw_if_index),
862 ipsec_tun_mk_remote_sa_id
863 (sw_if_index));
864 }
865 else
866 rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700867
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700868 switch (rv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700869 {
870 case 0:
871 break;
872 case VNET_API_ERROR_INVALID_VALUE:
Neale Rannsd14fccd2019-11-15 15:03:27 +0000873 error = clib_error_return (0,
874 "IPSec tunnel interface already exists...");
Billy McFalla9a20e72017-02-15 11:39:12 -0500875 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700876 default:
Billy McFalla9a20e72017-02-15 11:39:12 -0500877 error = clib_error_return (0, "ipsec_register_interface returned %d",
878 rv);
879 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700880 }
881
Billy McFalla9a20e72017-02-15 11:39:12 -0500882done:
883 unformat_free (line_input);
884
885 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700886}
887
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700888/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700889VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
890 .path = "create ipsec tunnel",
Pierre Pfister4c422f92018-12-10 11:19:08 +0100891 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
Kingwel Xie2baf9422019-02-04 02:07:06 -0800892 "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
Pierre Pfister4c422f92018-12-10 11:19:08 +0100893 "[tx-table <table-id>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700894 .function = create_ipsec_tunnel_command_fn,
895};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700896/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700897
Neale Rannsc87b66c2019-02-07 07:26:12 -0800898static clib_error_t *
899ipsec_tun_protect_cmd (vlib_main_t * vm,
900 unformat_input_t * input, vlib_cli_command_t * cmd)
901{
902 unformat_input_t _line_input, *line_input = &_line_input;
903 u32 sw_if_index, is_del, sa_in, sa_out, *sa_ins = NULL;
904 vnet_main_t *vnm;
905
906 is_del = 0;
907 sw_if_index = ~0;
908 vnm = vnet_get_main ();
909
910 if (!unformat_user (input, unformat_line_input, line_input))
911 return 0;
912
913 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
914 {
915 if (unformat (line_input, "del"))
916 is_del = 1;
917 else if (unformat (line_input, "add"))
918 is_del = 0;
919 else if (unformat (line_input, "sa-in %d", &sa_in))
920 vec_add1 (sa_ins, sa_in);
921 else if (unformat (line_input, "sa-out %d", &sa_out))
922 ;
923 else if (unformat (line_input, "%U",
924 unformat_vnet_sw_interface, vnm, &sw_if_index))
925 ;
926 else
927 return (clib_error_return (0, "unknown input '%U'",
928 format_unformat_error, line_input));
929 }
930
931 if (!is_del)
932 ipsec_tun_protect_update (sw_if_index, sa_out, sa_ins);
933
934 unformat_free (line_input);
935 return NULL;
936}
937
938/**
939 * Protect tunnel with IPSEC
940 */
941/* *INDENT-OFF* */
942VLIB_CLI_COMMAND (ipsec_tun_protect_cmd_node, static) =
943{
944 .path = "ipsec tunnel protect",
945 .function = ipsec_tun_protect_cmd,
946 .short_help = "ipsec tunnel protect <interface> input-sa <SA> output-sa <SA>",
947 // this is not MP safe
948};
949/* *INDENT-ON* */
950
Neale Rannsc87b66c2019-02-07 07:26:12 -0800951
952static clib_error_t *
953ipsec_tun_protect_show (vlib_main_t * vm,
954 unformat_input_t * input, vlib_cli_command_t * cmd)
955{
956 ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm);
957
958 return NULL;
959}
960
961/**
962 * show IPSEC tunnel protection
963 */
964/* *INDENT-OFF* */
965VLIB_CLI_COMMAND (ipsec_tun_protect_show_node, static) =
966{
967 .path = "show ipsec protect",
968 .function = ipsec_tun_protect_show,
969 .short_help = "show ipsec protect",
970};
971/* *INDENT-ON* */
972
Neale Ranns41afb332019-07-16 06:19:35 -0700973static clib_error_t *
974ipsec_tun_protect_hash_show (vlib_main_t * vm,
975 unformat_input_t * input,
976 vlib_cli_command_t * cmd)
977{
978 ipsec_main_t *im = &ipsec_main;
979
980 {
981 ipsec_tun_lkup_result_t value;
982 ipsec4_tunnel_key_t key;
983
984 vlib_cli_output (vm, "IPv4:");
985
986 /* *INDENT-OFF* */
987 hash_foreach(key.as_u64, value.as_u64, im->tun4_protect_by_key,
988 ({
989 vlib_cli_output (vm, " %U", format_ipsec4_tunnel_key, &key);
990 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
991 }));
992 /* *INDENT-ON* */
993 }
994
995 {
996 ipsec_tun_lkup_result_t value;
997 ipsec6_tunnel_key_t *key;
998
999 vlib_cli_output (vm, "IPv6:");
1000
1001 /* *INDENT-OFF* */
1002 hash_foreach_mem(key, value.as_u64, im->tun6_protect_by_key,
1003 ({
1004 vlib_cli_output (vm, " %U", format_ipsec6_tunnel_key, key);
1005 vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
1006 }));
1007 /* *INDENT-ON* */
1008 }
1009
1010 return NULL;
1011}
1012
1013/**
1014 * show IPSEC tunnel protection hash tables
1015 */
1016/* *INDENT-OFF* */
1017VLIB_CLI_COMMAND (ipsec_tun_protect_hash_show_node, static) =
1018{
1019 .path = "show ipsec protect-hash",
1020 .function = ipsec_tun_protect_hash_show,
1021 .short_help = "show ipsec protect-hash",
1022};
1023/* *INDENT-ON* */
1024
Ed Warnickecb9cada2015-12-08 15:45:58 -07001025clib_error_t *
1026ipsec_cli_init (vlib_main_t * vm)
1027{
1028 return 0;
1029}
1030
1031VLIB_INIT_FUNCTION (ipsec_cli_init);
1032
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -07001033
1034/*
1035 * fd.io coding-style-patch-verification: ON
1036 *
1037 * Local Variables:
1038 * eval: (c-set-style "gnu")
1039 * End:
1040 */