blob: 9cbd2493ed4ff7a7db40fb561577e94467989236 [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>
22
23#include <vnet/ipsec/ipsec.h>
24
25static clib_error_t *
26set_interface_spd_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070027 unformat_input_t * input,
28 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -070029{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070030 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -070031 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070032 u32 sw_if_index = (u32) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070033 u32 spd_id;
34 int is_add = 1;
Billy McFalla9a20e72017-02-15 11:39:12 -050035 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -070036
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070037 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -070038 return 0;
39
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070040 if (unformat
41 (line_input, "%U %u", unformat_vnet_sw_interface, im->vnet_main,
42 &sw_if_index, &spd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -070043 ;
44 else if (unformat (line_input, "del"))
45 is_add = 0;
46 else
Billy McFalla9a20e72017-02-15 11:39:12 -050047 {
48 error = clib_error_return (0, "parse error: '%U'",
49 format_unformat_error, line_input);
50 goto done;
51 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070052
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070053 ipsec_set_interface_spd (vm, sw_if_index, spd_id, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -070054
Billy McFalla9a20e72017-02-15 11:39:12 -050055done:
56 unformat_free (line_input);
57
58 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -070059}
60
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070061/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070062VLIB_CLI_COMMAND (set_interface_spd_command, static) = {
63 .path = "set interface ipsec spd",
64 .short_help =
65 "set interface ipsec spd <int> <id>",
66 .function = set_interface_spd_command_fn,
67};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070068/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -070069
70static clib_error_t *
71ipsec_sa_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070072 unformat_input_t * input,
73 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -070074{
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +000075 ipsec_main_t *im = &ipsec_main;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070076 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -070077 ipsec_sa_t sa;
78 int is_add = ~0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070079 u8 *ck = 0, *ik = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -050080 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -070081
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070082 memset (&sa, 0, sizeof (sa));
Ed Warnickecb9cada2015-12-08 15:45:58 -070083
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070084 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -070085 return 0;
86
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -070087 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
88 {
89 if (unformat (line_input, "add %u", &sa.id))
90 is_add = 1;
91 else if (unformat (line_input, "del %u", &sa.id))
92 is_add = 0;
93 else if (unformat (line_input, "spi %u", &sa.spi))
94 ;
95 else if (unformat (line_input, "esp"))
96 sa.protocol = IPSEC_PROTOCOL_ESP;
97 else if (unformat (line_input, "ah"))
Billy McFalla9a20e72017-02-15 11:39:12 -050098 {
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080099 sa.protocol = IPSEC_PROTOCOL_AH;
Billy McFalla9a20e72017-02-15 11:39:12 -0500100 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700101 else
102 if (unformat (line_input, "crypto-key %U", unformat_hex_string, &ck))
103 sa.crypto_key_len = vec_len (ck);
104 else
105 if (unformat
106 (line_input, "crypto-alg %U", unformat_ipsec_crypto_alg,
107 &sa.crypto_alg))
108 {
Radu Nicolau89c27812018-05-04 12:51:53 +0100109 if (sa.crypto_alg < IPSEC_CRYPTO_ALG_NONE ||
Radu Nicolau6929ea92016-11-29 11:00:30 +0000110 sa.crypto_alg >= IPSEC_CRYPTO_N_ALG)
Billy McFalla9a20e72017-02-15 11:39:12 -0500111 {
112 error = clib_error_return (0, "unsupported crypto-alg: '%U'",
113 format_ipsec_crypto_alg,
114 sa.crypto_alg);
115 goto done;
116 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700117 }
118 else
119 if (unformat (line_input, "integ-key %U", unformat_hex_string, &ik))
120 sa.integ_key_len = vec_len (ik);
121 else if (unformat (line_input, "integ-alg %U", unformat_ipsec_integ_alg,
122 &sa.integ_alg))
123 {
Radu Nicolau89c27812018-05-04 12:51:53 +0100124 if (sa.integ_alg < IPSEC_INTEG_ALG_NONE ||
Radu Nicolau6929ea92016-11-29 11:00:30 +0000125 sa.integ_alg >= IPSEC_INTEG_N_ALG)
Billy McFalla9a20e72017-02-15 11:39:12 -0500126 {
127 error = clib_error_return (0, "unsupported integ-alg: '%U'",
128 format_ipsec_integ_alg,
129 sa.integ_alg);
130 goto done;
131 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700132 }
133 else if (unformat (line_input, "tunnel-src %U",
134 unformat_ip4_address, &sa.tunnel_src_addr.ip4))
135 sa.is_tunnel = 1;
136 else if (unformat (line_input, "tunnel-dst %U",
137 unformat_ip4_address, &sa.tunnel_dst_addr.ip4))
138 sa.is_tunnel = 1;
139 else if (unformat (line_input, "tunnel-src %U",
140 unformat_ip6_address, &sa.tunnel_src_addr.ip6))
141 {
142 sa.is_tunnel = 1;
143 sa.is_tunnel_ip6 = 1;
144 }
145 else if (unformat (line_input, "tunnel-dst %U",
146 unformat_ip6_address, &sa.tunnel_dst_addr.ip6))
147 {
148 sa.is_tunnel = 1;
149 sa.is_tunnel_ip6 = 1;
150 }
Radu Nicolau717de092018-08-03 10:37:24 +0100151 else if (unformat (line_input, "udp-encap"))
152 {
153 sa.udp_encap = 1;
154 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700155 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500156 {
157 error = clib_error_return (0, "parse error: '%U'",
158 format_unformat_error, line_input);
159 goto done;
160 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700161 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700163 if (sa.crypto_key_len > sizeof (sa.crypto_key))
164 sa.crypto_key_len = sizeof (sa.crypto_key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700166 if (sa.integ_key_len > sizeof (sa.integ_key))
167 sa.integ_key_len = sizeof (sa.integ_key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168
169 if (ck)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700170 strncpy ((char *) sa.crypto_key, (char *) ck, sa.crypto_key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171
172 if (ik)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700173 strncpy ((char *) sa.integ_key, (char *) ik, sa.integ_key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000175 if (is_add)
176 {
177 ASSERT (im->cb.check_support_cb);
Billy McFalla9a20e72017-02-15 11:39:12 -0500178 error = im->cb.check_support_cb (&sa);
179 if (error)
180 goto done;
Sergio Gonzalez Monroyd04b60b2017-01-20 15:35:23 +0000181 }
182
Radu Nicolau717de092018-08-03 10:37:24 +0100183 ipsec_add_del_sa (vm, &sa, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184
Billy McFalla9a20e72017-02-15 11:39:12 -0500185done:
186 unformat_free (line_input);
187
188 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189}
190
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700191/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
193 .path = "ipsec sa",
194 .short_help =
195 "ipsec sa [add|del]",
196 .function = ipsec_sa_add_del_command_fn,
197};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700198/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199
200static clib_error_t *
201ipsec_spd_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700202 unformat_input_t * input,
203 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700205 unformat_input_t _line_input, *line_input = &_line_input;
Damjan Marion3f54b182016-08-16 11:27:02 +0200206 u32 spd_id = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207 int is_add = ~0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500208 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700210 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211 return 0;
212
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700213 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
214 {
215 if (unformat (line_input, "add"))
216 is_add = 1;
217 else if (unformat (line_input, "del"))
218 is_add = 0;
219 else if (unformat (line_input, "%u", &spd_id))
220 ;
221 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500222 {
223 error = clib_error_return (0, "parse error: '%U'",
224 format_unformat_error, line_input);
225 goto done;
226 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700227 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228
Damjan Marion3f54b182016-08-16 11:27:02 +0200229 if (spd_id == ~0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500230 {
231 error = clib_error_return (0, "please specify SPD ID");
232 goto done;
233 }
Damjan Marion3f54b182016-08-16 11:27:02 +0200234
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700235 ipsec_add_del_spd (vm, spd_id, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236
Billy McFalla9a20e72017-02-15 11:39:12 -0500237done:
238 unformat_free (line_input);
239
240 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241}
242
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700243/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
245 .path = "ipsec spd",
246 .short_help =
247 "ipsec spd [add|del] <id>",
248 .function = ipsec_spd_add_del_command_fn,
249};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700250/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251
252
253static clib_error_t *
254ipsec_policy_add_del_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700255 unformat_input_t * input,
256 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700258 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259 ipsec_policy_t p;
260 int is_add = 0;
261 int is_ip_any = 1;
262 u32 tmp, tmp2;
Billy McFalla9a20e72017-02-15 11:39:12 -0500263 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700265 memset (&p, 0, sizeof (p));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266 p.lport.stop = p.rport.stop = ~0;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700267 p.laddr.stop.ip4.as_u32 = p.raddr.stop.ip4.as_u32 = (u32) ~ 0;
268 p.laddr.stop.ip6.as_u64[0] = p.laddr.stop.ip6.as_u64[1] = (u64) ~ 0;
269 p.raddr.stop.ip6.as_u64[0] = p.raddr.stop.ip6.as_u64[1] = (u64) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700271 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272 return 0;
273
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700274 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
275 {
276 if (unformat (line_input, "add"))
277 is_add = 1;
278 else if (unformat (line_input, "del"))
279 is_add = 0;
280 else if (unformat (line_input, "spd %u", &p.id))
281 ;
282 else if (unformat (line_input, "inbound"))
283 p.is_outbound = 0;
284 else if (unformat (line_input, "outbound"))
285 p.is_outbound = 1;
286 else if (unformat (line_input, "priority %d", &p.priority))
287 ;
288 else if (unformat (line_input, "protocol %u", &tmp))
289 p.protocol = (u8) tmp;
290 else
291 if (unformat
292 (line_input, "action %U", unformat_ipsec_policy_action,
293 &p.policy))
294 {
295 if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
Billy McFalla9a20e72017-02-15 11:39:12 -0500296 {
297 error = clib_error_return (0, "unsupported action: 'resolve'");
298 goto done;
299 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700300 }
301 else if (unformat (line_input, "sa %u", &p.sa_id))
302 ;
303 else if (unformat (line_input, "local-ip-range %U - %U",
304 unformat_ip4_address, &p.laddr.start.ip4,
305 unformat_ip4_address, &p.laddr.stop.ip4))
306 is_ip_any = 0;
307 else if (unformat (line_input, "remote-ip-range %U - %U",
308 unformat_ip4_address, &p.raddr.start.ip4,
309 unformat_ip4_address, &p.raddr.stop.ip4))
310 is_ip_any = 0;
311 else if (unformat (line_input, "local-ip-range %U - %U",
312 unformat_ip6_address, &p.laddr.start.ip6,
313 unformat_ip6_address, &p.laddr.stop.ip6))
314 {
315 p.is_ipv6 = 1;
316 is_ip_any = 0;
317 }
318 else if (unformat (line_input, "remote-ip-range %U - %U",
319 unformat_ip6_address, &p.raddr.start.ip6,
320 unformat_ip6_address, &p.raddr.stop.ip6))
321 {
322 p.is_ipv6 = 1;
323 is_ip_any = 0;
324 }
325 else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
326 {
327 p.lport.start = tmp;
328 p.lport.stop = tmp2;
329 }
330 else
331 if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
332 {
333 p.rport.start = tmp;
334 p.rport.stop = tmp2;
335 }
336 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500337 {
338 error = clib_error_return (0, "parse error: '%U'",
339 format_unformat_error, line_input);
340 goto done;
341 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700342 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800344 /* Check if SA is for IPv6/AH which is not supported. Return error if TRUE. */
345 if (p.sa_id)
346 {
347 uword *p1;
348 ipsec_main_t *im = &ipsec_main;
349 ipsec_sa_t *sa = 0;
350 p1 = hash_get (im->sa_index_by_sa_id, p.sa_id);
Klement Sekerac2fc57e2018-06-28 14:20:12 +0200351 if (!p1)
352 {
353 error =
354 clib_error_return (0, "SA with index %u not found", p.sa_id);
355 goto done;
356 }
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800357 sa = pool_elt_at_index (im->sad, p1[0]);
358 if (sa && sa->protocol == IPSEC_PROTOCOL_AH && is_add && p.is_ipv6)
359 {
360 error = clib_error_return (0, "AH not supported for IPV6: '%U'",
361 format_unformat_error, line_input);
362 goto done;
363 }
364 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700365 ipsec_add_del_policy (vm, &p, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700366 if (is_ip_any)
367 {
368 p.is_ipv6 = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700369 ipsec_add_del_policy (vm, &p, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370 }
Billy McFalla9a20e72017-02-15 11:39:12 -0500371
372done:
373 unformat_free (line_input);
374
375 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700376}
377
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700378/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
380 .path = "ipsec policy",
381 .short_help =
382 "ipsec policy [add|del] spd <id> priority <n> ",
383 .function = ipsec_policy_add_del_command_fn,
384};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700385/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386
387static clib_error_t *
388set_ipsec_sa_key_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700389 unformat_input_t * input,
390 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700392 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393 ipsec_sa_t sa;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700394 u8 *ck = 0, *ik = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500395 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700397 memset (&sa, 0, sizeof (sa));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700399 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700400 return 0;
401
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700402 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
403 {
404 if (unformat (line_input, "%u", &sa.id))
405 ;
406 else
407 if (unformat (line_input, "crypto-key %U", unformat_hex_string, &ck))
408 sa.crypto_key_len = vec_len (ck);
409 else
410 if (unformat (line_input, "integ-key %U", unformat_hex_string, &ik))
411 sa.integ_key_len = vec_len (ik);
412 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500413 {
414 error = clib_error_return (0, "parse error: '%U'",
415 format_unformat_error, line_input);
416 goto done;
417 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700418 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700420 if (sa.crypto_key_len > sizeof (sa.crypto_key))
421 sa.crypto_key_len = sizeof (sa.crypto_key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700423 if (sa.integ_key_len > sizeof (sa.integ_key))
424 sa.integ_key_len = sizeof (sa.integ_key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425
426 if (ck)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700427 strncpy ((char *) sa.crypto_key, (char *) ck, sa.crypto_key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428
429 if (ik)
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700430 strncpy ((char *) sa.integ_key, (char *) ik, sa.integ_key_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700432 ipsec_set_sa_key (vm, &sa);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433
Billy McFalla9a20e72017-02-15 11:39:12 -0500434done:
435 unformat_free (line_input);
436
437 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438}
439
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700440/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441VLIB_CLI_COMMAND (set_ipsec_sa_key_command, static) = {
442 .path = "set ipsec sa",
443 .short_help =
444 "set ipsec sa <id> crypto-key <key> integ-key <key>",
445 .function = set_ipsec_sa_key_command_fn,
446};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700447/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448
449static clib_error_t *
450show_ipsec_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700451 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700453 ipsec_spd_t *spd;
454 ipsec_sa_t *sa;
455 ipsec_policy_t *p;
456 ipsec_main_t *im = &ipsec_main;
457 u32 *i;
458 ipsec_tunnel_if_t *t;
459 vnet_hw_interface_t *hi;
Klement Sekeraea841302018-05-11 12:59:05 +0200460 u8 *protocol = NULL;
461 u8 *policy = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700463 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464 pool_foreach (sa, im->sad, ({
465 if (sa->id) {
Klement Sekera4b089f22018-04-17 18:04:57 +0200466 vlib_cli_output(vm, "sa %u spi %u mode %s protocol %s%s", sa->id, sa->spi,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467 sa->is_tunnel ? "tunnel" : "transport",
Klement Sekera4b089f22018-04-17 18:04:57 +0200468 sa->protocol ? "esp" : "ah",
469 sa->udp_encap ? " udp-encap-enabled" : "");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700470 if (sa->protocol == IPSEC_PROTOCOL_ESP) {
471 vlib_cli_output(vm, " crypto alg %U%s%U integrity alg %U%s%U",
472 format_ipsec_crypto_alg, sa->crypto_alg,
473 sa->crypto_alg ? " key " : "",
474 format_hex_bytes, sa->crypto_key, sa->crypto_key_len,
475 format_ipsec_integ_alg, sa->integ_alg,
476 sa->integ_alg ? " key " : "",
477 format_hex_bytes, sa->integ_key, sa->integ_key_len);
478 }
479 if (sa->is_tunnel && sa->is_tunnel_ip6) {
480 vlib_cli_output(vm, " tunnel src %U dst %U",
481 format_ip6_address, &sa->tunnel_src_addr.ip6,
482 format_ip6_address, &sa->tunnel_dst_addr.ip6);
483 } else if (sa->is_tunnel) {
484 vlib_cli_output(vm, " tunnel src %U dst %U",
485 format_ip4_address, &sa->tunnel_src_addr.ip4,
486 format_ip4_address, &sa->tunnel_dst_addr.ip4);
487 }
488 }
489 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700490 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700491
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700492 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493 pool_foreach (spd, im->spds, ({
494 vlib_cli_output(vm, "spd %u", spd->id);
495
496 vlib_cli_output(vm, " outbound policies");
497 vec_foreach(i, spd->ipv4_outbound_policies)
498 {
499 p = pool_elt_at_index(spd->policies, *i);
Klement Sekeraea841302018-05-11 12:59:05 +0200500 vec_reset_length(protocol);
501 vec_reset_length(policy);
502 if (p->protocol) {
503 protocol = format(protocol, "%U", format_ip_protocol, p->protocol);
504 } else {
505 protocol = format(protocol, "any");
506 }
507 if (p->policy == IPSEC_POLICY_ACTION_PROTECT) {
508 policy = format(policy, " sa %u", p->sa_id);
509 }
510
511 vlib_cli_output(vm, " priority %d action %U protocol %v%v",
512 p->priority, format_ipsec_policy_action, p->policy,
513 protocol, policy);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514 vlib_cli_output(vm, " local addr range %U - %U port range %u - %u",
515 format_ip4_address, &p->laddr.start.ip4,
516 format_ip4_address, &p->laddr.stop.ip4,
517 p->lport.start, p->lport.stop);
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700518 vlib_cli_output(vm, " remote addr range %U - %U port range %u - %u",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519 format_ip4_address, &p->raddr.start.ip4,
520 format_ip4_address, &p->raddr.stop.ip4,
521 p->rport.start, p->rport.stop);
522 vlib_cli_output(vm, " packets %u bytes %u", p->counter.packets,
523 p->counter.bytes);
524 };
525 vec_foreach(i, spd->ipv6_outbound_policies)
526 {
527 p = pool_elt_at_index(spd->policies, *i);
Klement Sekeraea841302018-05-11 12:59:05 +0200528 vec_reset_length(protocol);
529 vec_reset_length(policy);
530 if (p->protocol) {
531 protocol = format(protocol, "%U", format_ip_protocol, p->protocol);
532 } else {
533 protocol = format(protocol, "any");
534 }
535 if (p->policy == IPSEC_POLICY_ACTION_PROTECT) {
536 policy = format(policy, " sa %u", p->sa_id);
537 }
538 vlib_cli_output(vm, " priority %d action %U protocol %v%v",
539 p->priority, format_ipsec_policy_action, p->policy,
540 protocol, policy);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541 vlib_cli_output(vm, " local addr range %U - %U port range %u - %u",
542 format_ip6_address, &p->laddr.start.ip6,
543 format_ip6_address, &p->laddr.stop.ip6,
544 p->lport.start, p->lport.stop);
545 vlib_cli_output(vm, " remote addr range %U - %U port range %u - %u",
546 format_ip6_address, &p->raddr.start.ip6,
547 format_ip6_address, &p->raddr.stop.ip6,
548 p->rport.start, p->rport.stop);
549 vlib_cli_output(vm, " packets %u bytes %u", p->counter.packets,
550 p->counter.bytes);
551 };
552 vlib_cli_output(vm, " inbound policies");
553 vec_foreach(i, spd->ipv4_inbound_protect_policy_indices)
554 {
555 p = pool_elt_at_index(spd->policies, *i);
Klement Sekeraea841302018-05-11 12:59:05 +0200556 vec_reset_length(protocol);
557 vec_reset_length(policy);
558 if (p->protocol) {
559 protocol = format(protocol, "%U", format_ip_protocol, p->protocol);
560 } else {
561 protocol = format(protocol, "any");
562 }
563 if (p->policy == IPSEC_POLICY_ACTION_PROTECT) {
564 policy = format(policy, " sa %u", p->sa_id);
565 }
566 vlib_cli_output(vm, " priority %d action %U protocol %v%v",
567 p->priority, format_ipsec_policy_action, p->policy,
568 protocol, policy);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569 vlib_cli_output(vm, " local addr range %U - %U port range %u - %u",
570 format_ip4_address, &p->laddr.start.ip4,
571 format_ip4_address, &p->laddr.stop.ip4,
572 p->lport.start, p->lport.stop);
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700573 vlib_cli_output(vm, " remote addr range %U - %U port range %u - %u",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700574 format_ip4_address, &p->raddr.start.ip4,
575 format_ip4_address, &p->raddr.stop.ip4,
576 p->rport.start, p->rport.stop);
577 vlib_cli_output(vm, " packets %u bytes %u", p->counter.packets,
578 p->counter.bytes);
579 };
580 vec_foreach(i, spd->ipv4_inbound_policy_discard_and_bypass_indices)
581 {
582 p = pool_elt_at_index(spd->policies, *i);
Klement Sekeraea841302018-05-11 12:59:05 +0200583 vec_reset_length(protocol);
584 vec_reset_length(policy);
585 if (p->protocol) {
586 protocol = format(protocol, "%U", format_ip_protocol, p->protocol);
587 } else {
588 protocol = format(protocol, "any");
589 }
590 if (p->policy == IPSEC_POLICY_ACTION_PROTECT) {
591 policy = format(policy, " sa %u", p->sa_id);
592 }
593 vlib_cli_output(vm, " priority %d action %U protocol %v%v",
594 p->priority, format_ipsec_policy_action, p->policy,
595 protocol, policy);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700596 vlib_cli_output(vm, " local addr range %U - %U port range %u - %u",
597 format_ip4_address, &p->laddr.start.ip4,
598 format_ip4_address, &p->laddr.stop.ip4,
599 p->lport.start, p->lport.stop);
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700600 vlib_cli_output(vm, " remote addr range %U - %U port range %u - %u",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601 format_ip4_address, &p->raddr.start.ip4,
602 format_ip4_address, &p->raddr.stop.ip4,
603 p->rport.start, p->rport.stop);
604 vlib_cli_output(vm, " packets %u bytes %u", p->counter.packets,
605 p->counter.bytes);
606 };
607 vec_foreach(i, spd->ipv6_inbound_protect_policy_indices)
608 {
609 p = pool_elt_at_index(spd->policies, *i);
Klement Sekeraea841302018-05-11 12:59:05 +0200610 vec_reset_length(protocol);
611 vec_reset_length(policy);
612 if (p->protocol) {
613 protocol = format(protocol, "%U", format_ip_protocol, p->protocol);
614 } else {
615 protocol = format(protocol, "any");
616 }
617 if (p->policy == IPSEC_POLICY_ACTION_PROTECT) {
618 policy = format(policy, " sa %u", p->sa_id);
619 }
620 vlib_cli_output(vm, " priority %d action %U protocol %v%v",
621 p->priority, format_ipsec_policy_action, p->policy,
622 protocol, policy);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623 vlib_cli_output(vm, " local addr range %U - %U port range %u - %u",
624 format_ip6_address, &p->laddr.start.ip6,
625 format_ip6_address, &p->laddr.stop.ip6,
626 p->lport.start, p->lport.stop);
627 vlib_cli_output(vm, " remote addr range %U - %U port range %u - %u",
628 format_ip6_address, &p->raddr.start.ip6,
629 format_ip6_address, &p->raddr.stop.ip6,
630 p->rport.start, p->rport.stop);
631 vlib_cli_output(vm, " packets %u bytes %u", p->counter.packets,
632 p->counter.bytes);
633 };
634 vec_foreach(i, spd->ipv6_inbound_policy_discard_and_bypass_indices)
635 {
636 p = pool_elt_at_index(spd->policies, *i);
Klement Sekeraea841302018-05-11 12:59:05 +0200637 vec_reset_length(protocol);
638 vec_reset_length(policy);
639 if (p->protocol) {
640 protocol = format(protocol, "%U", format_ip_protocol, p->protocol);
641 } else {
642 protocol = format(protocol, "any");
643 }
644 if (p->policy == IPSEC_POLICY_ACTION_PROTECT) {
645 policy = format(policy, " sa %u", p->sa_id);
646 }
647 vlib_cli_output(vm, " priority %d action %U protocol %v%v",
648 p->priority, format_ipsec_policy_action, p->policy,
649 protocol, policy);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650 vlib_cli_output(vm, " local addr range %U - %U port range %u - %u",
651 format_ip6_address, &p->laddr.start.ip6,
652 format_ip6_address, &p->laddr.stop.ip6,
653 p->lport.start, p->lport.stop);
654 vlib_cli_output(vm, " remote addr range %U - %U port range %u - %u",
655 format_ip6_address, &p->raddr.start.ip6,
656 format_ip6_address, &p->raddr.stop.ip6,
657 p->rport.start, p->rport.stop);
658 vlib_cli_output(vm, " packets %u bytes %u", p->counter.packets,
659 p->counter.bytes);
660 };
661 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700662 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700663
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700664 vlib_cli_output (vm, "tunnel interfaces");
665 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700666 pool_foreach (t, im->tunnel_interfaces, ({
Matus Fabian694265d2016-08-10 01:55:36 -0700667 if (t->hw_if_index == ~0)
668 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700669 hi = vnet_get_hw_interface (im->vnet_main, t->hw_if_index);
670 vlib_cli_output(vm, " %s seq", hi->name);
671 sa = pool_elt_at_index(im->sad, t->output_sa_index);
Radu Nicolau717de092018-08-03 10:37:24 +0100672 vlib_cli_output(vm, " seq %u seq-hi %u esn %u anti-replay %u udp-encap %u",
673 sa->seq, sa->seq_hi, sa->use_esn, sa->use_anti_replay, sa->udp_encap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700674 vlib_cli_output(vm, " local-spi %u local-ip %U", sa->spi,
675 format_ip4_address, &sa->tunnel_src_addr.ip4);
676 vlib_cli_output(vm, " local-crypto %U %U",
677 format_ipsec_crypto_alg, sa->crypto_alg,
678 format_hex_bytes, sa->crypto_key, sa->crypto_key_len);
679 vlib_cli_output(vm, " local-integrity %U %U",
680 format_ipsec_integ_alg, sa->integ_alg,
681 format_hex_bytes, sa->integ_key, sa->integ_key_len);
682 sa = pool_elt_at_index(im->sad, t->input_sa_index);
683 vlib_cli_output(vm, " last-seq %u last-seq-hi %u esn %u anti-replay %u window %U",
684 sa->last_seq, sa->last_seq_hi, sa->use_esn,
685 sa->use_anti_replay,
686 format_ipsec_replay_window, sa->replay_window);
687 vlib_cli_output(vm, " remote-spi %u remote-ip %U", sa->spi,
688 format_ip4_address, &sa->tunnel_src_addr.ip4);
689 vlib_cli_output(vm, " remote-crypto %U %U",
690 format_ipsec_crypto_alg, sa->crypto_alg,
691 format_hex_bytes, sa->crypto_key, sa->crypto_key_len);
692 vlib_cli_output(vm, " remote-integrity %U %U",
693 format_ipsec_integ_alg, sa->integ_alg,
694 format_hex_bytes, sa->integ_key, sa->integ_key_len);
695 }));
Klement Sekeraea841302018-05-11 12:59:05 +0200696 vec_free(policy);
697 vec_free(protocol);
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700698 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699 return 0;
700}
701
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700702/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700703VLIB_CLI_COMMAND (show_ipsec_command, static) = {
704 .path = "show ipsec",
705 .short_help = "show ipsec",
706 .function = show_ipsec_command_fn,
707};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700708/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700709
710static clib_error_t *
711clear_ipsec_counters_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700712 unformat_input_t * input,
713 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700715 ipsec_main_t *im = &ipsec_main;
716 ipsec_spd_t *spd;
717 ipsec_policy_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700718
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700719 /* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720 pool_foreach (spd, im->spds, ({
721 pool_foreach(p, spd->policies, ({
722 p->counter.packets = p->counter.bytes = 0;
723 }));
724 }));
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700725 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726
727 return 0;
728}
729
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700730/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700731VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
732 .path = "clear ipsec counters",
733 .short_help = "clear ipsec counters",
734 .function = clear_ipsec_counters_command_fn,
735};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700736/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737
738static clib_error_t *
739create_ipsec_tunnel_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700740 unformat_input_t * input,
741 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700742{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700743 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700744 ipsec_add_del_tunnel_args_t a;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745 int rv;
746 u32 num_m_args = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500747 clib_error_t *error = NULL;
Matthew Smith2838a232016-06-21 16:05:09 -0500748
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700749 memset (&a, 0, sizeof (a));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750 a.is_add = 1;
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
759 (line_input, "local-ip %U", unformat_ip4_address, &a.local_ip))
760 num_m_args++;
761 else
762 if (unformat
763 (line_input, "remote-ip %U", unformat_ip4_address, &a.remote_ip))
764 num_m_args++;
765 else if (unformat (line_input, "local-spi %u", &a.local_spi))
766 num_m_args++;
767 else if (unformat (line_input, "remote-spi %u", &a.remote_spi))
768 num_m_args++;
Matthew Smith8e1039a2018-04-12 07:32:56 -0500769 else if (unformat (line_input, "instance %u", &a.show_instance))
770 a.renumber = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700771 else if (unformat (line_input, "del"))
772 a.is_add = 0;
Radu Nicolau717de092018-08-03 10:37:24 +0100773 else if (unformat (line_input, "udp-encap"))
774 a.udp_encap = 1;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700775 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500776 {
777 error = clib_error_return (0, "unknown input `%U'",
778 format_unformat_error, line_input);
779 goto done;
780 }
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700781 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782
783 if (num_m_args < 4)
Billy McFalla9a20e72017-02-15 11:39:12 -0500784 {
785 error = clib_error_return (0, "mandatory argument(s) missing");
786 goto done;
787 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700788
Matthew Smith2838a232016-06-21 16:05:09 -0500789 rv = ipsec_add_del_tunnel_if (&a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700790
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700791 switch (rv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700792 {
793 case 0:
794 break;
795 case VNET_API_ERROR_INVALID_VALUE:
796 if (a.is_add)
Billy McFalla9a20e72017-02-15 11:39:12 -0500797 error = clib_error_return (0,
798 "IPSec tunnel interface already exists...");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500800 error = clib_error_return (0, "IPSec tunnel interface not exists...");
801 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700802 default:
Billy McFalla9a20e72017-02-15 11:39:12 -0500803 error = clib_error_return (0, "ipsec_register_interface returned %d",
804 rv);
805 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700806 }
807
Billy McFalla9a20e72017-02-15 11:39:12 -0500808done:
809 unformat_free (line_input);
810
811 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700812}
813
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700814/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700815VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
816 .path = "create ipsec tunnel",
Radu Nicolau717de092018-08-03 10:37:24 +0100817 .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700818 .function = create_ipsec_tunnel_command_fn,
819};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700820/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700821
822static clib_error_t *
823set_interface_key_command_fn (vlib_main_t * vm,
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700824 unformat_input_t * input,
825 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700826{
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700827 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700828 ipsec_main_t *im = &ipsec_main;
829 ipsec_if_set_key_type_t type = IPSEC_IF_SET_KEY_TYPE_NONE;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700830 u32 hw_if_index = (u32) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700831 u32 alg;
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700832 u8 *key = 0;
Billy McFalla9a20e72017-02-15 11:39:12 -0500833 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700834
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700835 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700836 return 0;
837
838 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
839 {
840 if (unformat (line_input, "%U",
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700841 unformat_vnet_hw_interface, im->vnet_main, &hw_if_index))
842 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700843 else
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700844 if (unformat
845 (line_input, "local crypto %U", unformat_ipsec_crypto_alg, &alg))
846 type = IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO;
847 else
848 if (unformat
849 (line_input, "remote crypto %U", unformat_ipsec_crypto_alg, &alg))
850 type = IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO;
851 else
852 if (unformat
853 (line_input, "local integ %U", unformat_ipsec_integ_alg, &alg))
854 type = IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG;
855 else
856 if (unformat
857 (line_input, "remote integ %U", unformat_ipsec_integ_alg, &alg))
858 type = IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG;
859 else if (unformat (line_input, "%U", unformat_hex_string, &key))
860 ;
861 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500862 {
863 error = clib_error_return (0, "parse error: '%U'",
864 format_unformat_error, line_input);
865 goto done;
866 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700867 }
868
Ed Warnickecb9cada2015-12-08 15:45:58 -0700869 if (type == IPSEC_IF_SET_KEY_TYPE_NONE)
Billy McFalla9a20e72017-02-15 11:39:12 -0500870 {
871 error = clib_error_return (0, "unknown key type");
872 goto done;
873 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700874
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700875 if (alg > 0 && vec_len (key) == 0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500876 {
877 error = clib_error_return (0, "key is not specified");
878 goto done;
879 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700880
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700881 if (hw_if_index == (u32) ~ 0)
Billy McFalla9a20e72017-02-15 11:39:12 -0500882 {
883 error = clib_error_return (0, "interface not specified");
884 goto done;
885 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700886
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700887 ipsec_set_interface_key (im->vnet_main, hw_if_index, type, alg, key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700888
Billy McFalla9a20e72017-02-15 11:39:12 -0500889done:
890 vec_free (key);
891 unformat_free (line_input);
892
893 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700894}
895
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700896/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700897VLIB_CLI_COMMAND (set_interface_key_command, static) = {
898 .path = "set interface ipsec key",
899 .short_help =
900 "set interface ipsec key <int> <local|remote> <crypto|integ> <key type> <key>",
901 .function = set_interface_key_command_fn,
902};
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700903/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700904
Ed Warnickecb9cada2015-12-08 15:45:58 -0700905clib_error_t *
906ipsec_cli_init (vlib_main_t * vm)
907{
908 return 0;
909}
910
911VLIB_INIT_FUNCTION (ipsec_cli_init);
912
Keith Burns (alagalah)166a9d42016-08-06 11:00:56 -0700913
914/*
915 * fd.io coding-style-patch-verification: ON
916 *
917 * Local Variables:
918 * eval: (c-set-style "gnu")
919 * End:
920 */