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>
29 vlib_log_class_t ipsec_tun_protect_logger;
32 * Pool of tunnel protection objects
34 ipsec_tun_protect_t *ipsec_tun_protect_pool;
37 * Adj delegate registered type
39 static adj_delegate_type_t ipsec_tun_adj_delegate_type;
42 * Adj index to TX SA mapping
44 index_t *ipsec_tun_protect_sa_by_adj_index;
46 const ip_address_t IP_ADDR_ALL_0 = IP_ADDRESS_V4_ALL_0S;
49 * The DB of all added per-nh tunnel protectiond
51 typedef struct ipsec_tun_protect_itf_db_t_
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 */
58 } ipsec_tun_protect_itf_db_t;
60 typedef struct ipsec_tun_protect_db_t_
62 /** Per-interface vector */
63 ipsec_tun_protect_itf_db_t *id_itf;
64 } ipsec_tun_protect_db_t;
66 static ipsec_tun_protect_db_t itp_db;
68 const static ipsec_tun_protect_itf_db_t IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY = {
69 .id_itp = INDEX_INVALID,
72 #define ITP_DBG(_itp, _fmt, _args...) \
74 vlib_log_debug(ipsec_tun_protect_logger, \
76 format_ipsec_tun_protect, \
80 #define ITP_DBG2(_fmt, _args...) \
82 vlib_log_debug(ipsec_tun_protect_logger, \
86 static u32 ipsec_tun_node_regs[N_AF];
89 ipsec_tun_register_nodes (ip_address_family_t af)
91 if (0 == ipsec_tun_node_regs[af]++)
95 ipsec_register_udp_port (UDP_DST_PORT_ipsec);
96 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
97 ipsec4_tun_input_node.index);
100 ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP,
101 ipsec6_tun_input_node.index);
106 ipsec_tun_unregister_nodes (ip_address_family_t af)
108 ASSERT (0 != ipsec_tun_node_regs[af]);
109 if (0 == --ipsec_tun_node_regs[af])
113 ipsec_unregister_udp_port (UDP_DST_PORT_ipsec);
114 ip4_unregister_protocol (IP_PROTOCOL_IPSEC_ESP);
117 ip6_unregister_protocol (IP_PROTOCOL_IPSEC_ESP);
121 static inline const ipsec_tun_protect_t *
122 ipsec_tun_protect_from_const_base (const adj_delegate_t * ad)
126 return (pool_elt_at_index (ipsec_tun_protect_pool, ad->ad_index));
130 ipsec_tun_protect_get_adj_next (vnet_link_t linkt,
131 const ipsec_tun_protect_t * itp)
139 if (itp->itp_flags & IPSEC_PROTECT_ITF)
140 is_ip4 = linkt == VNET_LINK_IP4;
142 is_ip4 = ip46_address_is_ip4 (&itp->itp_tun.src);
144 sa = ipsec_sa_get (itp->itp_out_sa);
147 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_NONE &&
148 sa->integ_alg == IPSEC_INTEG_ALG_NONE)
150 im->esp4_no_crypto_tun_node_index :
151 im->esp6_no_crypto_tun_node_index);
152 else if (itp->itp_flags & IPSEC_PROTECT_L2)
154 im->esp4_encrypt_l2_tun_node_index :
155 im->esp6_encrypt_l2_tun_node_index);
158 im->esp4_encrypt_tun_node_index :
159 im->esp6_encrypt_tun_node_index);
165 ipsec_tun_protect_add_adj (adj_index_t ai, const ipsec_tun_protect_t * itp)
167 vec_validate_init_empty (ipsec_tun_protect_sa_by_adj_index, ai,
172 ipsec_tun_protect_sa_by_adj_index[ai] = INDEX_INVALID;
173 adj_nbr_midchain_reset_next_node (ai);
177 ipsec_tun_protect_sa_by_adj_index[ai] = itp->itp_out_sa;
178 adj_nbr_midchain_update_next_node
179 (ai, ipsec_tun_protect_get_adj_next (adj_get_link_type (ai), itp));
184 ipsec_tun_protect_find (u32 sw_if_index, const ip_address_t * nh)
186 ipsec_tun_protect_itf_db_t *idi;
189 if (vec_len (itp_db.id_itf) <= sw_if_index)
190 return INDEX_INVALID;
192 if (vnet_sw_interface_is_p2p (vnet_get_main (), sw_if_index))
193 return (itp_db.id_itf[sw_if_index].id_itp);
195 idi = &itp_db.id_itf[sw_if_index];
196 p = hash_get_mem (idi->id_hash, nh);
200 return INDEX_INVALID;
206 ipsec_tun_protect_rx_db_add (ipsec_main_t * im,
207 const ipsec_tun_protect_t * itp)
209 const ipsec_sa_t *sa;
212 if (ip46_address_is_zero (&itp->itp_crypto.dst))
216 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
218 sa = ipsec_sa_get (sai);
220 ipsec_tun_lkup_result_t res = {
221 .tun_index = itp - ipsec_tun_protect_pool,
226 * The key is formed from the tunnel's destination
227 * as the packet lookup is done from the packet's source
229 if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
231 ipsec4_tunnel_key_t key = {
232 .remote_ip = itp->itp_crypto.dst.ip4,
233 .spi = clib_host_to_net_u32 (sa->spi),
235 hash_set (im->tun4_protect_by_key, key.as_u64, res.as_u64);
236 ipsec_tun_register_nodes(AF_IP4);
240 ipsec6_tunnel_key_t key = {
241 .remote_ip = itp->itp_crypto.dst.ip6,
242 .spi = clib_host_to_net_u32 (sa->spi),
244 hash_set_mem_alloc (&im->tun6_protect_by_key, &key, res.as_u64);
245 ipsec_tun_register_nodes(AF_IP6);
252 ipsec_tun_protect_adj_add (adj_index_t ai, void *arg)
254 ipsec_tun_protect_t *itp = arg;
255 adj_delegate_add (adj_get (ai), ipsec_tun_adj_delegate_type,
256 itp - ipsec_tun_protect_pool);
257 ipsec_tun_protect_add_adj (ai, itp);
259 if (itp->itp_flags & IPSEC_PROTECT_ITF)
260 ipsec_itf_adj_stack (ai, itp->itp_out_sa);
262 return (ADJ_WALK_RC_CONTINUE);
266 ipsec_tun_protect_tx_db_add (ipsec_tun_protect_t * itp)
269 * add the delegate to the adj
271 ipsec_tun_protect_itf_db_t *idi;
272 fib_protocol_t nh_proto;
275 vec_validate_init_empty (itp_db.id_itf,
276 itp->itp_sw_if_index,
277 IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY);
279 idi = &itp_db.id_itf[itp->itp_sw_if_index];
281 if (vnet_sw_interface_is_p2p (vnet_get_main (), itp->itp_sw_if_index))
283 if (INDEX_INVALID == idi->id_itp)
285 // ipsec_tun_protect_feature_set (itp, 1);
287 idi->id_itp = itp - ipsec_tun_protect_pool;
289 FOR_EACH_FIB_IP_PROTOCOL (nh_proto)
290 adj_nbr_walk (itp->itp_sw_if_index,
291 nh_proto, ipsec_tun_protect_adj_add, itp);
295 if (NULL == idi->id_hash)
298 hash_create_mem (0, sizeof (ip_address_t), sizeof (uword));
300 * enable the encrypt feature for egress if this is the first addition
303 // ipsec_tun_protect_feature_set (itp, 1);
306 hash_set_mem (idi->id_hash, itp->itp_key, itp - ipsec_tun_protect_pool);
309 * walk all the adjs with the same nh on this interface
310 * to associate them with this protection
312 nh_proto = ip_address_to_46 (itp->itp_key, &nh);
314 adj_nbr_walk_nh (itp->itp_sw_if_index,
315 nh_proto, &nh, ipsec_tun_protect_adj_add, itp);
317 ipsec_tun_register_nodes (FIB_PROTOCOL_IP6 == nh_proto ?
323 ipsec_tun_protect_rx_db_remove (ipsec_main_t * im,
324 const ipsec_tun_protect_t * itp)
326 const ipsec_sa_t *sa;
329 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
331 if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
333 ipsec4_tunnel_key_t key = {
334 .remote_ip = itp->itp_crypto.dst.ip4,
335 .spi = clib_host_to_net_u32 (sa->spi),
337 if (hash_get(im->tun4_protect_by_key, key.as_u64))
339 hash_unset (im->tun4_protect_by_key, key.as_u64);
340 ipsec_tun_unregister_nodes(AF_IP4);
345 ipsec6_tunnel_key_t key = {
346 .remote_ip = itp->itp_crypto.dst.ip6,
347 .spi = clib_host_to_net_u32 (sa->spi),
349 if (hash_get_mem(im->tun6_protect_by_key, &key))
351 hash_unset_mem_free (&im->tun6_protect_by_key, &key);
352 ipsec_tun_unregister_nodes(AF_IP6);
360 ipsec_tun_protect_adj_remove (adj_index_t ai, void *arg)
362 ipsec_tun_protect_t *itp = arg;
364 adj_delegate_remove (ai, ipsec_tun_adj_delegate_type);
365 ipsec_tun_protect_add_adj (ai, NULL);
367 if (itp->itp_flags & IPSEC_PROTECT_ITF)
368 ipsec_itf_adj_unstack (ai);
370 return (ADJ_WALK_RC_CONTINUE);
374 ipsec_tun_protect_tx_db_remove (ipsec_tun_protect_t * itp)
376 ipsec_tun_protect_itf_db_t *idi;
377 fib_protocol_t nh_proto;
380 nh_proto = ip_address_to_46 (itp->itp_key, &nh);
381 idi = &itp_db.id_itf[itp->itp_sw_if_index];
383 if (vnet_sw_interface_is_p2p (vnet_get_main (), itp->itp_sw_if_index))
385 // ipsec_tun_protect_feature_set (itp, 0);
386 idi->id_itp = INDEX_INVALID;
388 FOR_EACH_FIB_IP_PROTOCOL (nh_proto)
389 adj_nbr_walk (itp->itp_sw_if_index,
390 nh_proto, ipsec_tun_protect_adj_remove, itp);
394 adj_nbr_walk_nh (itp->itp_sw_if_index,
395 nh_proto, &nh, ipsec_tun_protect_adj_remove, itp);
397 hash_unset_mem (idi->id_hash, itp->itp_key);
399 if (0 == hash_elts (idi->id_hash))
401 // ipsec_tun_protect_feature_set (itp, 0);
402 hash_free (idi->id_hash);
405 ipsec_tun_unregister_nodes (FIB_PROTOCOL_IP6 == nh_proto ?
411 ipsec_tun_protect_set_crypto_addr (ipsec_tun_protect_t * itp)
416 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
418 if (ipsec_sa_is_set_IS_TUNNEL (sa))
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))
424 ipsec_sa_set_IS_PROTECT (sa);
425 itp->itp_flags |= IPSEC_PROTECT_ENCAPED;
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;
439 ipsec_tun_protect_config (ipsec_main_t * im,
440 ipsec_tun_protect_t * itp, u32 sa_out, u32 * sas_in)
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;
450 ipsec_sa_lock (itp->itp_out_sa);
453 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
457 ipsec_tun_protect_set_crypto_addr(itp);
461 * add to the DB against each SA
463 ipsec_tun_protect_rx_db_add (im, itp);
464 ipsec_tun_protect_tx_db_add (itp);
466 ITP_DBG (itp, "configured");
470 ipsec_tun_protect_unconfig (ipsec_main_t * im, ipsec_tun_protect_t * itp)
476 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
478 ipsec_sa_unset_IS_PROTECT (sa);
481 ipsec_tun_protect_rx_db_remove (im, itp);
482 ipsec_tun_protect_tx_db_remove (itp);
484 ipsec_sa_unlock(itp->itp_out_sa);
486 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
488 ipsec_sa_unlock(sai);
491 ITP_DBG (itp, "unconfigured");
495 ipsec_tun_protect_update_one (u32 sw_if_index,
496 const ip_address_t * nh, u32 sa_out, u32 sa_in)
501 vec_add1 (sas_in, sa_in);
502 rv = ipsec_tun_protect_update (sw_if_index, nh, sa_out, sas_in);
508 ipsec_tun_protect_update_out (u32 sw_if_index,
509 const ip_address_t * nh, u32 sa_out)
511 u32 itpi, *sas_in, sai, *saip;
512 ipsec_tun_protect_t *itp;
520 itpi = ipsec_tun_protect_find (sw_if_index, nh);
522 if (INDEX_INVALID == itpi)
524 return (VNET_API_ERROR_INVALID_INTERFACE);
527 itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
530 FOR_EACH_IPSEC_PROTECT_INPUT_SAI (itp, sai,
533 vec_add1 (sas_in, sai);
537 sa_out = ipsec_sa_find_and_lock (sa_out);
541 rv = VNET_API_ERROR_INVALID_VALUE;
545 ipsec_tun_protect_unconfig (im, itp);
546 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
548 ipsec_sa_unlock (sa_out);
549 vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
557 ipsec_tun_protect_update_in (u32 sw_if_index,
558 const ip_address_t * nh, u32 sa_in)
560 u32 itpi, *sas_in, sa_out;
561 ipsec_tun_protect_t *itp;
568 itpi = ipsec_tun_protect_find (sw_if_index, nh);
570 if (INDEX_INVALID == itpi)
572 return (VNET_API_ERROR_INVALID_INTERFACE);
575 sa_in = ipsec_sa_find_and_lock (sa_in);
579 rv = VNET_API_ERROR_INVALID_VALUE;
582 vec_add1 (sas_in, sa_in);
584 itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
585 sa_out = itp->itp_out_sa;
587 ipsec_sa_lock (sa_out);
589 ipsec_tun_protect_unconfig (im, itp);
590 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
592 ipsec_sa_unlock (sa_out);
593 ipsec_sa_unlock (sa_in);
600 ipsec_tun_protect_update_from_teib (ipsec_tun_protect_t * itp,
601 const teib_entry_t * ne)
605 const fib_prefix_t *pfx;
607 pfx = teib_entry_get_nh (ne);
609 ip46_address_copy (&itp->itp_tun.dst, &pfx->fp_addr);
612 ip46_address_reset (&itp->itp_tun.dst);
616 ipsec_tun_protect_update (u32 sw_if_index,
617 const ip_address_t * nh, u32 sa_out, u32 * sas_in)
619 ipsec_tun_protect_t *itp;
624 ITP_DBG2 ("update: %U/%U",
625 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
626 format_ip_address, nh);
628 if (vec_len (sas_in) > ITP_MAX_N_SA_IN)
630 rv = VNET_API_ERROR_LIMIT_EXCEEDED;
638 itpi = ipsec_tun_protect_find (sw_if_index, nh);
640 vec_foreach_index (ii, sas_in)
642 sas_in[ii] = ipsec_sa_find_and_lock (sas_in[ii]);
643 if (~0 == sas_in[ii])
645 rv = VNET_API_ERROR_INVALID_VALUE;
650 sa_out = ipsec_sa_find_and_lock (sa_out);
654 rv = VNET_API_ERROR_INVALID_VALUE;
658 if (INDEX_INVALID == itpi)
660 vnet_device_class_t *dev_class;
661 vnet_hw_interface_t *hi;
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);
669 if (NULL == dev_class->ip_tun_desc)
671 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
675 pool_get_zero (ipsec_tun_protect_pool, itp);
677 itp->itp_sw_if_index = sw_if_index;
678 itp->itp_ai = ADJ_INDEX_INVALID;
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;
685 itp->itp_key = clib_mem_alloc (sizeof (*itp->itp_key));
686 ip_address_copy (itp->itp_key, nh);
688 rv = dev_class->ip_tun_desc (sw_if_index,
690 &itp->itp_tun.dst, &is_l2);
695 if (ip46_address_is_zero (&itp->itp_tun.src))
697 /* must be one of thos pesky ipsec interfaces that has no encap.
698 * the encap then MUST comefrom the tunnel mode SA.
702 sa = ipsec_sa_get (itp->itp_out_sa);
704 if (!ipsec_sa_is_set_IS_TUNNEL (sa))
706 rv = VNET_API_ERROR_INVALID_DST_ADDRESS;
710 itp->itp_flags |= IPSEC_PROTECT_ITF;
712 else if (ip46_address_is_zero (&itp->itp_tun.dst))
714 /* tunnel has no destination address, presumably because it's p2mp
715 in which case we use the nh that this is protection for */
718 ip_address_to_46 (nh, &peer);
720 ipsec_tun_protect_update_from_teib
721 (itp, teib_entry_find (sw_if_index, &peer));
725 itp->itp_flags |= IPSEC_PROTECT_L2;
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.
749 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
753 /* updating SAs only */
754 itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
756 ipsec_tun_protect_unconfig (im, itp);
757 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
760 ipsec_sa_unlock (sa_out);
761 vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
769 ipsec_tun_protect_del (u32 sw_if_index, const ip_address_t * nh)
771 ipsec_tun_protect_t *itp;
775 ITP_DBG2 ("delete: %U/%U",
776 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
777 format_ip_address, nh);
783 itpi = ipsec_tun_protect_find (sw_if_index, nh);
785 if (INDEX_INVALID == itpi)
786 return (VNET_API_ERROR_NO_SUCH_ENTRY);
788 itp = ipsec_tun_protect_get (itpi);
789 ipsec_tun_protect_unconfig (im, itp);
791 if (ADJ_INDEX_INVALID != itp->itp_ai)
792 adj_unlock (itp->itp_ai);
794 clib_mem_free (itp->itp_key);
795 pool_put (ipsec_tun_protect_pool, itp);
801 ipsec_tun_protect_walk (ipsec_tun_protect_walk_cb_t fn, void *ctx)
806 pool_foreach_index(itpi, ipsec_tun_protect_pool,
814 ipsec_tun_protect_walk_itf (u32 sw_if_index,
815 ipsec_tun_protect_walk_cb_t fn, void *ctx)
817 ipsec_tun_protect_itf_db_t *idi;
821 if (vec_len (itp_db.id_itf) <= sw_if_index)
824 idi = &itp_db.id_itf[sw_if_index];
827 hash_foreach(key, itpi, idi->id_hash,
832 if (INDEX_INVALID != idi->id_itp)
833 fn (idi->id_itp, ctx);
837 ipsec_tun_protect_adj_delegate_adj_deleted (adj_delegate_t * ad)
839 /* remove our delegate */
840 ipsec_tun_protect_add_adj (ad->ad_adj_index, NULL);
841 adj_delegate_remove (ad->ad_adj_index, ipsec_tun_adj_delegate_type);
845 ipsec_tun_protect_adj_delegate_adj_modified (adj_delegate_t * ad)
847 ipsec_tun_protect_add_adj (ad->ad_adj_index,
848 ipsec_tun_protect_get (ad->ad_index));
852 ipsec_tun_protect_adj_delegate_adj_created (adj_index_t ai)
854 /* add our delegate if there is protection for this neighbour */
855 ip_address_t ip = IP_ADDRESS_V4_ALL_0S;
859 if (!adj_is_midchain (ai))
864 ip_address_from_46 (&adj->sub_type.midchain.next_hop,
865 adj->ia_nh_proto, &ip);
867 itpi = ipsec_tun_protect_find (adj->rewrite_header.sw_if_index, &ip);
869 if (INDEX_INVALID != itpi)
870 ipsec_tun_protect_adj_add (ai, ipsec_tun_protect_get (itpi));
874 ipsec_tun_protect_adj_delegate_format (const adj_delegate_t * aed, u8 * s)
876 const ipsec_tun_protect_t *itp;
878 itp = ipsec_tun_protect_from_const_base (aed);
879 s = format (s, "ipsec-tun-protect:\n%U", format_ipsec_tun_protect, itp);
885 ipsec_tun_teib_entry_added (const teib_entry_t * ne)
887 const ip46_address_t *peer46;
888 ipsec_tun_protect_t *itp;
892 peer46 = teib_entry_get_peer (ne);
893 ip_address_from_46 (peer46,
894 (ip46_address_is_ip4 (peer46) ?
895 FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6), &peer);
897 itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne), &peer);
899 if (INDEX_INVALID == itpi)
902 itp = ipsec_tun_protect_get (itpi);
903 ipsec_tun_protect_rx_db_remove (&ipsec_main, itp);
904 ipsec_tun_protect_update_from_teib (itp, ne);
905 ipsec_tun_protect_set_crypto_addr (itp);
906 ipsec_tun_protect_rx_db_add (&ipsec_main, itp);
908 ITP_DBG (itp, "teib-added");
912 ipsec_tun_teib_entry_deleted (const teib_entry_t * ne)
914 const ip46_address_t *peer46;
915 ipsec_tun_protect_t *itp;
919 peer46 = teib_entry_get_peer (ne);
920 ip_address_from_46 (peer46,
921 (ip46_address_is_ip4 (peer46) ?
922 FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6), &peer);
924 itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne), &peer);
926 if (INDEX_INVALID == itpi)
929 itp = ipsec_tun_protect_get (itpi);
930 ipsec_tun_protect_rx_db_remove (&ipsec_main, itp);
931 ipsec_tun_protect_update_from_teib (itp, NULL);
932 ipsec_tun_protect_set_crypto_addr (itp);
934 ITP_DBG (itp, "teib-removed");
938 * VFT registered with the adjacency delegate
940 const static adj_delegate_vft_t ipsec_tun_adj_delegate_vft = {
941 .adv_adj_deleted = ipsec_tun_protect_adj_delegate_adj_deleted,
942 .adv_adj_created = ipsec_tun_protect_adj_delegate_adj_created,
943 .adv_adj_modified = ipsec_tun_protect_adj_delegate_adj_modified,
944 .adv_format = ipsec_tun_protect_adj_delegate_format,
947 const static teib_vft_t ipsec_tun_teib_vft = {
948 .nv_added = ipsec_tun_teib_entry_added,
949 .nv_deleted = ipsec_tun_teib_entry_deleted,
954 ipsec_tunnel_protect_init (vlib_main_t * vm)
959 im->tun6_protect_by_key = hash_create_mem (0,
960 sizeof (ipsec6_tunnel_key_t),
962 im->tun4_protect_by_key = hash_create (0, sizeof (u64));
964 /* set up feature nodes to drop outbound packets with no crypto alg set */
965 im->esp4_no_crypto_tun_node_index =
966 vlib_get_node_by_name (vm, (u8 *) "esp4-no-crypto")->index;
967 im->esp6_no_crypto_tun_node_index =
968 vlib_get_node_by_name (vm, (u8 *) "esp6-no-crypto")->index;
969 im->esp6_encrypt_l2_tun_node_index =
970 vlib_get_node_by_name (vm, (u8 *) "esp6-encrypt-tun")->index;
971 im->esp4_encrypt_l2_tun_node_index =
972 vlib_get_node_by_name (vm, (u8 *) "esp4-encrypt-tun")->index;
974 ipsec_tun_adj_delegate_type =
975 adj_delegate_register_new_type (&ipsec_tun_adj_delegate_vft);
977 ipsec_tun_protect_logger = vlib_log_register_class ("ipsec", "tun");
979 teib_register (&ipsec_tun_teib_vft);
984 VLIB_INIT_FUNCTION (ipsec_tunnel_protect_init);
988 * fd.io coding-style-patch-verification: ON
991 * eval: (c-set-style "gnu")