blob: 543be8a7faaf210d900a3b5fdfb55626ec5b4ade [file] [log] [blame]
Neale Rannsc87b66c2019-02-07 07:26:12 -08001/*
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>
Neale Rannsdd4ccf22020-06-30 07:47:14 +000019#include <vnet/ipsec/ipsec_itf.h>
Neale Rannsc87b66c2019-02-07 07:26:12 -080020#include <vnet/ipsec/esp.h>
Florin Corasb040f982020-10-20 14:59:43 -070021#include <vnet/udp/udp_local.h>
Neale Ranns28287212019-12-16 00:53:11 +000022#include <vnet/adj/adj_delegate.h>
Neale Ranns4ec36c52020-03-31 09:21:29 -040023#include <vnet/adj/adj_midchain.h>
Neale Ranns28287212019-12-16 00:53:11 +000024#include <vnet/teib/teib.h>
Neale Ranns6fdcc3d2021-10-08 07:30:47 +000025#include <vnet/mpls/mpls.h>
Neale Ranns28287212019-12-16 00:53:11 +000026
Neale Ranns302b25a2020-10-19 13:23:33 +000027/* instantiate the bihash functions */
28#include <vppinfra/bihash_8_16.h>
29#include <vppinfra/bihash_template.c>
30#include <vppinfra/bihash_24_16.h>
31#include <vppinfra/bihash_template.c>
32
Neale Ranns7b4e52f2020-05-24 16:17:50 +000033#define IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS (64 * 1024)
34#define IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE 512 << 20
35
Neale Ranns28287212019-12-16 00:53:11 +000036/**
37 * The logger
38 */
39vlib_log_class_t ipsec_tun_protect_logger;
Neale Rannsc87b66c2019-02-07 07:26:12 -080040
41/**
42 * Pool of tunnel protection objects
43 */
Neale Ranns28287212019-12-16 00:53:11 +000044ipsec_tun_protect_t *ipsec_tun_protect_pool;
Neale Rannsc87b66c2019-02-07 07:26:12 -080045
46/**
Neale Ranns28287212019-12-16 00:53:11 +000047 * Adj delegate registered type
Neale Rannsc87b66c2019-02-07 07:26:12 -080048 */
Neale Ranns28287212019-12-16 00:53:11 +000049static adj_delegate_type_t ipsec_tun_adj_delegate_type;
Neale Rannsc87b66c2019-02-07 07:26:12 -080050
Neale Ranns28287212019-12-16 00:53:11 +000051/**
52 * Adj index to TX SA mapping
53 */
54index_t *ipsec_tun_protect_sa_by_adj_index;
55
56const ip_address_t IP_ADDR_ALL_0 = IP_ADDRESS_V4_ALL_0S;
57
58/**
59 * The DB of all added per-nh tunnel protectiond
60 */
61typedef struct ipsec_tun_protect_itf_db_t_
62{
63 /** A hash table key'd on IP (4 or 6) address */
64 uword *id_hash;
65 /** If the interface is P2P then there is only one protect
66 * object associated with the auto-adj for each NH proto */
67 index_t id_itp;
68} ipsec_tun_protect_itf_db_t;
69
70typedef struct ipsec_tun_protect_db_t_
71{
72 /** Per-interface vector */
73 ipsec_tun_protect_itf_db_t *id_itf;
74} ipsec_tun_protect_db_t;
75
76static ipsec_tun_protect_db_t itp_db;
77
78const static ipsec_tun_protect_itf_db_t IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY = {
79 .id_itp = INDEX_INVALID,
80};
81
82#define ITP_DBG(_itp, _fmt, _args...) \
83{ \
84 vlib_log_debug(ipsec_tun_protect_logger, \
85 "[%U]: " _fmt, \
86 format_ipsec_tun_protect, \
87 _itp, ##_args); \
88}
89
90#define ITP_DBG2(_fmt, _args...) \
91{ \
92 vlib_log_debug(ipsec_tun_protect_logger, \
93 _fmt, ##_args); \
94}
95
Neale Ranns8d6d74c2020-02-20 09:45:16 +000096static u32 ipsec_tun_node_regs[N_AF];
97
98void
99ipsec_tun_register_nodes (ip_address_family_t af)
100{
101 if (0 == ipsec_tun_node_regs[af]++)
102 {
103 if (AF_IP4 == af)
104 {
Neale Rannsabc56602020-04-01 09:45:23 +0000105 ipsec_register_udp_port (UDP_DST_PORT_ipsec);
Neale Ranns8d6d74c2020-02-20 09:45:16 +0000106 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
107 ipsec4_tun_input_node.index);
108 }
109 else
110 ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP,
111 ipsec6_tun_input_node.index);
112 }
113}
114
115void
116ipsec_tun_unregister_nodes (ip_address_family_t af)
117{
118 ASSERT (0 != ipsec_tun_node_regs[af]);
119 if (0 == --ipsec_tun_node_regs[af])
120 {
121 if (AF_IP4 == af)
122 {
Neale Rannsabc56602020-04-01 09:45:23 +0000123 ipsec_unregister_udp_port (UDP_DST_PORT_ipsec);
Neale Ranns8d6d74c2020-02-20 09:45:16 +0000124 ip4_unregister_protocol (IP_PROTOCOL_IPSEC_ESP);
125 }
126 else
127 ip6_unregister_protocol (IP_PROTOCOL_IPSEC_ESP);
128 }
129}
130
Neale Ranns28287212019-12-16 00:53:11 +0000131static inline const ipsec_tun_protect_t *
132ipsec_tun_protect_from_const_base (const adj_delegate_t * ad)
133{
134 if (ad == NULL)
135 return (NULL);
136 return (pool_elt_at_index (ipsec_tun_protect_pool, ad->ad_index));
137}
Neale Rannsc87b66c2019-02-07 07:26:12 -0800138
Neale Ranns4ec36c52020-03-31 09:21:29 -0400139static u32
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000140ipsec_tun_protect_get_adj_next (vnet_link_t linkt,
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000141 const ipsec_tun_protect_t *itp)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800142{
Neale Ranns4ec36c52020-03-31 09:21:29 -0400143 ipsec_main_t *im;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400144 u32 next;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800145
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000146 im = &ipsec_main;
147 next = 0;
148
Neale Ranns4a58e492020-12-21 13:19:10 +0000149 if (!(itp->itp_flags & IPSEC_PROTECT_ITF))
150 {
151 if (ip46_address_is_ip4 (&itp->itp_tun.src))
152 linkt = VNET_LINK_IP4;
153 else
154 linkt = VNET_LINK_IP6;
155 }
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000156
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000157 switch (linkt)
Neale Ranns4a58e492020-12-21 13:19:10 +0000158 {
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000159 case VNET_LINK_IP4:
160 next = im->esp4_encrypt_tun_node_index;
161 break;
162 case VNET_LINK_IP6:
163 next = im->esp6_encrypt_tun_node_index;
164 break;
165 case VNET_LINK_MPLS:
166 next = im->esp_mpls_encrypt_tun_node_index;
167 break;
168 case VNET_LINK_ARP:
169 case VNET_LINK_NSH:
170 case VNET_LINK_ETHERNET:
171 ASSERT (0);
172 break;
Neale Ranns4a58e492020-12-21 13:19:10 +0000173 }
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000174
Neale Ranns4ec36c52020-03-31 09:21:29 -0400175 return (next);
176}
177
178static void
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000179ipsec_tun_setup_tx_nodes (u32 sw_if_index, const ipsec_tun_protect_t *itp)
180{
181 vnet_feature_modify_end_node (
182 ip4_main.lookup_main.output_feature_arc_index, sw_if_index,
183 ipsec_tun_protect_get_adj_next (VNET_LINK_IP4, itp));
184 vnet_feature_modify_end_node (
185 ip6_main.lookup_main.output_feature_arc_index, sw_if_index,
186 ipsec_tun_protect_get_adj_next (VNET_LINK_IP6, itp));
187 vnet_feature_modify_end_node (
188 mpls_main.output_feature_arc_index, sw_if_index,
189 ipsec_tun_protect_get_adj_next (VNET_LINK_MPLS, itp));
190}
191
192static void
Neale Ranns4ec36c52020-03-31 09:21:29 -0400193ipsec_tun_protect_add_adj (adj_index_t ai, const ipsec_tun_protect_t * itp)
194{
195 vec_validate_init_empty (ipsec_tun_protect_sa_by_adj_index, ai,
196 INDEX_INVALID);
197
198 if (NULL == itp)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800199 {
Neale Ranns4ec36c52020-03-31 09:21:29 -0400200 ipsec_tun_protect_sa_by_adj_index[ai] = INDEX_INVALID;
201 adj_nbr_midchain_reset_next_node (ai);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800202 }
203 else
204 {
Neale Ranns4ec36c52020-03-31 09:21:29 -0400205 ipsec_tun_protect_sa_by_adj_index[ai] = itp->itp_out_sa;
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000206 adj_nbr_midchain_update_next_node (
207 ai, ipsec_tun_protect_get_adj_next (adj_get_link_type (ai), itp));
Neale Rannsc87b66c2019-02-07 07:26:12 -0800208 }
Neale Rannsc87b66c2019-02-07 07:26:12 -0800209}
210
Neale Ranns28287212019-12-16 00:53:11 +0000211static index_t
212ipsec_tun_protect_find (u32 sw_if_index, const ip_address_t * nh)
213{
214 ipsec_tun_protect_itf_db_t *idi;
215 uword *p;
216
217 if (vec_len (itp_db.id_itf) <= sw_if_index)
218 return INDEX_INVALID;
219
220 if (vnet_sw_interface_is_p2p (vnet_get_main (), sw_if_index))
221 return (itp_db.id_itf[sw_if_index].id_itp);
222
223 idi = &itp_db.id_itf[sw_if_index];
224 p = hash_get_mem (idi->id_hash, nh);
225
226 if (NULL == p)
227 {
228 return INDEX_INVALID;
229 }
230 return (p[0]);
231}
232
Neale Rannsc87b66c2019-02-07 07:26:12 -0800233static void
Neale Ranns28287212019-12-16 00:53:11 +0000234ipsec_tun_protect_rx_db_add (ipsec_main_t * im,
235 const ipsec_tun_protect_t * itp)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800236{
237 const ipsec_sa_t *sa;
238 u32 sai;
239
Neale Ranns28287212019-12-16 00:53:11 +0000240 if (ip46_address_is_zero (&itp->itp_crypto.dst))
241 return;
242
Neale Rannsc87b66c2019-02-07 07:26:12 -0800243 /* *INDENT-OFF* */
244 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
245 ({
246 sa = ipsec_sa_get (sai);
247
248 ipsec_tun_lkup_result_t res = {
Neale Ranns28287212019-12-16 00:53:11 +0000249 .tun_index = itp - ipsec_tun_protect_pool,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800250 .sa_index = sai,
Neale Ranns302b25a2020-10-19 13:23:33 +0000251 .flags = itp->itp_flags,
252 .sw_if_index = itp->itp_sw_if_index,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800253 };
254
255 /*
256 * The key is formed from the tunnel's destination
257 * as the packet lookup is done from the packet's source
258 */
259 if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
260 {
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000261 ipsec4_tunnel_kv_t key = {
262 .value = res,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800263 };
Neale Ranns302b25a2020-10-19 13:23:33 +0000264 clib_bihash_kv_8_16_t *bkey = (clib_bihash_kv_8_16_t*)&key;
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000265
266 ipsec4_tunnel_mk_key(&key, &itp->itp_crypto.dst.ip4,
267 clib_host_to_net_u32 (sa->spi));
268
Neale Ranns0ebbbfc2021-02-08 09:53:10 +0000269 if (!clib_bihash_is_initialised_8_16 (&im->tun4_protect_by_key))
270 clib_bihash_init_8_16 (&im->tun4_protect_by_key,
271 "IPSec IPv4 tunnels",
272 IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS,
273 IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE);
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000274
Neale Ranns0ebbbfc2021-02-08 09:53:10 +0000275 clib_bihash_add_del_8_16 (&im->tun4_protect_by_key, bkey, 1);
276 ipsec_tun_register_nodes (AF_IP4);
277 }
Neale Rannsc87b66c2019-02-07 07:26:12 -0800278 else
279 {
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000280 ipsec6_tunnel_kv_t key = {
281 .key = {
282 .remote_ip = itp->itp_crypto.dst.ip6,
283 .spi = clib_host_to_net_u32 (sa->spi),
284 },
285 .value = res,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800286 };
Neale Ranns302b25a2020-10-19 13:23:33 +0000287 clib_bihash_kv_24_16_t *bkey = (clib_bihash_kv_24_16_t*)&key;
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000288
Neale Ranns0ebbbfc2021-02-08 09:53:10 +0000289 if (!clib_bihash_is_initialised_24_16 (&im->tun6_protect_by_key))
290 clib_bihash_init_24_16 (&im->tun6_protect_by_key,
291 "IPSec IPv6 tunnels",
292 IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS,
293 IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE);
294 clib_bihash_add_del_24_16 (&im->tun6_protect_by_key, bkey, 1);
295 ipsec_tun_register_nodes (AF_IP6);
296 }
Neale Rannsc87b66c2019-02-07 07:26:12 -0800297 }))
298 /* *INDENT-ON* */
299}
300
Neale Ranns28287212019-12-16 00:53:11 +0000301static adj_walk_rc_t
302ipsec_tun_protect_adj_add (adj_index_t ai, void *arg)
303{
304 ipsec_tun_protect_t *itp = arg;
305 adj_delegate_add (adj_get (ai), ipsec_tun_adj_delegate_type,
306 itp - ipsec_tun_protect_pool);
Neale Ranns4ec36c52020-03-31 09:21:29 -0400307 ipsec_tun_protect_add_adj (ai, itp);
Neale Ranns28287212019-12-16 00:53:11 +0000308
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000309 if (itp->itp_flags & IPSEC_PROTECT_ITF)
310 ipsec_itf_adj_stack (ai, itp->itp_out_sa);
311
Neale Ranns28287212019-12-16 00:53:11 +0000312 return (ADJ_WALK_RC_CONTINUE);
313}
314
Neale Rannsc87b66c2019-02-07 07:26:12 -0800315static void
Neale Ranns28287212019-12-16 00:53:11 +0000316ipsec_tun_protect_tx_db_add (ipsec_tun_protect_t * itp)
317{
318 /*
319 * add the delegate to the adj
320 */
321 ipsec_tun_protect_itf_db_t *idi;
322 fib_protocol_t nh_proto;
323 ip46_address_t nh;
324
325 vec_validate_init_empty (itp_db.id_itf,
326 itp->itp_sw_if_index,
327 IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY);
328
329 idi = &itp_db.id_itf[itp->itp_sw_if_index];
330
331 if (vnet_sw_interface_is_p2p (vnet_get_main (), itp->itp_sw_if_index))
332 {
333 if (INDEX_INVALID == idi->id_itp)
334 {
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000335 ipsec_tun_setup_tx_nodes (itp->itp_sw_if_index, itp);
Neale Ranns28287212019-12-16 00:53:11 +0000336 }
337 idi->id_itp = itp - ipsec_tun_protect_pool;
338
339 FOR_EACH_FIB_IP_PROTOCOL (nh_proto)
340 adj_nbr_walk (itp->itp_sw_if_index,
341 nh_proto, ipsec_tun_protect_adj_add, itp);
342 }
343 else
344 {
345 if (NULL == idi->id_hash)
346 {
347 idi->id_hash =
348 hash_create_mem (0, sizeof (ip_address_t), sizeof (uword));
349 /*
350 * enable the encrypt feature for egress if this is the first addition
351 * on this interface
352 */
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000353 ipsec_tun_setup_tx_nodes (itp->itp_sw_if_index, itp);
Neale Ranns28287212019-12-16 00:53:11 +0000354 }
355
356 hash_set_mem (idi->id_hash, itp->itp_key, itp - ipsec_tun_protect_pool);
357
358 /*
359 * walk all the adjs with the same nh on this interface
360 * to associate them with this protection
361 */
362 nh_proto = ip_address_to_46 (itp->itp_key, &nh);
363
364 adj_nbr_walk_nh (itp->itp_sw_if_index,
365 nh_proto, &nh, ipsec_tun_protect_adj_add, itp);
Neale Ranns8d6d74c2020-02-20 09:45:16 +0000366
367 ipsec_tun_register_nodes (FIB_PROTOCOL_IP6 == nh_proto ?
368 AF_IP6 : AF_IP4);
Neale Ranns28287212019-12-16 00:53:11 +0000369 }
370}
371
372static void
373ipsec_tun_protect_rx_db_remove (ipsec_main_t * im,
374 const ipsec_tun_protect_t * itp)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800375{
376 const ipsec_sa_t *sa;
377
378 /* *INDENT-OFF* */
379 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
380 ({
Neale Ranns4ec36c52020-03-31 09:21:29 -0400381 if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
382 {
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000383 ipsec4_tunnel_kv_t key;
Neale Ranns302b25a2020-10-19 13:23:33 +0000384 clib_bihash_kv_8_16_t res, *bkey = (clib_bihash_kv_8_16_t*)&key;
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000385
386 ipsec4_tunnel_mk_key(&key, &itp->itp_crypto.dst.ip4,
387 clib_host_to_net_u32 (sa->spi));
388
Neale Ranns302b25a2020-10-19 13:23:33 +0000389 if (!clib_bihash_search_8_16 (&im->tun4_protect_by_key, bkey, &res))
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000390 {
Neale Ranns302b25a2020-10-19 13:23:33 +0000391 clib_bihash_add_del_8_16 (&im->tun4_protect_by_key, bkey, 0);
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000392 ipsec_tun_unregister_nodes(AF_IP4);
393 }
Neale Ranns4ec36c52020-03-31 09:21:29 -0400394 }
395 else
396 {
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000397 ipsec6_tunnel_kv_t key = {
398 .key = {
399 .remote_ip = itp->itp_crypto.dst.ip6,
400 .spi = clib_host_to_net_u32 (sa->spi),
401 },
Neale Ranns4ec36c52020-03-31 09:21:29 -0400402 };
Neale Ranns302b25a2020-10-19 13:23:33 +0000403 clib_bihash_kv_24_16_t res, *bkey = (clib_bihash_kv_24_16_t*)&key;
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000404
Neale Ranns302b25a2020-10-19 13:23:33 +0000405 if (!clib_bihash_search_24_16 (&im->tun6_protect_by_key, bkey, &res))
Neale Ranns4ec36c52020-03-31 09:21:29 -0400406 {
Neale Ranns302b25a2020-10-19 13:23:33 +0000407 clib_bihash_add_del_24_16 (&im->tun6_protect_by_key, bkey, 0);
Neale Ranns4ec36c52020-03-31 09:21:29 -0400408 ipsec_tun_unregister_nodes(AF_IP6);
409 }
410 }
411 }));
Neale Rannsc87b66c2019-02-07 07:26:12 -0800412 /* *INDENT-ON* */
413}
414
Neale Ranns28287212019-12-16 00:53:11 +0000415static adj_walk_rc_t
416ipsec_tun_protect_adj_remove (adj_index_t ai, void *arg)
417{
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000418 ipsec_tun_protect_t *itp = arg;
419
Neale Ranns28287212019-12-16 00:53:11 +0000420 adj_delegate_remove (ai, ipsec_tun_adj_delegate_type);
Neale Ranns4ec36c52020-03-31 09:21:29 -0400421 ipsec_tun_protect_add_adj (ai, NULL);
Neale Ranns28287212019-12-16 00:53:11 +0000422
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000423 if (itp->itp_flags & IPSEC_PROTECT_ITF)
424 ipsec_itf_adj_unstack (ai);
425
Neale Ranns28287212019-12-16 00:53:11 +0000426 return (ADJ_WALK_RC_CONTINUE);
427}
428
Neale Rannsc87b66c2019-02-07 07:26:12 -0800429static void
Neale Ranns28287212019-12-16 00:53:11 +0000430ipsec_tun_protect_tx_db_remove (ipsec_tun_protect_t * itp)
431{
432 ipsec_tun_protect_itf_db_t *idi;
433 fib_protocol_t nh_proto;
434 ip46_address_t nh;
435
436 nh_proto = ip_address_to_46 (itp->itp_key, &nh);
437 idi = &itp_db.id_itf[itp->itp_sw_if_index];
438
439 if (vnet_sw_interface_is_p2p (vnet_get_main (), itp->itp_sw_if_index))
440 {
Neale Ranns49378f22022-01-10 10:38:43 +0000441 ipsec_itf_reset_tx_nodes (itp->itp_sw_if_index);
Neale Ranns28287212019-12-16 00:53:11 +0000442 idi->id_itp = INDEX_INVALID;
443
444 FOR_EACH_FIB_IP_PROTOCOL (nh_proto)
445 adj_nbr_walk (itp->itp_sw_if_index,
446 nh_proto, ipsec_tun_protect_adj_remove, itp);
447 }
448 else
449 {
450 adj_nbr_walk_nh (itp->itp_sw_if_index,
451 nh_proto, &nh, ipsec_tun_protect_adj_remove, itp);
452
453 hash_unset_mem (idi->id_hash, itp->itp_key);
454
455 if (0 == hash_elts (idi->id_hash))
456 {
Neale Ranns49378f22022-01-10 10:38:43 +0000457 ipsec_itf_reset_tx_nodes (itp->itp_sw_if_index);
Neale Ranns28287212019-12-16 00:53:11 +0000458 hash_free (idi->id_hash);
459 idi->id_hash = NULL;
460 }
Neale Ranns8d6d74c2020-02-20 09:45:16 +0000461 ipsec_tun_unregister_nodes (FIB_PROTOCOL_IP6 == nh_proto ?
462 AF_IP6 : AF_IP4);
Neale Ranns28287212019-12-16 00:53:11 +0000463 }
464}
465
466static void
467ipsec_tun_protect_set_crypto_addr (ipsec_tun_protect_t * itp)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800468{
469 ipsec_sa_t *sa;
Neale Ranns12989b52019-09-26 16:20:19 +0000470
Neale Rannsc87b66c2019-02-07 07:26:12 -0800471 /* *INDENT-OFF* */
472 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
473 ({
474 if (ipsec_sa_is_set_IS_TUNNEL (sa))
475 {
Neale Ranns9ec846c2021-02-09 14:04:02 +0000476 itp->itp_crypto.src = ip_addr_46 (&sa->tunnel.t_dst);
477 itp->itp_crypto.dst = ip_addr_46 (&sa->tunnel.t_src);
478 if (!(itp->itp_flags & IPSEC_PROTECT_ITF))
479 {
480 ipsec_sa_set_IS_PROTECT (sa);
481 itp->itp_flags |= IPSEC_PROTECT_ENCAPED;
482 }
Neale Rannsc87b66c2019-02-07 07:26:12 -0800483 }
484 else
485 {
486 itp->itp_crypto.src = itp->itp_tun.src;
487 itp->itp_crypto.dst = itp->itp_tun.dst;
488 itp->itp_flags &= ~IPSEC_PROTECT_ENCAPED;
489 }
490 }));
491 /* *INDENT-ON* */
Neale Ranns28287212019-12-16 00:53:11 +0000492}
493
494static void
495ipsec_tun_protect_config (ipsec_main_t * im,
496 ipsec_tun_protect_t * itp, u32 sa_out, u32 * sas_in)
497{
498 index_t sai;
499 u32 ii;
500
501 itp->itp_n_sa_in = vec_len (sas_in);
502 for (ii = 0; ii < itp->itp_n_sa_in; ii++)
503 itp->itp_in_sas[ii] = sas_in[ii];
504 itp->itp_out_sa = sa_out;
505
506 ipsec_sa_lock (itp->itp_out_sa);
507
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000508 if (itp->itp_flags & IPSEC_PROTECT_ITF)
509 ipsec_sa_set_NO_ALGO_NO_DROP (ipsec_sa_get (itp->itp_out_sa));
510
Neale Ranns28287212019-12-16 00:53:11 +0000511 /* *INDENT-OFF* */
512 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
513 ({
514 ipsec_sa_lock(sai);
515 }));
516 ipsec_tun_protect_set_crypto_addr(itp);
517 /* *INDENT-ON* */
Neale Rannsc87b66c2019-02-07 07:26:12 -0800518
519 /*
520 * add to the DB against each SA
521 */
Neale Ranns28287212019-12-16 00:53:11 +0000522 ipsec_tun_protect_rx_db_add (im, itp);
523 ipsec_tun_protect_tx_db_add (itp);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800524
Neale Ranns28287212019-12-16 00:53:11 +0000525 ITP_DBG (itp, "configured");
Neale Rannsc87b66c2019-02-07 07:26:12 -0800526}
527
528static void
529ipsec_tun_protect_unconfig (ipsec_main_t * im, ipsec_tun_protect_t * itp)
530{
531 ipsec_sa_t *sa;
Neale Ranns495d7ff2019-07-12 09:15:26 +0000532 index_t sai;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800533
Neale Rannsc87b66c2019-02-07 07:26:12 -0800534 /* *INDENT-OFF* */
535 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
536 ({
537 ipsec_sa_unset_IS_PROTECT (sa);
538 }));
Neale Rannsc87b66c2019-02-07 07:26:12 -0800539
Neale Ranns28287212019-12-16 00:53:11 +0000540 ipsec_tun_protect_rx_db_remove (im, itp);
541 ipsec_tun_protect_tx_db_remove (itp);
Neale Ranns495d7ff2019-07-12 09:15:26 +0000542
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000543 ipsec_sa_unset_NO_ALGO_NO_DROP (ipsec_sa_get (itp->itp_out_sa));
Neale Ranns495d7ff2019-07-12 09:15:26 +0000544 ipsec_sa_unlock(itp->itp_out_sa);
545
546 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
547 ({
548 ipsec_sa_unlock(sai);
549 }));
550 /* *INDENT-ON* */
Neale Ranns28287212019-12-16 00:53:11 +0000551 ITP_DBG (itp, "unconfigured");
Neale Rannsc87b66c2019-02-07 07:26:12 -0800552}
553
Neale Ranns28287212019-12-16 00:53:11 +0000554static void
555ipsec_tun_protect_update_from_teib (ipsec_tun_protect_t * itp,
556 const teib_entry_t * ne)
557{
558 if (NULL != ne)
559 {
560 const fib_prefix_t *pfx;
561
562 pfx = teib_entry_get_nh (ne);
563
564 ip46_address_copy (&itp->itp_tun.dst, &pfx->fp_addr);
565 }
566 else
567 ip46_address_reset (&itp->itp_tun.dst);
568}
569
Neale Ranns12989b52019-09-26 16:20:19 +0000570int
Neale Ranns28287212019-12-16 00:53:11 +0000571ipsec_tun_protect_update (u32 sw_if_index,
572 const ip_address_t * nh, u32 sa_out, u32 * sas_in)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800573{
Neale Rannsc87b66c2019-02-07 07:26:12 -0800574 ipsec_tun_protect_t *itp;
Neale Ranns12989b52019-09-26 16:20:19 +0000575 u32 itpi, ii, *saip;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800576 ipsec_main_t *im;
577 int rv;
578
Benoît Ganneac55a722021-10-07 17:37:07 +0200579 if (NULL == nh)
580 nh = &IP_ADDR_ALL_0;
581
Neale Ranns28287212019-12-16 00:53:11 +0000582 ITP_DBG2 ("update: %U/%U",
583 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
584 format_ip_address, nh);
585
Matthew Smithdc3e9662020-04-10 20:27:33 -0500586 if (vec_len (sas_in) > ITP_MAX_N_SA_IN)
587 {
588 rv = VNET_API_ERROR_LIMIT_EXCEEDED;
589 goto out;
590 }
591
Neale Rannsc87b66c2019-02-07 07:26:12 -0800592 rv = 0;
593 im = &ipsec_main;
Neale Ranns28287212019-12-16 00:53:11 +0000594 itpi = ipsec_tun_protect_find (sw_if_index, nh);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800595
596 vec_foreach_index (ii, sas_in)
597 {
Neale Ranns495d7ff2019-07-12 09:15:26 +0000598 sas_in[ii] = ipsec_sa_find_and_lock (sas_in[ii]);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800599 if (~0 == sas_in[ii])
600 {
601 rv = VNET_API_ERROR_INVALID_VALUE;
602 goto out;
603 }
604 }
605
Neale Ranns495d7ff2019-07-12 09:15:26 +0000606 sa_out = ipsec_sa_find_and_lock (sa_out);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800607
608 if (~0 == sa_out)
609 {
610 rv = VNET_API_ERROR_INVALID_VALUE;
611 goto out;
612 }
613
614 if (INDEX_INVALID == itpi)
615 {
616 vnet_device_class_t *dev_class;
617 vnet_hw_interface_t *hi;
618 vnet_main_t *vnm;
619 u8 is_l2;
620
621 vnm = vnet_get_main ();
622 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
623 dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
624
625 if (NULL == dev_class->ip_tun_desc)
626 {
627 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
628 goto out;
629 }
630
Neale Ranns28287212019-12-16 00:53:11 +0000631 pool_get_zero (ipsec_tun_protect_pool, itp);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800632
633 itp->itp_sw_if_index = sw_if_index;
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000634 itp->itp_ai = ADJ_INDEX_INVALID;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800635
636 itp->itp_n_sa_in = vec_len (sas_in);
637 for (ii = 0; ii < itp->itp_n_sa_in; ii++)
638 itp->itp_in_sas[ii] = sas_in[ii];
639 itp->itp_out_sa = sa_out;
640
Neale Ranns28287212019-12-16 00:53:11 +0000641 itp->itp_key = clib_mem_alloc (sizeof (*itp->itp_key));
642 ip_address_copy (itp->itp_key, nh);
643
Neale Rannsc87b66c2019-02-07 07:26:12 -0800644 rv = dev_class->ip_tun_desc (sw_if_index,
645 &itp->itp_tun.src,
646 &itp->itp_tun.dst, &is_l2);
647
648 if (rv)
649 goto out;
650
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000651 if (ip46_address_is_zero (&itp->itp_tun.src))
652 {
Neale Ranns6ba4e412020-10-19 09:59:41 +0000653 /*
654 * must be one of those pesky ipsec interfaces that has no encap.
655 * the encap then MUST come from the tunnel mode SA.
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000656 */
657 ipsec_sa_t *sa;
658
659 sa = ipsec_sa_get (itp->itp_out_sa);
660
661 if (!ipsec_sa_is_set_IS_TUNNEL (sa))
662 {
663 rv = VNET_API_ERROR_INVALID_DST_ADDRESS;
664 goto out;
665 }
666
667 itp->itp_flags |= IPSEC_PROTECT_ITF;
668 }
669 else if (ip46_address_is_zero (&itp->itp_tun.dst))
Neale Ranns28287212019-12-16 00:53:11 +0000670 {
671 /* tunnel has no destination address, presumably because it's p2mp
672 in which case we use the nh that this is protection for */
Neale Ranns28287212019-12-16 00:53:11 +0000673 ipsec_tun_protect_update_from_teib
Neale Rannse6b83052020-09-17 12:56:47 +0000674 (itp, teib_entry_find (sw_if_index, nh));
Neale Ranns28287212019-12-16 00:53:11 +0000675 }
676
Neale Rannsc87b66c2019-02-07 07:26:12 -0800677 if (is_l2)
678 itp->itp_flags |= IPSEC_PROTECT_L2;
679
680 /*
681 * add to the tunnel DB for ingress
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000682 * - if the SA is in trasnport mode, then the packates will arrive
Neale Rannsc87b66c2019-02-07 07:26:12 -0800683 * with the IP src,dst of the protected tunnel, in which case we can
684 * simply strip the IP header and hand the payload to the protocol
685 * appropriate input handler
686 * - if the SA is in tunnel mode then there are two IP headers present
687 * one for the crytpo tunnel endpoints (described in the SA) and one
688 * for the tunnel endpoints. The outer IP headers in the srriving
689 * packets will have the crypto endpoints. So the DB needs to contain
690 * the crpto endpoint. Once the crypto header is stripped, revealing,
691 * the tunnel-IP we have 2 choices:
692 * 1) do a tunnel lookup based on the revealed header
693 * 2) skip the tunnel lookup and assume that the packet matches the
694 * one that is protected here.
695 * If we did 1) then we would allow our peer to use the SA for tunnel
696 * X to inject traffic onto tunnel Y, this is not good. If we do 2)
697 * then we don't verify that the peer is indeed using SA for tunnel
698 * X and addressing tunnel X. So we take a compromise, once the SA
699 * matches to tunnel X we veriy that the inner IP matches the value
700 * of the tunnel we are protecting, else it's dropped.
701 */
702 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800703 }
704 else
705 {
706 /* updating SAs only */
Neale Ranns28287212019-12-16 00:53:11 +0000707 itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800708
709 ipsec_tun_protect_unconfig (im, itp);
710 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
711 }
712
Neale Ranns12989b52019-09-26 16:20:19 +0000713 ipsec_sa_unlock (sa_out);
714 vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800715 vec_free (sas_in);
Neale Ranns12989b52019-09-26 16:20:19 +0000716
Neale Rannsc87b66c2019-02-07 07:26:12 -0800717out:
718 return (rv);
719}
720
721int
Neale Ranns28287212019-12-16 00:53:11 +0000722ipsec_tun_protect_del (u32 sw_if_index, const ip_address_t * nh)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800723{
724 ipsec_tun_protect_t *itp;
725 ipsec_main_t *im;
726 index_t itpi;
727
Neale Ranns28287212019-12-16 00:53:11 +0000728 ITP_DBG2 ("delete: %U/%U",
729 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
730 format_ip_address, nh);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800731
Neale Ranns28287212019-12-16 00:53:11 +0000732 im = &ipsec_main;
733 if (NULL == nh)
734 nh = &IP_ADDR_ALL_0;
735
736 itpi = ipsec_tun_protect_find (sw_if_index, nh);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800737
738 if (INDEX_INVALID == itpi)
739 return (VNET_API_ERROR_NO_SUCH_ENTRY);
740
741 itp = ipsec_tun_protect_get (itpi);
742 ipsec_tun_protect_unconfig (im, itp);
743
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000744 if (ADJ_INDEX_INVALID != itp->itp_ai)
745 adj_unlock (itp->itp_ai);
746
Neale Ranns28287212019-12-16 00:53:11 +0000747 clib_mem_free (itp->itp_key);
748 pool_put (ipsec_tun_protect_pool, itp);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800749
Neale Rannsc87b66c2019-02-07 07:26:12 -0800750 return (0);
751}
752
753void
754ipsec_tun_protect_walk (ipsec_tun_protect_walk_cb_t fn, void *ctx)
755{
756 index_t itpi;
757
758 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100759 pool_foreach_index (itpi, ipsec_tun_protect_pool)
760 {
Neale Rannsc87b66c2019-02-07 07:26:12 -0800761 fn (itpi, ctx);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100762 }
Neale Rannsc87b66c2019-02-07 07:26:12 -0800763 /* *INDENT-ON* */
764}
765
Neale Ranns28287212019-12-16 00:53:11 +0000766void
767ipsec_tun_protect_walk_itf (u32 sw_if_index,
768 ipsec_tun_protect_walk_cb_t fn, void *ctx)
769{
770 ipsec_tun_protect_itf_db_t *idi;
771 ip_address_t *key;
772 index_t itpi;
773
774 if (vec_len (itp_db.id_itf) <= sw_if_index)
775 return;
776
777 idi = &itp_db.id_itf[sw_if_index];
778
779 /* *INDENT-OFF* */
780 hash_foreach(key, itpi, idi->id_hash,
781 ({
782 fn (itpi, ctx);
783 }));
784 /* *INDENT-ON* */
785 if (INDEX_INVALID != idi->id_itp)
786 fn (idi->id_itp, ctx);
787}
788
789static void
Brian Russell7a29a2d2021-02-22 18:42:24 +0000790ipsec_tun_feature_update (u32 sw_if_index, u8 arc_index, u8 is_enable,
791 void *data)
792{
793 ipsec_tun_protect_t *itp;
794 index_t itpi;
795
796 if (arc_index != feature_main.device_input_feature_arc_index)
797 return;
798
799 /* Only p2p tunnels supported */
800 itpi = ipsec_tun_protect_find (sw_if_index, &IP_ADDR_ALL_0);
801 if (itpi == INDEX_INVALID)
802 return;
803
804 itp = ipsec_tun_protect_get (itpi);
805
806 if (is_enable)
807 {
808 u32 decrypt_tun = ip46_address_is_ip4 (&itp->itp_crypto.dst) ?
809 ipsec_main.esp4_decrypt_tun_node_index :
810 ipsec_main.esp6_decrypt_tun_node_index;
811
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000812 if (!(itp->itp_flags & IPSEC_PROTECT_FEAT))
813 {
814 itp->itp_flags |= IPSEC_PROTECT_FEAT;
815 vnet_feature_modify_end_node (
816 feature_main.device_input_feature_arc_index, sw_if_index,
817 decrypt_tun);
818 }
Brian Russell7a29a2d2021-02-22 18:42:24 +0000819 }
820 else
821 {
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000822 if (itp->itp_flags & IPSEC_PROTECT_FEAT)
823 {
824 itp->itp_flags &= ~IPSEC_PROTECT_FEAT;
Brian Russell7a29a2d2021-02-22 18:42:24 +0000825
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000826 u32 eth_in =
827 vlib_get_node_by_name (vlib_get_main (), (u8 *) "ethernet-input")
828 ->index;
829
830 vnet_feature_modify_end_node (
831 feature_main.device_input_feature_arc_index, sw_if_index, eth_in);
832 }
Brian Russell7a29a2d2021-02-22 18:42:24 +0000833 }
834
835 /* Propagate flag change into lookup entries */
836 ipsec_tun_protect_rx_db_remove (&ipsec_main, itp);
837 ipsec_tun_protect_rx_db_add (&ipsec_main, itp);
838}
839
840static void
Neale Ranns28287212019-12-16 00:53:11 +0000841ipsec_tun_protect_adj_delegate_adj_deleted (adj_delegate_t * ad)
842{
843 /* remove our delegate */
Neale Ranns4ec36c52020-03-31 09:21:29 -0400844 ipsec_tun_protect_add_adj (ad->ad_adj_index, NULL);
Benoît Ganneea9bc282020-04-16 12:40:04 +0200845 adj_delegate_remove (ad->ad_adj_index, ipsec_tun_adj_delegate_type);
Neale Ranns28287212019-12-16 00:53:11 +0000846}
847
848static void
Neale Ranns4ec36c52020-03-31 09:21:29 -0400849ipsec_tun_protect_adj_delegate_adj_modified (adj_delegate_t * ad)
850{
851 ipsec_tun_protect_add_adj (ad->ad_adj_index,
852 ipsec_tun_protect_get (ad->ad_index));
853}
854
855static void
Neale Ranns28287212019-12-16 00:53:11 +0000856ipsec_tun_protect_adj_delegate_adj_created (adj_index_t ai)
857{
858 /* add our delegate if there is protection for this neighbour */
859 ip_address_t ip = IP_ADDRESS_V4_ALL_0S;
860 ip_adjacency_t *adj;
861 index_t itpi;
862
Neale Ranns4ec36c52020-03-31 09:21:29 -0400863 if (!adj_is_midchain (ai))
Neale Ranns28287212019-12-16 00:53:11 +0000864 return;
865
Neale Ranns6fdcc3d2021-10-08 07:30:47 +0000866 vec_validate_init_empty (ipsec_tun_protect_sa_by_adj_index, ai,
867 INDEX_INVALID);
868
Neale Ranns4ec36c52020-03-31 09:21:29 -0400869 adj = adj_get (ai);
870
Neale Ranns28287212019-12-16 00:53:11 +0000871 ip_address_from_46 (&adj->sub_type.midchain.next_hop,
872 adj->ia_nh_proto, &ip);
873
874 itpi = ipsec_tun_protect_find (adj->rewrite_header.sw_if_index, &ip);
875
876 if (INDEX_INVALID != itpi)
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000877 ipsec_tun_protect_adj_add (ai, ipsec_tun_protect_get (itpi));
Neale Ranns28287212019-12-16 00:53:11 +0000878}
879
880static u8 *
881ipsec_tun_protect_adj_delegate_format (const adj_delegate_t * aed, u8 * s)
882{
883 const ipsec_tun_protect_t *itp;
884
885 itp = ipsec_tun_protect_from_const_base (aed);
886 s = format (s, "ipsec-tun-protect:\n%U", format_ipsec_tun_protect, itp);
887
888 return (s);
889}
890
891static void
892ipsec_tun_teib_entry_added (const teib_entry_t * ne)
893{
Neale Ranns28287212019-12-16 00:53:11 +0000894 ipsec_tun_protect_t *itp;
Neale Ranns28287212019-12-16 00:53:11 +0000895 index_t itpi;
896
Neale Rannse6b83052020-09-17 12:56:47 +0000897 itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne),
898 teib_entry_get_peer (ne));
Neale Ranns28287212019-12-16 00:53:11 +0000899
900 if (INDEX_INVALID == itpi)
901 return;
902
903 itp = ipsec_tun_protect_get (itpi);
904 ipsec_tun_protect_rx_db_remove (&ipsec_main, itp);
905 ipsec_tun_protect_update_from_teib (itp, ne);
906 ipsec_tun_protect_set_crypto_addr (itp);
907 ipsec_tun_protect_rx_db_add (&ipsec_main, itp);
908
909 ITP_DBG (itp, "teib-added");
910}
911
912static void
913ipsec_tun_teib_entry_deleted (const teib_entry_t * ne)
914{
Neale Ranns28287212019-12-16 00:53:11 +0000915 ipsec_tun_protect_t *itp;
Neale Ranns28287212019-12-16 00:53:11 +0000916 index_t itpi;
917
Neale Rannse6b83052020-09-17 12:56:47 +0000918 itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne),
919 teib_entry_get_peer (ne));
Neale Ranns28287212019-12-16 00:53:11 +0000920
921 if (INDEX_INVALID == itpi)
922 return;
923
924 itp = ipsec_tun_protect_get (itpi);
925 ipsec_tun_protect_rx_db_remove (&ipsec_main, itp);
926 ipsec_tun_protect_update_from_teib (itp, NULL);
927 ipsec_tun_protect_set_crypto_addr (itp);
928
929 ITP_DBG (itp, "teib-removed");
930}
931
932/**
933 * VFT registered with the adjacency delegate
934 */
935const static adj_delegate_vft_t ipsec_tun_adj_delegate_vft = {
936 .adv_adj_deleted = ipsec_tun_protect_adj_delegate_adj_deleted,
937 .adv_adj_created = ipsec_tun_protect_adj_delegate_adj_created,
Neale Ranns4ec36c52020-03-31 09:21:29 -0400938 .adv_adj_modified = ipsec_tun_protect_adj_delegate_adj_modified,
Neale Ranns28287212019-12-16 00:53:11 +0000939 .adv_format = ipsec_tun_protect_adj_delegate_format,
940};
941
942const static teib_vft_t ipsec_tun_teib_vft = {
943 .nv_added = ipsec_tun_teib_entry_added,
944 .nv_deleted = ipsec_tun_teib_entry_deleted,
945};
946
Zachary Leafb2d36782021-07-27 05:18:47 -0500947void
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000948ipsec_tun_table_init (ip_address_family_t af, uword table_size, u32 n_buckets)
949{
950 ipsec_main_t *im;
951
952 im = &ipsec_main;
953
954 if (AF_IP4 == af)
Neale Ranns302b25a2020-10-19 13:23:33 +0000955 clib_bihash_init_8_16 (&im->tun4_protect_by_key,
956 "IPSec IPv4 tunnels", n_buckets, table_size);
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000957 else
Neale Ranns302b25a2020-10-19 13:23:33 +0000958 clib_bihash_init_24_16 (&im->tun6_protect_by_key,
959 "IPSec IPv6 tunnels", n_buckets, table_size);
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000960}
Neale Ranns4ec36c52020-03-31 09:21:29 -0400961
Neale Rannsa9e27742020-12-23 16:22:28 +0000962static clib_error_t *
963ipsec_tunnel_protect_init (vlib_main_t *vm)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800964{
965 ipsec_main_t *im;
966
967 im = &ipsec_main;
Neale Ranns302b25a2020-10-19 13:23:33 +0000968 clib_bihash_init_24_16 (&im->tun6_protect_by_key,
969 "IPSec IPv6 tunnels",
970 IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS,
971 IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE);
972 clib_bihash_init_8_16 (&im->tun4_protect_by_key,
973 "IPSec IPv4 tunnels",
Neale Ranns7b4e52f2020-05-24 16:17:50 +0000974 IPSEC_TUN_DEFAULT_HASH_NUM_BUCKETS,
975 IPSEC_TUN_DEFAULT_HASH_MEMORY_SIZE);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800976
Neale Ranns28287212019-12-16 00:53:11 +0000977 ipsec_tun_adj_delegate_type =
978 adj_delegate_register_new_type (&ipsec_tun_adj_delegate_vft);
979
980 ipsec_tun_protect_logger = vlib_log_register_class ("ipsec", "tun");
981
982 teib_register (&ipsec_tun_teib_vft);
983
Brian Russell7a29a2d2021-02-22 18:42:24 +0000984 vnet_feature_register (ipsec_tun_feature_update, NULL);
985
Neale Rannsc87b66c2019-02-07 07:26:12 -0800986 return 0;
987}
988
989VLIB_INIT_FUNCTION (ipsec_tunnel_protect_init);
990
Neale Rannsc87b66c2019-02-07 07:26:12 -0800991/*
992 * fd.io coding-style-patch-verification: ON
993 *
994 * Local Variables:
995 * eval: (c-set-style "gnu")
996 * End:
997 */