FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
ipsec_cli.c
Go to the documentation of this file.
1 /*
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 #include <vnet/fib/fib.h>
23 
24 #include <vnet/ipsec/ipsec.h>
25 #include <vnet/ipsec/ipsec_tun.h>
26 
27 static clib_error_t *
29  unformat_input_t * input,
30  vlib_cli_command_t * cmd)
31 {
32  unformat_input_t _line_input, *line_input = &_line_input;
33  ipsec_main_t *im = &ipsec_main;
34  u32 sw_if_index = (u32) ~ 0;
35  u32 spd_id;
36  int is_add = 1;
37  clib_error_t *error = NULL;
38 
39  if (!unformat_user (input, unformat_line_input, line_input))
40  return 0;
41 
42  if (unformat
43  (line_input, "%U %u", unformat_vnet_sw_interface, im->vnet_main,
44  &sw_if_index, &spd_id))
45  ;
46  else if (unformat (line_input, "del"))
47  is_add = 0;
48  else
49  {
50  error = clib_error_return (0, "parse error: '%U'",
51  format_unformat_error, line_input);
52  goto done;
53  }
54 
55  ipsec_set_interface_spd (vm, sw_if_index, spd_id, is_add);
56 
57 done:
58  unformat_free (line_input);
59 
60  return error;
61 }
62 
63 /* *INDENT-OFF* */
64 VLIB_CLI_COMMAND (set_interface_spd_command, static) = {
65  .path = "set interface ipsec spd",
66  .short_help =
67  "set interface ipsec spd <int> <id>",
68  .function = set_interface_spd_command_fn,
69 };
70 /* *INDENT-ON* */
71 
72 static clib_error_t *
74  unformat_input_t * input,
75  vlib_cli_command_t * cmd)
76 {
77  unformat_input_t _line_input, *line_input = &_line_input;
78  ip46_address_t tun_src = { }, tun_dst =
79  {
80  };
81  ipsec_crypto_alg_t crypto_alg;
82  ipsec_integ_alg_t integ_alg;
85  clib_error_t *error;
86  ipsec_key_t ck = { 0 };
87  ipsec_key_t ik = { 0 };
88  u32 id, spi, salt;
89  int is_add, rv;
90 
91  salt = 0;
92  error = NULL;
93  is_add = 0;
94  flags = IPSEC_SA_FLAG_NONE;
95  proto = IPSEC_PROTOCOL_ESP;
96  integ_alg = IPSEC_INTEG_ALG_NONE;
97  crypto_alg = IPSEC_CRYPTO_ALG_NONE;
98 
99  if (!unformat_user (input, unformat_line_input, line_input))
100  return 0;
101 
102  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
103  {
104  if (unformat (line_input, "add %u", &id))
105  is_add = 1;
106  else if (unformat (line_input, "del %u", &id))
107  is_add = 0;
108  else if (unformat (line_input, "spi %u", &spi))
109  ;
110  else if (unformat (line_input, "salt 0x%x", &salt))
111  ;
112  else if (unformat (line_input, "esp"))
113  proto = IPSEC_PROTOCOL_ESP;
114  else if (unformat (line_input, "ah"))
115  proto = IPSEC_PROTOCOL_AH;
116  else if (unformat (line_input, "crypto-key %U",
117  unformat_ipsec_key, &ck))
118  ;
119  else if (unformat (line_input, "crypto-alg %U",
120  unformat_ipsec_crypto_alg, &crypto_alg))
121  ;
122  else if (unformat (line_input, "integ-key %U", unformat_ipsec_key, &ik))
123  ;
124  else if (unformat (line_input, "integ-alg %U",
125  unformat_ipsec_integ_alg, &integ_alg))
126  ;
127  else if (unformat (line_input, "tunnel-src %U",
129  {
130  flags |= IPSEC_SA_FLAG_IS_TUNNEL;
131  if (!ip46_address_is_ip4 (&tun_src))
132  flags |= IPSEC_SA_FLAG_IS_TUNNEL_V6;
133  }
134  else if (unformat (line_input, "tunnel-dst %U",
136  ;
137  else if (unformat (line_input, "udp-encap"))
138  flags |= IPSEC_SA_FLAG_UDP_ENCAP;
139  else
140  {
141  error = clib_error_return (0, "parse error: '%U'",
142  format_unformat_error, line_input);
143  goto done;
144  }
145  }
146 
147  if (is_add)
148  rv = ipsec_sa_add_and_lock (id, spi, proto, crypto_alg,
149  &ck, integ_alg, &ik, flags,
150  0, clib_host_to_net_u32 (salt),
151  &tun_src, &tun_dst, NULL);
152  else
153  rv = ipsec_sa_unlock_id (id);
154 
155  if (rv)
156  error = clib_error_return (0, "failed");
157 
158 done:
159  unformat_free (line_input);
160 
161  return error;
162 }
163 
164 /* *INDENT-OFF* */
165 VLIB_CLI_COMMAND (ipsec_sa_add_del_command, static) = {
166  .path = "ipsec sa",
167  .short_help =
168  "ipsec sa [add|del]",
169  .function = ipsec_sa_add_del_command_fn,
170 };
171 /* *INDENT-ON* */
172 
173 static clib_error_t *
175  unformat_input_t * input,
176  vlib_cli_command_t * cmd)
177 {
178  unformat_input_t _line_input, *line_input = &_line_input;
179  u32 spd_id = ~0;
180  int is_add = ~0;
181  clib_error_t *error = NULL;
182 
183  if (!unformat_user (input, unformat_line_input, line_input))
184  return 0;
185 
186  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
187  {
188  if (unformat (line_input, "add"))
189  is_add = 1;
190  else if (unformat (line_input, "del"))
191  is_add = 0;
192  else if (unformat (line_input, "%u", &spd_id))
193  ;
194  else
195  {
196  error = clib_error_return (0, "parse error: '%U'",
197  format_unformat_error, line_input);
198  goto done;
199  }
200  }
201 
202  if (spd_id == ~0)
203  {
204  error = clib_error_return (0, "please specify SPD ID");
205  goto done;
206  }
207 
208  ipsec_add_del_spd (vm, spd_id, is_add);
209 
210 done:
211  unformat_free (line_input);
212 
213  return error;
214 }
215 
216 /* *INDENT-OFF* */
217 VLIB_CLI_COMMAND (ipsec_spd_add_del_command, static) = {
218  .path = "ipsec spd",
219  .short_help =
220  "ipsec spd [add|del] <id>",
221  .function = ipsec_spd_add_del_command_fn,
222 };
223 /* *INDENT-ON* */
224 
225 
226 static clib_error_t *
228  unformat_input_t * input,
229  vlib_cli_command_t * cmd)
230 {
231  unformat_input_t _line_input, *line_input = &_line_input;
232  ipsec_policy_t p;
233  int rv, is_add = 0;
234  u32 tmp, tmp2, stat_index;
235  clib_error_t *error = NULL;
237 
238  clib_memset (&p, 0, sizeof (p));
239  p.lport.stop = p.rport.stop = ~0;
240  is_outbound = 0;
241 
242  if (!unformat_user (input, unformat_line_input, line_input))
243  return 0;
244 
245  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
246  {
247  if (unformat (line_input, "add"))
248  is_add = 1;
249  else if (unformat (line_input, "del"))
250  is_add = 0;
251  else if (unformat (line_input, "spd %u", &p.id))
252  ;
253  else if (unformat (line_input, "inbound"))
254  is_outbound = 0;
255  else if (unformat (line_input, "outbound"))
256  is_outbound = 1;
257  else if (unformat (line_input, "priority %d", &p.priority))
258  ;
259  else if (unformat (line_input, "protocol %u", &tmp))
260  p.protocol = (u8) tmp;
261  else
262  if (unformat
263  (line_input, "action %U", unformat_ipsec_policy_action,
264  &p.policy))
265  {
266  if (p.policy == IPSEC_POLICY_ACTION_RESOLVE)
267  {
268  error = clib_error_return (0, "unsupported action: 'resolve'");
269  goto done;
270  }
271  }
272  else if (unformat (line_input, "sa %u", &p.sa_id))
273  ;
274  else if (unformat (line_input, "local-ip-range %U - %U",
277  ;
278  else if (unformat (line_input, "remote-ip-range %U - %U",
281  ;
282  else if (unformat (line_input, "local-ip-range %U - %U",
285  {
286  p.is_ipv6 = 1;
287  }
288  else if (unformat (line_input, "remote-ip-range %U - %U",
291  {
292  p.is_ipv6 = 1;
293  }
294  else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2))
295  {
296  p.lport.start = tmp;
297  p.lport.stop = tmp2;
298  }
299  else
300  if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2))
301  {
302  p.rport.start = tmp;
303  p.rport.stop = tmp2;
304  }
305  else
306  {
307  error = clib_error_return (0, "parse error: '%U'",
308  format_unformat_error, line_input);
309  goto done;
310  }
311  }
312 
313  rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type);
314 
315  if (rv)
316  {
317  error = clib_error_return (0, "unsupported policy type for:",
318  " outboud:%s %s action:%U",
319  (is_outbound ? "yes" : "no"),
320  (p.is_ipv6 ? "IPv4" : "IPv6"),
322  goto done;
323  }
324 
325  rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index);
326 
327  if (!rv)
328  vlib_cli_output (vm, "policy-index:%d", stat_index);
329  else
330  vlib_cli_output (vm, "error:%d", rv);
331 
332 done:
333  unformat_free (line_input);
334 
335  return error;
336 }
337 
338 /* *INDENT-OFF* */
339 VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = {
340  .path = "ipsec policy",
341  .short_help =
342  "ipsec policy [add|del] spd <id> priority <n> ",
344 };
345 /* *INDENT-ON* */
346 
347 static void
349 {
350  u32 sai;
351 
352  /* *INDENT-OFF* */
353  pool_foreach_index (sai, im->sad, ({
354  vlib_cli_output(vm, "%U", format_ipsec_sa, sai,
355  (detail ? IPSEC_FORMAT_DETAIL : IPSEC_FORMAT_BRIEF));
356  }));
357  /* *INDENT-ON* */
358 }
359 
360 static void
362 {
363  u32 spdi;
364 
365  /* *INDENT-OFF* */
366  pool_foreach_index (spdi, im->spds, ({
367  vlib_cli_output(vm, "%U", format_ipsec_spd, spdi);
368  }));
369  /* *INDENT-ON* */
370 }
371 
372 static void
374 {
375  u32 spd_id, sw_if_index;
376  ipsec_spd_t *spd;
377 
378  vlib_cli_output (vm, "SPD Bindings:");
379 
380  /* *INDENT-OFF* */
381  hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({
382  spd = pool_elt_at_index (im->spds, spd_id);
383  vlib_cli_output (vm, " %d -> %U", spd->id,
384  format_vnet_sw_if_index_name, im->vnet_main,
385  sw_if_index);
386  }));
387  /* *INDENT-ON* */
388 }
389 
390 static void
392 {
393  u32 ti;
394 
395  vlib_cli_output (vm, "Tunnel interfaces");
396  /* *INDENT-OFF* */
398  vlib_cli_output(vm, " %U", format_ipsec_tunnel, ti);
399  }));
400  /* *INDENT-ON* */
401 }
402 
403 static clib_error_t *
405  unformat_input_t * input, vlib_cli_command_t * cmd)
406 {
407  ipsec_main_t *im = &ipsec_main;
408 
409  ipsec_sa_show_all (vm, im, 0);
410  ipsec_spd_show_all (vm, im);
412  ipsec_tunnel_show_all (vm, im);
413 
414  return 0;
415 }
416 
417 /* *INDENT-OFF* */
418 VLIB_CLI_COMMAND (show_ipsec_command, static) = {
419  .path = "show ipsec all",
420  .short_help = "show ipsec all",
421  .function = show_ipsec_command_fn,
422 };
423 /* *INDENT-ON* */
424 
425 static clib_error_t *
427  unformat_input_t * input, vlib_cli_command_t * cmd)
428 {
429  ipsec_main_t *im = &ipsec_main;
430  u32 sai = ~0;
431  u8 detail = 0;
432 
434  {
435  if (unformat (input, "%u", &sai))
436  ;
437  if (unformat (input, "detail"))
438  detail = 1;
439  else
440  break;
441  }
442 
443  if (~0 == sai)
444  ipsec_sa_show_all (vm, im, detail);
445  else
446  vlib_cli_output (vm, "%U", format_ipsec_sa, sai,
448 
449  return 0;
450 }
451 
452 static clib_error_t *
454  unformat_input_t * input, vlib_cli_command_t * cmd)
455 {
456  ipsec_main_t *im = &ipsec_main;
457  u32 sai = ~0;
458 
460  {
461  if (unformat (input, "%u", &sai))
462  ;
463  else
464  break;
465  }
466 
467  if (~0 == sai)
468  {
469  /* *INDENT-OFF* */
470  pool_foreach_index (sai, im->sad, ({
471  ipsec_sa_clear(sai);
472  }));
473  /* *INDENT-ON* */
474  }
475  else
476  {
477  if (pool_is_free_index (im->sad, sai))
478  return clib_error_return (0, "unknown SA index: %d", sai);
479  else
480  ipsec_sa_clear (sai);
481  }
482 
483  return 0;
484 }
485 
486 /* *INDENT-OFF* */
487 VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = {
488  .path = "show ipsec sa",
489  .short_help = "show ipsec sa [index]",
490  .function = show_ipsec_sa_command_fn,
491 };
492 
493 VLIB_CLI_COMMAND (clear_ipsec_sa_command, static) = {
494  .path = "clear ipsec sa",
495  .short_help = "clear ipsec sa [index]",
496  .function = clear_ipsec_sa_command_fn,
497 };
498 /* *INDENT-ON* */
499 
500 static clib_error_t *
502  unformat_input_t * input, vlib_cli_command_t * cmd)
503 {
504  ipsec_main_t *im = &ipsec_main;
505  u8 show_bindings = 0;
506  u32 spdi = ~0;
507 
509  {
510  if (unformat (input, "%u", &spdi))
511  ;
512  else if (unformat (input, "bindings"))
513  show_bindings = 1;
514  else
515  break;
516  }
517 
518  if (show_bindings)
520  else if (~0 != spdi)
521  vlib_cli_output (vm, "%U", format_ipsec_spd, spdi);
522  else
523  ipsec_spd_show_all (vm, im);
524 
525  return 0;
526 }
527 
528 /* *INDENT-OFF* */
529 VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = {
530  .path = "show ipsec spd",
531  .short_help = "show ipsec spd [index]",
532  .function = show_ipsec_spd_command_fn,
533 };
534 /* *INDENT-ON* */
535 
536 static clib_error_t *
538  unformat_input_t * input,
539  vlib_cli_command_t * cmd)
540 {
541  ipsec_main_t *im = &ipsec_main;
542  u32 ti = ~0;
543 
545  {
546  if (unformat (input, "%u", &ti))
547  ;
548  else
549  break;
550  }
551 
552  if (~0 != ti)
553  vlib_cli_output (vm, "%U", format_ipsec_tunnel, ti);
554  else
555  ipsec_tunnel_show_all (vm, im);
556 
557  return 0;
558 }
559 
560 /* *INDENT-OFF* */
561 VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = {
562  .path = "show ipsec tunnel",
563  .short_help = "show ipsec tunnel [index]",
564  .function = show_ipsec_tunnel_command_fn,
565 };
566 /* *INDENT-ON* */
567 
568 static clib_error_t *
570  unformat_input_t * input,
571  vlib_cli_command_t * cmd)
572 {
573  ipsec_main_t *im = &ipsec_main;
574  u32 verbose = 0;
575 
576  (void) unformat (input, "verbose %u", &verbose);
577 
578  vlib_cli_output (vm, "IPsec AH backends available:");
579  u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
580  ipsec_ah_backend_t *ab;
581  /* *INDENT-OFF* */
582  pool_foreach (ab, im->ah_backends, {
583  s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends,
584  ab - im->ah_backends == im->ah_current_backend ? "yes" : "no");
585  if (verbose) {
586  vlib_node_t *n;
587  n = vlib_get_node (vm, ab->ah4_encrypt_node_index);
588  s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index);
589  n = vlib_get_node (vm, ab->ah4_decrypt_node_index);
590  s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index);
591  n = vlib_get_node (vm, ab->ah6_encrypt_node_index);
592  s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index);
593  n = vlib_get_node (vm, ab->ah6_decrypt_node_index);
594  s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index);
595  }
596  });
597  /* *INDENT-ON* */
598  vlib_cli_output (vm, "%v", s);
599  _vec_len (s) = 0;
600  vlib_cli_output (vm, "IPsec ESP backends available:");
601  s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active");
603  /* *INDENT-OFF* */
604  pool_foreach (eb, im->esp_backends, {
605  s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends,
606  eb - im->esp_backends == im->esp_current_backend ? "yes"
607  : "no");
608  if (verbose) {
609  vlib_node_t *n;
610  n = vlib_get_node (vm, eb->esp4_encrypt_node_index);
611  s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index);
612  n = vlib_get_node (vm, eb->esp4_decrypt_node_index);
613  s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index);
614  n = vlib_get_node (vm, eb->esp6_encrypt_node_index);
615  s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index);
616  n = vlib_get_node (vm, eb->esp6_decrypt_node_index);
617  s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index);
618  }
619  });
620  /* *INDENT-ON* */
621  vlib_cli_output (vm, "%v", s);
622 
623  vec_free (s);
624  return 0;
625 }
626 
627 /* *INDENT-OFF* */
628 VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = {
629  .path = "show ipsec backends",
630  .short_help = "show ipsec backends",
631  .function = ipsec_show_backends_command_fn,
632 };
633 /* *INDENT-ON* */
634 
635 static clib_error_t *
637  unformat_input_t * input,
638  vlib_cli_command_t * cmd)
639 {
640  unformat_input_t _line_input, *line_input = &_line_input;
641  ipsec_main_t *im = &ipsec_main;
642  clib_error_t *error;
643  u32 backend_index;
644 
645  error = ipsec_rsc_in_use (im);
646 
647  if (error)
648  return error;
649 
650  /* Get a line of input. */
651  if (!unformat_user (input, unformat_line_input, line_input))
652  return 0;
653 
654  if (unformat (line_input, "ah"))
655  {
656  if (unformat (line_input, "%u", &backend_index))
657  {
658  if (ipsec_select_ah_backend (im, backend_index) < 0)
659  {
660  return clib_error_return (0, "Invalid AH backend index `%u'",
661  backend_index);
662  }
663  }
664  else
665  {
666  return clib_error_return (0, "Invalid backend index `%U'",
667  format_unformat_error, line_input);
668  }
669  }
670  else if (unformat (line_input, "esp"))
671  {
672  if (unformat (line_input, "%u", &backend_index))
673  {
674  if (ipsec_select_esp_backend (im, backend_index) < 0)
675  {
676  return clib_error_return (0, "Invalid ESP backend index `%u'",
677  backend_index);
678  }
679  }
680  else
681  {
682  return clib_error_return (0, "Invalid backend index `%U'",
683  format_unformat_error, line_input);
684  }
685  }
686  else
687  {
688  return clib_error_return (0, "Unknown input `%U'",
689  format_unformat_error, line_input);
690  }
691 
692  return 0;
693 }
694 
695 /* *INDENT-OFF* */
696 VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = {
697  .path = "ipsec select backend",
698  .short_help = "ipsec select backend <ah|esp> <backend index>",
700 };
701 
702 /* *INDENT-ON* */
703 
704 static clib_error_t *
706  unformat_input_t * input,
707  vlib_cli_command_t * cmd)
708 {
711 
712  return (NULL);
713 }
714 
715 /* *INDENT-OFF* */
716 VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = {
717  .path = "clear ipsec counters",
718  .short_help = "clear ipsec counters",
720 };
721 /* *INDENT-ON* */
722 
723 static clib_error_t *
725  unformat_input_t * input,
726  vlib_cli_command_t * cmd)
727 {
728  unformat_input_t _line_input, *line_input = &_line_input;
730  int rv;
731  u32 num_m_args = 0;
732  u8 ipv4_set = 0;
733  u8 ipv6_set = 0;
734  clib_error_t *error = NULL;
735  ipsec_key_t rck = { 0 };
736  ipsec_key_t lck = { 0 };
737  ipsec_key_t lik = { 0 };
738  ipsec_key_t rik = { 0 };
739 
740  clib_memset (&a, 0, sizeof (a));
741  a.is_add = 1;
742 
743  /* Get a line of input. */
744  if (!unformat_user (input, unformat_line_input, line_input))
745  return 0;
746 
747  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
748  {
749  if (unformat
750  (line_input, "local-ip %U", unformat_ip46_address, &a.local_ip,
751  IP46_TYPE_ANY))
752  {
753  ip46_address_is_ip4 (&a.local_ip) ? (ipv4_set = 1) : (ipv6_set = 1);
754  num_m_args++;
755  }
756  else
757  if (unformat
758  (line_input, "remote-ip %U", unformat_ip46_address, &a.remote_ip,
759  IP46_TYPE_ANY))
760  {
761  ip46_address_is_ip4 (&a.remote_ip) ? (ipv4_set = 1) : (ipv6_set =
762  1);
763  num_m_args++;
764  }
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++;
769  else if (unformat (line_input, "instance %u", &a.show_instance))
770  a.renumber = 1;
771  else if (unformat (line_input, "salt 0x%x", &a.salt))
772  ;
773  else if (unformat (line_input, "udp-encap"))
774  a.udp_encap = 1;
775  else if (unformat (line_input, "use-esn"))
776  a.esn = 1;
777  else if (unformat (line_input, "use-anti-replay"))
778  a.anti_replay = 1;
779  else if (unformat (line_input, "tx-table %u", &a.tx_table_id))
780  ;
781  else
782  if (unformat
783  (line_input, "local-crypto-key %U", unformat_ipsec_key, &lck))
784  ;
785  else
786  if (unformat
787  (line_input, "remote-crypto-key %U", unformat_ipsec_key, &rck))
788  ;
789  else if (unformat (line_input, "crypto-alg %U",
791  ;
792  else
793  if (unformat
794  (line_input, "local-integ-key %U", unformat_ipsec_key, &lik))
795  ;
796  else
797  if (unformat
798  (line_input, "remote-integ-key %U", unformat_ipsec_key, &rik))
799  ;
800  else if (unformat (line_input, "integ-alg %U",
802  ;
803  else if (unformat (line_input, "del"))
804  a.is_add = 0;
805  else
806  {
807  error = clib_error_return (0, "unknown input `%U'",
808  format_unformat_error, line_input);
809  goto done;
810  }
811  }
812 
813  if (num_m_args < 4)
814  {
815  error = clib_error_return (0, "mandatory argument(s) missing");
816  goto done;
817  }
818 
819  if (ipv4_set && ipv6_set)
820  return clib_error_return (0, "both IPv4 and IPv6 addresses specified");
821 
822  a.is_ip6 = ipv6_set;
823 
824  clib_memcpy (a.local_crypto_key, lck.data, lck.len);
825  a.local_crypto_key_len = lck.len;
826  clib_memcpy (a.remote_crypto_key, rck.data, rck.len);
827  a.remote_crypto_key_len = rck.len;
828 
829  clib_memcpy (a.local_integ_key, lik.data, lik.len);
830  a.local_integ_key_len = lck.len;
831  clib_memcpy (a.remote_integ_key, rik.data, rik.len);
832  a.remote_integ_key_len = rck.len;
833 
834  rv = ipsec_add_del_tunnel_if (&a);
835 
836  switch (rv)
837  {
838  case 0:
839  break;
840  case VNET_API_ERROR_INVALID_VALUE:
841  if (a.is_add)
842  error = clib_error_return (0,
843  "IPSec tunnel interface already exists...");
844  else
845  error = clib_error_return (0, "IPSec tunnel interface not exists...");
846  goto done;
847  default:
848  error = clib_error_return (0, "ipsec_register_interface returned %d",
849  rv);
850  goto done;
851  }
852 
853 done:
854  unformat_free (line_input);
855 
856  return error;
857 }
858 
859 /* *INDENT-OFF* */
860 VLIB_CLI_COMMAND (create_ipsec_tunnel_command, static) = {
861  .path = "create ipsec tunnel",
862  .short_help = "create ipsec tunnel local-ip <addr> local-spi <spi> "
863  "remote-ip <addr> remote-spi <spi> [instance <inst_num>] [udp-encap] [use-esn] [use-anti-replay] "
864  "[tx-table <table-id>]",
865  .function = create_ipsec_tunnel_command_fn,
866 };
867 /* *INDENT-ON* */
868 
869 static clib_error_t *
871  unformat_input_t * input, vlib_cli_command_t * cmd)
872 {
873  unformat_input_t _line_input, *line_input = &_line_input;
874  u32 sw_if_index, is_del, sa_in, sa_out, *sa_ins = NULL;
875  vnet_main_t *vnm;
876 
877  is_del = 0;
878  sw_if_index = ~0;
879  vnm = vnet_get_main ();
880 
881  if (!unformat_user (input, unformat_line_input, line_input))
882  return 0;
883 
884  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
885  {
886  if (unformat (line_input, "del"))
887  is_del = 1;
888  else if (unformat (line_input, "add"))
889  is_del = 0;
890  else if (unformat (line_input, "sa-in %d", &sa_in))
891  vec_add1 (sa_ins, sa_in);
892  else if (unformat (line_input, "sa-out %d", &sa_out))
893  ;
894  else if (unformat (line_input, "%U",
895  unformat_vnet_sw_interface, vnm, &sw_if_index))
896  ;
897  else
898  return (clib_error_return (0, "unknown input '%U'",
899  format_unformat_error, line_input));
900  }
901 
902  if (!is_del)
903  ipsec_tun_protect_update (sw_if_index, sa_out, sa_ins);
904 
905  unformat_free (line_input);
906  return NULL;
907 }
908 
909 /**
910  * Protect tunnel with IPSEC
911  */
912 /* *INDENT-OFF* */
913 VLIB_CLI_COMMAND (ipsec_tun_protect_cmd_node, static) =
914 {
915  .path = "ipsec tunnel protect",
916  .function = ipsec_tun_protect_cmd,
917  .short_help = "ipsec tunnel protect <interface> input-sa <SA> output-sa <SA>",
918  // this is not MP safe
919 };
920 /* *INDENT-ON* */
921 
922 static walk_rc_t
924 {
925  vlib_cli_output (ctx, "%U", format_ipsec_tun_protect, itpi);
926 
927  return (WALK_CONTINUE);
928 }
929 
930 static clib_error_t *
932  unformat_input_t * input, vlib_cli_command_t * cmd)
933 {
935 
936  return NULL;
937 }
938 
939 /**
940  * show IPSEC tunnel protection
941  */
942 /* *INDENT-OFF* */
943 VLIB_CLI_COMMAND (ipsec_tun_protect_show_node, static) =
944 {
945  .path = "show ipsec protect",
946  .function = ipsec_tun_protect_show,
947  .short_help = "show ipsec protect",
948 };
949 /* *INDENT-ON* */
950 
951 static clib_error_t *
953  unformat_input_t * input,
954  vlib_cli_command_t * cmd)
955 {
956  ipsec_main_t *im = &ipsec_main;
957 
958  {
960  ipsec4_tunnel_key_t key;
961 
962  vlib_cli_output (vm, "IPv4:");
963 
964  /* *INDENT-OFF* */
965  hash_foreach(key.as_u64, value.as_u64, im->tun4_protect_by_key,
966  ({
967  vlib_cli_output (vm, " %U", format_ipsec4_tunnel_key, &key);
968  vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
969  }));
970  /* *INDENT-ON* */
971  }
972 
973  {
975  ipsec6_tunnel_key_t *key;
976 
977  vlib_cli_output (vm, "IPv6:");
978 
979  /* *INDENT-OFF* */
981  ({
982  vlib_cli_output (vm, " %U", format_ipsec6_tunnel_key, key);
983  vlib_cli_output (vm, " tun:%d sa:%d", value.tun_index, value.sa_index);
984  }));
985  /* *INDENT-ON* */
986  }
987 
988  return NULL;
989 }
990 
991 /**
992  * show IPSEC tunnel protection hash tables
993  */
994 /* *INDENT-OFF* */
995 VLIB_CLI_COMMAND (ipsec_tun_protect_hash_show_node, static) =
996 {
997  .path = "show ipsec protect-hash",
998  .function = ipsec_tun_protect_hash_show,
999  .short_help = "show ipsec protect-hash",
1000 };
1001 /* *INDENT-ON* */
1002 
1003 clib_error_t *
1005 {
1006  return 0;
1007 }
1008 
1010 
1011 
1012 /*
1013  * fd.io coding-style-patch-verification: ON
1014  *
1015  * Local Variables:
1016  * eval: (c-set-style "gnu")
1017  * End:
1018  */
static clib_error_t * ipsec_select_backend_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ipsec_cli.c:636
ipsec_spd_t * spds
Definition: ipsec.h:95
u32 flags
Definition: vhost_user.h:141
ipsec_tunnel_if_t * tunnel_interfaces
Definition: ipsec.h:102
uword * tun6_protect_by_key
Definition: ipsec.h:119
a
Definition: bitmap.h:538
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
ip46_address_t local_ip
Definition: ipsec_if.h:38
ipsec_integ_alg_t
Definition: ipsec_sa.h:58
ip46_address_range_t laddr
static clib_error_t * show_ipsec_spd_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ipsec_cli.c:501
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
int ipsec_policy_mk_type(bool is_outbound, bool is_ipv6, ipsec_policy_action_t action, ipsec_spd_policy_type_t *type)
static clib_error_t * create_ipsec_tunnel_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ipsec_cli.c:724
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:41
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
ipsec_protocol_t
Definition: ipsec_sa.h:66
int ipsec_add_del_tunnel_if(ipsec_add_del_tunnel_args_t *args)
Definition: ipsec_if.c:214
void ipsec_sa_clear(index_t sai)
Definition: ipsec_sa.c:348
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
int ipsec_select_ah_backend(ipsec_main_t *im, u32 backend_idx)
Definition: ipsec.c:218
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
unformat_function_t unformat_vnet_sw_interface
A Secruity Policy Database.
Definition: ipsec_spd.h:44
void ipsec_tun_protect_walk(ipsec_tun_protect_walk_cb_t fn, void *ctx)
Definition: ipsec_tun.c:370
unsigned char u8
Definition: types.h:56
uword * spd_index_by_sw_if_index
Definition: ipsec.h:112
#define clib_memcpy(d, s, n)
Definition: string.h:180
enum walk_rc_t_ walk_rc_t
Walk return code.
void vlib_clear_combined_counters(vlib_combined_counter_main_t *cm)
Clear a collection of combined counters.
Definition: counter.c:61
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
unformat_function_t unformat_ip4_address
Definition: format.h:70
vl_api_interface_index_t sw_if_index
Definition: gre.api:50
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
ipsec_main_t ipsec_main
Definition: ipsec.c:28
u8 * format_ipsec_tun_protect(u8 *s, va_list *args)
Definition: ipsec_format.c:372
int ipsec_select_esp_backend(ipsec_main_t *im, u32 backend_idx)
Definition: ipsec.c:241
#define hash_foreach(key_var, value_var, h, body)
Definition: hash.h:442
uword unformat_ipsec_crypto_alg(unformat_input_t *input, va_list *args)
Definition: ipsec_format.c:96
#define clib_error_return(e, args...)
Definition: error.h:99
int ipsec_sa_unlock_id(u32 id)
Definition: ipsec_sa.c:332
port_range_t rport
u8 * format_ipsec_sa(u8 *s, va_list *args)
Definition: ipsec_format.c:269
unsigned int u32
Definition: types.h:88
static void ipsec_tunnel_show_all(vlib_main_t *vm, ipsec_main_t *im)
Definition: ipsec_cli.c:391
u32 sa_in[n_sa_in]
Definition: ipsec.api:354
static clib_error_t * ipsec_tun_protect_show(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ipsec_cli.c:931
unformat_function_t unformat_line_input
Definition: format.h:283
u32 sa_out
Definition: ipsec.api:352
vlib_combined_counter_main_t ipsec_spd_policy_counters
Policy packet & bytes counters.
u8 * format_ipsec_spd(u8 *s, va_list *args)
Definition: ipsec_format.c:203
static clib_error_t * ipsec_policy_add_del_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ipsec_cli.c:227
long ctx[MAX_CONNS]
Definition: main.c:144
vnet_main_t * vnet_main
Definition: ipsec.h:108
struct _unformat_input_t unformat_input_t
int ipsec_add_del_policy(vlib_main_t *vm, ipsec_policy_t *policy, int is_add, u32 *stat_index)
Add/Delete a SPD.
static clib_error_t * set_interface_spd_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ipsec_cli.c:28
ip46_address_t remote_ip
Definition: ipsec_if.h:38
u8 * format_ipsec_tunnel(u8 *s, va_list *args)
Definition: ipsec_format.c:338
clib_error_t * ipsec_rsc_in_use(ipsec_main_t *im)
Definition: ipsec.c:201
static clib_error_t * ipsec_show_backends_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ipsec_cli.c:569
ipsec_spd_policy_type_t type
#define hash_foreach_mem(key_var, value_var, h, body)
Definition: hash.h:461
static void ipsec_spd_bindings_show_all(vlib_main_t *vm, ipsec_main_t *im)
Definition: ipsec_cli.c:373
static clib_error_t * clear_ipsec_counters_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ipsec_cli.c:705
#define ip46_address_is_ip4(ip46)
Definition: ip6_packet.h:88
unformat_function_t unformat_ip6_address
Definition: format.h:91
ipsec_crypto_alg_t crypto_alg
Definition: ipsec_if.h:41
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
static void ipsec_sa_show_all(vlib_main_t *vm, ipsec_main_t *im, u8 detail)
Definition: ipsec_cli.c:348
vlib_main_t * vm
Definition: buffer.c:323
int ipsec_tun_protect_update(u32 sw_if_index, u32 sa_out, u32 *sas_in)
Definition: ipsec_tun.c:224
ipsec_ah_backend_t * ah_backends
Definition: ipsec.h:150
clib_error_t * ipsec_cli_init(vlib_main_t *vm)
Definition: ipsec_cli.c:1004
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
static clib_error_t * clear_ipsec_sa_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ipsec_cli.c:453
ipsec_policy_action_t policy
ip46_address_t start
enum ipsec_sad_flags_t_ ipsec_sa_flags_t
u8 is_outbound
Definition: ipsec.api:93
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:283
A Secruity Policy.
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:161
static void ipsec_spd_show_all(vlib_main_t *vm, ipsec_main_t *im)
Definition: ipsec_cli.c:361
vlib_combined_counter_main_t ipsec_sa_counters
SA packet & bytes counters.
Definition: ipsec_sa.c:27
u8 value
Definition: qos.api:53
u32 spi
Definition: ipsec.api:274
ipsec_integ_alg_t integ_alg
Definition: ipsec_if.h:46
ipsec_sa_t * sad
Definition: ipsec.h:97
static clib_error_t * show_ipsec_sa_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ipsec_cli.c:426
uword unformat_ipsec_integ_alg(unformat_input_t *input, va_list *args)
Definition: ipsec_format.c:128
static clib_error_t * ipsec_tun_protect_cmd(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ipsec_cli.c:870
u8 * format_ipsec_policy_action(u8 *s, va_list *args)
Definition: ipsec_format.c:28
uword unformat_ipsec_policy_action(unformat_input_t *input, va_list *args)
Definition: ipsec_format.c:64
unformat_function_t unformat_ip46_address
Definition: format.h:65
int ipsec_sa_add_and_lock(u32 id, u32 spi, ipsec_protocol_t proto, ipsec_crypto_alg_t crypto_alg, const ipsec_key_t *ck, ipsec_integ_alg_t integ_alg, const ipsec_key_t *ik, ipsec_sa_flags_t flags, u32 tx_table_id, u32 salt, const ip46_address_t *tun_src, const ip46_address_t *tun_dst, u32 *sa_out_index)
Definition: ipsec_sa.c:127
ip46_address_range_t raddr
static clib_error_t * show_ipsec_tunnel_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ipsec_cli.c:537
static void unformat_free(unformat_input_t *i)
Definition: format.h:163
ipsec_crypto_alg_t
Definition: ipsec_sa.h:36
typedef key
Definition: ipsec.api:247
static clib_error_t * ipsec_sa_add_del_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ipsec_cli.c:73
int ipsec_set_interface_spd(vlib_main_t *vm, u32 sw_if_index, u32 spd_id, int is_add)
Bind/attach a SPD to an interface.
Definition: ipsec_spd.c:63
static clib_error_t * ipsec_tun_protect_hash_show(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ipsec_cli.c:952
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
port_range_t lport
int ipsec_add_del_spd(vlib_main_t *vm, u32 spd_id, int is_add)
Add/Delete a SPD.
Definition: ipsec_spd.c:20
static clib_error_t * show_ipsec_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ipsec_cli.c:404
static walk_rc_t ipsec_tun_protect_show_one(index_t itpi, void *ctx)
Definition: ipsec_cli.c:923
u32 id
Definition: udp.api:45
#define pool_foreach_index(i, v, body)
Iterate pool by index.
Definition: pool.h:538
static clib_error_t * ipsec_spd_add_del_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: ipsec_cli.c:174
u32 salt
Definition: ipsec.api:289
uword unformat_ipsec_key(unformat_input_t *input, va_list *args)
Definition: ipsec_format.c:242
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:772
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
vl_api_fib_path_nh_proto_t proto
Definition: fib_types.api:125
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171
uword * tun4_protect_by_key
Definition: ipsec.h:118