blob: b247d5d87c69d1dd337eba9af7d665b55787fb7f [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * decap.c : IPSec tunnel support
3 *
4 * Copyright (c) 2015 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <vnet/vnet.h>
19#include <vnet/api_errno.h>
20#include <vnet/ip/ip.h>
21#include <vnet/interface.h>
Pierre Pfister4c422f92018-12-10 11:19:08 +010022#include <vnet/fib/fib.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070023
24#include <vnet/ipsec/ipsec.h>
25
26static clib_error_t *
27set_interface_spd_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070028 unformat_input_t * input,
29 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -070030{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070031 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -070032 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070033 u32 sw_if_index = (u32) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070034 u32 spd_id;
35 int is_add = 1;
Billy McFalla9a20e72017-02-15 11:39:12 -050036 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -070037
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070038 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -070039 return 0;
40
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070041 if (unformat
42 (line_input, "%U %u", unformat_vnet_sw_interface, im->vnet_main,
43 &sw_if_index, &spd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -070044 ;
45 else if (unformat (line_input, "del"))
46 is_add = 0;
47 else
Billy McFalla9a20e72017-02-15 11:39:12 -050048 {
49 error = clib_error_return (0, "parse error: '%U'",
50 format_unformat_error, line_input);
51 goto done;
52 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070053
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070054 ipsec_set_interface_spd (vm, sw_if_index, spd_id, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -070055
Billy McFalla9a20e72017-02-15 11:39:12 -050056done:
57 unformat_free (line_input);
58
59 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -070060}
61
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070062/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070063VLIB_CLI_COMMAND (set_interface_spd_command, static) = {
64 .path = "set interface ipsec spd",
65 .short_help =
66 "set interface ipsec spd <int> <id>",
67 .function = set_interface_spd_command_fn,
68};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070069/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070070
71static clib_error_t *
72ipsec_sa_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070073 unformat_input_t * input,
74 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -070075{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070076 unformat_input_t _line_input, *line_input = &_line_input;
Neale Ranns8d7c5022019-02-06 01:41:05 -080077 ip46_address_t tun_src = { }, tun_dst =
78 {
79 };
80 ipsec_crypto_alg_t crypto_alg;
81 ipsec_integ_alg_t integ_alg;
82 ipsec_protocol_t proto;
83 ipsec_sa_flags_t flags;
84 clib_error_t *error;
Kingwel Xied3d12052019-03-07 06:34:30 -050085 ipsec_key_t ck = { 0 };
86 ipsec_key_t ik = { 0 };
Neale Ranns8d7c5022019-02-06 01:41:05 -080087 int is_add, rv;
88 u32 id, spi;
Ed Warnickecb9cada2015-12-08 15:45:58 -070089
Neale Ranns8d7c5022019-02-06 01:41:05 -080090 error = NULL;
91 is_add = 0;
92 flags = IPSEC_SA_FLAG_NONE;
93 proto = IPSEC_PROTOCOL_ESP;
Ed Warnickecb9cada2015-12-08 15:45:58 -070094
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070095 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -070096 return 0;
97
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070098 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
99 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800100 if (unformat (line_input, "add %u", &id))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700101 is_add = 1;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800102 else if (unformat (line_input, "del %u", &id))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700103 is_add = 0;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800104 else if (unformat (line_input, "spi %u", &spi))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700105 ;
106 else if (unformat (line_input, "esp"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800107 proto = IPSEC_PROTOCOL_ESP;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700108 else if (unformat (line_input, "ah"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800109 proto = IPSEC_PROTOCOL_AH;
110 else if (unformat (line_input, "crypto-key %U",
111 unformat_ipsec_key, &ck))
112 ;
113 else if (unformat (line_input, "crypto-alg %U",
114 unformat_ipsec_crypto_alg, &crypto_alg))
115 ;
116 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
117 ;
118 else if (unformat (line_input, "integ-alg %U",
119 unformat_ipsec_integ_alg, &integ_alg))
120 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700121 else if (unformat (line_input, "tunnel-src %U",
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500122 unformat_ip46_address, &tun_src, IP46_TYPE_ANY))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700123 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800124 flags |= IPSEC_SA_FLAG_IS_TUNNEL;
125 if (!ip46_address_is_ip4 (&tun_src))
126 flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700127 }
128 else if (unformat (line_input, "tunnel-dst %U",
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500129 unformat_ip46_address, &tun_dst, IP46_TYPE_ANY))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800130 ;
Radu Nicolau717de092018-08-03 10:37:24 +0100131 else if (unformat (line_input, "udp-encap"))
Neale Ranns8d7c5022019-02-06 01:41:05 -0800132 flags |= IPSEC_SA_FLAG_UDP_ENCAP;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700133 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500134 {
135 error = clib_error_return (0, "parse error: '%U'",
136 format_unformat_error, line_input);
137 goto done;
138 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700139 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000141 if (is_add)
Neale Ranns8d7c5022019-02-06 01:41:05 -0800142 rv = ipsec_sa_add (id, spi, proto, crypto_alg,
143 &ck, integ_alg, &ik, flags,
144 0, &tun_src, &tun_dst, NULL);
145 else
146 rv = ipsec_sa_del (id);
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000147
Neale Ranns8d7c5022019-02-06 01:41:05 -0800148 if (rv)
149 clib_error_return (0, "failed");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150
Billy McFalla9a20e72017-02-15 11:39:12 -0500151done:
152 unformat_free (line_input);
153
154 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155}
156
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700157/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
159 .path = "ipsec sa",
160 .short_help =
161 "ipsec sa [add|del]",
162 .function = ipsec_sa_add_del_command_fn,
163};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700164/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165
166static clib_error_t *
167ipsec_spd_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700168 unformat_input_t * input,
169 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700171 unformat_input_t _line_input, *line_input = &_line_input;
Damjan Marion3f54b182016-08-16 11:27:02 +0200172 u32 spd_id = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173 int is_add = ~0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500174 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700176 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 return 0;
178
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700179 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
180 {
181 if (unformat (line_input, "add"))
182 is_add = 1;
183 else if (unformat (line_input, "del"))
184 is_add = 0;
185 else if (unformat (line_input, "%u", &spd_id))
186 ;
187 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500188 {
189 error = clib_error_return (0, "parse error: '%U'",
190 format_unformat_error, line_input);
191 goto done;
192 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700193 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194
Damjan Marion3f54b182016-08-16 11:27:02 +0200195 if (spd_id == ~0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500196 {
197 error = clib_error_return (0, "please specify SPD ID");
198 goto done;
199 }
Damjan Marion3f54b182016-08-16 11:27:02 +0200200
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700201 ipsec_add_del_spd (vm, spd_id, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202
Billy McFalla9a20e72017-02-15 11:39:12 -0500203done:
204 unformat_free (line_input);
205
206 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207}
208
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700209/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
211 .path = "ipsec spd",
212 .short_help =
213 "ipsec spd [add|del] <id>",
214 .function = ipsec_spd_add_del_command_fn,
215};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700216/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217
218
219static clib_error_t *
220ipsec_policy_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700221 unformat_input_t * input,
222 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700224 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225 ipsec_policy_t p;
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800226 int rv, is_add = 0;
227 u32 tmp, tmp2, stat_index;
Billy McFalla9a20e72017-02-15 11:39:12 -0500228 clib_error_t *error = NULL;
Neale Ranns9f231d42019-03-19 10:06:00 +0000229 u32 is_outbound;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230
Dave Barachb7b92992018-10-17 10:38:51 -0400231 clib_memset (&p, 0, sizeof (p));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232 p.lport.stop = p.rport.stop = ~0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700233 p.laddr.stop.ip4.as_u32 = p.raddr.stop.ip4.as_u32 = (u32) ~ 0;
234 p.laddr.stop.ip6.as_u64[0] = p.laddr.stop.ip6.as_u64[1] = (u64) ~ 0;
235 p.raddr.stop.ip6.as_u64[0] = p.raddr.stop.ip6.as_u64[1] = (u64) ~ 0;
Neale Ranns9f231d42019-03-19 10:06:00 +0000236 is_outbound = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700238 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239 return 0;
240
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700241 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
242 {
243 if (unformat (line_input, "add"))
244 is_add = 1;
245 else if (unformat (line_input, "del"))
246 is_add = 0;
247 else if (unformat (line_input, "spd %u", &p.id))
248 ;
249 else if (unformat (line_input, "inbound"))
Neale Ranns9f231d42019-03-19 10:06:00 +0000250 is_outbound = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700251 else if (unformat (line_input, "outbound"))
Neale Ranns9f231d42019-03-19 10:06:00 +0000252 is_outbound = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700253 else if (unformat (line_input, "priority %d", &p.priority))
254 ;
255 else if (unformat (line_input, "protocol %u", &tmp))
256 p.protocol = (u8) tmp;
257 else
258 if (unformat
259 (line_input, "action %U", unformat_ipsec_policy_action,
260 &p.policy))
261 {
262 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
Billy McFalla9a20e72017-02-15 11:39:12 -0500263 {
264 error = clib_error_return (0, "unsupported action: 'resolve'");
265 goto done;
266 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700267 }
268 else if (unformat (line_input, "sa %u", &p.sa_id))
269 ;
270 else if (unformat (line_input, "local-ip-range %U - %U",
271 unformat_ip4_address, &p.laddr.start.ip4,
272 unformat_ip4_address, &p.laddr.stop.ip4))
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800273 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700274 else if (unformat (line_input, "remote-ip-range %U - %U",
275 unformat_ip4_address, &p.raddr.start.ip4,
276 unformat_ip4_address, &p.raddr.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, "local-ip-range %U - %U",
279 unformat_ip6_address, &p.laddr.start.ip6,
280 unformat_ip6_address, &p.laddr.stop.ip6))
281 {
282 p.is_ipv6 = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700283 }
284 else if (unformat (line_input, "remote-ip-range %U - %U",
285 unformat_ip6_address, &p.raddr.start.ip6,
286 unformat_ip6_address, &p.raddr.stop.ip6))
287 {
288 p.is_ipv6 = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700289 }
290 else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
291 {
292 p.lport.start = tmp;
293 p.lport.stop = tmp2;
Neale Ranns231c4692019-03-18 17:11:28 +0000294 p.lport.start = clib_host_to_net_u16 (p.lport.start);
295 p.lport.stop = clib_host_to_net_u16 (p.lport.stop);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700296 }
297 else
298 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
299 {
300 p.rport.start = tmp;
301 p.rport.stop = tmp2;
Neale Ranns231c4692019-03-18 17:11:28 +0000302 p.rport.start = clib_host_to_net_u16 (p.rport.start);
303 p.rport.stop = clib_host_to_net_u16 (p.rport.stop);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700304 }
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
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800313 /* Check if SA is for IPv6/AH which is not supported. Return error if TRUE. */
314 if (p.sa_id)
315 {
316 uword *p1;
317 ipsec_main_t *im = &ipsec_main;
318 ipsec_sa_t *sa = 0;
319 p1 = hash_get (im->sa_index_by_sa_id, p.sa_id);
Klement Sekerac2fc57e2018-06-28 14:20:12 +0200320 if (!p1)
321 {
322 error =
323 clib_error_return (0, "SA with index %u not found", p.sa_id);
324 goto done;
325 }
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800326 sa = pool_elt_at_index (im->sad, p1[0]);
327 if (sa && sa->protocol == IPSEC_PROTOCOL_AH && is_add && p.is_ipv6)
328 {
329 error = clib_error_return (0, "AH not supported for IPV6: '%U'",
330 format_unformat_error, line_input);
331 goto done;
332 }
333 }
Neale Ranns9f231d42019-03-19 10:06:00 +0000334
335 rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
336
337 if (rv)
338 {
339 error = clib_error_return (0, "unsupported policy type for:",
340 " outboud:%s %s action:%U",
341 (is_outbound ? "yes" : "no"),
342 (p.is_ipv6 ? "IPv4" : "IPv6"),
343 format_ipsec_policy_action, p.policy);
344 goto done;
345 }
346
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800347 rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
348
349 if (!rv)
350 vlib_cli_output (vm, "policy-index:%d", stat_index);
351 else
352 vlib_cli_output (vm, "error:%d", rv);
Billy McFalla9a20e72017-02-15 11:39:12 -0500353
354done:
355 unformat_free (line_input);
356
357 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358}
359
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700360/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
362 .path = "ipsec policy",
363 .short_help =
364 "ipsec policy [add|del] spd <id> priority <n> ",
365 .function = ipsec_policy_add_del_command_fn,
366};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700367/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368
369static clib_error_t *
370set_ipsec_sa_key_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700371 unformat_input_t * input,
372 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700374 unformat_input_t _line_input, *line_input = &_line_input;
Billy McFalla9a20e72017-02-15 11:39:12 -0500375 clib_error_t *error = NULL;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800376 ipsec_key_t ck, ik;
377 u32 id;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700379 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380 return 0;
381
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700382 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
383 {
Neale Ranns8d7c5022019-02-06 01:41:05 -0800384 if (unformat (line_input, "%u", &id))
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700385 ;
386 else
Neale Ranns8d7c5022019-02-06 01:41:05 -0800387 if (unformat (line_input, "crypto-key %U", unformat_ipsec_key, &ck))
388 ;
389 else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
390 ;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700391 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500392 {
393 error = clib_error_return (0, "parse error: '%U'",
394 format_unformat_error, line_input);
395 goto done;
396 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700397 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398
Neale Ranns8d7c5022019-02-06 01:41:05 -0800399 ipsec_set_sa_key (id, &ck, &ik);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700400
Billy McFalla9a20e72017-02-15 11:39:12 -0500401done:
402 unformat_free (line_input);
403
404 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405}
406
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700407/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408VLIB_CLI_COMMAND (set_ipsec_sa_key_command, static) = {
409 .path = "set ipsec sa",
Neale Rannsb294f102019-04-03 13:17:50 +0000410 .short_help = "set ipsec sa <id> crypto-key <key> integ-key <key>",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411 .function = set_ipsec_sa_key_command_fn,
412};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700413/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414
Neale Rannsb294f102019-04-03 13:17:50 +0000415static void
416ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417{
Neale Rannsb294f102019-04-03 13:17:50 +0000418 u32 sai;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700420 /* *INDENT-OFF* */
Neale Ranns8d7c5022019-02-06 01:41:05 -0800421 pool_foreach_index (sai, im->sad, ({
Neale Rannsb294f102019-04-03 13:17:50 +0000422 vlib_cli_output(vm, "%U", format_ipsec_sa, sai, IPSEC_FORMAT_BRIEF);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423 }));
Neale Rannsb294f102019-04-03 13:17:50 +0000424 /* *INDENT-ON* */
425}
426
427static void
428ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im)
429{
430 u32 spdi;
431
432 /* *INDENT-OFF* */
433 pool_foreach_index (spdi, im->spds, ({
434 vlib_cli_output(vm, "%U", format_ipsec_spd, spdi);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435 }));
Neale Rannsb294f102019-04-03 13:17:50 +0000436 /* *INDENT-ON* */
437}
438
439static void
440ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im)
441{
442 u32 spd_id, sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443
Neale Ranns311124e2019-01-24 04:52:25 -0800444 vlib_cli_output (vm, "SPD Bindings:");
Neale Ranns8d7c5022019-02-06 01:41:05 -0800445
Neale Rannsb294f102019-04-03 13:17:50 +0000446 /* *INDENT-OFF* */
Neale Ranns311124e2019-01-24 04:52:25 -0800447 hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
Neale Ranns8d7c5022019-02-06 01:41:05 -0800448 vlib_cli_output (vm, " %d -> %U", spd_id,
449 format_vnet_sw_if_index_name, im->vnet_main,
450 sw_if_index);
Neale Ranns311124e2019-01-24 04:52:25 -0800451 }));
452 /* *INDENT-ON* */
Neale Rannsb294f102019-04-03 13:17:50 +0000453}
Neale Ranns311124e2019-01-24 04:52:25 -0800454
Neale Rannsb294f102019-04-03 13:17:50 +0000455static void
456ipsec_tunnel_show_all (vlib_main_t * vm, ipsec_main_t * im)
457{
458 u32 ti;
459
460 vlib_cli_output (vm, "Tunnel interfaces");
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700461 /* *INDENT-OFF* */
Neale Rannsb294f102019-04-03 13:17:50 +0000462 pool_foreach_index (ti, im->tunnel_interfaces, ({
463 vlib_cli_output(vm, " %U", format_ipsec_tunnel, ti);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700465 /* *INDENT-ON* */
Neale Rannsb294f102019-04-03 13:17:50 +0000466}
467
468static clib_error_t *
469show_ipsec_command_fn (vlib_main_t * vm,
470 unformat_input_t * input, vlib_cli_command_t * cmd)
471{
472 ipsec_main_t *im = &ipsec_main;
473
474 ipsec_sa_show_all (vm, im);
475 ipsec_spd_show_all (vm, im);
476 ipsec_spd_bindings_show_all (vm, im);
477 ipsec_tunnel_show_all (vm, im);
478
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479 return 0;
480}
481
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700482/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700483VLIB_CLI_COMMAND (show_ipsec_command, static) = {
Neale Rannsb294f102019-04-03 13:17:50 +0000484 .path = "show ipsec all",
485 .short_help = "show ipsec all",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700486 .function = show_ipsec_command_fn,
487};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700488/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700489
490static clib_error_t *
Neale Rannsb294f102019-04-03 13:17:50 +0000491show_ipsec_sa_command_fn (vlib_main_t * vm,
492 unformat_input_t * input, vlib_cli_command_t * cmd)
493{
494 ipsec_main_t *im = &ipsec_main;
495 u32 sai = ~0;
496
497 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
498 {
499 if (unformat (input, "%u", &sai))
500 ;
501 else
502 break;
503 }
504
505 if (~0 == sai)
506 ipsec_sa_show_all (vm, im);
507 else
508 vlib_cli_output (vm, "%U", format_ipsec_sa, sai, IPSEC_FORMAT_DETAIL);
509
510 return 0;
511}
512
513/* *INDENT-OFF* */
514VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
515 .path = "show ipsec sa",
516 .short_help = "show ipsec sa [index]",
517 .function = show_ipsec_sa_command_fn,
518};
519/* *INDENT-ON* */
520
521static clib_error_t *
522show_ipsec_spd_command_fn (vlib_main_t * vm,
523 unformat_input_t * input, vlib_cli_command_t * cmd)
524{
525 ipsec_main_t *im = &ipsec_main;
526 u8 show_bindings = 0;
527 u32 spdi = ~0;
528
529 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
530 {
531 if (unformat (input, "%u", &spdi))
532 ;
533 else if (unformat (input, "bindings"))
534 show_bindings = 1;
535 else
536 break;
537 }
538
539 if (show_bindings)
540 ipsec_spd_bindings_show_all (vm, im);
541 else if (~0 != spdi)
542 vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
543 else
544 ipsec_spd_show_all (vm, im);
545
546 return 0;
547}
548
549/* *INDENT-OFF* */
550VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
551 .path = "show ipsec spd",
552 .short_help = "show ipsec spd [index]",
553 .function = show_ipsec_spd_command_fn,
554};
555/* *INDENT-ON* */
556
557static clib_error_t *
558show_ipsec_tunnel_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 ti = ~0;
564
565 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
566 {
567 if (unformat (input, "%u", &ti))
568 ;
569 else
570 break;
571 }
572
573 if (~0 != ti)
574 vlib_cli_output (vm, "%U", format_ipsec_tunnel, ti);
575 else
576 ipsec_tunnel_show_all (vm, im);
577
578 return 0;
579}
580
581/* *INDENT-OFF* */
582VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
583 .path = "show ipsec tunnel",
584 .short_help = "show ipsec tunnel [index]",
585 .function = show_ipsec_tunnel_command_fn,
586};
587/* *INDENT-ON* */
588
589static clib_error_t *
Klement Sekerab4d30532018-11-08 13:00:02 +0100590ipsec_show_backends_command_fn (vlib_main_t * vm,
591 unformat_input_t * input,
592 vlib_cli_command_t * cmd)
593{
594 ipsec_main_t *im = &ipsec_main;
595 u32 verbose = 0;
596
597 (void) unformat (input, "verbose %u", &verbose);
598
599 vlib_cli_output (vm, "IPsec AH backends available:");
600 u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
601 ipsec_ah_backend_t *ab;
602 /* *INDENT-OFF* */
603 pool_foreach (ab, im->ah_backends, {
604 s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
605 ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
606 if (verbose) {
607 vlib_node_t *n;
608 n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
609 s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
610 n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
611 s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
612 n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
613 s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
614 n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
615 s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
616 }
617 });
618 /* *INDENT-ON* */
619 vlib_cli_output (vm, "%v", s);
620 _vec_len (s) = 0;
621 vlib_cli_output (vm, "IPsec ESP backends available:");
622 s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
623 ipsec_esp_backend_t *eb;
624 /* *INDENT-OFF* */
625 pool_foreach (eb, im->esp_backends, {
626 s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
627 eb - im->esp_backends == im->esp_current_backend ? "yes"
628 : "no");
629 if (verbose) {
630 vlib_node_t *n;
631 n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
632 s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
633 n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
634 s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
635 n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
636 s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
637 n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
638 s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
639 }
640 });
641 /* *INDENT-ON* */
642 vlib_cli_output (vm, "%v", s);
643
644 vec_free (s);
645 return 0;
646}
647
648/* *INDENT-OFF* */
649VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
650 .path = "show ipsec backends",
651 .short_help = "show ipsec backends",
652 .function = ipsec_show_backends_command_fn,
653};
654/* *INDENT-ON* */
655
656static clib_error_t *
657ipsec_select_backend_command_fn (vlib_main_t * vm,
658 unformat_input_t * input,
659 vlib_cli_command_t * cmd)
660{
661 u32 backend_index;
662 ipsec_main_t *im = &ipsec_main;
663
664 if (pool_elts (im->sad) > 0)
665 {
666 return clib_error_return (0,
667 "Cannot change IPsec backend, while %u SA entries are configured",
668 pool_elts (im->sad));
669 }
670
671 unformat_input_t _line_input, *line_input = &_line_input;
672 /* Get a line of input. */
673 if (!unformat_user (input, unformat_line_input, line_input))
674 return 0;
675
676 if (unformat (line_input, "ah"))
677 {
678 if (unformat (line_input, "%u", &backend_index))
679 {
680 if (ipsec_select_ah_backend (im, backend_index) < 0)
681 {
682 return clib_error_return (0, "Invalid AH backend index `%u'",
683 backend_index);
684 }
685 }
686 else
687 {
688 return clib_error_return (0, "Invalid backend index `%U'",
689 format_unformat_error, line_input);
690 }
691 }
692 else if (unformat (line_input, "esp"))
693 {
694 if (unformat (line_input, "%u", &backend_index))
695 {
696 if (ipsec_select_esp_backend (im, backend_index) < 0)
697 {
698 return clib_error_return (0, "Invalid ESP backend index `%u'",
699 backend_index);
700 }
701 }
702 else
703 {
704 return clib_error_return (0, "Invalid backend index `%U'",
705 format_unformat_error, line_input);
706 }
707 }
708 else
709 {
710 return clib_error_return (0, "Unknown input `%U'",
711 format_unformat_error, line_input);
712 }
713
714 return 0;
715}
716
717/* *INDENT-OFF* */
718VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
719 .path = "ipsec select backend",
720 .short_help = "ipsec select backend <ah|esp> <backend index>",
721 .function = ipsec_select_backend_command_fn,
722};
723
724/* *INDENT-ON* */
725
726static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -0700727clear_ipsec_counters_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700728 unformat_input_t * input,
729 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700730{
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800731 vlib_clear_combined_counters (&ipsec_spd_policy_counters);
Neale Rannseba31ec2019-02-17 18:04:27 +0000732 vlib_clear_combined_counters (&ipsec_sa_counters);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700733
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800734 return (NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700735}
736
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700737/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700738VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
739 .path = "clear ipsec counters",
740 .short_help = "clear ipsec counters",
741 .function = clear_ipsec_counters_command_fn,
742};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700743/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700744
745static clib_error_t *
746create_ipsec_tunnel_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700747 unformat_input_t * input,
748 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700750 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751 ipsec_add_del_tunnel_args_t a;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700752 int rv;
753 u32 num_m_args = 0;
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500754 u8 ipv4_set = 0;
755 u8 ipv6_set = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500756 clib_error_t *error = NULL;
Kingwel Xied3d12052019-03-07 06:34:30 -0500757 ipsec_key_t rck = { 0 };
758 ipsec_key_t lck = { 0 };
759 ipsec_key_t lik = { 0 };
760 ipsec_key_t rik = { 0 };
Matthew Smith2838a232016-06-21 16:05:09 -0500761
Dave Barachb7b92992018-10-17 10:38:51 -0400762 clib_memset (&a, 0, sizeof (a));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700763 a.is_add = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700764
765 /* Get a line of input. */
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700766 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767 return 0;
768
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700769 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
770 {
771 if (unformat
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500772 (line_input, "local-ip %U", unformat_ip46_address, &a.local_ip,
773 IP46_TYPE_ANY))
774 {
775 ip46_address_is_ip4 (&a.local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
776 num_m_args++;
777 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700778 else
779 if (unformat
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500780 (line_input, "remote-ip %U", unformat_ip46_address, &a.remote_ip,
781 IP46_TYPE_ANY))
782 {
783 ip46_address_is_ip4 (&a.remote_ip) ? (ipv4_set = 1) : (ipv6_set =
784 1);
785 num_m_args++;
786 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700787 else if (unformat (line_input, "local-spi %u", &a.local_spi))
788 num_m_args++;
789 else if (unformat (line_input, "remote-spi %u", &a.remote_spi))
790 num_m_args++;
Matthew Smith8e1039a2018-04-12 07:32:56 -0500791 else if (unformat (line_input, "instance %u", &a.show_instance))
792 a.renumber = 1;
Radu Nicolau717de092018-08-03 10:37:24 +0100793 else if (unformat (line_input, "udp-encap"))
794 a.udp_encap = 1;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800795 else if (unformat (line_input, "use-esn"))
796 a.esn = 1;
797 else if (unformat (line_input, "use-anti-replay"))
798 a.anti_replay = 1;
Pierre Pfister4c422f92018-12-10 11:19:08 +0100799 else if (unformat (line_input, "tx-table %u", &a.tx_table_id))
800 ;
Neale Rannsfd060842019-03-04 13:44:42 +0000801 else
802 if (unformat
803 (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
804 ;
805 else
806 if (unformat
807 (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
808 ;
809 else if (unformat (line_input, "crypto-alg %U",
810 unformat_ipsec_crypto_alg, &a.crypto_alg))
811 ;
812 else
813 if (unformat
814 (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
815 ;
816 else
817 if (unformat
818 (line_input, "rmote-integ-key %U", unformat_ipsec_key, &rik))
819 ;
820 else if (unformat (line_input, "integ-alg %U",
821 unformat_ipsec_integ_alg, &a.integ_alg))
822 ;
Kingwel Xie2baf9422019-02-04 02:07:06 -0800823 else if (unformat (line_input, "del"))
824 a.is_add = 0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700825 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500826 {
827 error = clib_error_return (0, "unknown input `%U'",
828 format_unformat_error, line_input);
829 goto done;
830 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700831 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700832
833 if (num_m_args < 4)
Billy McFalla9a20e72017-02-15 11:39:12 -0500834 {
835 error = clib_error_return (0, "mandatory argument(s) missing");
836 goto done;
837 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700838
Kingwel Xie72b3bce2019-02-12 06:45:08 -0500839 if (ipv4_set && ipv6_set)
840 return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
841
Kingwel Xie1ba5bc82019-03-20 07:21:58 -0400842 a.is_ip6 = ipv6_set;
843
Neale Rannsfd060842019-03-04 13:44:42 +0000844 clib_memcpy (a.local_crypto_key, lck.data, lck.len);
845 a.local_crypto_key_len = lck.len;
846 clib_memcpy (a.remote_crypto_key, rck.data, rck.len);
847 a.remote_crypto_key_len = rck.len;
848
Kingwel Xied3d12052019-03-07 06:34:30 -0500849 clib_memcpy (a.local_integ_key, lik.data, lik.len);
Neale Rannsfd060842019-03-04 13:44:42 +0000850 a.local_integ_key_len = lck.len;
Kingwel Xied3d12052019-03-07 06:34:30 -0500851 clib_memcpy (a.remote_integ_key, rik.data, rik.len);
Neale Rannsfd060842019-03-04 13:44:42 +0000852 a.remote_integ_key_len = rck.len;
853
Matthew Smith2838a232016-06-21 16:05:09 -0500854 rv = ipsec_add_del_tunnel_if (&a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700855
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700856 switch (rv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700857 {
858 case 0:
859 break;
860 case VNET_API_ERROR_INVALID_VALUE:
861 if (a.is_add)
Billy McFalla9a20e72017-02-15 11:39:12 -0500862 error = clib_error_return (0,
863 "IPSec tunnel interface already exists...");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700864 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500865 error = clib_error_return (0, "IPSec tunnel interface not exists...");
866 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700867 default:
Billy McFalla9a20e72017-02-15 11:39:12 -0500868 error = clib_error_return (0, "ipsec_register_interface returned %d",
869 rv);
870 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700871 }
872
Billy McFalla9a20e72017-02-15 11:39:12 -0500873done:
874 unformat_free (line_input);
875
876 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700877}
878
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700879/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700880VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
881 .path = "create ipsec tunnel",
Pierre Pfister4c422f92018-12-10 11:19:08 +0100882 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
Kingwel Xie2baf9422019-02-04 02:07:06 -0800883 "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
Pierre Pfister4c422f92018-12-10 11:19:08 +0100884 "[tx-table <table-id>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700885 .function = create_ipsec_tunnel_command_fn,
886};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700887/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700888
889static clib_error_t *
890set_interface_key_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700891 unformat_input_t * input,
892 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700893{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700894 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700895 ipsec_main_t *im = &ipsec_main;
896 ipsec_if_set_key_type_t type = IPSEC_IF_SET_KEY_TYPE_NONE;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700897 u32 hw_if_index = (u32) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700898 u32 alg;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700899 u8 *key = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500900 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700901
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700902 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700903 return 0;
904
905 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
906 {
907 if (unformat (line_input, "%U",
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700908 unformat_vnet_hw_interface, im->vnet_main, &hw_if_index))
909 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700910 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700911 if (unformat
912 (line_input, "local crypto %U", unformat_ipsec_crypto_alg, &alg))
913 type = IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO;
914 else
915 if (unformat
916 (line_input, "remote crypto %U", unformat_ipsec_crypto_alg, &alg))
917 type = IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO;
918 else
919 if (unformat
920 (line_input, "local integ %U", unformat_ipsec_integ_alg, &alg))
921 type = IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG;
922 else
923 if (unformat
924 (line_input, "remote integ %U", unformat_ipsec_integ_alg, &alg))
925 type = IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG;
926 else if (unformat (line_input, "%U", unformat_hex_string, &key))
927 ;
928 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500929 {
930 error = clib_error_return (0, "parse error: '%U'",
931 format_unformat_error, line_input);
932 goto done;
933 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700934 }
935
Ed Warnickecb9cada2015-12-08 15:45:58 -0700936 if (type == IPSEC_IF_SET_KEY_TYPE_NONE)
Billy McFalla9a20e72017-02-15 11:39:12 -0500937 {
938 error = clib_error_return (0, "unknown key type");
939 goto done;
940 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700941
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700942 if (alg > 0 && vec_len (key) == 0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500943 {
944 error = clib_error_return (0, "key is not specified");
945 goto done;
946 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700947
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700948 if (hw_if_index == (u32) ~ 0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500949 {
950 error = clib_error_return (0, "interface not specified");
951 goto done;
952 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700953
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700954 ipsec_set_interface_key (im->vnet_main, hw_if_index, type, alg, key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700955
Billy McFalla9a20e72017-02-15 11:39:12 -0500956done:
957 vec_free (key);
958 unformat_free (line_input);
959
960 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700961}
962
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700963/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700964VLIB_CLI_COMMAND (set_interface_key_command, static) = {
965 .path = "set interface ipsec key",
966 .short_help =
967 "set interface ipsec key <int> <local|remote> <crypto|integ> <key type> <key>",
968 .function = set_interface_key_command_fn,
969};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700970/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971
Ed Warnickecb9cada2015-12-08 15:45:58 -0700972clib_error_t *
973ipsec_cli_init (vlib_main_t * vm)
974{
975 return 0;
976}
977
978VLIB_INIT_FUNCTION (ipsec_cli_init);
979
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700980
981/*
982 * fd.io coding-style-patch-verification: ON
983 *
984 * Local Variables:
985 * eval: (c-set-style "gnu")
986 * End:
987 */