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_local.h>
22 #include <vnet/adj/adj_delegate.h>
23 #include <vnet/adj/adj_midchain.h>
24 #include <vnet/teib/teib.h>
26 /* instantiate the bihash functions */
27 #include <vppinfra/bihash_8_16.h>
28 #include <vppinfra/bihash_template.c>
29 #include <vppinfra/bihash_24_16.h>
30 #include <vppinfra/bihash_template.c>
32 #define IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS (64 * 1024)
33 #define IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE 512 << 20
38 vlib_log_class_t ipsec_tun_protect_logger;
41 * Pool of tunnel protection objects
43 ipsec_tun_protect_t *ipsec_tun_protect_pool;
46 * Adj delegate registered type
48 static adj_delegate_type_t ipsec_tun_adj_delegate_type;
51 * Adj index to TX SA mapping
53 index_t *ipsec_tun_protect_sa_by_adj_index;
55 const ip_address_t IP_ADDR_ALL_0 = IP_ADDRESS_V4_ALL_0S;
58 * The DB of all added per-nh tunnel protectiond
60 typedef struct ipsec_tun_protect_itf_db_t_
62 /** A hash table key'd on IP (4 or 6) address */
64 /** If the interface is P2P then there is only one protect
65 * object associated with the auto-adj for each NH proto */
67 } ipsec_tun_protect_itf_db_t;
69 typedef struct ipsec_tun_protect_db_t_
71 /** Per-interface vector */
72 ipsec_tun_protect_itf_db_t *id_itf;
73 } ipsec_tun_protect_db_t;
75 static ipsec_tun_protect_db_t itp_db;
77 const static ipsec_tun_protect_itf_db_t IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY = {
78 .id_itp = INDEX_INVALID,
81 #define ITP_DBG(_itp, _fmt, _args...) \
83 vlib_log_debug(ipsec_tun_protect_logger, \
85 format_ipsec_tun_protect, \
89 #define ITP_DBG2(_fmt, _args...) \
91 vlib_log_debug(ipsec_tun_protect_logger, \
95 static u32 ipsec_tun_node_regs[N_AF];
98 ipsec_tun_register_nodes (ip_address_family_t af)
100 if (0 == ipsec_tun_node_regs[af]++)
104 ipsec_register_udp_port (UDP_DST_PORT_ipsec);
105 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
106 ipsec4_tun_input_node.index);
109 ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP,
110 ipsec6_tun_input_node.index);
115 ipsec_tun_unregister_nodes (ip_address_family_t af)
117 ASSERT (0 != ipsec_tun_node_regs[af]);
118 if (0 == --ipsec_tun_node_regs[af])
122 ipsec_unregister_udp_port (UDP_DST_PORT_ipsec);
123 ip4_unregister_protocol (IP_PROTOCOL_IPSEC_ESP);
126 ip6_unregister_protocol (IP_PROTOCOL_IPSEC_ESP);
130 static inline const ipsec_tun_protect_t *
131 ipsec_tun_protect_from_const_base (const adj_delegate_t * ad)
135 return (pool_elt_at_index (ipsec_tun_protect_pool, ad->ad_index));
139 ipsec_tun_protect_get_adj_next (vnet_link_t linkt,
140 const ipsec_tun_protect_t * itp)
146 if (!(itp->itp_flags & IPSEC_PROTECT_ITF))
148 if (ip46_address_is_ip4 (&itp->itp_tun.src))
149 linkt = VNET_LINK_IP4;
151 linkt = VNET_LINK_IP6;
154 sa = ipsec_sa_get (itp->itp_out_sa);
158 if ((sa->crypto_alg == IPSEC_CRYPTO_ALG_NONE &&
159 sa->integ_alg == IPSEC_INTEG_ALG_NONE) &&
160 !(itp->itp_flags & IPSEC_PROTECT_ITF))
161 next = (VNET_LINK_IP4 == linkt ? im->esp4_no_crypto_tun_node_index :
162 im->esp6_no_crypto_tun_node_index);
163 else if (itp->itp_flags & IPSEC_PROTECT_L2)
164 next = (VNET_LINK_IP4 == linkt ? im->esp4_encrypt_l2_tun_node_index :
165 im->esp6_encrypt_l2_tun_node_index);
171 next = im->esp4_encrypt_tun_node_index;
174 next = im->esp6_encrypt_tun_node_index;
177 next = im->esp_mpls_encrypt_tun_node_index;
181 case VNET_LINK_ETHERNET:
190 ipsec_tun_protect_add_adj (adj_index_t ai, const ipsec_tun_protect_t * itp)
192 vec_validate_init_empty (ipsec_tun_protect_sa_by_adj_index, ai,
197 ipsec_tun_protect_sa_by_adj_index[ai] = INDEX_INVALID;
198 adj_nbr_midchain_reset_next_node (ai);
202 ipsec_tun_protect_sa_by_adj_index[ai] = itp->itp_out_sa;
203 adj_nbr_midchain_update_next_node
204 (ai, ipsec_tun_protect_get_adj_next (adj_get_link_type (ai), itp));
209 ipsec_tun_protect_find (u32 sw_if_index, const ip_address_t * nh)
211 ipsec_tun_protect_itf_db_t *idi;
214 if (vec_len (itp_db.id_itf) <= sw_if_index)
215 return INDEX_INVALID;
217 if (vnet_sw_interface_is_p2p (vnet_get_main (), sw_if_index))
218 return (itp_db.id_itf[sw_if_index].id_itp);
220 idi = &itp_db.id_itf[sw_if_index];
221 p = hash_get_mem (idi->id_hash, nh);
225 return INDEX_INVALID;
231 ipsec_tun_protect_rx_db_add (ipsec_main_t * im,
232 const ipsec_tun_protect_t * itp)
234 const ipsec_sa_t *sa;
237 if (ip46_address_is_zero (&itp->itp_crypto.dst))
241 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
243 sa = ipsec_sa_get (sai);
245 ipsec_tun_lkup_result_t res = {
246 .tun_index = itp - ipsec_tun_protect_pool,
248 .flags = itp->itp_flags,
249 .sw_if_index = itp->itp_sw_if_index,
253 * The key is formed from the tunnel's destination
254 * as the packet lookup is done from the packet's source
256 if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
258 ipsec4_tunnel_kv_t key = {
261 clib_bihash_kv_8_16_t *bkey = (clib_bihash_kv_8_16_t*)&key;
263 ipsec4_tunnel_mk_key(&key, &itp->itp_crypto.dst.ip4,
264 clib_host_to_net_u32 (sa->spi));
266 if (!im->tun4_protect_by_key.nbuckets)
267 clib_bihash_init_8_16 (&im->tun4_protect_by_key,
268 "IPSec IPv4 tunnels",
269 IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS,
270 IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE);
272 clib_bihash_add_del_8_16 (&im->tun4_protect_by_key, bkey, 1);
273 ipsec_tun_register_nodes(AF_IP4);
277 ipsec6_tunnel_kv_t key = {
279 .remote_ip = itp->itp_crypto.dst.ip6,
280 .spi = clib_host_to_net_u32 (sa->spi),
284 clib_bihash_kv_24_16_t *bkey = (clib_bihash_kv_24_16_t*)&key;
286 if (!im->tun4_protect_by_key.nbuckets)
287 clib_bihash_init_24_16 (&im->tun6_protect_by_key,
288 "IPSec IPv6 tunnels",
289 IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS,
290 IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE);
291 clib_bihash_add_del_24_16 (&im->tun6_protect_by_key, bkey, 1);
292 ipsec_tun_register_nodes(AF_IP6);
299 ipsec_tun_protect_adj_add (adj_index_t ai, void *arg)
301 ipsec_tun_protect_t *itp = arg;
302 adj_delegate_add (adj_get (ai), ipsec_tun_adj_delegate_type,
303 itp - ipsec_tun_protect_pool);
304 ipsec_tun_protect_add_adj (ai, itp);
306 if (itp->itp_flags & IPSEC_PROTECT_ITF)
307 ipsec_itf_adj_stack (ai, itp->itp_out_sa);
309 return (ADJ_WALK_RC_CONTINUE);
313 ipsec_tun_protect_tx_db_add (ipsec_tun_protect_t * itp)
316 * add the delegate to the adj
318 ipsec_tun_protect_itf_db_t *idi;
319 fib_protocol_t nh_proto;
322 vec_validate_init_empty (itp_db.id_itf,
323 itp->itp_sw_if_index,
324 IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY);
326 idi = &itp_db.id_itf[itp->itp_sw_if_index];
328 if (vnet_sw_interface_is_p2p (vnet_get_main (), itp->itp_sw_if_index))
330 if (INDEX_INVALID == idi->id_itp)
332 // ipsec_tun_protect_feature_set (itp, 1);
334 idi->id_itp = itp - ipsec_tun_protect_pool;
336 FOR_EACH_FIB_IP_PROTOCOL (nh_proto)
337 adj_nbr_walk (itp->itp_sw_if_index,
338 nh_proto, ipsec_tun_protect_adj_add, itp);
342 if (NULL == idi->id_hash)
345 hash_create_mem (0, sizeof (ip_address_t), sizeof (uword));
347 * enable the encrypt feature for egress if this is the first addition
350 // ipsec_tun_protect_feature_set (itp, 1);
353 hash_set_mem (idi->id_hash, itp->itp_key, itp - ipsec_tun_protect_pool);
356 * walk all the adjs with the same nh on this interface
357 * to associate them with this protection
359 nh_proto = ip_address_to_46 (itp->itp_key, &nh);
361 adj_nbr_walk_nh (itp->itp_sw_if_index,
362 nh_proto, &nh, ipsec_tun_protect_adj_add, itp);
364 ipsec_tun_register_nodes (FIB_PROTOCOL_IP6 == nh_proto ?
370 ipsec_tun_protect_rx_db_remove (ipsec_main_t * im,
371 const ipsec_tun_protect_t * itp)
373 const ipsec_sa_t *sa;
376 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
378 if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
380 ipsec4_tunnel_kv_t key;
381 clib_bihash_kv_8_16_t res, *bkey = (clib_bihash_kv_8_16_t*)&key;
383 ipsec4_tunnel_mk_key(&key, &itp->itp_crypto.dst.ip4,
384 clib_host_to_net_u32 (sa->spi));
386 if (!clib_bihash_search_8_16 (&im->tun4_protect_by_key, bkey, &res))
388 clib_bihash_add_del_8_16 (&im->tun4_protect_by_key, bkey, 0);
389 ipsec_tun_unregister_nodes(AF_IP4);
394 ipsec6_tunnel_kv_t key = {
396 .remote_ip = itp->itp_crypto.dst.ip6,
397 .spi = clib_host_to_net_u32 (sa->spi),
400 clib_bihash_kv_24_16_t res, *bkey = (clib_bihash_kv_24_16_t*)&key;
402 if (!clib_bihash_search_24_16 (&im->tun6_protect_by_key, bkey, &res))
404 clib_bihash_add_del_24_16 (&im->tun6_protect_by_key, bkey, 0);
405 ipsec_tun_unregister_nodes(AF_IP6);
413 ipsec_tun_protect_adj_remove (adj_index_t ai, void *arg)
415 ipsec_tun_protect_t *itp = arg;
417 adj_delegate_remove (ai, ipsec_tun_adj_delegate_type);
418 ipsec_tun_protect_add_adj (ai, NULL);
420 if (itp->itp_flags & IPSEC_PROTECT_ITF)
421 ipsec_itf_adj_unstack (ai);
423 return (ADJ_WALK_RC_CONTINUE);
427 ipsec_tun_protect_tx_db_remove (ipsec_tun_protect_t * itp)
429 ipsec_tun_protect_itf_db_t *idi;
430 fib_protocol_t nh_proto;
433 nh_proto = ip_address_to_46 (itp->itp_key, &nh);
434 idi = &itp_db.id_itf[itp->itp_sw_if_index];
436 if (vnet_sw_interface_is_p2p (vnet_get_main (), itp->itp_sw_if_index))
438 // ipsec_tun_protect_feature_set (itp, 0);
439 idi->id_itp = INDEX_INVALID;
441 FOR_EACH_FIB_IP_PROTOCOL (nh_proto)
442 adj_nbr_walk (itp->itp_sw_if_index,
443 nh_proto, ipsec_tun_protect_adj_remove, itp);
447 adj_nbr_walk_nh (itp->itp_sw_if_index,
448 nh_proto, &nh, ipsec_tun_protect_adj_remove, itp);
450 hash_unset_mem (idi->id_hash, itp->itp_key);
452 if (0 == hash_elts (idi->id_hash))
454 // ipsec_tun_protect_feature_set (itp, 0);
455 hash_free (idi->id_hash);
458 ipsec_tun_unregister_nodes (FIB_PROTOCOL_IP6 == nh_proto ?
464 ipsec_tun_protect_set_crypto_addr (ipsec_tun_protect_t * itp)
469 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
471 if (ipsec_sa_is_set_IS_TUNNEL (sa))
473 itp->itp_crypto.src = sa->tunnel_dst_addr;
474 itp->itp_crypto.dst = sa->tunnel_src_addr;
475 if (!(itp->itp_flags & IPSEC_PROTECT_ITF))
477 ipsec_sa_set_IS_PROTECT (sa);
478 itp->itp_flags |= IPSEC_PROTECT_ENCAPED;
483 itp->itp_crypto.src = itp->itp_tun.src;
484 itp->itp_crypto.dst = itp->itp_tun.dst;
485 itp->itp_flags &= ~IPSEC_PROTECT_ENCAPED;
492 ipsec_tun_protect_config (ipsec_main_t * im,
493 ipsec_tun_protect_t * itp, u32 sa_out, u32 * sas_in)
498 itp->itp_n_sa_in = vec_len (sas_in);
499 for (ii = 0; ii < itp->itp_n_sa_in; ii++)
500 itp->itp_in_sas[ii] = sas_in[ii];
501 itp->itp_out_sa = sa_out;
503 ipsec_sa_lock (itp->itp_out_sa);
506 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
510 ipsec_tun_protect_set_crypto_addr(itp);
514 * add to the DB against each SA
516 ipsec_tun_protect_rx_db_add (im, itp);
517 ipsec_tun_protect_tx_db_add (itp);
519 ITP_DBG (itp, "configured");
523 ipsec_tun_protect_unconfig (ipsec_main_t * im, ipsec_tun_protect_t * itp)
529 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
531 ipsec_sa_unset_IS_PROTECT (sa);
534 ipsec_tun_protect_rx_db_remove (im, itp);
535 ipsec_tun_protect_tx_db_remove (itp);
537 ipsec_sa_unlock(itp->itp_out_sa);
539 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
541 ipsec_sa_unlock(sai);
544 ITP_DBG (itp, "unconfigured");
548 ipsec_tun_protect_update_from_teib (ipsec_tun_protect_t * itp,
549 const teib_entry_t * ne)
553 const fib_prefix_t *pfx;
555 pfx = teib_entry_get_nh (ne);
557 ip46_address_copy (&itp->itp_tun.dst, &pfx->fp_addr);
560 ip46_address_reset (&itp->itp_tun.dst);
564 ipsec_tun_protect_update (u32 sw_if_index,
565 const ip_address_t * nh, u32 sa_out, u32 * sas_in)
567 ipsec_tun_protect_t *itp;
572 ITP_DBG2 ("update: %U/%U",
573 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
574 format_ip_address, nh);
576 if (vec_len (sas_in) > ITP_MAX_N_SA_IN)
578 rv = VNET_API_ERROR_LIMIT_EXCEEDED;
586 itpi = ipsec_tun_protect_find (sw_if_index, nh);
588 vec_foreach_index (ii, sas_in)
590 sas_in[ii] = ipsec_sa_find_and_lock (sas_in[ii]);
591 if (~0 == sas_in[ii])
593 rv = VNET_API_ERROR_INVALID_VALUE;
598 sa_out = ipsec_sa_find_and_lock (sa_out);
602 rv = VNET_API_ERROR_INVALID_VALUE;
606 if (INDEX_INVALID == itpi)
608 vnet_device_class_t *dev_class;
609 vnet_hw_interface_t *hi;
613 vnm = vnet_get_main ();
614 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
615 dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
617 if (NULL == dev_class->ip_tun_desc)
619 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
623 pool_get_zero (ipsec_tun_protect_pool, itp);
625 itp->itp_sw_if_index = sw_if_index;
626 itp->itp_ai = ADJ_INDEX_INVALID;
628 itp->itp_n_sa_in = vec_len (sas_in);
629 for (ii = 0; ii < itp->itp_n_sa_in; ii++)
630 itp->itp_in_sas[ii] = sas_in[ii];
631 itp->itp_out_sa = sa_out;
633 itp->itp_key = clib_mem_alloc (sizeof (*itp->itp_key));
634 ip_address_copy (itp->itp_key, nh);
636 rv = dev_class->ip_tun_desc (sw_if_index,
638 &itp->itp_tun.dst, &is_l2);
643 if (ip46_address_is_zero (&itp->itp_tun.src))
646 * must be one of those pesky ipsec interfaces that has no encap.
647 * the encap then MUST come from the tunnel mode SA.
651 sa = ipsec_sa_get (itp->itp_out_sa);
653 if (!ipsec_sa_is_set_IS_TUNNEL (sa))
655 rv = VNET_API_ERROR_INVALID_DST_ADDRESS;
659 itp->itp_flags |= IPSEC_PROTECT_ITF;
661 else if (ip46_address_is_zero (&itp->itp_tun.dst))
663 /* tunnel has no destination address, presumably because it's p2mp
664 in which case we use the nh that this is protection for */
665 ipsec_tun_protect_update_from_teib
666 (itp, teib_entry_find (sw_if_index, nh));
670 itp->itp_flags |= IPSEC_PROTECT_L2;
673 * add to the tunnel DB for ingress
674 * - if the SA is in trasnport mode, then the packates will arrive
675 * with the IP src,dst of the protected tunnel, in which case we can
676 * simply strip the IP header and hand the payload to the protocol
677 * appropriate input handler
678 * - if the SA is in tunnel mode then there are two IP headers present
679 * one for the crytpo tunnel endpoints (described in the SA) and one
680 * for the tunnel endpoints. The outer IP headers in the srriving
681 * packets will have the crypto endpoints. So the DB needs to contain
682 * the crpto endpoint. Once the crypto header is stripped, revealing,
683 * the tunnel-IP we have 2 choices:
684 * 1) do a tunnel lookup based on the revealed header
685 * 2) skip the tunnel lookup and assume that the packet matches the
686 * one that is protected here.
687 * If we did 1) then we would allow our peer to use the SA for tunnel
688 * X to inject traffic onto tunnel Y, this is not good. If we do 2)
689 * then we don't verify that the peer is indeed using SA for tunnel
690 * X and addressing tunnel X. So we take a compromise, once the SA
691 * matches to tunnel X we veriy that the inner IP matches the value
692 * of the tunnel we are protecting, else it's dropped.
694 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
698 /* updating SAs only */
699 itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
701 ipsec_tun_protect_unconfig (im, itp);
702 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
705 ipsec_sa_unlock (sa_out);
706 vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
714 ipsec_tun_protect_del (u32 sw_if_index, const ip_address_t * nh)
716 ipsec_tun_protect_t *itp;
720 ITP_DBG2 ("delete: %U/%U",
721 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
722 format_ip_address, nh);
728 itpi = ipsec_tun_protect_find (sw_if_index, nh);
730 if (INDEX_INVALID == itpi)
731 return (VNET_API_ERROR_NO_SUCH_ENTRY);
733 itp = ipsec_tun_protect_get (itpi);
734 ipsec_tun_protect_unconfig (im, itp);
736 if (ADJ_INDEX_INVALID != itp->itp_ai)
737 adj_unlock (itp->itp_ai);
739 clib_mem_free (itp->itp_key);
740 pool_put (ipsec_tun_protect_pool, itp);
746 ipsec_tun_protect_walk (ipsec_tun_protect_walk_cb_t fn, void *ctx)
751 pool_foreach_index (itpi, ipsec_tun_protect_pool)
759 ipsec_tun_protect_walk_itf (u32 sw_if_index,
760 ipsec_tun_protect_walk_cb_t fn, void *ctx)
762 ipsec_tun_protect_itf_db_t *idi;
766 if (vec_len (itp_db.id_itf) <= sw_if_index)
769 idi = &itp_db.id_itf[sw_if_index];
772 hash_foreach(key, itpi, idi->id_hash,
777 if (INDEX_INVALID != idi->id_itp)
778 fn (idi->id_itp, ctx);
782 ipsec_tun_protect_adj_delegate_adj_deleted (adj_delegate_t * ad)
784 /* remove our delegate */
785 ipsec_tun_protect_add_adj (ad->ad_adj_index, NULL);
786 adj_delegate_remove (ad->ad_adj_index, ipsec_tun_adj_delegate_type);
790 ipsec_tun_protect_adj_delegate_adj_modified (adj_delegate_t * ad)
792 ipsec_tun_protect_add_adj (ad->ad_adj_index,
793 ipsec_tun_protect_get (ad->ad_index));
797 ipsec_tun_protect_adj_delegate_adj_created (adj_index_t ai)
799 /* add our delegate if there is protection for this neighbour */
800 ip_address_t ip = IP_ADDRESS_V4_ALL_0S;
804 if (!adj_is_midchain (ai))
809 ip_address_from_46 (&adj->sub_type.midchain.next_hop,
810 adj->ia_nh_proto, &ip);
812 itpi = ipsec_tun_protect_find (adj->rewrite_header.sw_if_index, &ip);
814 if (INDEX_INVALID != itpi)
815 ipsec_tun_protect_adj_add (ai, ipsec_tun_protect_get (itpi));
819 ipsec_tun_protect_adj_delegate_format (const adj_delegate_t * aed, u8 * s)
821 const ipsec_tun_protect_t *itp;
823 itp = ipsec_tun_protect_from_const_base (aed);
824 s = format (s, "ipsec-tun-protect:\n%U", format_ipsec_tun_protect, itp);
830 ipsec_tun_teib_entry_added (const teib_entry_t * ne)
832 ipsec_tun_protect_t *itp;
835 itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne),
836 teib_entry_get_peer (ne));
838 if (INDEX_INVALID == itpi)
841 itp = ipsec_tun_protect_get (itpi);
842 ipsec_tun_protect_rx_db_remove (&ipsec_main, itp);
843 ipsec_tun_protect_update_from_teib (itp, ne);
844 ipsec_tun_protect_set_crypto_addr (itp);
845 ipsec_tun_protect_rx_db_add (&ipsec_main, itp);
847 ITP_DBG (itp, "teib-added");
851 ipsec_tun_teib_entry_deleted (const teib_entry_t * ne)
853 ipsec_tun_protect_t *itp;
856 itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne),
857 teib_entry_get_peer (ne));
859 if (INDEX_INVALID == itpi)
862 itp = ipsec_tun_protect_get (itpi);
863 ipsec_tun_protect_rx_db_remove (&ipsec_main, itp);
864 ipsec_tun_protect_update_from_teib (itp, NULL);
865 ipsec_tun_protect_set_crypto_addr (itp);
867 ITP_DBG (itp, "teib-removed");
871 * VFT registered with the adjacency delegate
873 const static adj_delegate_vft_t ipsec_tun_adj_delegate_vft = {
874 .adv_adj_deleted = ipsec_tun_protect_adj_delegate_adj_deleted,
875 .adv_adj_created = ipsec_tun_protect_adj_delegate_adj_created,
876 .adv_adj_modified = ipsec_tun_protect_adj_delegate_adj_modified,
877 .adv_format = ipsec_tun_protect_adj_delegate_format,
880 const static teib_vft_t ipsec_tun_teib_vft = {
881 .nv_added = ipsec_tun_teib_entry_added,
882 .nv_deleted = ipsec_tun_teib_entry_deleted,
886 ipsec_tun_table_init (ip_address_family_t af, uword table_size, u32 n_buckets)
893 clib_bihash_init_8_16 (&im->tun4_protect_by_key,
894 "IPSec IPv4 tunnels", n_buckets, table_size);
896 clib_bihash_init_24_16 (&im->tun6_protect_by_key,
897 "IPSec IPv6 tunnels", n_buckets, table_size);
900 static clib_error_t *
901 ipsec_tunnel_protect_init (vlib_main_t *vm)
906 clib_bihash_init_24_16 (&im->tun6_protect_by_key,
907 "IPSec IPv6 tunnels",
908 IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS,
909 IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE);
910 clib_bihash_init_8_16 (&im->tun4_protect_by_key,
911 "IPSec IPv4 tunnels",
912 IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS,
913 IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE);
915 /* set up feature nodes to drop outbound packets with no crypto alg set */
916 im->esp4_no_crypto_tun_node_index =
917 vlib_get_node_by_name (vm, (u8 *) "esp4-no-crypto")->index;
918 im->esp6_no_crypto_tun_node_index =
919 vlib_get_node_by_name (vm, (u8 *) "esp6-no-crypto")->index;
920 im->esp6_encrypt_l2_tun_node_index =
921 vlib_get_node_by_name (vm, (u8 *) "esp6-encrypt-tun")->index;
922 im->esp4_encrypt_l2_tun_node_index =
923 vlib_get_node_by_name (vm, (u8 *) "esp4-encrypt-tun")->index;
925 ipsec_tun_adj_delegate_type =
926 adj_delegate_register_new_type (&ipsec_tun_adj_delegate_vft);
928 ipsec_tun_protect_logger = vlib_log_register_class ("ipsec", "tun");
930 teib_register (&ipsec_tun_teib_vft);
935 VLIB_INIT_FUNCTION (ipsec_tunnel_protect_init);
937 static clib_error_t *
938 ipsec_config (vlib_main_t * vm, unformat_input_t * input)
940 unformat_input_t sub_input;
942 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
944 if (unformat (input, "ip4 %U", unformat_vlib_cli_sub_input, &sub_input))
946 uword table_size = ~0;
949 while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
951 if (unformat (&sub_input, "num-buckets %u", &n_buckets))
954 return clib_error_return (0, "unknown input `%U'",
955 format_unformat_error, &sub_input);
958 ipsec_tun_table_init (AF_IP4, table_size, n_buckets);
960 else if (unformat (input, "ip6 %U", unformat_vlib_cli_sub_input,
963 uword table_size = ~0;
966 while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
968 if (unformat (&sub_input, "num-buckets %u", &n_buckets))
971 return clib_error_return (0, "unknown input `%U'",
972 format_unformat_error, &sub_input);
975 ipsec_tun_table_init (AF_IP6, table_size, n_buckets);
978 return clib_error_return (0, "unknown input `%U'",
979 format_unformat_error, input);
985 VLIB_CONFIG_FUNCTION (ipsec_config, "ipsec");
988 * fd.io coding-style-patch-verification: ON
991 * eval: (c-set-style "gnu")