FD.io VPP  v20.05-21-gb1500e9ff
Vector Packet Processing
ipsec_tun.c
Go to the documentation of this file.
1 /*
2  * ipsec_tun.h : IPSEC tunnel protection
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/ipsec/ipsec_tun.h>
19 #include <vnet/ipsec/esp.h>
20 #include <vnet/udp/udp.h>
21 #include <vnet/adj/adj_delegate.h>
22 #include <vnet/adj/adj_midchain.h>
23 #include <vnet/teib/teib.h>
24 
25 /**
26  * The logger
27  */
29 
30 /**
31  * Pool of tunnel protection objects
32  */
34 
35 /**
36  * Adj delegate registered type
37  */
39 
40 /**
41  * Adj index to TX SA mapping
42  */
44 
45 const ip_address_t IP_ADDR_ALL_0 = IP_ADDRESS_V4_ALL_0S;
46 
47 /**
48  * The DB of all added per-nh tunnel protectiond
49  */
51 {
52  /** A hash table key'd on IP (4 or 6) address */
54  /** If the interface is P2P then there is only one protect
55  * object associated with the auto-adj for each NH proto */
58 
60 {
61  /** Per-interface vector */
64 
66 
67 const static ipsec_tun_protect_itf_db_t IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY = {
69 };
70 
71 #define ITP_DBG(_itp, _fmt, _args...) \
72 { \
73  vlib_log_debug(ipsec_tun_protect_logger, \
74  "[%U]: " _fmt, \
75  format_ipsec_tun_protect, \
76  _itp, ##_args); \
77 }
78 
79 #define ITP_DBG2(_fmt, _args...) \
80 { \
81  vlib_log_debug(ipsec_tun_protect_logger, \
82  _fmt, ##_args); \
83 }
84 
86 
87 void
89 {
90  if (0 == ipsec_tun_node_regs[af]++)
91  {
92  if (AF_IP4 == af)
93  {
94  ipsec_register_udp_port (UDP_DST_PORT_ipsec);
95  ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
96  ipsec4_tun_input_node.index);
97  }
98  else
99  ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP,
100  ipsec6_tun_input_node.index);
101  }
102 }
103 
104 void
106 {
107  ASSERT (0 != ipsec_tun_node_regs[af]);
108  if (0 == --ipsec_tun_node_regs[af])
109  {
110  if (AF_IP4 == af)
111  {
112  ipsec_unregister_udp_port (UDP_DST_PORT_ipsec);
113  ip4_unregister_protocol (IP_PROTOCOL_IPSEC_ESP);
114  }
115  else
116  ip6_unregister_protocol (IP_PROTOCOL_IPSEC_ESP);
117  }
118 }
119 
120 static inline const ipsec_tun_protect_t *
122 {
123  if (ad == NULL)
124  return (NULL);
125  return (pool_elt_at_index (ipsec_tun_protect_pool, ad->ad_index));
126 }
127 
128 static u32
130 {
131  ipsec_main_t *im;
132  ipsec_sa_t *sa;
133  bool is_ip4;
134  u32 next;
135 
136  is_ip4 = ip46_address_is_ip4 (&itp->itp_tun.src);
137  sa = ipsec_sa_get (itp->itp_out_sa);
138  im = &ipsec_main;
139 
140  if (sa->crypto_alg == IPSEC_CRYPTO_ALG_NONE &&
141  sa->integ_alg == IPSEC_INTEG_ALG_NONE)
142  next = (is_ip4 ?
145  else if (itp->itp_flags & IPSEC_PROTECT_L2)
146  next = (is_ip4 ?
149  else
150  next = (is_ip4 ?
153 
154  return (next);
155 }
156 
157 static void
159 {
161  INDEX_INVALID);
162 
163  if (NULL == itp)
164  {
167  }
168  else
169  {
172  (ai, ipsec_tun_protect_get_adj_next (itp));
173  }
174 }
175 
176 static index_t
178 {
180  uword *p;
181 
182  if (vec_len (itp_db.id_itf) <= sw_if_index)
183  return INDEX_INVALID;
184 
185  if (vnet_sw_interface_is_p2p (vnet_get_main (), sw_if_index))
186  return (itp_db.id_itf[sw_if_index].id_itp);
187 
188  idi = &itp_db.id_itf[sw_if_index];
189  p = hash_get_mem (idi->id_hash, nh);
190 
191  if (NULL == p)
192  {
193  return INDEX_INVALID;
194  }
195  return (p[0]);
196 }
197 
198 static void
200  const ipsec_tun_protect_t * itp)
201 {
202  const ipsec_sa_t *sa;
203  u32 sai;
204 
205  if (ip46_address_is_zero (&itp->itp_crypto.dst))
206  return;
207 
208  /* *INDENT-OFF* */
210  ({
211  sa = ipsec_sa_get (sai);
212 
215  .sa_index = sai,
216  };
217 
218  /*
219  * The key is formed from the tunnel's destination
220  * as the packet lookup is done from the packet's source
221  */
222  if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
223  {
224  ipsec4_tunnel_key_t key = {
225  .remote_ip = itp->itp_crypto.dst.ip4,
226  .spi = clib_host_to_net_u32 (sa->spi),
227  };
228  hash_set (im->tun4_protect_by_key, key.as_u64, res.as_u64);
230  }
231  else
232  {
233  ipsec6_tunnel_key_t key = {
234  .remote_ip = itp->itp_crypto.dst.ip6,
235  .spi = clib_host_to_net_u32 (sa->spi),
236  };
239  }
240  }))
241  /* *INDENT-ON* */
242 }
243 
244 static adj_walk_rc_t
246 {
247  ipsec_tun_protect_t *itp = arg;
249  itp - ipsec_tun_protect_pool);
250  ipsec_tun_protect_add_adj (ai, itp);
251 
252  return (ADJ_WALK_RC_CONTINUE);
253 }
254 
255 static void
257 {
258  /*
259  * add the delegate to the adj
260  */
262  fib_protocol_t nh_proto;
263  ip46_address_t nh;
264 
266  itp->itp_sw_if_index,
267  IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY);
268 
269  idi = &itp_db.id_itf[itp->itp_sw_if_index];
270 
272  {
273  if (INDEX_INVALID == idi->id_itp)
274  {
275  // ipsec_tun_protect_feature_set (itp, 1);
276  }
277  idi->id_itp = itp - ipsec_tun_protect_pool;
278 
279  FOR_EACH_FIB_IP_PROTOCOL (nh_proto)
281  nh_proto, ipsec_tun_protect_adj_add, itp);
282  }
283  else
284  {
285  if (NULL == idi->id_hash)
286  {
287  idi->id_hash =
288  hash_create_mem (0, sizeof (ip_address_t), sizeof (uword));
289  /*
290  * enable the encrypt feature for egress if this is the first addition
291  * on this interface
292  */
293  // ipsec_tun_protect_feature_set (itp, 1);
294  }
295 
296  hash_set_mem (idi->id_hash, itp->itp_key, itp - ipsec_tun_protect_pool);
297 
298  /*
299  * walk all the adjs with the same nh on this interface
300  * to associate them with this protection
301  */
302  nh_proto = ip_address_to_46 (itp->itp_key, &nh);
303 
305  nh_proto, &nh, ipsec_tun_protect_adj_add, itp);
306 
308  AF_IP6 : AF_IP4);
309  }
310 }
311 
312 static void
314  const ipsec_tun_protect_t * itp)
315 {
316  const ipsec_sa_t *sa;
317 
318  /* *INDENT-OFF* */
320  ({
321  if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
322  {
323  ipsec4_tunnel_key_t key = {
324  .remote_ip = itp->itp_crypto.dst.ip4,
325  .spi = clib_host_to_net_u32 (sa->spi),
326  };
327  if (hash_get(im->tun4_protect_by_key, key.as_u64))
328  {
329  hash_unset (im->tun4_protect_by_key, key.as_u64);
330  ipsec_tun_unregister_nodes(AF_IP4);
331  }
332  }
333  else
334  {
335  ipsec6_tunnel_key_t key = {
336  .remote_ip = itp->itp_crypto.dst.ip6,
337  .spi = clib_host_to_net_u32 (sa->spi),
338  };
339  if (hash_get_mem(im->tun6_protect_by_key, &key))
340  {
343  }
344  }
345  }));
346  /* *INDENT-ON* */
347 }
348 
349 static adj_walk_rc_t
351 {
353  ipsec_tun_protect_add_adj (ai, NULL);
354 
355  return (ADJ_WALK_RC_CONTINUE);
356 }
357 
358 static void
360 {
362  fib_protocol_t nh_proto;
363  ip46_address_t nh;
364 
365  nh_proto = ip_address_to_46 (itp->itp_key, &nh);
366  idi = &itp_db.id_itf[itp->itp_sw_if_index];
367 
369  {
370  // ipsec_tun_protect_feature_set (itp, 0);
371  idi->id_itp = INDEX_INVALID;
372 
373  FOR_EACH_FIB_IP_PROTOCOL (nh_proto)
375  nh_proto, ipsec_tun_protect_adj_remove, itp);
376  }
377  else
378  {
380  nh_proto, &nh, ipsec_tun_protect_adj_remove, itp);
381 
382  hash_unset_mem (idi->id_hash, itp->itp_key);
383 
384  if (0 == hash_elts (idi->id_hash))
385  {
386  // ipsec_tun_protect_feature_set (itp, 0);
387  hash_free (idi->id_hash);
388  idi->id_hash = NULL;
389  }
391  AF_IP6 : AF_IP4);
392  }
393 }
394 
395 static void
397 {
398  ipsec_sa_t *sa;
399 
400  /* *INDENT-OFF* */
402  ({
403  if (ipsec_sa_is_set_IS_TUNNEL (sa))
404  {
405  itp->itp_crypto.src = sa->tunnel_dst_addr;
406  itp->itp_crypto.dst = sa->tunnel_src_addr;
407  ipsec_sa_set_IS_PROTECT (sa);
409  }
410  else
411  {
412  itp->itp_crypto.src = itp->itp_tun.src;
413  itp->itp_crypto.dst = itp->itp_tun.dst;
415  }
416  }));
417  /* *INDENT-ON* */
418 }
419 
420 static void
422  ipsec_tun_protect_t * itp, u32 sa_out, u32 * sas_in)
423 {
424  index_t sai;
425  u32 ii;
426 
427  itp->itp_n_sa_in = vec_len (sas_in);
428  for (ii = 0; ii < itp->itp_n_sa_in; ii++)
429  itp->itp_in_sas[ii] = sas_in[ii];
430  itp->itp_out_sa = sa_out;
431 
432  ipsec_sa_lock (itp->itp_out_sa);
433 
434  /* *INDENT-OFF* */
436  ({
437  ipsec_sa_lock(sai);
438  }));
440  /* *INDENT-ON* */
441 
442  /*
443  * add to the DB against each SA
444  */
445  ipsec_tun_protect_rx_db_add (im, itp);
447 
448  ITP_DBG (itp, "configured");
449 }
450 
451 static void
453 {
454  ipsec_sa_t *sa;
455  index_t sai;
456 
457  /* *INDENT-OFF* */
459  ({
460  ipsec_sa_unset_IS_PROTECT (sa);
461  }));
462 
465 
467 
469  ({
470  ipsec_sa_unlock(sai);
471  }));
472  /* *INDENT-ON* */
473  ITP_DBG (itp, "unconfigured");
474 }
475 
476 int
478  const ip_address_t * nh, u32 sa_out, u32 sa_in)
479 {
480  u32 *sas_in = NULL;
481  int rv;
482 
483  vec_add1 (sas_in, sa_in);
484  rv = ipsec_tun_protect_update (sw_if_index, nh, sa_out, sas_in);
485 
486  return (rv);
487 }
488 
489 int
491  const ip_address_t * nh, u32 sa_out)
492 {
493  u32 itpi, *sas_in, sai, *saip;
494  ipsec_tun_protect_t *itp;
495  ipsec_main_t *im;
496  int rv;
497 
498  sas_in = NULL;
499  rv = 0;
500  im = &ipsec_main;
501 
502  itpi = ipsec_tun_protect_find (sw_if_index, nh);
503 
504  if (INDEX_INVALID == itpi)
505  {
506  return (VNET_API_ERROR_INVALID_INTERFACE);
507  }
508 
509  itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
510 
511  /* *INDENT-OFF* */
513  ({
514  ipsec_sa_lock (sai);
515  vec_add1 (sas_in, sai);
516  }));
517  /* *INDENT-ON* */
518 
519  sa_out = ipsec_sa_find_and_lock (sa_out);
520 
521  if (~0 == sa_out)
522  {
523  rv = VNET_API_ERROR_INVALID_VALUE;
524  goto out;
525  }
526 
527  ipsec_tun_protect_unconfig (im, itp);
528  ipsec_tun_protect_config (im, itp, sa_out, sas_in);
529 
530  ipsec_sa_unlock (sa_out);
531  vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
532 
533 out:
534  vec_free (sas_in);
535  return (rv);
536 }
537 
538 int
540  const ip_address_t * nh, u32 sa_in)
541 {
542  u32 itpi, *sas_in, sa_out;
543  ipsec_tun_protect_t *itp;
544  ipsec_main_t *im;
545  int rv;
546 
547  sas_in = NULL;
548  rv = 0;
549  im = &ipsec_main;
550  itpi = ipsec_tun_protect_find (sw_if_index, nh);
551 
552  if (INDEX_INVALID == itpi)
553  {
554  return (VNET_API_ERROR_INVALID_INTERFACE);
555  }
556 
557  sa_in = ipsec_sa_find_and_lock (sa_in);
558 
559  if (~0 == sa_in)
560  {
561  rv = VNET_API_ERROR_INVALID_VALUE;
562  goto out;
563  }
564  vec_add1 (sas_in, sa_in);
565 
566  itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
567  sa_out = itp->itp_out_sa;
568 
569  ipsec_sa_lock (sa_out);
570 
571  ipsec_tun_protect_unconfig (im, itp);
572  ipsec_tun_protect_config (im, itp, sa_out, sas_in);
573 
574  ipsec_sa_unlock (sa_out);
575  ipsec_sa_unlock (sa_in);
576 out:
577  vec_free (sas_in);
578  return (rv);
579 }
580 
581 static void
583  const teib_entry_t * ne)
584 {
585  if (NULL != ne)
586  {
587  const fib_prefix_t *pfx;
588 
589  pfx = teib_entry_get_nh (ne);
590 
591  ip46_address_copy (&itp->itp_tun.dst, &pfx->fp_addr);
592  }
593  else
595 }
596 
597 int
599  const ip_address_t * nh, u32 sa_out, u32 * sas_in)
600 {
601  ipsec_tun_protect_t *itp;
602  u32 itpi, ii, *saip;
603  ipsec_main_t *im;
604  int rv;
605 
606  ITP_DBG2 ("update: %U/%U",
608  format_ip_address, nh);
609 
610  if (vec_len (sas_in) > ITP_MAX_N_SA_IN)
611  {
612  rv = VNET_API_ERROR_LIMIT_EXCEEDED;
613  goto out;
614  }
615 
616  rv = 0;
617  im = &ipsec_main;
618  if (NULL == nh)
619  nh = &IP_ADDR_ALL_0;
620  itpi = ipsec_tun_protect_find (sw_if_index, nh);
621 
622  vec_foreach_index (ii, sas_in)
623  {
624  sas_in[ii] = ipsec_sa_find_and_lock (sas_in[ii]);
625  if (~0 == sas_in[ii])
626  {
627  rv = VNET_API_ERROR_INVALID_VALUE;
628  goto out;
629  }
630  }
631 
632  sa_out = ipsec_sa_find_and_lock (sa_out);
633 
634  if (~0 == sa_out)
635  {
636  rv = VNET_API_ERROR_INVALID_VALUE;
637  goto out;
638  }
639 
640  if (INDEX_INVALID == itpi)
641  {
642  vnet_device_class_t *dev_class;
644  vnet_main_t *vnm;
645  u8 is_l2;
646 
647  vnm = vnet_get_main ();
648  hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
649  dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
650 
651  if (NULL == dev_class->ip_tun_desc)
652  {
653  rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
654  goto out;
655  }
656 
657  pool_get_zero (ipsec_tun_protect_pool, itp);
658 
660 
661  itp->itp_n_sa_in = vec_len (sas_in);
662  for (ii = 0; ii < itp->itp_n_sa_in; ii++)
663  itp->itp_in_sas[ii] = sas_in[ii];
664  itp->itp_out_sa = sa_out;
665 
666  itp->itp_key = clib_mem_alloc (sizeof (*itp->itp_key));
667  ip_address_copy (itp->itp_key, nh);
668 
669  rv = dev_class->ip_tun_desc (sw_if_index,
670  &itp->itp_tun.src,
671  &itp->itp_tun.dst, &is_l2);
672 
673  if (rv)
674  goto out;
675 
676  if (ip46_address_is_zero (&itp->itp_tun.dst))
677  {
678  /* tunnel has no destination address, presumably because it's p2mp
679  in which case we use the nh that this is protection for */
680  ip46_address_t peer;
681 
682  ip_address_to_46 (nh, &peer);
683 
685  (itp, teib_entry_find (sw_if_index, &peer));
686  }
687 
688  if (is_l2)
689  itp->itp_flags |= IPSEC_PROTECT_L2;
690 
691  /*
692  * add to the tunnel DB for ingress
693  * - if the SA is in trasnport mode, then the packates will arrivw
694  * with the IP src,dst of the protected tunnel, in which case we can
695  * simply strip the IP header and hand the payload to the protocol
696  * appropriate input handler
697  * - if the SA is in tunnel mode then there are two IP headers present
698  * one for the crytpo tunnel endpoints (described in the SA) and one
699  * for the tunnel endpoints. The outer IP headers in the srriving
700  * packets will have the crypto endpoints. So the DB needs to contain
701  * the crpto endpoint. Once the crypto header is stripped, revealing,
702  * the tunnel-IP we have 2 choices:
703  * 1) do a tunnel lookup based on the revealed header
704  * 2) skip the tunnel lookup and assume that the packet matches the
705  * one that is protected here.
706  * If we did 1) then we would allow our peer to use the SA for tunnel
707  * X to inject traffic onto tunnel Y, this is not good. If we do 2)
708  * then we don't verify that the peer is indeed using SA for tunnel
709  * X and addressing tunnel X. So we take a compromise, once the SA
710  * matches to tunnel X we veriy that the inner IP matches the value
711  * of the tunnel we are protecting, else it's dropped.
712  */
713  ipsec_tun_protect_config (im, itp, sa_out, sas_in);
714  }
715  else
716  {
717  /* updating SAs only */
718  itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
719 
720  ipsec_tun_protect_unconfig (im, itp);
721  ipsec_tun_protect_config (im, itp, sa_out, sas_in);
722  }
723 
724  ipsec_sa_unlock (sa_out);
725  vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
726  vec_free (sas_in);
727 
728 out:
729  return (rv);
730 }
731 
732 int
734 {
735  ipsec_tun_protect_t *itp;
736  ipsec_main_t *im;
737  index_t itpi;
738 
739  ITP_DBG2 ("delete: %U/%U",
741  format_ip_address, nh);
742 
743  im = &ipsec_main;
744  if (NULL == nh)
745  nh = &IP_ADDR_ALL_0;
746 
747  itpi = ipsec_tun_protect_find (sw_if_index, nh);
748 
749  if (INDEX_INVALID == itpi)
750  return (VNET_API_ERROR_NO_SUCH_ENTRY);
751 
752  itp = ipsec_tun_protect_get (itpi);
753  ipsec_tun_protect_unconfig (im, itp);
754 
755  clib_mem_free (itp->itp_key);
756  pool_put (ipsec_tun_protect_pool, itp);
757 
758  return (0);
759 }
760 
761 void
763 {
764  index_t itpi;
765 
766  /* *INDENT-OFF* */
767  pool_foreach_index(itpi, ipsec_tun_protect_pool,
768  ({
769  fn (itpi, ctx);
770  }));
771  /* *INDENT-ON* */
772 }
773 
774 void
777 {
779  ip_address_t *key;
780  index_t itpi;
781 
782  if (vec_len (itp_db.id_itf) <= sw_if_index)
783  return;
784 
785  idi = &itp_db.id_itf[sw_if_index];
786 
787  /* *INDENT-OFF* */
788  hash_foreach(key, itpi, idi->id_hash,
789  ({
790  fn (itpi, ctx);
791  }));
792  /* *INDENT-ON* */
793  if (INDEX_INVALID != idi->id_itp)
794  fn (idi->id_itp, ctx);
795 }
796 
797 static void
799 {
800  /* remove our delegate */
803 }
804 
805 static void
807 {
810 }
811 
812 static void
814 {
815  /* add our delegate if there is protection for this neighbour */
817  ip_adjacency_t *adj;
818  index_t itpi;
819 
820  if (!adj_is_midchain (ai))
821  return;
822 
823  adj = adj_get (ai);
824 
825  ip_address_from_46 (&adj->sub_type.midchain.next_hop,
826  adj->ia_nh_proto, &ip);
827 
828  itpi = ipsec_tun_protect_find (adj->rewrite_header.sw_if_index, &ip);
829 
830  if (INDEX_INVALID != itpi)
831  {
832  const ipsec_tun_protect_t *itp;
833 
834  itp = ipsec_tun_protect_get (itpi);
836  ipsec_tun_protect_add_adj (ai, itp);
837  }
838 }
839 
840 static u8 *
842 {
843  const ipsec_tun_protect_t *itp;
844 
846  s = format (s, "ipsec-tun-protect:\n%U", format_ipsec_tun_protect, itp);
847 
848  return (s);
849 }
850 
851 static void
853 {
854  const ip46_address_t *peer46;
855  ipsec_tun_protect_t *itp;
857  index_t itpi;
858 
859  peer46 = teib_entry_get_peer (ne);
860  ip_address_from_46 (peer46,
861  (ip46_address_is_ip4 (peer46) ?
863 
865 
866  if (INDEX_INVALID == itpi)
867  return;
868 
869  itp = ipsec_tun_protect_get (itpi);
874 
875  ITP_DBG (itp, "teib-added");
876 }
877 
878 static void
880 {
881  const ip46_address_t *peer46;
882  ipsec_tun_protect_t *itp;
884  index_t itpi;
885 
886  peer46 = teib_entry_get_peer (ne);
887  ip_address_from_46 (peer46,
888  (ip46_address_is_ip4 (peer46) ?
890 
892 
893  if (INDEX_INVALID == itpi)
894  return;
895 
896  itp = ipsec_tun_protect_get (itpi);
900 
901  ITP_DBG (itp, "teib-removed");
902 }
903 
904 /**
905  * VFT registered with the adjacency delegate
906  */
907 const static adj_delegate_vft_t ipsec_tun_adj_delegate_vft = {
912 };
913 
914 const static teib_vft_t ipsec_tun_teib_vft = {
916  .nv_deleted = ipsec_tun_teib_entry_deleted,
917 };
918 
919 
920 clib_error_t *
922 {
923  ipsec_main_t *im;
924 
925  im = &ipsec_main;
927  sizeof (ipsec6_tunnel_key_t),
928  sizeof (u64));
929  im->tun4_protect_by_key = hash_create (0, sizeof (u64));
930 
931  /* set up feature nodes to drop outbound packets with no crypto alg set */
933  vlib_get_node_by_name (vm, (u8 *) "esp4-no-crypto")->index;
935  vlib_get_node_by_name (vm, (u8 *) "esp6-no-crypto")->index;
937  vlib_get_node_by_name (vm, (u8 *) "esp6-encrypt-tun")->index;
939  vlib_get_node_by_name (vm, (u8 *) "esp4-encrypt-tun")->index;
940 
942  adj_delegate_register_new_type (&ipsec_tun_adj_delegate_vft);
943 
945 
946  teib_register (&ipsec_tun_teib_vft);
947 
948  return 0;
949 }
950 
952 
953 
954 /*
955  * fd.io coding-style-patch-verification: ON
956  *
957  * Local Variables:
958  * eval: (c-set-style "gnu")
959  * End:
960  */
vlib_log_class_t vlib_log_register_class(char *class, char *subclass)
Definition: log.c:209
void adj_nbr_walk_nh(u32 sw_if_index, fib_protocol_t adj_nh_proto, const ip46_address_t *nh, adj_walk_cb_t cb, void *ctx)
Walk adjacencies on a link with a given next-hop.
Definition: adj_nbr.c:642
#define vec_foreach_index(var, v)
Iterate over vector indices.
walk_rc_t(* ipsec_tun_protect_walk_cb_t)(index_t itpi, void *arg)
Definition: ipsec_tun.h:116
#define hash_set(h, key, value)
Definition: hash.h:255
static adj_walk_rc_t ipsec_tun_protect_adj_add(adj_index_t ai, void *arg)
Definition: ipsec_tun.c:245
ip46_address_t tunnel_src_addr
Definition: ipsec_sa.h:175
uword * tun6_protect_by_key
Definition: ipsec.h:129
adj_delegate_adj_deleted_t adv_adj_deleted
Definition: adj_delegate.h:96
void ip6_register_protocol(u32 protocol, u32 node_index)
Definition: ip6_forward.c:1664
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
void ipsec_unregister_udp_port(u16 port)
Definition: ipsec.c:139
#define pool_get_zero(P, E)
Allocate an object E from a pool P and zero it.
Definition: pool.h:255
vl_api_fib_path_nh_t nh
Definition: fib_types.api:126
void ip6_unregister_protocol(u32 protocol)
Definition: ip6_forward.c:1676
static void ipsec_tun_protect_adj_delegate_adj_modified(adj_delegate_t *ad)
Definition: ipsec_tun.c:806
unsigned long u64
Definition: types.h:89
static void ipsec_tun_teib_entry_added(const teib_entry_t *ne)
Definition: ipsec_tun.c:852
struct ipsec_tun_protect_db_t_ ipsec_tun_protect_db_t
u32 index
Definition: node.h:282
static void ipsec_tun_protect_add_adj(adj_index_t ai, const ipsec_tun_protect_t *itp)
Definition: ipsec_tun.c:158
IP unicast adjacency.
Definition: adj.h:227
ipsec_integ_alg_t integ_alg
Definition: ipsec_sa.h:171
static void ipsec_tun_protect_update_from_teib(ipsec_tun_protect_t *itp, const teib_entry_t *ne)
Definition: ipsec_tun.c:582
void ipsec_sa_lock(index_t sai)
Definition: ipsec_sa.c:395
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:590
void ipsec_tun_protect_walk_itf(u32 sw_if_index, ipsec_tun_protect_walk_cb_t fn, void *ctx)
Definition: ipsec_tun.c:775
static void ipsec_tun_protect_adj_delegate_adj_created(adj_index_t ai)
Definition: ipsec_tun.c:813
#define hash_set_mem(h, key, value)
Definition: hash.h:275
union ip_adjacency_t_::@137 sub_type
#define FOR_EACH_IPSEC_PROTECT_INPUT_SA(_itp, _sa, body)
Definition: ipsec_tun.h:94
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
struct _vnet_device_class vnet_device_class_t
void ip4_register_protocol(u32 protocol, u32 node_index)
Definition: ip4_forward.c:1933
static u8 ip46_address_is_ip4(const ip46_address_t *ip46)
Definition: ip46_address.h:55
u32 esp6_encrypt_l2_tun_node_index
Definition: ipsec.h:163
void ipsec_tun_protect_walk(ipsec_tun_protect_walk_cb_t fn, void *ctx)
Definition: ipsec_tun.c:762
const ip46_address_t * teib_entry_get_peer(const teib_entry_t *te)
Definition: teib.c:62
format_function_t format_vnet_sw_if_index_name
unsigned char u8
Definition: types.h:56
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
static const ipsec_tun_protect_t * ipsec_tun_protect_from_const_base(const adj_delegate_t *ad)
Definition: ipsec_tun.c:121
const ip_address_t IP_ADDR_ALL_0
Definition: ipsec_tun.c:45
static void ip46_address_reset(ip46_address_t *ip46)
Definition: ip46_address.h:74
static ip_adjacency_t * adj_get(adj_index_t adj_index)
Get a pointer to an adjacency object from its index.
Definition: adj.h:459
u32 vlib_log_class_t
Definition: vlib.h:51
ip46_address_t src
Definition: ipsec_tun.h:58
vl_api_interface_index_t sw_if_index
Definition: gre.api:53
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
ipsec_main_t ipsec_main
Definition: ipsec.c:28
static vnet_device_class_t * vnet_get_device_class(vnet_main_t *vnm, u32 dev_class_index)
int ipsec_tun_protect_del(u32 sw_if_index, const ip_address_t *nh)
Definition: ipsec_tun.c:733
static void ipsec_tun_protect_unconfig(ipsec_main_t *im, ipsec_tun_protect_t *itp)
Definition: ipsec_tun.c:452
void ipsec_register_udp_port(u16 port)
Definition: ipsec.c:164
teib_entry_added_t nv_added
Definition: teib.h:71
enum adj_walk_rc_t_ adj_walk_rc_t
return codes from a adjacency walker callback function
u8 * format_ipsec_tun_protect(u8 *s, va_list *args)
Definition: ipsec_format.c:358
#define hash_foreach(key_var, value_var, h, body)
Definition: hash.h:442
void adj_nbr_midchain_update_next_node(adj_index_t adj_index, u32 next_node)
Update the VLIB node to which packets are sent post processing.
Definition: adj_midchain.c:467
ipsec_ep_t itp_tun
Definition: ipsec_tun.h:80
Aggregate type for a prefix.
Definition: fib_types.h:203
index_t * ipsec_tun_protect_sa_by_adj_index
Adj index to TX SA mapping.
Definition: ipsec_tun.c:43
static void ipsec_tun_protect_rx_db_add(ipsec_main_t *im, const ipsec_tun_protect_t *itp)
Definition: ipsec_tun.c:199
struct ip_adjacency_t_::@137::@139 midchain
IP_LOOKUP_NEXT_MIDCHAIN.
Adj delegate.
Definition: adj_delegate.h:50
unsigned int u32
Definition: types.h:88
u32 sa_in[n_sa_in]
Definition: ipsec.api:252
u32 sa_out
Definition: ipsec.api:250
u32 esp4_encrypt_l2_tun_node_index
Definition: ipsec.h:162
#define hash_create_mem(elts, key_bytes, value_bytes)
Definition: hash.h:661
#define hash_get(h, key)
Definition: hash.h:249
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:534
#define hash_unset_mem(h, key)
Definition: hash.h:291
int adj_delegate_add(ip_adjacency_t *adj, adj_delegate_type_t adt, index_t adi)
Add a delegate to an adjacency.
Definition: adj_delegate.c:107
static_always_inline void ip46_address_copy(ip46_address_t *dst, const ip46_address_t *src)
Definition: ip46_address.h:123
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:45
index_t ipsec_sa_find_and_lock(u32 id)
Definition: ipsec_sa.c:409
ipsec_tun_protect_t * ipsec_tun_protect_pool
Pool of tunnel protection objects.
Definition: ipsec_tun.c:33
void ip_address_from_46(const ip46_address_t *nh, fib_protocol_t fproto, ip_address_t *ip)
Definition: ip_types.c:257
vlib_log_class_t ipsec_tun_protect_logger
The logger.
Definition: ipsec_tun.c:28
ip46_address_t fp_addr
The address type is not deriveable from the fp_addr member.
Definition: fib_types.h:226
u32 teib_entry_get_sw_if_index(const teib_entry_t *te)
accessors for the opaque struct
Definition: teib.c:50
static void ipsec_tun_protect_config(ipsec_main_t *im, ipsec_tun_protect_t *itp, u32 sa_out, u32 *sas_in)
Definition: ipsec_tun.c:421
int vnet_sw_interface_is_p2p(vnet_main_t *vnm, u32 sw_if_index)
Definition: interface.c:1240
static u8 ip46_address_is_zero(const ip46_address_t *ip46)
Definition: ip46_address.h:87
long ctx[MAX_CONNS]
Definition: main.c:144
enum adj_delegate_type_t_ adj_delegate_type_t
A Delagate is a means to implement the Delagation design pattern; the extension of an object&#39;s functi...
u32 esp6_no_crypto_tun_node_index
Definition: ipsec.h:159
static u32 ipsec_tun_protect_get_adj_next(const ipsec_tun_protect_t *itp)
Definition: ipsec_tun.c:129
adj_index_t ad_adj_index
The ADJ entry object to which the delagate is attached.
Definition: adj_delegate.h:55
#define hash_free(h)
Definition: hash.h:310
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:302
static void ipsec_tun_teib_entry_deleted(const teib_entry_t *ne)
Definition: ipsec_tun.c:879
fib_protocol_t ip_address_to_46(const ip_address_t *addr, ip46_address_t *a)
Definition: ip_types.c:250
void ipsec_tun_register_nodes(ip_address_family_t af)
Definition: ipsec_tun.c:88
vlib_main_t * vm
Definition: in2out_ed.c:1599
static ipsec_sa_t * ipsec_sa_get(u32 sa_index)
Definition: ipsec.h:285
vlib_node_registration_t ipsec6_tun_input_node
(constructor) VLIB_REGISTER_NODE (ipsec6_tun_input_node)
Definition: ipsec_tun_in.c:390
vl_api_address_t peer
Definition: teib.api:28
#define IP_ADDRESS_V4_ALL_0S
Definition: ip_types.h:54
ip46_address_t tunnel_dst_addr
Definition: ipsec_sa.h:176
int ipsec_tun_protect_update_one(u32 sw_if_index, const ip_address_t *nh, u32 sa_out, u32 sa_in)
Definition: ipsec_tun.c:477
static adj_delegate_type_t ipsec_tun_adj_delegate_type
Adj delegate registered type.
Definition: ipsec_tun.c:38
index_t itp_in_sas[ITP_MAX_N_SA_IN]
Definition: ipsec_tun.h:72
Definition: teib.c:29
index_t ad_index
The index passed by the provider to identify its delegate instance.
Definition: adj_delegate.h:66
static ipsec_tun_protect_db_t itp_db
Definition: ipsec_tun.c:65
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
clib_error_t * ipsec_tunnel_protect_init(vlib_main_t *vm)
Definition: ipsec_tun.c:921
u8 * format_ip_address(u8 *s, va_list *args)
Definition: ip_types.c:21
int ipsec_tun_protect_update_out(u32 sw_if_index, const ip_address_t *nh, u32 sa_out)
Definition: ipsec_tun.c:490
#define FOR_EACH_IPSEC_PROTECT_INPUT_SAI(_itp, _sai, body)
Definition: ipsec_tun.h:86
#define ITP_DBG2(_fmt, _args...)
Definition: ipsec_tun.c:79
u32 esp4_encrypt_tun_node_index
Definition: ipsec.h:136
u32 adj_index_t
An index for adjacencies.
Definition: adj_types.h:30
uword * id_hash
A hash table key&#39;d on IP (4 or 6) address.
Definition: ipsec_tun.c:53
u32 esp4_no_crypto_tun_node_index
Definition: ipsec.h:158
void adj_nbr_walk(u32 sw_if_index, fib_protocol_t adj_nh_proto, adj_walk_cb_t cb, void *ctx)
Walk all adjacencies on a link for a given next-hop protocol.
Definition: adj_nbr.c:552
#define ITP_MAX_N_SA_IN
Definition: ipsec_tun.h:62
void ipsec_sa_unlock(index_t sai)
Definition: ipsec_sa.c:381
struct ipsec_tun_protect_itf_db_t_ ipsec_tun_protect_itf_db_t
The DB of all added per-nh tunnel protectiond.
index_t id_itp
If the interface is P2P then there is only one protect object associated with the auto-adj for each N...
Definition: ipsec_tun.c:56
#define hash_create(elts, value_bytes)
Definition: hash.h:696
ip46_address_t dst
Definition: ipsec_tun.h:59
static uword hash_elts(void *v)
Definition: hash.h:118
#define ASSERT(truth)
adj_delegate_type_t adj_delegate_register_new_type(const adj_delegate_vft_t *vft)
adj_delegate_register_new_type
Definition: adj_delegate.c:218
static void ipsec_tun_protect_tx_db_remove(ipsec_tun_protect_t *itp)
Definition: ipsec_tun.c:359
static ipsec_tun_protect_t * ipsec_tun_protect_get(u32 index)
Definition: ipsec_tun.h:152
static void ipsec_tun_protect_adj_delegate_adj_deleted(adj_delegate_t *ad)
Definition: ipsec_tun.c:798
static void ipsec_tun_protect_rx_db_remove(ipsec_main_t *im, const ipsec_tun_protect_t *itp)
Definition: ipsec_tun.c:313
int ipsec_tun_protect_update_in(u32 sw_if_index, const ip_address_t *nh, u32 sa_in)
Definition: ipsec_tun.c:539
static void clib_mem_free(void *p)
Definition: mem.h:215
static void hash_unset_mem_free(uword **h, const void *key)
Definition: hash.h:295
static void * clib_mem_alloc(uword size)
Definition: mem.h:157
vlib_node_registration_t ipsec4_tun_input_node
(constructor) VLIB_REGISTER_NODE (ipsec4_tun_input_node)
Definition: ipsec_tun_in.c:367
vl_api_ip4_address_t hi
Definition: arp.api:37
enum ip_address_family_t_ ip_address_family_t
typedef key
Definition: ipsec_types.api:85
#define N_AF
Definition: ip_types.h:29
fib_protocol_t ia_nh_proto
The protocol of the neighbor/peer.
Definition: adj.h:342
u8 adj_is_midchain(adj_index_t ai)
Definition: adj_midchain.c:254
teib_entry_t * teib_entry_find(u32 sw_if_index, const ip46_address_t *peer)
Definition: teib.c:86
vl_api_address_t ip
Definition: l2.api:501
#define ITP_DBG(_itp, _fmt, _args...)
Definition: ipsec_tun.c:71
ipsec_ep_t itp_crypto
Definition: ipsec_tun.h:76
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define INDEX_INVALID
Invalid index - used when no index is known blazoned capitals INVALID speak volumes where ~0 does not...
Definition: dpo.h:47
static adj_walk_rc_t ipsec_tun_protect_adj_remove(adj_index_t ai, void *arg)
Definition: ipsec_tun.c:350
u64 uword
Definition: types.h:112
ipsec_protect_flags_t itp_flags
Definition: ipsec_tun.h:78
An ADJ delegate virtual function table.
Definition: adj_delegate.h:94
#define FOR_EACH_FIB_IP_PROTOCOL(_item)
Definition: fib_types.h:70
#define hash_get_mem(h, key)
Definition: hash.h:269
The DB of all added per-nh tunnel protectiond.
Definition: ipsec_tun.c:50
void ip_address_copy(ip_address_t *dst, const ip_address_t *src)
Definition: ip_types.c:130
ipsec_crypto_alg_t crypto_alg
Definition: ipsec_sa.h:167
void adj_nbr_midchain_reset_next_node(adj_index_t adj_index)
Return the adjacency&#39;s next node to its default value.
Definition: adj_midchain.c:493
static u8 * ipsec_tun_protect_adj_delegate_format(const adj_delegate_t *aed, u8 *s)
Definition: ipsec_tun.c:841
static void ipsec_tun_protect_tx_db_add(ipsec_tun_protect_t *itp)
Definition: ipsec_tun.c:256
static void ipsec_tun_protect_set_crypto_addr(ipsec_tun_protect_t *itp)
Definition: ipsec_tun.c:396
#define vec_foreach(var, vec)
Vector iterator.
void ipsec_tun_unregister_nodes(ip_address_family_t af)
Definition: ipsec_tun.c:105
ip_address_t * itp_key
Definition: ipsec_tun.h:82
static void hash_set_mem_alloc(uword **h, const void *key, uword v)
Definition: hash.h:279
ipsec_tun_protect_itf_db_t * id_itf
Per-interface vector.
Definition: ipsec_tun.c:62
#define pool_foreach_index(i, v, body)
Iterate pool by index.
Definition: pool.h:558
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
Definition: vec.h:554
static u32 ipsec_tun_node_regs[N_AF]
Definition: ipsec_tun.c:85
void teib_register(const teib_vft_t *vft)
Definition: teib.c:223
static index_t ipsec_tun_protect_find(u32 sw_if_index, const ip_address_t *nh)
Definition: ipsec_tun.c:177
void ip4_unregister_protocol(u32 protocolx)
Definition: ip4_forward.c:1945
u32 esp6_encrypt_tun_node_index
Definition: ipsec.h:142
void adj_delegate_remove(adj_index_t ai, adj_delegate_type_t type)
Remove a delegate from an adjacency.
Definition: adj_delegate.c:65
uword * tun4_protect_by_key
Definition: ipsec.h:128
const fib_prefix_t * teib_entry_get_nh(const teib_entry_t *te)
Definition: teib.c:68
int ipsec_tun_protect_update(u32 sw_if_index, const ip_address_t *nh, u32 sa_out, u32 *sas_in)
Definition: ipsec_tun.c:598