2 * ipsec_tun.h : IPSEC tunnel protection
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
26 #define IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS (64 * 1024)
27 #define IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE 512 << 20
32 vlib_log_class_t ipsec_tun_protect_logger;
35 * Pool of tunnel protection objects
37 ipsec_tun_protect_t *ipsec_tun_protect_pool;
40 * Adj delegate registered type
42 static adj_delegate_type_t ipsec_tun_adj_delegate_type;
45 * Adj index to TX SA mapping
47 index_t *ipsec_tun_protect_sa_by_adj_index;
49 const ip_address_t IP_ADDR_ALL_0 = IP_ADDRESS_V4_ALL_0S;
52 * The DB of all added per-nh tunnel protectiond
54 typedef struct ipsec_tun_protect_itf_db_t_
56 /** A hash table key'd on IP (4 or 6) address */
58 /** If the interface is P2P then there is only one protect
59 * object associated with the auto-adj for each NH proto */
61 } ipsec_tun_protect_itf_db_t;
63 typedef struct ipsec_tun_protect_db_t_
65 /** Per-interface vector */
66 ipsec_tun_protect_itf_db_t *id_itf;
67 } ipsec_tun_protect_db_t;
69 static ipsec_tun_protect_db_t itp_db;
71 const static ipsec_tun_protect_itf_db_t IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY = {
72 .id_itp = INDEX_INVALID,
75 #define ITP_DBG(_itp, _fmt, _args...) \
77 vlib_log_debug(ipsec_tun_protect_logger, \
79 format_ipsec_tun_protect, \
83 #define ITP_DBG2(_fmt, _args...) \
85 vlib_log_debug(ipsec_tun_protect_logger, \
89 static u32 ipsec_tun_node_regs[N_AF];
92 ipsec_tun_register_nodes (ip_address_family_t af)
94 if (0 == ipsec_tun_node_regs[af]++)
98 ipsec_register_udp_port (UDP_DST_PORT_ipsec);
99 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
100 ipsec4_tun_input_node.index);
103 ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP,
104 ipsec6_tun_input_node.index);
109 ipsec_tun_unregister_nodes (ip_address_family_t af)
111 ASSERT (0 != ipsec_tun_node_regs[af]);
112 if (0 == --ipsec_tun_node_regs[af])
116 ipsec_unregister_udp_port (UDP_DST_PORT_ipsec);
117 ip4_unregister_protocol (IP_PROTOCOL_IPSEC_ESP);
120 ip6_unregister_protocol (IP_PROTOCOL_IPSEC_ESP);
124 static inline const ipsec_tun_protect_t *
125 ipsec_tun_protect_from_const_base (const adj_delegate_t * ad)
129 return (pool_elt_at_index (ipsec_tun_protect_pool, ad->ad_index));
133 ipsec_tun_protect_get_adj_next (vnet_link_t linkt,
134 const ipsec_tun_protect_t * itp)
142 if (itp->itp_flags & IPSEC_PROTECT_ITF)
143 is_ip4 = linkt == VNET_LINK_IP4;
145 is_ip4 = ip46_address_is_ip4 (&itp->itp_tun.src);
147 sa = ipsec_sa_get (itp->itp_out_sa);
150 if ((sa->crypto_alg == IPSEC_CRYPTO_ALG_NONE &&
151 sa->integ_alg == IPSEC_INTEG_ALG_NONE) &&
152 !(itp->itp_flags & IPSEC_PROTECT_ITF))
154 im->esp4_no_crypto_tun_node_index :
155 im->esp6_no_crypto_tun_node_index);
156 else if (itp->itp_flags & IPSEC_PROTECT_L2)
158 im->esp4_encrypt_l2_tun_node_index :
159 im->esp6_encrypt_l2_tun_node_index);
162 im->esp4_encrypt_tun_node_index :
163 im->esp6_encrypt_tun_node_index);
169 ipsec_tun_protect_add_adj (adj_index_t ai, const ipsec_tun_protect_t * itp)
171 vec_validate_init_empty (ipsec_tun_protect_sa_by_adj_index, ai,
176 ipsec_tun_protect_sa_by_adj_index[ai] = INDEX_INVALID;
177 adj_nbr_midchain_reset_next_node (ai);
181 ipsec_tun_protect_sa_by_adj_index[ai] = itp->itp_out_sa;
182 adj_nbr_midchain_update_next_node
183 (ai, ipsec_tun_protect_get_adj_next (adj_get_link_type (ai), itp));
188 ipsec_tun_protect_find (u32 sw_if_index, const ip_address_t * nh)
190 ipsec_tun_protect_itf_db_t *idi;
193 if (vec_len (itp_db.id_itf) <= sw_if_index)
194 return INDEX_INVALID;
196 if (vnet_sw_interface_is_p2p (vnet_get_main (), sw_if_index))
197 return (itp_db.id_itf[sw_if_index].id_itp);
199 idi = &itp_db.id_itf[sw_if_index];
200 p = hash_get_mem (idi->id_hash, nh);
204 return INDEX_INVALID;
210 ipsec_tun_protect_rx_db_add (ipsec_main_t * im,
211 const ipsec_tun_protect_t * itp)
213 const ipsec_sa_t *sa;
216 if (ip46_address_is_zero (&itp->itp_crypto.dst))
220 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
222 sa = ipsec_sa_get (sai);
224 ipsec_tun_lkup_result_t res = {
225 .tun_index = itp - ipsec_tun_protect_pool,
230 * The key is formed from the tunnel's destination
231 * as the packet lookup is done from the packet's source
233 if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
235 ipsec4_tunnel_kv_t key = {
238 clib_bihash_kv_8_8_t *bkey = (clib_bihash_kv_8_8_t*)&key;
240 ipsec4_tunnel_mk_key(&key, &itp->itp_crypto.dst.ip4,
241 clib_host_to_net_u32 (sa->spi));
243 if (!im->tun4_protect_by_key.nbuckets)
244 clib_bihash_init_8_8 (&im->tun4_protect_by_key,
245 "IPSec IPv4 tunnels",
246 IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS,
247 IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE);
249 clib_bihash_add_del_8_8 (&im->tun4_protect_by_key, bkey, 1);
250 ipsec_tun_register_nodes(AF_IP4);
254 ipsec6_tunnel_kv_t key = {
256 .remote_ip = itp->itp_crypto.dst.ip6,
257 .spi = clib_host_to_net_u32 (sa->spi),
261 clib_bihash_kv_24_8_t *bkey = (clib_bihash_kv_24_8_t*)&key;
263 if (!im->tun4_protect_by_key.nbuckets)
264 clib_bihash_init_24_8 (&im->tun6_protect_by_key,
265 "IPSec IPv6 tunnels",
266 IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS,
267 IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE);
268 clib_bihash_add_del_24_8 (&im->tun6_protect_by_key, bkey, 1);
269 ipsec_tun_register_nodes(AF_IP6);
276 ipsec_tun_protect_adj_add (adj_index_t ai, void *arg)
278 ipsec_tun_protect_t *itp = arg;
279 adj_delegate_add (adj_get (ai), ipsec_tun_adj_delegate_type,
280 itp - ipsec_tun_protect_pool);
281 ipsec_tun_protect_add_adj (ai, itp);
283 if (itp->itp_flags & IPSEC_PROTECT_ITF)
284 ipsec_itf_adj_stack (ai, itp->itp_out_sa);
286 return (ADJ_WALK_RC_CONTINUE);
290 ipsec_tun_protect_tx_db_add (ipsec_tun_protect_t * itp)
293 * add the delegate to the adj
295 ipsec_tun_protect_itf_db_t *idi;
296 fib_protocol_t nh_proto;
299 vec_validate_init_empty (itp_db.id_itf,
300 itp->itp_sw_if_index,
301 IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY);
303 idi = &itp_db.id_itf[itp->itp_sw_if_index];
305 if (vnet_sw_interface_is_p2p (vnet_get_main (), itp->itp_sw_if_index))
307 if (INDEX_INVALID == idi->id_itp)
309 // ipsec_tun_protect_feature_set (itp, 1);
311 idi->id_itp = itp - ipsec_tun_protect_pool;
313 FOR_EACH_FIB_IP_PROTOCOL (nh_proto)
314 adj_nbr_walk (itp->itp_sw_if_index,
315 nh_proto, ipsec_tun_protect_adj_add, itp);
319 if (NULL == idi->id_hash)
322 hash_create_mem (0, sizeof (ip_address_t), sizeof (uword));
324 * enable the encrypt feature for egress if this is the first addition
327 // ipsec_tun_protect_feature_set (itp, 1);
330 hash_set_mem (idi->id_hash, itp->itp_key, itp - ipsec_tun_protect_pool);
333 * walk all the adjs with the same nh on this interface
334 * to associate them with this protection
336 nh_proto = ip_address_to_46 (itp->itp_key, &nh);
338 adj_nbr_walk_nh (itp->itp_sw_if_index,
339 nh_proto, &nh, ipsec_tun_protect_adj_add, itp);
341 ipsec_tun_register_nodes (FIB_PROTOCOL_IP6 == nh_proto ?
347 ipsec_tun_protect_rx_db_remove (ipsec_main_t * im,
348 const ipsec_tun_protect_t * itp)
350 const ipsec_sa_t *sa;
353 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
355 if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
357 ipsec4_tunnel_kv_t key;
358 clib_bihash_kv_8_8_t res, *bkey = (clib_bihash_kv_8_8_t*)&key;
360 ipsec4_tunnel_mk_key(&key, &itp->itp_crypto.dst.ip4,
361 clib_host_to_net_u32 (sa->spi));
363 if (!clib_bihash_search_8_8 (&im->tun4_protect_by_key, bkey, &res))
365 clib_bihash_add_del_8_8 (&im->tun4_protect_by_key, bkey, 0);
366 ipsec_tun_unregister_nodes(AF_IP4);
371 ipsec6_tunnel_kv_t key = {
373 .remote_ip = itp->itp_crypto.dst.ip6,
374 .spi = clib_host_to_net_u32 (sa->spi),
377 clib_bihash_kv_24_8_t res, *bkey = (clib_bihash_kv_24_8_t*)&key;
379 if (!clib_bihash_search_24_8 (&im->tun6_protect_by_key, bkey, &res))
381 clib_bihash_add_del_24_8 (&im->tun6_protect_by_key, bkey, 0);
382 ipsec_tun_unregister_nodes(AF_IP6);
390 ipsec_tun_protect_adj_remove (adj_index_t ai, void *arg)
392 ipsec_tun_protect_t *itp = arg;
394 adj_delegate_remove (ai, ipsec_tun_adj_delegate_type);
395 ipsec_tun_protect_add_adj (ai, NULL);
397 if (itp->itp_flags & IPSEC_PROTECT_ITF)
398 ipsec_itf_adj_unstack (ai);
400 return (ADJ_WALK_RC_CONTINUE);
404 ipsec_tun_protect_tx_db_remove (ipsec_tun_protect_t * itp)
406 ipsec_tun_protect_itf_db_t *idi;
407 fib_protocol_t nh_proto;
410 nh_proto = ip_address_to_46 (itp->itp_key, &nh);
411 idi = &itp_db.id_itf[itp->itp_sw_if_index];
413 if (vnet_sw_interface_is_p2p (vnet_get_main (), itp->itp_sw_if_index))
415 // ipsec_tun_protect_feature_set (itp, 0);
416 idi->id_itp = INDEX_INVALID;
418 FOR_EACH_FIB_IP_PROTOCOL (nh_proto)
419 adj_nbr_walk (itp->itp_sw_if_index,
420 nh_proto, ipsec_tun_protect_adj_remove, itp);
424 adj_nbr_walk_nh (itp->itp_sw_if_index,
425 nh_proto, &nh, ipsec_tun_protect_adj_remove, itp);
427 hash_unset_mem (idi->id_hash, itp->itp_key);
429 if (0 == hash_elts (idi->id_hash))
431 // ipsec_tun_protect_feature_set (itp, 0);
432 hash_free (idi->id_hash);
435 ipsec_tun_unregister_nodes (FIB_PROTOCOL_IP6 == nh_proto ?
441 ipsec_tun_protect_set_crypto_addr (ipsec_tun_protect_t * itp)
446 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
448 if (ipsec_sa_is_set_IS_TUNNEL (sa))
450 itp->itp_crypto.src = sa->tunnel_dst_addr;
451 itp->itp_crypto.dst = sa->tunnel_src_addr;
452 if (!(itp->itp_flags & IPSEC_PROTECT_ITF))
454 ipsec_sa_set_IS_PROTECT (sa);
455 itp->itp_flags |= IPSEC_PROTECT_ENCAPED;
460 itp->itp_crypto.src = itp->itp_tun.src;
461 itp->itp_crypto.dst = itp->itp_tun.dst;
462 itp->itp_flags &= ~IPSEC_PROTECT_ENCAPED;
469 ipsec_tun_protect_config (ipsec_main_t * im,
470 ipsec_tun_protect_t * itp, u32 sa_out, u32 * sas_in)
475 itp->itp_n_sa_in = vec_len (sas_in);
476 for (ii = 0; ii < itp->itp_n_sa_in; ii++)
477 itp->itp_in_sas[ii] = sas_in[ii];
478 itp->itp_out_sa = sa_out;
480 ipsec_sa_lock (itp->itp_out_sa);
483 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
487 ipsec_tun_protect_set_crypto_addr(itp);
491 * add to the DB against each SA
493 ipsec_tun_protect_rx_db_add (im, itp);
494 ipsec_tun_protect_tx_db_add (itp);
496 ITP_DBG (itp, "configured");
500 ipsec_tun_protect_unconfig (ipsec_main_t * im, ipsec_tun_protect_t * itp)
506 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
508 ipsec_sa_unset_IS_PROTECT (sa);
511 ipsec_tun_protect_rx_db_remove (im, itp);
512 ipsec_tun_protect_tx_db_remove (itp);
514 ipsec_sa_unlock(itp->itp_out_sa);
516 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
518 ipsec_sa_unlock(sai);
521 ITP_DBG (itp, "unconfigured");
525 ipsec_tun_protect_update_one (u32 sw_if_index,
526 const ip_address_t * nh, u32 sa_out, u32 sa_in)
531 vec_add1 (sas_in, sa_in);
532 rv = ipsec_tun_protect_update (sw_if_index, nh, sa_out, sas_in);
538 ipsec_tun_protect_update_out (u32 sw_if_index,
539 const ip_address_t * nh, u32 sa_out)
541 u32 itpi, *sas_in, sai, *saip;
542 ipsec_tun_protect_t *itp;
550 itpi = ipsec_tun_protect_find (sw_if_index, nh);
552 if (INDEX_INVALID == itpi)
554 return (VNET_API_ERROR_INVALID_INTERFACE);
557 itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
560 FOR_EACH_IPSEC_PROTECT_INPUT_SAI (itp, sai,
563 vec_add1 (sas_in, sai);
567 sa_out = ipsec_sa_find_and_lock (sa_out);
571 rv = VNET_API_ERROR_INVALID_VALUE;
575 ipsec_tun_protect_unconfig (im, itp);
576 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
578 ipsec_sa_unlock (sa_out);
579 vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
587 ipsec_tun_protect_update_in (u32 sw_if_index,
588 const ip_address_t * nh, u32 sa_in)
590 u32 itpi, *sas_in, sa_out;
591 ipsec_tun_protect_t *itp;
598 itpi = ipsec_tun_protect_find (sw_if_index, nh);
600 if (INDEX_INVALID == itpi)
602 return (VNET_API_ERROR_INVALID_INTERFACE);
605 sa_in = ipsec_sa_find_and_lock (sa_in);
609 rv = VNET_API_ERROR_INVALID_VALUE;
612 vec_add1 (sas_in, sa_in);
614 itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
615 sa_out = itp->itp_out_sa;
617 ipsec_sa_lock (sa_out);
619 ipsec_tun_protect_unconfig (im, itp);
620 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
622 ipsec_sa_unlock (sa_out);
623 ipsec_sa_unlock (sa_in);
630 ipsec_tun_protect_update_from_teib (ipsec_tun_protect_t * itp,
631 const teib_entry_t * ne)
635 const fib_prefix_t *pfx;
637 pfx = teib_entry_get_nh (ne);
639 ip46_address_copy (&itp->itp_tun.dst, &pfx->fp_addr);
642 ip46_address_reset (&itp->itp_tun.dst);
646 ipsec_tun_protect_update (u32 sw_if_index,
647 const ip_address_t * nh, u32 sa_out, u32 * sas_in)
649 ipsec_tun_protect_t *itp;
654 ITP_DBG2 ("update: %U/%U",
655 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
656 format_ip_address, nh);
658 if (vec_len (sas_in) > ITP_MAX_N_SA_IN)
660 rv = VNET_API_ERROR_LIMIT_EXCEEDED;
668 itpi = ipsec_tun_protect_find (sw_if_index, nh);
670 vec_foreach_index (ii, sas_in)
672 sas_in[ii] = ipsec_sa_find_and_lock (sas_in[ii]);
673 if (~0 == sas_in[ii])
675 rv = VNET_API_ERROR_INVALID_VALUE;
680 sa_out = ipsec_sa_find_and_lock (sa_out);
684 rv = VNET_API_ERROR_INVALID_VALUE;
688 if (INDEX_INVALID == itpi)
690 vnet_device_class_t *dev_class;
691 vnet_hw_interface_t *hi;
695 vnm = vnet_get_main ();
696 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
697 dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
699 if (NULL == dev_class->ip_tun_desc)
701 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
705 pool_get_zero (ipsec_tun_protect_pool, itp);
707 itp->itp_sw_if_index = sw_if_index;
708 itp->itp_ai = ADJ_INDEX_INVALID;
710 itp->itp_n_sa_in = vec_len (sas_in);
711 for (ii = 0; ii < itp->itp_n_sa_in; ii++)
712 itp->itp_in_sas[ii] = sas_in[ii];
713 itp->itp_out_sa = sa_out;
715 itp->itp_key = clib_mem_alloc (sizeof (*itp->itp_key));
716 ip_address_copy (itp->itp_key, nh);
718 rv = dev_class->ip_tun_desc (sw_if_index,
720 &itp->itp_tun.dst, &is_l2);
725 if (ip46_address_is_zero (&itp->itp_tun.src))
727 /* must be one of thos pesky ipsec interfaces that has no encap.
728 * the encap then MUST comefrom the tunnel mode SA.
732 sa = ipsec_sa_get (itp->itp_out_sa);
734 if (!ipsec_sa_is_set_IS_TUNNEL (sa))
736 rv = VNET_API_ERROR_INVALID_DST_ADDRESS;
740 itp->itp_flags |= IPSEC_PROTECT_ITF;
742 else if (ip46_address_is_zero (&itp->itp_tun.dst))
744 /* tunnel has no destination address, presumably because it's p2mp
745 in which case we use the nh that this is protection for */
746 ipsec_tun_protect_update_from_teib
747 (itp, teib_entry_find (sw_if_index, nh));
751 itp->itp_flags |= IPSEC_PROTECT_L2;
754 * add to the tunnel DB for ingress
755 * - if the SA is in trasnport mode, then the packates will arrive
756 * with the IP src,dst of the protected tunnel, in which case we can
757 * simply strip the IP header and hand the payload to the protocol
758 * appropriate input handler
759 * - if the SA is in tunnel mode then there are two IP headers present
760 * one for the crytpo tunnel endpoints (described in the SA) and one
761 * for the tunnel endpoints. The outer IP headers in the srriving
762 * packets will have the crypto endpoints. So the DB needs to contain
763 * the crpto endpoint. Once the crypto header is stripped, revealing,
764 * the tunnel-IP we have 2 choices:
765 * 1) do a tunnel lookup based on the revealed header
766 * 2) skip the tunnel lookup and assume that the packet matches the
767 * one that is protected here.
768 * If we did 1) then we would allow our peer to use the SA for tunnel
769 * X to inject traffic onto tunnel Y, this is not good. If we do 2)
770 * then we don't verify that the peer is indeed using SA for tunnel
771 * X and addressing tunnel X. So we take a compromise, once the SA
772 * matches to tunnel X we veriy that the inner IP matches the value
773 * of the tunnel we are protecting, else it's dropped.
775 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
779 /* updating SAs only */
780 itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
782 ipsec_tun_protect_unconfig (im, itp);
783 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
786 ipsec_sa_unlock (sa_out);
787 vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
795 ipsec_tun_protect_del (u32 sw_if_index, const ip_address_t * nh)
797 ipsec_tun_protect_t *itp;
801 ITP_DBG2 ("delete: %U/%U",
802 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
803 format_ip_address, nh);
809 itpi = ipsec_tun_protect_find (sw_if_index, nh);
811 if (INDEX_INVALID == itpi)
812 return (VNET_API_ERROR_NO_SUCH_ENTRY);
814 itp = ipsec_tun_protect_get (itpi);
815 ipsec_tun_protect_unconfig (im, itp);
817 if (ADJ_INDEX_INVALID != itp->itp_ai)
818 adj_unlock (itp->itp_ai);
820 clib_mem_free (itp->itp_key);
821 pool_put (ipsec_tun_protect_pool, itp);
827 ipsec_tun_protect_walk (ipsec_tun_protect_walk_cb_t fn, void *ctx)
832 pool_foreach_index(itpi, ipsec_tun_protect_pool,
840 ipsec_tun_protect_walk_itf (u32 sw_if_index,
841 ipsec_tun_protect_walk_cb_t fn, void *ctx)
843 ipsec_tun_protect_itf_db_t *idi;
847 if (vec_len (itp_db.id_itf) <= sw_if_index)
850 idi = &itp_db.id_itf[sw_if_index];
853 hash_foreach(key, itpi, idi->id_hash,
858 if (INDEX_INVALID != idi->id_itp)
859 fn (idi->id_itp, ctx);
863 ipsec_tun_protect_adj_delegate_adj_deleted (adj_delegate_t * ad)
865 /* remove our delegate */
866 ipsec_tun_protect_add_adj (ad->ad_adj_index, NULL);
867 adj_delegate_remove (ad->ad_adj_index, ipsec_tun_adj_delegate_type);
871 ipsec_tun_protect_adj_delegate_adj_modified (adj_delegate_t * ad)
873 ipsec_tun_protect_add_adj (ad->ad_adj_index,
874 ipsec_tun_protect_get (ad->ad_index));
878 ipsec_tun_protect_adj_delegate_adj_created (adj_index_t ai)
880 /* add our delegate if there is protection for this neighbour */
881 ip_address_t ip = IP_ADDRESS_V4_ALL_0S;
885 if (!adj_is_midchain (ai))
890 ip_address_from_46 (&adj->sub_type.midchain.next_hop,
891 adj->ia_nh_proto, &ip);
893 itpi = ipsec_tun_protect_find (adj->rewrite_header.sw_if_index, &ip);
895 if (INDEX_INVALID != itpi)
896 ipsec_tun_protect_adj_add (ai, ipsec_tun_protect_get (itpi));
900 ipsec_tun_protect_adj_delegate_format (const adj_delegate_t * aed, u8 * s)
902 const ipsec_tun_protect_t *itp;
904 itp = ipsec_tun_protect_from_const_base (aed);
905 s = format (s, "ipsec-tun-protect:\n%U", format_ipsec_tun_protect, itp);
911 ipsec_tun_teib_entry_added (const teib_entry_t * ne)
913 ipsec_tun_protect_t *itp;
916 itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne),
917 teib_entry_get_peer (ne));
919 if (INDEX_INVALID == itpi)
922 itp = ipsec_tun_protect_get (itpi);
923 ipsec_tun_protect_rx_db_remove (&ipsec_main, itp);
924 ipsec_tun_protect_update_from_teib (itp, ne);
925 ipsec_tun_protect_set_crypto_addr (itp);
926 ipsec_tun_protect_rx_db_add (&ipsec_main, itp);
928 ITP_DBG (itp, "teib-added");
932 ipsec_tun_teib_entry_deleted (const teib_entry_t * ne)
934 ipsec_tun_protect_t *itp;
937 itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne),
938 teib_entry_get_peer (ne));
940 if (INDEX_INVALID == itpi)
943 itp = ipsec_tun_protect_get (itpi);
944 ipsec_tun_protect_rx_db_remove (&ipsec_main, itp);
945 ipsec_tun_protect_update_from_teib (itp, NULL);
946 ipsec_tun_protect_set_crypto_addr (itp);
948 ITP_DBG (itp, "teib-removed");
952 * VFT registered with the adjacency delegate
954 const static adj_delegate_vft_t ipsec_tun_adj_delegate_vft = {
955 .adv_adj_deleted = ipsec_tun_protect_adj_delegate_adj_deleted,
956 .adv_adj_created = ipsec_tun_protect_adj_delegate_adj_created,
957 .adv_adj_modified = ipsec_tun_protect_adj_delegate_adj_modified,
958 .adv_format = ipsec_tun_protect_adj_delegate_format,
961 const static teib_vft_t ipsec_tun_teib_vft = {
962 .nv_added = ipsec_tun_teib_entry_added,
963 .nv_deleted = ipsec_tun_teib_entry_deleted,
967 ipsec_tun_table_init (ip_address_family_t af, uword table_size, u32 n_buckets)
974 clib_bihash_init_8_8 (&im->tun4_protect_by_key,
975 "IPSec IPv4 tunnels", n_buckets, table_size);
977 clib_bihash_init_24_8 (&im->tun6_protect_by_key,
978 "IPSec IPv6 tunnels", n_buckets, table_size);
982 ipsec_tunnel_protect_init (vlib_main_t * vm)
987 clib_bihash_init_24_8 (&im->tun6_protect_by_key,
988 "IPSec IPv6 tunnels",
989 IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS,
990 IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE);
991 clib_bihash_init_8_8 (&im->tun4_protect_by_key,
992 "IPSec IPv4 tunnels",
993 IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS,
994 IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE);
996 /* set up feature nodes to drop outbound packets with no crypto alg set */
997 im->esp4_no_crypto_tun_node_index =
998 vlib_get_node_by_name (vm, (u8 *) "esp4-no-crypto")->index;
999 im->esp6_no_crypto_tun_node_index =
1000 vlib_get_node_by_name (vm, (u8 *) "esp6-no-crypto")->index;
1001 im->esp6_encrypt_l2_tun_node_index =
1002 vlib_get_node_by_name (vm, (u8 *) "esp6-encrypt-tun")->index;
1003 im->esp4_encrypt_l2_tun_node_index =
1004 vlib_get_node_by_name (vm, (u8 *) "esp4-encrypt-tun")->index;
1006 ipsec_tun_adj_delegate_type =
1007 adj_delegate_register_new_type (&ipsec_tun_adj_delegate_vft);
1009 ipsec_tun_protect_logger = vlib_log_register_class ("ipsec", "tun");
1011 teib_register (&ipsec_tun_teib_vft);
1016 VLIB_INIT_FUNCTION (ipsec_tunnel_protect_init);
1018 static clib_error_t *
1019 ipsec_config (vlib_main_t * vm, unformat_input_t * input)
1021 unformat_input_t sub_input;
1023 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1025 if (unformat (input, "ip4 %U", unformat_vlib_cli_sub_input, &sub_input))
1027 uword table_size = ~0;
1030 while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
1032 if (unformat (&sub_input, "num-buckets %u", &n_buckets))
1035 return clib_error_return (0, "unknown input `%U'",
1036 format_unformat_error, &sub_input);
1039 ipsec_tun_table_init (AF_IP4, table_size, n_buckets);
1041 else if (unformat (input, "ip6 %U", unformat_vlib_cli_sub_input,
1044 uword table_size = ~0;
1047 while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
1049 if (unformat (&sub_input, "num-buckets %u", &n_buckets))
1052 return clib_error_return (0, "unknown input `%U'",
1053 format_unformat_error, &sub_input);
1056 ipsec_tun_table_init (AF_IP6, table_size, n_buckets);
1059 return clib_error_return (0, "unknown input `%U'",
1060 format_unformat_error, input);
1066 VLIB_CONFIG_FUNCTION (ipsec_config, "ipsec");
1069 * fd.io coding-style-patch-verification: ON
1072 * eval: (c-set-style "gnu")