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)
153 im->esp4_no_crypto_tun_node_index :
154 im->esp6_no_crypto_tun_node_index);
155 else if (itp->itp_flags & IPSEC_PROTECT_L2)
157 im->esp4_encrypt_l2_tun_node_index :
158 im->esp6_encrypt_l2_tun_node_index);
161 im->esp4_encrypt_tun_node_index :
162 im->esp6_encrypt_tun_node_index);
168 ipsec_tun_protect_add_adj (adj_index_t ai, const ipsec_tun_protect_t * itp)
170 vec_validate_init_empty (ipsec_tun_protect_sa_by_adj_index, ai,
175 ipsec_tun_protect_sa_by_adj_index[ai] = INDEX_INVALID;
176 adj_nbr_midchain_reset_next_node (ai);
180 ipsec_tun_protect_sa_by_adj_index[ai] = itp->itp_out_sa;
181 adj_nbr_midchain_update_next_node
182 (ai, ipsec_tun_protect_get_adj_next (adj_get_link_type (ai), itp));
187 ipsec_tun_protect_find (u32 sw_if_index, const ip_address_t * nh)
189 ipsec_tun_protect_itf_db_t *idi;
192 if (vec_len (itp_db.id_itf) <= sw_if_index)
193 return INDEX_INVALID;
195 if (vnet_sw_interface_is_p2p (vnet_get_main (), sw_if_index))
196 return (itp_db.id_itf[sw_if_index].id_itp);
198 idi = &itp_db.id_itf[sw_if_index];
199 p = hash_get_mem (idi->id_hash, nh);
203 return INDEX_INVALID;
209 ipsec_tun_protect_rx_db_add (ipsec_main_t * im,
210 const ipsec_tun_protect_t * itp)
212 const ipsec_sa_t *sa;
215 if (ip46_address_is_zero (&itp->itp_crypto.dst))
219 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
221 sa = ipsec_sa_get (sai);
223 ipsec_tun_lkup_result_t res = {
224 .tun_index = itp - ipsec_tun_protect_pool,
229 * The key is formed from the tunnel's destination
230 * as the packet lookup is done from the packet's source
232 if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
234 ipsec4_tunnel_kv_t key = {
237 clib_bihash_kv_8_8_t *bkey = (clib_bihash_kv_8_8_t*)&key;
239 ipsec4_tunnel_mk_key(&key, &itp->itp_crypto.dst.ip4,
240 clib_host_to_net_u32 (sa->spi));
242 if (!im->tun4_protect_by_key.nbuckets)
243 clib_bihash_init_8_8 (&im->tun4_protect_by_key,
244 "IPSec IPv4 tunnels",
245 IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS,
246 IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE);
248 clib_bihash_add_del_8_8 (&im->tun4_protect_by_key, bkey, 1);
249 ipsec_tun_register_nodes(AF_IP4);
253 ipsec6_tunnel_kv_t key = {
255 .remote_ip = itp->itp_crypto.dst.ip6,
256 .spi = clib_host_to_net_u32 (sa->spi),
260 clib_bihash_kv_24_8_t *bkey = (clib_bihash_kv_24_8_t*)&key;
262 if (!im->tun4_protect_by_key.nbuckets)
263 clib_bihash_init_24_8 (&im->tun6_protect_by_key,
264 "IPSec IPv6 tunnels",
265 IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS,
266 IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE);
267 clib_bihash_add_del_24_8 (&im->tun6_protect_by_key, bkey, 1);
268 ipsec_tun_register_nodes(AF_IP6);
275 ipsec_tun_protect_adj_add (adj_index_t ai, void *arg)
277 ipsec_tun_protect_t *itp = arg;
278 adj_delegate_add (adj_get (ai), ipsec_tun_adj_delegate_type,
279 itp - ipsec_tun_protect_pool);
280 ipsec_tun_protect_add_adj (ai, itp);
282 if (itp->itp_flags & IPSEC_PROTECT_ITF)
283 ipsec_itf_adj_stack (ai, itp->itp_out_sa);
285 return (ADJ_WALK_RC_CONTINUE);
289 ipsec_tun_protect_tx_db_add (ipsec_tun_protect_t * itp)
292 * add the delegate to the adj
294 ipsec_tun_protect_itf_db_t *idi;
295 fib_protocol_t nh_proto;
298 vec_validate_init_empty (itp_db.id_itf,
299 itp->itp_sw_if_index,
300 IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY);
302 idi = &itp_db.id_itf[itp->itp_sw_if_index];
304 if (vnet_sw_interface_is_p2p (vnet_get_main (), itp->itp_sw_if_index))
306 if (INDEX_INVALID == idi->id_itp)
308 // ipsec_tun_protect_feature_set (itp, 1);
310 idi->id_itp = itp - ipsec_tun_protect_pool;
312 FOR_EACH_FIB_IP_PROTOCOL (nh_proto)
313 adj_nbr_walk (itp->itp_sw_if_index,
314 nh_proto, ipsec_tun_protect_adj_add, itp);
318 if (NULL == idi->id_hash)
321 hash_create_mem (0, sizeof (ip_address_t), sizeof (uword));
323 * enable the encrypt feature for egress if this is the first addition
326 // ipsec_tun_protect_feature_set (itp, 1);
329 hash_set_mem (idi->id_hash, itp->itp_key, itp - ipsec_tun_protect_pool);
332 * walk all the adjs with the same nh on this interface
333 * to associate them with this protection
335 nh_proto = ip_address_to_46 (itp->itp_key, &nh);
337 adj_nbr_walk_nh (itp->itp_sw_if_index,
338 nh_proto, &nh, ipsec_tun_protect_adj_add, itp);
340 ipsec_tun_register_nodes (FIB_PROTOCOL_IP6 == nh_proto ?
346 ipsec_tun_protect_rx_db_remove (ipsec_main_t * im,
347 const ipsec_tun_protect_t * itp)
349 const ipsec_sa_t *sa;
352 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
354 if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
356 ipsec4_tunnel_kv_t key;
357 clib_bihash_kv_8_8_t res, *bkey = (clib_bihash_kv_8_8_t*)&key;
359 ipsec4_tunnel_mk_key(&key, &itp->itp_crypto.dst.ip4,
360 clib_host_to_net_u32 (sa->spi));
362 if (!clib_bihash_search_8_8 (&im->tun4_protect_by_key, bkey, &res))
364 clib_bihash_add_del_8_8 (&im->tun4_protect_by_key, bkey, 0);
365 ipsec_tun_unregister_nodes(AF_IP4);
370 ipsec6_tunnel_kv_t key = {
372 .remote_ip = itp->itp_crypto.dst.ip6,
373 .spi = clib_host_to_net_u32 (sa->spi),
376 clib_bihash_kv_24_8_t res, *bkey = (clib_bihash_kv_24_8_t*)&key;
378 if (!clib_bihash_search_24_8 (&im->tun6_protect_by_key, bkey, &res))
380 clib_bihash_add_del_24_8 (&im->tun6_protect_by_key, bkey, 0);
381 ipsec_tun_unregister_nodes(AF_IP6);
389 ipsec_tun_protect_adj_remove (adj_index_t ai, void *arg)
391 ipsec_tun_protect_t *itp = arg;
393 adj_delegate_remove (ai, ipsec_tun_adj_delegate_type);
394 ipsec_tun_protect_add_adj (ai, NULL);
396 if (itp->itp_flags & IPSEC_PROTECT_ITF)
397 ipsec_itf_adj_unstack (ai);
399 return (ADJ_WALK_RC_CONTINUE);
403 ipsec_tun_protect_tx_db_remove (ipsec_tun_protect_t * itp)
405 ipsec_tun_protect_itf_db_t *idi;
406 fib_protocol_t nh_proto;
409 nh_proto = ip_address_to_46 (itp->itp_key, &nh);
410 idi = &itp_db.id_itf[itp->itp_sw_if_index];
412 if (vnet_sw_interface_is_p2p (vnet_get_main (), itp->itp_sw_if_index))
414 // ipsec_tun_protect_feature_set (itp, 0);
415 idi->id_itp = INDEX_INVALID;
417 FOR_EACH_FIB_IP_PROTOCOL (nh_proto)
418 adj_nbr_walk (itp->itp_sw_if_index,
419 nh_proto, ipsec_tun_protect_adj_remove, itp);
423 adj_nbr_walk_nh (itp->itp_sw_if_index,
424 nh_proto, &nh, ipsec_tun_protect_adj_remove, itp);
426 hash_unset_mem (idi->id_hash, itp->itp_key);
428 if (0 == hash_elts (idi->id_hash))
430 // ipsec_tun_protect_feature_set (itp, 0);
431 hash_free (idi->id_hash);
434 ipsec_tun_unregister_nodes (FIB_PROTOCOL_IP6 == nh_proto ?
440 ipsec_tun_protect_set_crypto_addr (ipsec_tun_protect_t * itp)
445 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
447 if (ipsec_sa_is_set_IS_TUNNEL (sa))
449 itp->itp_crypto.src = sa->tunnel_dst_addr;
450 itp->itp_crypto.dst = sa->tunnel_src_addr;
451 if (!(itp->itp_flags & IPSEC_PROTECT_ITF))
453 ipsec_sa_set_IS_PROTECT (sa);
454 itp->itp_flags |= IPSEC_PROTECT_ENCAPED;
459 itp->itp_crypto.src = itp->itp_tun.src;
460 itp->itp_crypto.dst = itp->itp_tun.dst;
461 itp->itp_flags &= ~IPSEC_PROTECT_ENCAPED;
468 ipsec_tun_protect_config (ipsec_main_t * im,
469 ipsec_tun_protect_t * itp, u32 sa_out, u32 * sas_in)
474 itp->itp_n_sa_in = vec_len (sas_in);
475 for (ii = 0; ii < itp->itp_n_sa_in; ii++)
476 itp->itp_in_sas[ii] = sas_in[ii];
477 itp->itp_out_sa = sa_out;
479 ipsec_sa_lock (itp->itp_out_sa);
482 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
486 ipsec_tun_protect_set_crypto_addr(itp);
490 * add to the DB against each SA
492 ipsec_tun_protect_rx_db_add (im, itp);
493 ipsec_tun_protect_tx_db_add (itp);
495 ITP_DBG (itp, "configured");
499 ipsec_tun_protect_unconfig (ipsec_main_t * im, ipsec_tun_protect_t * itp)
505 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
507 ipsec_sa_unset_IS_PROTECT (sa);
510 ipsec_tun_protect_rx_db_remove (im, itp);
511 ipsec_tun_protect_tx_db_remove (itp);
513 ipsec_sa_unlock(itp->itp_out_sa);
515 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
517 ipsec_sa_unlock(sai);
520 ITP_DBG (itp, "unconfigured");
524 ipsec_tun_protect_update_one (u32 sw_if_index,
525 const ip_address_t * nh, u32 sa_out, u32 sa_in)
530 vec_add1 (sas_in, sa_in);
531 rv = ipsec_tun_protect_update (sw_if_index, nh, sa_out, sas_in);
537 ipsec_tun_protect_update_out (u32 sw_if_index,
538 const ip_address_t * nh, u32 sa_out)
540 u32 itpi, *sas_in, sai, *saip;
541 ipsec_tun_protect_t *itp;
549 itpi = ipsec_tun_protect_find (sw_if_index, nh);
551 if (INDEX_INVALID == itpi)
553 return (VNET_API_ERROR_INVALID_INTERFACE);
556 itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
559 FOR_EACH_IPSEC_PROTECT_INPUT_SAI (itp, sai,
562 vec_add1 (sas_in, sai);
566 sa_out = ipsec_sa_find_and_lock (sa_out);
570 rv = VNET_API_ERROR_INVALID_VALUE;
574 ipsec_tun_protect_unconfig (im, itp);
575 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
577 ipsec_sa_unlock (sa_out);
578 vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
586 ipsec_tun_protect_update_in (u32 sw_if_index,
587 const ip_address_t * nh, u32 sa_in)
589 u32 itpi, *sas_in, sa_out;
590 ipsec_tun_protect_t *itp;
597 itpi = ipsec_tun_protect_find (sw_if_index, nh);
599 if (INDEX_INVALID == itpi)
601 return (VNET_API_ERROR_INVALID_INTERFACE);
604 sa_in = ipsec_sa_find_and_lock (sa_in);
608 rv = VNET_API_ERROR_INVALID_VALUE;
611 vec_add1 (sas_in, sa_in);
613 itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
614 sa_out = itp->itp_out_sa;
616 ipsec_sa_lock (sa_out);
618 ipsec_tun_protect_unconfig (im, itp);
619 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
621 ipsec_sa_unlock (sa_out);
622 ipsec_sa_unlock (sa_in);
629 ipsec_tun_protect_update_from_teib (ipsec_tun_protect_t * itp,
630 const teib_entry_t * ne)
634 const fib_prefix_t *pfx;
636 pfx = teib_entry_get_nh (ne);
638 ip46_address_copy (&itp->itp_tun.dst, &pfx->fp_addr);
641 ip46_address_reset (&itp->itp_tun.dst);
645 ipsec_tun_protect_update (u32 sw_if_index,
646 const ip_address_t * nh, u32 sa_out, u32 * sas_in)
648 ipsec_tun_protect_t *itp;
653 ITP_DBG2 ("update: %U/%U",
654 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
655 format_ip_address, nh);
657 if (vec_len (sas_in) > ITP_MAX_N_SA_IN)
659 rv = VNET_API_ERROR_LIMIT_EXCEEDED;
667 itpi = ipsec_tun_protect_find (sw_if_index, nh);
669 vec_foreach_index (ii, sas_in)
671 sas_in[ii] = ipsec_sa_find_and_lock (sas_in[ii]);
672 if (~0 == sas_in[ii])
674 rv = VNET_API_ERROR_INVALID_VALUE;
679 sa_out = ipsec_sa_find_and_lock (sa_out);
683 rv = VNET_API_ERROR_INVALID_VALUE;
687 if (INDEX_INVALID == itpi)
689 vnet_device_class_t *dev_class;
690 vnet_hw_interface_t *hi;
694 vnm = vnet_get_main ();
695 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
696 dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
698 if (NULL == dev_class->ip_tun_desc)
700 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
704 pool_get_zero (ipsec_tun_protect_pool, itp);
706 itp->itp_sw_if_index = sw_if_index;
707 itp->itp_ai = ADJ_INDEX_INVALID;
709 itp->itp_n_sa_in = vec_len (sas_in);
710 for (ii = 0; ii < itp->itp_n_sa_in; ii++)
711 itp->itp_in_sas[ii] = sas_in[ii];
712 itp->itp_out_sa = sa_out;
714 itp->itp_key = clib_mem_alloc (sizeof (*itp->itp_key));
715 ip_address_copy (itp->itp_key, nh);
717 rv = dev_class->ip_tun_desc (sw_if_index,
719 &itp->itp_tun.dst, &is_l2);
724 if (ip46_address_is_zero (&itp->itp_tun.src))
726 /* must be one of thos pesky ipsec interfaces that has no encap.
727 * the encap then MUST comefrom the tunnel mode SA.
731 sa = ipsec_sa_get (itp->itp_out_sa);
733 if (!ipsec_sa_is_set_IS_TUNNEL (sa))
735 rv = VNET_API_ERROR_INVALID_DST_ADDRESS;
739 itp->itp_flags |= IPSEC_PROTECT_ITF;
741 else if (ip46_address_is_zero (&itp->itp_tun.dst))
743 /* tunnel has no destination address, presumably because it's p2mp
744 in which case we use the nh that this is protection for */
745 ipsec_tun_protect_update_from_teib
746 (itp, teib_entry_find (sw_if_index, nh));
750 itp->itp_flags |= IPSEC_PROTECT_L2;
753 * add to the tunnel DB for ingress
754 * - if the SA is in trasnport mode, then the packates will arrive
755 * with the IP src,dst of the protected tunnel, in which case we can
756 * simply strip the IP header and hand the payload to the protocol
757 * appropriate input handler
758 * - if the SA is in tunnel mode then there are two IP headers present
759 * one for the crytpo tunnel endpoints (described in the SA) and one
760 * for the tunnel endpoints. The outer IP headers in the srriving
761 * packets will have the crypto endpoints. So the DB needs to contain
762 * the crpto endpoint. Once the crypto header is stripped, revealing,
763 * the tunnel-IP we have 2 choices:
764 * 1) do a tunnel lookup based on the revealed header
765 * 2) skip the tunnel lookup and assume that the packet matches the
766 * one that is protected here.
767 * If we did 1) then we would allow our peer to use the SA for tunnel
768 * X to inject traffic onto tunnel Y, this is not good. If we do 2)
769 * then we don't verify that the peer is indeed using SA for tunnel
770 * X and addressing tunnel X. So we take a compromise, once the SA
771 * matches to tunnel X we veriy that the inner IP matches the value
772 * of the tunnel we are protecting, else it's dropped.
774 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
778 /* updating SAs only */
779 itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
781 ipsec_tun_protect_unconfig (im, itp);
782 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
785 ipsec_sa_unlock (sa_out);
786 vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
794 ipsec_tun_protect_del (u32 sw_if_index, const ip_address_t * nh)
796 ipsec_tun_protect_t *itp;
800 ITP_DBG2 ("delete: %U/%U",
801 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
802 format_ip_address, nh);
808 itpi = ipsec_tun_protect_find (sw_if_index, nh);
810 if (INDEX_INVALID == itpi)
811 return (VNET_API_ERROR_NO_SUCH_ENTRY);
813 itp = ipsec_tun_protect_get (itpi);
814 ipsec_tun_protect_unconfig (im, itp);
816 if (ADJ_INDEX_INVALID != itp->itp_ai)
817 adj_unlock (itp->itp_ai);
819 clib_mem_free (itp->itp_key);
820 pool_put (ipsec_tun_protect_pool, itp);
826 ipsec_tun_protect_walk (ipsec_tun_protect_walk_cb_t fn, void *ctx)
831 pool_foreach_index(itpi, ipsec_tun_protect_pool,
839 ipsec_tun_protect_walk_itf (u32 sw_if_index,
840 ipsec_tun_protect_walk_cb_t fn, void *ctx)
842 ipsec_tun_protect_itf_db_t *idi;
846 if (vec_len (itp_db.id_itf) <= sw_if_index)
849 idi = &itp_db.id_itf[sw_if_index];
852 hash_foreach(key, itpi, idi->id_hash,
857 if (INDEX_INVALID != idi->id_itp)
858 fn (idi->id_itp, ctx);
862 ipsec_tun_protect_adj_delegate_adj_deleted (adj_delegate_t * ad)
864 /* remove our delegate */
865 ipsec_tun_protect_add_adj (ad->ad_adj_index, NULL);
866 adj_delegate_remove (ad->ad_adj_index, ipsec_tun_adj_delegate_type);
870 ipsec_tun_protect_adj_delegate_adj_modified (adj_delegate_t * ad)
872 ipsec_tun_protect_add_adj (ad->ad_adj_index,
873 ipsec_tun_protect_get (ad->ad_index));
877 ipsec_tun_protect_adj_delegate_adj_created (adj_index_t ai)
879 /* add our delegate if there is protection for this neighbour */
880 ip_address_t ip = IP_ADDRESS_V4_ALL_0S;
884 if (!adj_is_midchain (ai))
889 ip_address_from_46 (&adj->sub_type.midchain.next_hop,
890 adj->ia_nh_proto, &ip);
892 itpi = ipsec_tun_protect_find (adj->rewrite_header.sw_if_index, &ip);
894 if (INDEX_INVALID != itpi)
895 ipsec_tun_protect_adj_add (ai, ipsec_tun_protect_get (itpi));
899 ipsec_tun_protect_adj_delegate_format (const adj_delegate_t * aed, u8 * s)
901 const ipsec_tun_protect_t *itp;
903 itp = ipsec_tun_protect_from_const_base (aed);
904 s = format (s, "ipsec-tun-protect:\n%U", format_ipsec_tun_protect, itp);
910 ipsec_tun_teib_entry_added (const teib_entry_t * ne)
912 ipsec_tun_protect_t *itp;
915 itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne),
916 teib_entry_get_peer (ne));
918 if (INDEX_INVALID == itpi)
921 itp = ipsec_tun_protect_get (itpi);
922 ipsec_tun_protect_rx_db_remove (&ipsec_main, itp);
923 ipsec_tun_protect_update_from_teib (itp, ne);
924 ipsec_tun_protect_set_crypto_addr (itp);
925 ipsec_tun_protect_rx_db_add (&ipsec_main, itp);
927 ITP_DBG (itp, "teib-added");
931 ipsec_tun_teib_entry_deleted (const teib_entry_t * ne)
933 ipsec_tun_protect_t *itp;
936 itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne),
937 teib_entry_get_peer (ne));
939 if (INDEX_INVALID == itpi)
942 itp = ipsec_tun_protect_get (itpi);
943 ipsec_tun_protect_rx_db_remove (&ipsec_main, itp);
944 ipsec_tun_protect_update_from_teib (itp, NULL);
945 ipsec_tun_protect_set_crypto_addr (itp);
947 ITP_DBG (itp, "teib-removed");
951 * VFT registered with the adjacency delegate
953 const static adj_delegate_vft_t ipsec_tun_adj_delegate_vft = {
954 .adv_adj_deleted = ipsec_tun_protect_adj_delegate_adj_deleted,
955 .adv_adj_created = ipsec_tun_protect_adj_delegate_adj_created,
956 .adv_adj_modified = ipsec_tun_protect_adj_delegate_adj_modified,
957 .adv_format = ipsec_tun_protect_adj_delegate_format,
960 const static teib_vft_t ipsec_tun_teib_vft = {
961 .nv_added = ipsec_tun_teib_entry_added,
962 .nv_deleted = ipsec_tun_teib_entry_deleted,
966 ipsec_tun_table_init (ip_address_family_t af, uword table_size, u32 n_buckets)
973 clib_bihash_init_8_8 (&im->tun4_protect_by_key,
974 "IPSec IPv4 tunnels", n_buckets, table_size);
976 clib_bihash_init_24_8 (&im->tun6_protect_by_key,
977 "IPSec IPv6 tunnels", n_buckets, table_size);
981 ipsec_tunnel_protect_init (vlib_main_t * vm)
986 clib_bihash_init_24_8 (&im->tun6_protect_by_key,
987 "IPSec IPv6 tunnels",
988 IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS,
989 IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE);
990 clib_bihash_init_8_8 (&im->tun4_protect_by_key,
991 "IPSec IPv4 tunnels",
992 IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS,
993 IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE);
995 /* set up feature nodes to drop outbound packets with no crypto alg set */
996 im->esp4_no_crypto_tun_node_index =
997 vlib_get_node_by_name (vm, (u8 *) "esp4-no-crypto")->index;
998 im->esp6_no_crypto_tun_node_index =
999 vlib_get_node_by_name (vm, (u8 *) "esp6-no-crypto")->index;
1000 im->esp6_encrypt_l2_tun_node_index =
1001 vlib_get_node_by_name (vm, (u8 *) "esp6-encrypt-tun")->index;
1002 im->esp4_encrypt_l2_tun_node_index =
1003 vlib_get_node_by_name (vm, (u8 *) "esp4-encrypt-tun")->index;
1005 ipsec_tun_adj_delegate_type =
1006 adj_delegate_register_new_type (&ipsec_tun_adj_delegate_vft);
1008 ipsec_tun_protect_logger = vlib_log_register_class ("ipsec", "tun");
1010 teib_register (&ipsec_tun_teib_vft);
1015 VLIB_INIT_FUNCTION (ipsec_tunnel_protect_init);
1017 static clib_error_t *
1018 ipsec_config (vlib_main_t * vm, unformat_input_t * input)
1020 unformat_input_t sub_input;
1022 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1024 if (unformat (input, "ip4 %U", unformat_vlib_cli_sub_input, &sub_input))
1026 uword table_size = ~0;
1029 while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
1031 if (unformat (&sub_input, "num-buckets %u", &n_buckets))
1034 return clib_error_return (0, "unknown input `%U'",
1035 format_unformat_error, &sub_input);
1038 ipsec_tun_table_init (AF_IP4, table_size, n_buckets);
1040 else if (unformat (input, "ip6 %U", unformat_vlib_cli_sub_input,
1043 uword table_size = ~0;
1046 while (unformat_check_input (&sub_input) != UNFORMAT_END_OF_INPUT)
1048 if (unformat (&sub_input, "num-buckets %u", &n_buckets))
1051 return clib_error_return (0, "unknown input `%U'",
1052 format_unformat_error, &sub_input);
1055 ipsec_tun_table_init (AF_IP6, table_size, n_buckets);
1058 return clib_error_return (0, "unknown input `%U'",
1059 format_unformat_error, input);
1065 VLIB_CONFIG_FUNCTION (ipsec_config, "ipsec");
1068 * fd.io coding-style-patch-verification: ON
1071 * eval: (c-set-style "gnu")