FD.io VPP  v20.09-64-g4f7b92f0a
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/ipsec_itf.h>
20 #include <vnet/ipsec/esp.h>
21 #include <vnet/udp/udp.h>
22 #include <vnet/adj/adj_delegate.h>
23 #include <vnet/adj/adj_midchain.h>
24 #include <vnet/teib/teib.h>
25 
26 /**
27  * The logger
28  */
30 
31 /**
32  * Pool of tunnel protection objects
33  */
35 
36 /**
37  * Adj delegate registered type
38  */
40 
41 /**
42  * Adj index to TX SA mapping
43  */
45 
46 const ip_address_t IP_ADDR_ALL_0 = IP_ADDRESS_V4_ALL_0S;
47 
48 /**
49  * The DB of all added per-nh tunnel protectiond
50  */
52 {
53  /** A hash table key'd on IP (4 or 6) address */
55  /** If the interface is P2P then there is only one protect
56  * object associated with the auto-adj for each NH proto */
59 
61 {
62  /** Per-interface vector */
65 
67 
68 const static ipsec_tun_protect_itf_db_t IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY = {
70 };
71 
72 #define ITP_DBG(_itp, _fmt, _args...) \
73 { \
74  vlib_log_debug(ipsec_tun_protect_logger, \
75  "[%U]: " _fmt, \
76  format_ipsec_tun_protect, \
77  _itp, ##_args); \
78 }
79 
80 #define ITP_DBG2(_fmt, _args...) \
81 { \
82  vlib_log_debug(ipsec_tun_protect_logger, \
83  _fmt, ##_args); \
84 }
85 
87 
88 void
90 {
91  if (0 == ipsec_tun_node_regs[af]++)
92  {
93  if (AF_IP4 == af)
94  {
95  ipsec_register_udp_port (UDP_DST_PORT_ipsec);
96  ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
97  ipsec4_tun_input_node.index);
98  }
99  else
100  ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP,
101  ipsec6_tun_input_node.index);
102  }
103 }
104 
105 void
107 {
108  ASSERT (0 != ipsec_tun_node_regs[af]);
109  if (0 == --ipsec_tun_node_regs[af])
110  {
111  if (AF_IP4 == af)
112  {
113  ipsec_unregister_udp_port (UDP_DST_PORT_ipsec);
114  ip4_unregister_protocol (IP_PROTOCOL_IPSEC_ESP);
115  }
116  else
117  ip6_unregister_protocol (IP_PROTOCOL_IPSEC_ESP);
118  }
119 }
120 
121 static inline const ipsec_tun_protect_t *
123 {
124  if (ad == NULL)
125  return (NULL);
126  return (pool_elt_at_index (ipsec_tun_protect_pool, ad->ad_index));
127 }
128 
129 static u32
131  const ipsec_tun_protect_t * itp)
132 {
133  ipsec_main_t *im;
134  ipsec_sa_t *sa;
135  bool is_ip4;
136  u32 next;
137 
138 
139  if (itp->itp_flags & IPSEC_PROTECT_ITF)
140  is_ip4 = linkt == VNET_LINK_IP4;
141  else
142  is_ip4 = ip46_address_is_ip4 (&itp->itp_tun.src);
143 
144  sa = ipsec_sa_get (itp->itp_out_sa);
145  im = &ipsec_main;
146 
147  if (sa->crypto_alg == IPSEC_CRYPTO_ALG_NONE &&
148  sa->integ_alg == IPSEC_INTEG_ALG_NONE)
149  next = (is_ip4 ?
152  else if (itp->itp_flags & IPSEC_PROTECT_L2)
153  next = (is_ip4 ?
156  else
157  next = (is_ip4 ?
160 
161  return (next);
162 }
163 
164 static void
166 {
168  INDEX_INVALID);
169 
170  if (NULL == itp)
171  {
174  }
175  else
176  {
180  }
181 }
182 
183 static index_t
185 {
187  uword *p;
188 
189  if (vec_len (itp_db.id_itf) <= sw_if_index)
190  return INDEX_INVALID;
191 
192  if (vnet_sw_interface_is_p2p (vnet_get_main (), sw_if_index))
193  return (itp_db.id_itf[sw_if_index].id_itp);
194 
195  idi = &itp_db.id_itf[sw_if_index];
196  p = hash_get_mem (idi->id_hash, nh);
197 
198  if (NULL == p)
199  {
200  return INDEX_INVALID;
201  }
202  return (p[0]);
203 }
204 
205 static void
207  const ipsec_tun_protect_t * itp)
208 {
209  const ipsec_sa_t *sa;
210  u32 sai;
211 
212  if (ip46_address_is_zero (&itp->itp_crypto.dst))
213  return;
214 
215  /* *INDENT-OFF* */
217  ({
218  sa = ipsec_sa_get (sai);
219 
222  .sa_index = sai,
223  };
224 
225  /*
226  * The key is formed from the tunnel's destination
227  * as the packet lookup is done from the packet's source
228  */
229  if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
230  {
231  ipsec4_tunnel_key_t key = {
232  .remote_ip = itp->itp_crypto.dst.ip4,
233  .spi = clib_host_to_net_u32 (sa->spi),
234  };
235  hash_set (im->tun4_protect_by_key, key.as_u64, res.as_u64);
237  }
238  else
239  {
240  ipsec6_tunnel_key_t key = {
241  .remote_ip = itp->itp_crypto.dst.ip6,
242  .spi = clib_host_to_net_u32 (sa->spi),
243  };
246  }
247  }))
248  /* *INDENT-ON* */
249 }
250 
251 static adj_walk_rc_t
253 {
254  ipsec_tun_protect_t *itp = arg;
256  itp - ipsec_tun_protect_pool);
257  ipsec_tun_protect_add_adj (ai, itp);
258 
259  if (itp->itp_flags & IPSEC_PROTECT_ITF)
260  ipsec_itf_adj_stack (ai, itp->itp_out_sa);
261 
262  return (ADJ_WALK_RC_CONTINUE);
263 }
264 
265 static void
267 {
268  /*
269  * add the delegate to the adj
270  */
272  fib_protocol_t nh_proto;
273  ip46_address_t nh;
274 
276  itp->itp_sw_if_index,
277  IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY);
278 
279  idi = &itp_db.id_itf[itp->itp_sw_if_index];
280 
282  {
283  if (INDEX_INVALID == idi->id_itp)
284  {
285  // ipsec_tun_protect_feature_set (itp, 1);
286  }
287  idi->id_itp = itp - ipsec_tun_protect_pool;
288 
289  FOR_EACH_FIB_IP_PROTOCOL (nh_proto)
291  nh_proto, ipsec_tun_protect_adj_add, itp);
292  }
293  else
294  {
295  if (NULL == idi->id_hash)
296  {
297  idi->id_hash =
298  hash_create_mem (0, sizeof (ip_address_t), sizeof (uword));
299  /*
300  * enable the encrypt feature for egress if this is the first addition
301  * on this interface
302  */
303  // ipsec_tun_protect_feature_set (itp, 1);
304  }
305 
306  hash_set_mem (idi->id_hash, itp->itp_key, itp - ipsec_tun_protect_pool);
307 
308  /*
309  * walk all the adjs with the same nh on this interface
310  * to associate them with this protection
311  */
312  nh_proto = ip_address_to_46 (itp->itp_key, &nh);
313 
315  nh_proto, &nh, ipsec_tun_protect_adj_add, itp);
316 
318  AF_IP6 : AF_IP4);
319  }
320 }
321 
322 static void
324  const ipsec_tun_protect_t * itp)
325 {
326  const ipsec_sa_t *sa;
327 
328  /* *INDENT-OFF* */
330  ({
331  if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
332  {
333  ipsec4_tunnel_key_t key = {
334  .remote_ip = itp->itp_crypto.dst.ip4,
335  .spi = clib_host_to_net_u32 (sa->spi),
336  };
337  if (hash_get(im->tun4_protect_by_key, key.as_u64))
338  {
339  hash_unset (im->tun4_protect_by_key, key.as_u64);
340  ipsec_tun_unregister_nodes(AF_IP4);
341  }
342  }
343  else
344  {
345  ipsec6_tunnel_key_t key = {
346  .remote_ip = itp->itp_crypto.dst.ip6,
347  .spi = clib_host_to_net_u32 (sa->spi),
348  };
349  if (hash_get_mem(im->tun6_protect_by_key, &key))
350  {
353  }
354  }
355  }));
356  /* *INDENT-ON* */
357 }
358 
359 static adj_walk_rc_t
361 {
362  ipsec_tun_protect_t *itp = arg;
363 
365  ipsec_tun_protect_add_adj (ai, NULL);
366 
367  if (itp->itp_flags & IPSEC_PROTECT_ITF)
369 
370  return (ADJ_WALK_RC_CONTINUE);
371 }
372 
373 static void
375 {
377  fib_protocol_t nh_proto;
378  ip46_address_t nh;
379 
380  nh_proto = ip_address_to_46 (itp->itp_key, &nh);
381  idi = &itp_db.id_itf[itp->itp_sw_if_index];
382 
384  {
385  // ipsec_tun_protect_feature_set (itp, 0);
386  idi->id_itp = INDEX_INVALID;
387 
388  FOR_EACH_FIB_IP_PROTOCOL (nh_proto)
390  nh_proto, ipsec_tun_protect_adj_remove, itp);
391  }
392  else
393  {
395  nh_proto, &nh, ipsec_tun_protect_adj_remove, itp);
396 
397  hash_unset_mem (idi->id_hash, itp->itp_key);
398 
399  if (0 == hash_elts (idi->id_hash))
400  {
401  // ipsec_tun_protect_feature_set (itp, 0);
402  hash_free (idi->id_hash);
403  idi->id_hash = NULL;
404  }
406  AF_IP6 : AF_IP4);
407  }
408 }
409 
410 static void
412 {
413  ipsec_sa_t *sa;
414 
415  /* *INDENT-OFF* */
417  ({
418  if (ipsec_sa_is_set_IS_TUNNEL (sa))
419  {
420  itp->itp_crypto.src = sa->tunnel_dst_addr;
421  itp->itp_crypto.dst = sa->tunnel_src_addr;
422  if (!(itp->itp_flags & IPSEC_PROTECT_ITF))
423  {
424  ipsec_sa_set_IS_PROTECT (sa);
425  itp->itp_flags |= IPSEC_PROTECT_ENCAPED;
426  }
427  }
428  else
429  {
430  itp->itp_crypto.src = itp->itp_tun.src;
431  itp->itp_crypto.dst = itp->itp_tun.dst;
432  itp->itp_flags &= ~IPSEC_PROTECT_ENCAPED;
433  }
434  }));
435  /* *INDENT-ON* */
436 }
437 
438 static void
440  ipsec_tun_protect_t * itp, u32 sa_out, u32 * sas_in)
441 {
442  index_t sai;
443  u32 ii;
444 
445  itp->itp_n_sa_in = vec_len (sas_in);
446  for (ii = 0; ii < itp->itp_n_sa_in; ii++)
447  itp->itp_in_sas[ii] = sas_in[ii];
448  itp->itp_out_sa = sa_out;
449 
450  ipsec_sa_lock (itp->itp_out_sa);
451 
452  /* *INDENT-OFF* */
454  ({
455  ipsec_sa_lock(sai);
456  }));
458  /* *INDENT-ON* */
459 
460  /*
461  * add to the DB against each SA
462  */
463  ipsec_tun_protect_rx_db_add (im, itp);
465 
466  ITP_DBG (itp, "configured");
467 }
468 
469 static void
471 {
472  ipsec_sa_t *sa;
473  index_t sai;
474 
475  /* *INDENT-OFF* */
477  ({
478  ipsec_sa_unset_IS_PROTECT (sa);
479  }));
480 
483 
485 
487  ({
488  ipsec_sa_unlock(sai);
489  }));
490  /* *INDENT-ON* */
491  ITP_DBG (itp, "unconfigured");
492 }
493 
494 int
496  const ip_address_t * nh, u32 sa_out, u32 sa_in)
497 {
498  u32 *sas_in = NULL;
499  int rv;
500 
501  vec_add1 (sas_in, sa_in);
502  rv = ipsec_tun_protect_update (sw_if_index, nh, sa_out, sas_in);
503 
504  return (rv);
505 }
506 
507 int
509  const ip_address_t * nh, u32 sa_out)
510 {
511  u32 itpi, *sas_in, sai, *saip;
512  ipsec_tun_protect_t *itp;
513  ipsec_main_t *im;
514  int rv;
515 
516  sas_in = NULL;
517  rv = 0;
518  im = &ipsec_main;
519 
520  itpi = ipsec_tun_protect_find (sw_if_index, nh);
521 
522  if (INDEX_INVALID == itpi)
523  {
524  return (VNET_API_ERROR_INVALID_INTERFACE);
525  }
526 
527  itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
528 
529  /* *INDENT-OFF* */
531  ({
532  ipsec_sa_lock (sai);
533  vec_add1 (sas_in, sai);
534  }));
535  /* *INDENT-ON* */
536 
537  sa_out = ipsec_sa_find_and_lock (sa_out);
538 
539  if (~0 == sa_out)
540  {
541  rv = VNET_API_ERROR_INVALID_VALUE;
542  goto out;
543  }
544 
545  ipsec_tun_protect_unconfig (im, itp);
546  ipsec_tun_protect_config (im, itp, sa_out, sas_in);
547 
548  ipsec_sa_unlock (sa_out);
549  vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
550 
551 out:
552  vec_free (sas_in);
553  return (rv);
554 }
555 
556 int
558  const ip_address_t * nh, u32 sa_in)
559 {
560  u32 itpi, *sas_in, sa_out;
561  ipsec_tun_protect_t *itp;
562  ipsec_main_t *im;
563  int rv;
564 
565  sas_in = NULL;
566  rv = 0;
567  im = &ipsec_main;
568  itpi = ipsec_tun_protect_find (sw_if_index, nh);
569 
570  if (INDEX_INVALID == itpi)
571  {
572  return (VNET_API_ERROR_INVALID_INTERFACE);
573  }
574 
575  sa_in = ipsec_sa_find_and_lock (sa_in);
576 
577  if (~0 == sa_in)
578  {
579  rv = VNET_API_ERROR_INVALID_VALUE;
580  goto out;
581  }
582  vec_add1 (sas_in, sa_in);
583 
584  itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
585  sa_out = itp->itp_out_sa;
586 
587  ipsec_sa_lock (sa_out);
588 
589  ipsec_tun_protect_unconfig (im, itp);
590  ipsec_tun_protect_config (im, itp, sa_out, sas_in);
591 
592  ipsec_sa_unlock (sa_out);
593  ipsec_sa_unlock (sa_in);
594 out:
595  vec_free (sas_in);
596  return (rv);
597 }
598 
599 static void
601  const teib_entry_t * ne)
602 {
603  if (NULL != ne)
604  {
605  const fib_prefix_t *pfx;
606 
607  pfx = teib_entry_get_nh (ne);
608 
609  ip46_address_copy (&itp->itp_tun.dst, &pfx->fp_addr);
610  }
611  else
613 }
614 
615 int
617  const ip_address_t * nh, u32 sa_out, u32 * sas_in)
618 {
619  ipsec_tun_protect_t *itp;
620  u32 itpi, ii, *saip;
621  ipsec_main_t *im;
622  int rv;
623 
624  ITP_DBG2 ("update: %U/%U",
626  format_ip_address, nh);
627 
628  if (vec_len (sas_in) > ITP_MAX_N_SA_IN)
629  {
630  rv = VNET_API_ERROR_LIMIT_EXCEEDED;
631  goto out;
632  }
633 
634  rv = 0;
635  im = &ipsec_main;
636  if (NULL == nh)
637  nh = &IP_ADDR_ALL_0;
638  itpi = ipsec_tun_protect_find (sw_if_index, nh);
639 
640  vec_foreach_index (ii, sas_in)
641  {
642  sas_in[ii] = ipsec_sa_find_and_lock (sas_in[ii]);
643  if (~0 == sas_in[ii])
644  {
645  rv = VNET_API_ERROR_INVALID_VALUE;
646  goto out;
647  }
648  }
649 
650  sa_out = ipsec_sa_find_and_lock (sa_out);
651 
652  if (~0 == sa_out)
653  {
654  rv = VNET_API_ERROR_INVALID_VALUE;
655  goto out;
656  }
657 
658  if (INDEX_INVALID == itpi)
659  {
660  vnet_device_class_t *dev_class;
662  vnet_main_t *vnm;
663  u8 is_l2;
664 
665  vnm = vnet_get_main ();
666  hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
667  dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
668 
669  if (NULL == dev_class->ip_tun_desc)
670  {
671  rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
672  goto out;
673  }
674 
675  pool_get_zero (ipsec_tun_protect_pool, itp);
676 
678  itp->itp_ai = ADJ_INDEX_INVALID;
679 
680  itp->itp_n_sa_in = vec_len (sas_in);
681  for (ii = 0; ii < itp->itp_n_sa_in; ii++)
682  itp->itp_in_sas[ii] = sas_in[ii];
683  itp->itp_out_sa = sa_out;
684 
685  itp->itp_key = clib_mem_alloc (sizeof (*itp->itp_key));
686  ip_address_copy (itp->itp_key, nh);
687 
688  rv = dev_class->ip_tun_desc (sw_if_index,
689  &itp->itp_tun.src,
690  &itp->itp_tun.dst, &is_l2);
691 
692  if (rv)
693  goto out;
694 
695  if (ip46_address_is_zero (&itp->itp_tun.src))
696  {
697  /* must be one of thos pesky ipsec interfaces that has no encap.
698  * the encap then MUST comefrom the tunnel mode SA.
699  */
700  ipsec_sa_t *sa;
701 
702  sa = ipsec_sa_get (itp->itp_out_sa);
703 
704  if (!ipsec_sa_is_set_IS_TUNNEL (sa))
705  {
706  rv = VNET_API_ERROR_INVALID_DST_ADDRESS;
707  goto out;
708  }
709 
710  itp->itp_flags |= IPSEC_PROTECT_ITF;
711  }
712  else if (ip46_address_is_zero (&itp->itp_tun.dst))
713  {
714  /* tunnel has no destination address, presumably because it's p2mp
715  in which case we use the nh that this is protection for */
716  ip46_address_t peer;
717 
718  ip_address_to_46 (nh, &peer);
719 
721  (itp, teib_entry_find (sw_if_index, &peer));
722  }
723 
724  if (is_l2)
725  itp->itp_flags |= IPSEC_PROTECT_L2;
726 
727  /*
728  * add to the tunnel DB for ingress
729  * - if the SA is in trasnport mode, then the packates will arrive
730  * with the IP src,dst of the protected tunnel, in which case we can
731  * simply strip the IP header and hand the payload to the protocol
732  * appropriate input handler
733  * - if the SA is in tunnel mode then there are two IP headers present
734  * one for the crytpo tunnel endpoints (described in the SA) and one
735  * for the tunnel endpoints. The outer IP headers in the srriving
736  * packets will have the crypto endpoints. So the DB needs to contain
737  * the crpto endpoint. Once the crypto header is stripped, revealing,
738  * the tunnel-IP we have 2 choices:
739  * 1) do a tunnel lookup based on the revealed header
740  * 2) skip the tunnel lookup and assume that the packet matches the
741  * one that is protected here.
742  * If we did 1) then we would allow our peer to use the SA for tunnel
743  * X to inject traffic onto tunnel Y, this is not good. If we do 2)
744  * then we don't verify that the peer is indeed using SA for tunnel
745  * X and addressing tunnel X. So we take a compromise, once the SA
746  * matches to tunnel X we veriy that the inner IP matches the value
747  * of the tunnel we are protecting, else it's dropped.
748  */
749  ipsec_tun_protect_config (im, itp, sa_out, sas_in);
750  }
751  else
752  {
753  /* updating SAs only */
754  itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
755 
756  ipsec_tun_protect_unconfig (im, itp);
757  ipsec_tun_protect_config (im, itp, sa_out, sas_in);
758  }
759 
760  ipsec_sa_unlock (sa_out);
761  vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
762  vec_free (sas_in);
763 
764 out:
765  return (rv);
766 }
767 
768 int
770 {
771  ipsec_tun_protect_t *itp;
772  ipsec_main_t *im;
773  index_t itpi;
774 
775  ITP_DBG2 ("delete: %U/%U",
777  format_ip_address, nh);
778 
779  im = &ipsec_main;
780  if (NULL == nh)
781  nh = &IP_ADDR_ALL_0;
782 
783  itpi = ipsec_tun_protect_find (sw_if_index, nh);
784 
785  if (INDEX_INVALID == itpi)
786  return (VNET_API_ERROR_NO_SUCH_ENTRY);
787 
788  itp = ipsec_tun_protect_get (itpi);
789  ipsec_tun_protect_unconfig (im, itp);
790 
791  if (ADJ_INDEX_INVALID != itp->itp_ai)
792  adj_unlock (itp->itp_ai);
793 
794  clib_mem_free (itp->itp_key);
795  pool_put (ipsec_tun_protect_pool, itp);
796 
797  return (0);
798 }
799 
800 void
802 {
803  index_t itpi;
804 
805  /* *INDENT-OFF* */
806  pool_foreach_index(itpi, ipsec_tun_protect_pool,
807  ({
808  fn (itpi, ctx);
809  }));
810  /* *INDENT-ON* */
811 }
812 
813 void
816 {
818  ip_address_t *key;
819  index_t itpi;
820 
821  if (vec_len (itp_db.id_itf) <= sw_if_index)
822  return;
823 
824  idi = &itp_db.id_itf[sw_if_index];
825 
826  /* *INDENT-OFF* */
827  hash_foreach(key, itpi, idi->id_hash,
828  ({
829  fn (itpi, ctx);
830  }));
831  /* *INDENT-ON* */
832  if (INDEX_INVALID != idi->id_itp)
833  fn (idi->id_itp, ctx);
834 }
835 
836 static void
838 {
839  /* remove our delegate */
842 }
843 
844 static void
846 {
849 }
850 
851 static void
853 {
854  /* add our delegate if there is protection for this neighbour */
856  ip_adjacency_t *adj;
857  index_t itpi;
858 
859  if (!adj_is_midchain (ai))
860  return;
861 
862  adj = adj_get (ai);
863 
864  ip_address_from_46 (&adj->sub_type.midchain.next_hop,
865  adj->ia_nh_proto, &ip);
866 
867  itpi = ipsec_tun_protect_find (adj->rewrite_header.sw_if_index, &ip);
868 
869  if (INDEX_INVALID != itpi)
871 }
872 
873 static u8 *
875 {
876  const ipsec_tun_protect_t *itp;
877 
879  s = format (s, "ipsec-tun-protect:\n%U", format_ipsec_tun_protect, itp);
880 
881  return (s);
882 }
883 
884 static void
886 {
887  const ip46_address_t *peer46;
888  ipsec_tun_protect_t *itp;
890  index_t itpi;
891 
892  peer46 = teib_entry_get_peer (ne);
893  ip_address_from_46 (peer46,
894  (ip46_address_is_ip4 (peer46) ?
896 
898 
899  if (INDEX_INVALID == itpi)
900  return;
901 
902  itp = ipsec_tun_protect_get (itpi);
907 
908  ITP_DBG (itp, "teib-added");
909 }
910 
911 static void
913 {
914  const ip46_address_t *peer46;
915  ipsec_tun_protect_t *itp;
917  index_t itpi;
918 
919  peer46 = teib_entry_get_peer (ne);
920  ip_address_from_46 (peer46,
921  (ip46_address_is_ip4 (peer46) ?
923 
925 
926  if (INDEX_INVALID == itpi)
927  return;
928 
929  itp = ipsec_tun_protect_get (itpi);
933 
934  ITP_DBG (itp, "teib-removed");
935 }
936 
937 /**
938  * VFT registered with the adjacency delegate
939  */
940 const static adj_delegate_vft_t ipsec_tun_adj_delegate_vft = {
945 };
946 
947 const static teib_vft_t ipsec_tun_teib_vft = {
949  .nv_deleted = ipsec_tun_teib_entry_deleted,
950 };
951 
952 
953 clib_error_t *
955 {
956  ipsec_main_t *im;
957 
958  im = &ipsec_main;
960  sizeof (ipsec6_tunnel_key_t),
961  sizeof (u64));
962  im->tun4_protect_by_key = hash_create (0, sizeof (u64));
963 
964  /* set up feature nodes to drop outbound packets with no crypto alg set */
966  vlib_get_node_by_name (vm, (u8 *) "esp4-no-crypto")->index;
968  vlib_get_node_by_name (vm, (u8 *) "esp6-no-crypto")->index;
970  vlib_get_node_by_name (vm, (u8 *) "esp6-encrypt-tun")->index;
972  vlib_get_node_by_name (vm, (u8 *) "esp4-encrypt-tun")->index;
973 
975  adj_delegate_register_new_type (&ipsec_tun_adj_delegate_vft);
976 
978 
979  teib_register (&ipsec_tun_teib_vft);
980 
981  return 0;
982 }
983 
985 
986 
987 /*
988  * fd.io coding-style-patch-verification: ON
989  *
990  * Local Variables:
991  * eval: (c-set-style "gnu")
992  * End:
993  */
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:663
#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:126
#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:252
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:845
unsigned long u64
Definition: types.h:89
static void ipsec_tun_teib_entry_added(const teib_entry_t *ne)
Definition: ipsec_tun.c:885
vnet_link_t adj_get_link_type(adj_index_t ai)
Return the link type of the adjacency.
Definition: adj.c:507
struct ipsec_tun_protect_db_t_ ipsec_tun_protect_db_t
u32 index
Definition: node.h:279
static void ipsec_tun_protect_add_adj(adj_index_t ai, const ipsec_tun_protect_t *itp)
Definition: ipsec_tun.c:165
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:600
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:592
void ipsec_tun_protect_walk_itf(u32 sw_if_index, ipsec_tun_protect_walk_cb_t fn, void *ctx)
Definition: ipsec_tun.c:814
static void ipsec_tun_protect_adj_delegate_adj_created(adj_index_t ai)
Definition: ipsec_tun.c:852
#define hash_set_mem(h, key, value)
Definition: hash.h:275
vlib_main_t * vm
Definition: in2out_ed.c:1582
#define FOR_EACH_IPSEC_PROTECT_INPUT_SA(_itp, _sa, body)
Definition: ipsec_tun.h:104
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:801
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:122
const ip_address_t IP_ADDR_ALL_0
Definition: ipsec_tun.c:46
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
void ipsec_itf_adj_unstack(adj_index_t ai)
Definition: ipsec_itf.c:51
ip46_address_t src
Definition: ipsec_tun.h:67
#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:769
static void ipsec_tun_protect_unconfig(ipsec_main_t *im, ipsec_tun_protect_t *itp)
Definition: ipsec_tun.c:470
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:373
#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:90
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:44
static void ipsec_tun_protect_rx_db_add(ipsec_main_t *im, const ipsec_tun_protect_t *itp)
Definition: ipsec_tun.c:206
void adj_unlock(adj_index_t adj_index)
Release a reference counting lock on the adjacency.
Definition: adj.c:348
Adj delegate.
Definition: adj_delegate.h:50
unsigned int u32
Definition: types.h:88
adj_index_t itp_ai
Definition: ipsec_tun.h:88
u32 sa_in[n_sa_in]
Definition: ipsec.api:253
struct ip_adjacency_t_::@149::@151 midchain
IP_LOOKUP_NEXT_MIDCHAIN.
union ip_adjacency_t_::@149 sub_type
u32 sa_out
Definition: ipsec.api:251
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 ADJ_INDEX_INVALID
Invalid ADJ index - used when no adj is known likewise blazoned capitals INVALID speak volumes where ...
Definition: adj_types.h:36
#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:34
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:29
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:439
int vnet_sw_interface_is_p2p(vnet_main_t *vnm, u32 sw_if_index)
Definition: interface.c:1244
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
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:912
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:89
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:495
static adj_delegate_type_t ipsec_tun_adj_delegate_type
Adj delegate registered type.
Definition: ipsec_tun.c:39
index_t itp_in_sas[ITP_MAX_N_SA_IN]
Definition: ipsec_tun.h:81
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:66
#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:954
void ipsec_itf_adj_stack(adj_index_t ai, u32 sai)
Definition: ipsec_itf.c:57
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:508
#define FOR_EACH_IPSEC_PROTECT_INPUT_SAI(_itp, _sai, body)
Definition: ipsec_tun.h:96
#define ITP_DBG2(_fmt, _args...)
Definition: ipsec_tun.c:80
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:54
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:571
#define ITP_MAX_N_SA_IN
Definition: ipsec_tun.h:71
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:57
#define hash_create(elts, value_bytes)
Definition: hash.h:696
ip46_address_t dst
Definition: ipsec_tun.h:68
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:374
enum vnet_link_t_ vnet_link_t
Link Type: A description of the protocol of packets on the link.
static ipsec_tun_protect_t * ipsec_tun_protect_get(u32 index)
Definition: ipsec_tun.h:162
static void ipsec_tun_protect_adj_delegate_adj_deleted(adj_delegate_t *ad)
Definition: ipsec_tun.c:837
static void ipsec_tun_protect_rx_db_remove(ipsec_main_t *im, const ipsec_tun_protect_t *itp)
Definition: ipsec_tun.c:323
int ipsec_tun_protect_update_in(u32 sw_if_index, const ip_address_t *nh, u32 sa_in)
Definition: ipsec_tun.c:557
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
static u32 ipsec_tun_protect_get_adj_next(vnet_link_t linkt, const ipsec_tun_protect_t *itp)
Definition: ipsec_tun.c:130
#define ITP_DBG(_itp, _fmt, _args...)
Definition: ipsec_tun.c:72
ipsec_ep_t itp_crypto
Definition: ipsec_tun.h:85
#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:360
u64 uword
Definition: types.h:112
ipsec_protect_flags_t itp_flags
Definition: ipsec_tun.h:87
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:51
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:874
static void ipsec_tun_protect_tx_db_add(ipsec_tun_protect_t *itp)
Definition: ipsec_tun.c:266
static void ipsec_tun_protect_set_crypto_addr(ipsec_tun_protect_t *itp)
Definition: ipsec_tun.c:411
#define vec_foreach(var, vec)
Vector iterator.
void ipsec_tun_unregister_nodes(ip_address_family_t af)
Definition: ipsec_tun.c:106
ip_address_t * itp_key
Definition: ipsec_tun.h:92
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:63
#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:556
static u32 ipsec_tun_node_regs[N_AF]
Definition: ipsec_tun.c:86
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:184
void ip4_unregister_protocol(u32 protocolx)
Definition: ip4_forward.c:1945
u32 esp6_encrypt_tun_node_index
Definition: ipsec.h:142
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:33
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:616