blob: 9d352e7507594f1f943cd0058e2beeb2630da43b [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>
21#include <vnet/udp/udp.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>
25
26/**
27 * The logger
28 */
29vlib_log_class_t ipsec_tun_protect_logger;
Neale Rannsc87b66c2019-02-07 07:26:12 -080030
31/**
32 * Pool of tunnel protection objects
33 */
Neale Ranns28287212019-12-16 00:53:11 +000034ipsec_tun_protect_t *ipsec_tun_protect_pool;
Neale Rannsc87b66c2019-02-07 07:26:12 -080035
36/**
Neale Ranns28287212019-12-16 00:53:11 +000037 * Adj delegate registered type
Neale Rannsc87b66c2019-02-07 07:26:12 -080038 */
Neale Ranns28287212019-12-16 00:53:11 +000039static adj_delegate_type_t ipsec_tun_adj_delegate_type;
Neale Rannsc87b66c2019-02-07 07:26:12 -080040
Neale Ranns28287212019-12-16 00:53:11 +000041/**
42 * Adj index to TX SA mapping
43 */
44index_t *ipsec_tun_protect_sa_by_adj_index;
45
46const ip_address_t IP_ADDR_ALL_0 = IP_ADDRESS_V4_ALL_0S;
47
48/**
49 * The DB of all added per-nh tunnel protectiond
50 */
51typedef struct ipsec_tun_protect_itf_db_t_
52{
53 /** A hash table key'd on IP (4 or 6) address */
54 uword *id_hash;
55 /** If the interface is P2P then there is only one protect
56 * object associated with the auto-adj for each NH proto */
57 index_t id_itp;
58} ipsec_tun_protect_itf_db_t;
59
60typedef struct ipsec_tun_protect_db_t_
61{
62 /** Per-interface vector */
63 ipsec_tun_protect_itf_db_t *id_itf;
64} ipsec_tun_protect_db_t;
65
66static ipsec_tun_protect_db_t itp_db;
67
68const static ipsec_tun_protect_itf_db_t IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY = {
69 .id_itp = INDEX_INVALID,
70};
71
72#define ITP_DBG(_itp, _fmt, _args...) \
73{ \
74 vlib_log_debug(ipsec_tun_protect_logger, \
75 "[%U]: " _fmt, \
76 format_ipsec_tun_protect, \
77 _itp, ##_args); \
78}
79
80#define ITP_DBG2(_fmt, _args...) \
81{ \
82 vlib_log_debug(ipsec_tun_protect_logger, \
83 _fmt, ##_args); \
84}
85
Neale Ranns8d6d74c2020-02-20 09:45:16 +000086static u32 ipsec_tun_node_regs[N_AF];
87
88void
89ipsec_tun_register_nodes (ip_address_family_t af)
90{
91 if (0 == ipsec_tun_node_regs[af]++)
92 {
93 if (AF_IP4 == af)
94 {
Neale Rannsabc56602020-04-01 09:45:23 +000095 ipsec_register_udp_port (UDP_DST_PORT_ipsec);
Neale Ranns8d6d74c2020-02-20 09:45:16 +000096 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
97 ipsec4_tun_input_node.index);
98 }
99 else
100 ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP,
101 ipsec6_tun_input_node.index);
102 }
103}
104
105void
106ipsec_tun_unregister_nodes (ip_address_family_t af)
107{
108 ASSERT (0 != ipsec_tun_node_regs[af]);
109 if (0 == --ipsec_tun_node_regs[af])
110 {
111 if (AF_IP4 == af)
112 {
Neale Rannsabc56602020-04-01 09:45:23 +0000113 ipsec_unregister_udp_port (UDP_DST_PORT_ipsec);
Neale Ranns8d6d74c2020-02-20 09:45:16 +0000114 ip4_unregister_protocol (IP_PROTOCOL_IPSEC_ESP);
115 }
116 else
117 ip6_unregister_protocol (IP_PROTOCOL_IPSEC_ESP);
118 }
119}
120
Neale Ranns28287212019-12-16 00:53:11 +0000121static inline const ipsec_tun_protect_t *
122ipsec_tun_protect_from_const_base (const adj_delegate_t * ad)
123{
124 if (ad == NULL)
125 return (NULL);
126 return (pool_elt_at_index (ipsec_tun_protect_pool, ad->ad_index));
127}
Neale Rannsc87b66c2019-02-07 07:26:12 -0800128
Neale Ranns4ec36c52020-03-31 09:21:29 -0400129static u32
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000130ipsec_tun_protect_get_adj_next (vnet_link_t linkt,
131 const ipsec_tun_protect_t * itp)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800132{
Neale Ranns4ec36c52020-03-31 09:21:29 -0400133 ipsec_main_t *im;
134 ipsec_sa_t *sa;
135 bool is_ip4;
136 u32 next;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800137
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000138
139 if (itp->itp_flags & IPSEC_PROTECT_ITF)
140 is_ip4 = linkt == VNET_LINK_IP4;
141 else
142 is_ip4 = ip46_address_is_ip4 (&itp->itp_tun.src);
143
Neale Ranns4ec36c52020-03-31 09:21:29 -0400144 sa = ipsec_sa_get (itp->itp_out_sa);
145 im = &ipsec_main;
146
147 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_NONE &&
148 sa->integ_alg == IPSEC_INTEG_ALG_NONE)
149 next = (is_ip4 ?
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)
153 next = (is_ip4 ?
154 im->esp4_encrypt_l2_tun_node_index :
155 im->esp6_encrypt_l2_tun_node_index);
156 else
157 next = (is_ip4 ?
158 im->esp4_encrypt_tun_node_index :
159 im->esp6_encrypt_tun_node_index);
160
161 return (next);
162}
163
164static void
165ipsec_tun_protect_add_adj (adj_index_t ai, const ipsec_tun_protect_t * itp)
166{
167 vec_validate_init_empty (ipsec_tun_protect_sa_by_adj_index, ai,
168 INDEX_INVALID);
169
170 if (NULL == itp)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800171 {
Neale Ranns4ec36c52020-03-31 09:21:29 -0400172 ipsec_tun_protect_sa_by_adj_index[ai] = INDEX_INVALID;
173 adj_nbr_midchain_reset_next_node (ai);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800174 }
175 else
176 {
Neale Ranns4ec36c52020-03-31 09:21:29 -0400177 ipsec_tun_protect_sa_by_adj_index[ai] = itp->itp_out_sa;
178 adj_nbr_midchain_update_next_node
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000179 (ai, ipsec_tun_protect_get_adj_next (adj_get_link_type (ai), itp));
Neale Rannsc87b66c2019-02-07 07:26:12 -0800180 }
Neale Rannsc87b66c2019-02-07 07:26:12 -0800181}
182
Neale Ranns28287212019-12-16 00:53:11 +0000183static index_t
184ipsec_tun_protect_find (u32 sw_if_index, const ip_address_t * nh)
185{
186 ipsec_tun_protect_itf_db_t *idi;
187 uword *p;
188
189 if (vec_len (itp_db.id_itf) <= sw_if_index)
190 return INDEX_INVALID;
191
192 if (vnet_sw_interface_is_p2p (vnet_get_main (), sw_if_index))
193 return (itp_db.id_itf[sw_if_index].id_itp);
194
195 idi = &itp_db.id_itf[sw_if_index];
196 p = hash_get_mem (idi->id_hash, nh);
197
198 if (NULL == p)
199 {
200 return INDEX_INVALID;
201 }
202 return (p[0]);
203}
204
Neale Rannsc87b66c2019-02-07 07:26:12 -0800205static void
Neale Ranns28287212019-12-16 00:53:11 +0000206ipsec_tun_protect_rx_db_add (ipsec_main_t * im,
207 const ipsec_tun_protect_t * itp)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800208{
209 const ipsec_sa_t *sa;
210 u32 sai;
211
Neale Ranns28287212019-12-16 00:53:11 +0000212 if (ip46_address_is_zero (&itp->itp_crypto.dst))
213 return;
214
Neale Rannsc87b66c2019-02-07 07:26:12 -0800215 /* *INDENT-OFF* */
216 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
217 ({
218 sa = ipsec_sa_get (sai);
219
220 ipsec_tun_lkup_result_t res = {
Neale Ranns28287212019-12-16 00:53:11 +0000221 .tun_index = itp - ipsec_tun_protect_pool,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800222 .sa_index = sai,
223 };
224
225 /*
226 * The key is formed from the tunnel's destination
227 * as the packet lookup is done from the packet's source
228 */
229 if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
230 {
231 ipsec4_tunnel_key_t key = {
Neale Ranns41afb332019-07-16 06:19:35 -0700232 .remote_ip = itp->itp_crypto.dst.ip4,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800233 .spi = clib_host_to_net_u32 (sa->spi),
234 };
235 hash_set (im->tun4_protect_by_key, key.as_u64, res.as_u64);
Neale Ranns8d6d74c2020-02-20 09:45:16 +0000236 ipsec_tun_register_nodes(AF_IP4);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800237 }
238 else
239 {
240 ipsec6_tunnel_key_t key = {
241 .remote_ip = itp->itp_crypto.dst.ip6,
242 .spi = clib_host_to_net_u32 (sa->spi),
243 };
244 hash_set_mem_alloc (&im->tun6_protect_by_key, &key, res.as_u64);
Neale Ranns8d6d74c2020-02-20 09:45:16 +0000245 ipsec_tun_register_nodes(AF_IP6);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800246 }
247 }))
248 /* *INDENT-ON* */
249}
250
Neale Ranns28287212019-12-16 00:53:11 +0000251static adj_walk_rc_t
252ipsec_tun_protect_adj_add (adj_index_t ai, void *arg)
253{
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);
Neale Ranns4ec36c52020-03-31 09:21:29 -0400257 ipsec_tun_protect_add_adj (ai, itp);
Neale Ranns28287212019-12-16 00:53:11 +0000258
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000259 if (itp->itp_flags & IPSEC_PROTECT_ITF)
260 ipsec_itf_adj_stack (ai, itp->itp_out_sa);
261
Neale Ranns28287212019-12-16 00:53:11 +0000262 return (ADJ_WALK_RC_CONTINUE);
263}
264
Neale Rannsc87b66c2019-02-07 07:26:12 -0800265static void
Neale Ranns28287212019-12-16 00:53:11 +0000266ipsec_tun_protect_tx_db_add (ipsec_tun_protect_t * itp)
267{
268 /*
269 * add the delegate to the adj
270 */
271 ipsec_tun_protect_itf_db_t *idi;
272 fib_protocol_t nh_proto;
273 ip46_address_t nh;
274
275 vec_validate_init_empty (itp_db.id_itf,
276 itp->itp_sw_if_index,
277 IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY);
278
279 idi = &itp_db.id_itf[itp->itp_sw_if_index];
280
281 if (vnet_sw_interface_is_p2p (vnet_get_main (), itp->itp_sw_if_index))
282 {
283 if (INDEX_INVALID == idi->id_itp)
284 {
Neale Ranns4ec36c52020-03-31 09:21:29 -0400285 // ipsec_tun_protect_feature_set (itp, 1);
Neale Ranns28287212019-12-16 00:53:11 +0000286 }
287 idi->id_itp = itp - ipsec_tun_protect_pool;
288
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);
292 }
293 else
294 {
295 if (NULL == idi->id_hash)
296 {
297 idi->id_hash =
298 hash_create_mem (0, sizeof (ip_address_t), sizeof (uword));
299 /*
300 * enable the encrypt feature for egress if this is the first addition
301 * on this interface
302 */
Neale Ranns4ec36c52020-03-31 09:21:29 -0400303 // ipsec_tun_protect_feature_set (itp, 1);
Neale Ranns28287212019-12-16 00:53:11 +0000304 }
305
306 hash_set_mem (idi->id_hash, itp->itp_key, itp - ipsec_tun_protect_pool);
307
308 /*
309 * walk all the adjs with the same nh on this interface
310 * to associate them with this protection
311 */
312 nh_proto = ip_address_to_46 (itp->itp_key, &nh);
313
314 adj_nbr_walk_nh (itp->itp_sw_if_index,
315 nh_proto, &nh, ipsec_tun_protect_adj_add, itp);
Neale Ranns8d6d74c2020-02-20 09:45:16 +0000316
317 ipsec_tun_register_nodes (FIB_PROTOCOL_IP6 == nh_proto ?
318 AF_IP6 : AF_IP4);
Neale Ranns28287212019-12-16 00:53:11 +0000319 }
320}
321
322static void
323ipsec_tun_protect_rx_db_remove (ipsec_main_t * im,
324 const ipsec_tun_protect_t * itp)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800325{
326 const ipsec_sa_t *sa;
327
328 /* *INDENT-OFF* */
329 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
330 ({
Neale Ranns4ec36c52020-03-31 09:21:29 -0400331 if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
332 {
333 ipsec4_tunnel_key_t key = {
334 .remote_ip = itp->itp_crypto.dst.ip4,
335 .spi = clib_host_to_net_u32 (sa->spi),
336 };
337 if (hash_get(im->tun4_protect_by_key, key.as_u64))
338 {
339 hash_unset (im->tun4_protect_by_key, key.as_u64);
340 ipsec_tun_unregister_nodes(AF_IP4);
341 }
342 }
343 else
344 {
345 ipsec6_tunnel_key_t key = {
346 .remote_ip = itp->itp_crypto.dst.ip6,
347 .spi = clib_host_to_net_u32 (sa->spi),
348 };
349 if (hash_get_mem(im->tun6_protect_by_key, &key))
350 {
351 hash_unset_mem_free (&im->tun6_protect_by_key, &key);
352 ipsec_tun_unregister_nodes(AF_IP6);
353 }
354 }
355 }));
Neale Rannsc87b66c2019-02-07 07:26:12 -0800356 /* *INDENT-ON* */
357}
358
Neale Ranns28287212019-12-16 00:53:11 +0000359static adj_walk_rc_t
360ipsec_tun_protect_adj_remove (adj_index_t ai, void *arg)
361{
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000362 ipsec_tun_protect_t *itp = arg;
363
Neale Ranns28287212019-12-16 00:53:11 +0000364 adj_delegate_remove (ai, ipsec_tun_adj_delegate_type);
Neale Ranns4ec36c52020-03-31 09:21:29 -0400365 ipsec_tun_protect_add_adj (ai, NULL);
Neale Ranns28287212019-12-16 00:53:11 +0000366
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000367 if (itp->itp_flags & IPSEC_PROTECT_ITF)
368 ipsec_itf_adj_unstack (ai);
369
Neale Ranns28287212019-12-16 00:53:11 +0000370 return (ADJ_WALK_RC_CONTINUE);
371}
372
Neale Rannsc87b66c2019-02-07 07:26:12 -0800373static void
Neale Ranns28287212019-12-16 00:53:11 +0000374ipsec_tun_protect_tx_db_remove (ipsec_tun_protect_t * itp)
375{
376 ipsec_tun_protect_itf_db_t *idi;
377 fib_protocol_t nh_proto;
378 ip46_address_t nh;
379
380 nh_proto = ip_address_to_46 (itp->itp_key, &nh);
381 idi = &itp_db.id_itf[itp->itp_sw_if_index];
382
383 if (vnet_sw_interface_is_p2p (vnet_get_main (), itp->itp_sw_if_index))
384 {
Neale Ranns4ec36c52020-03-31 09:21:29 -0400385 // ipsec_tun_protect_feature_set (itp, 0);
Neale Ranns28287212019-12-16 00:53:11 +0000386 idi->id_itp = INDEX_INVALID;
387
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);
391 }
392 else
393 {
394 adj_nbr_walk_nh (itp->itp_sw_if_index,
395 nh_proto, &nh, ipsec_tun_protect_adj_remove, itp);
396
397 hash_unset_mem (idi->id_hash, itp->itp_key);
398
399 if (0 == hash_elts (idi->id_hash))
400 {
Neale Ranns4ec36c52020-03-31 09:21:29 -0400401 // ipsec_tun_protect_feature_set (itp, 0);
Neale Ranns28287212019-12-16 00:53:11 +0000402 hash_free (idi->id_hash);
403 idi->id_hash = NULL;
404 }
Neale Ranns8d6d74c2020-02-20 09:45:16 +0000405 ipsec_tun_unregister_nodes (FIB_PROTOCOL_IP6 == nh_proto ?
406 AF_IP6 : AF_IP4);
Neale Ranns28287212019-12-16 00:53:11 +0000407 }
408}
409
410static void
411ipsec_tun_protect_set_crypto_addr (ipsec_tun_protect_t * itp)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800412{
413 ipsec_sa_t *sa;
Neale Ranns12989b52019-09-26 16:20:19 +0000414
Neale Rannsc87b66c2019-02-07 07:26:12 -0800415 /* *INDENT-OFF* */
416 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
417 ({
418 if (ipsec_sa_is_set_IS_TUNNEL (sa))
419 {
420 itp->itp_crypto.src = sa->tunnel_dst_addr;
421 itp->itp_crypto.dst = sa->tunnel_src_addr;
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000422 if (!(itp->itp_flags & IPSEC_PROTECT_ITF))
423 {
424 ipsec_sa_set_IS_PROTECT (sa);
425 itp->itp_flags |= IPSEC_PROTECT_ENCAPED;
426 }
Neale Rannsc87b66c2019-02-07 07:26:12 -0800427 }
428 else
429 {
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;
433 }
434 }));
435 /* *INDENT-ON* */
Neale Ranns28287212019-12-16 00:53:11 +0000436}
437
438static void
439ipsec_tun_protect_config (ipsec_main_t * im,
440 ipsec_tun_protect_t * itp, u32 sa_out, u32 * sas_in)
441{
442 index_t sai;
443 u32 ii;
444
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;
449
450 ipsec_sa_lock (itp->itp_out_sa);
451
452 /* *INDENT-OFF* */
453 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
454 ({
455 ipsec_sa_lock(sai);
456 }));
457 ipsec_tun_protect_set_crypto_addr(itp);
458 /* *INDENT-ON* */
Neale Rannsc87b66c2019-02-07 07:26:12 -0800459
460 /*
461 * add to the DB against each SA
462 */
Neale Ranns28287212019-12-16 00:53:11 +0000463 ipsec_tun_protect_rx_db_add (im, itp);
464 ipsec_tun_protect_tx_db_add (itp);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800465
Neale Ranns28287212019-12-16 00:53:11 +0000466 ITP_DBG (itp, "configured");
Neale Rannsc87b66c2019-02-07 07:26:12 -0800467}
468
469static void
470ipsec_tun_protect_unconfig (ipsec_main_t * im, ipsec_tun_protect_t * itp)
471{
472 ipsec_sa_t *sa;
Neale Ranns495d7ff2019-07-12 09:15:26 +0000473 index_t sai;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800474
Neale Rannsc87b66c2019-02-07 07:26:12 -0800475 /* *INDENT-OFF* */
476 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
477 ({
478 ipsec_sa_unset_IS_PROTECT (sa);
479 }));
Neale Rannsc87b66c2019-02-07 07:26:12 -0800480
Neale Ranns28287212019-12-16 00:53:11 +0000481 ipsec_tun_protect_rx_db_remove (im, itp);
482 ipsec_tun_protect_tx_db_remove (itp);
Neale Ranns495d7ff2019-07-12 09:15:26 +0000483
484 ipsec_sa_unlock(itp->itp_out_sa);
485
486 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
487 ({
488 ipsec_sa_unlock(sai);
489 }));
490 /* *INDENT-ON* */
Neale Ranns28287212019-12-16 00:53:11 +0000491 ITP_DBG (itp, "unconfigured");
Neale Rannsc87b66c2019-02-07 07:26:12 -0800492}
493
494int
Neale Ranns28287212019-12-16 00:53:11 +0000495ipsec_tun_protect_update_one (u32 sw_if_index,
496 const ip_address_t * nh, u32 sa_out, u32 sa_in)
Neale Ranns12989b52019-09-26 16:20:19 +0000497{
498 u32 *sas_in = NULL;
499 int rv;
500
501 vec_add1 (sas_in, sa_in);
Neale Ranns28287212019-12-16 00:53:11 +0000502 rv = ipsec_tun_protect_update (sw_if_index, nh, sa_out, sas_in);
Neale Ranns12989b52019-09-26 16:20:19 +0000503
504 return (rv);
505}
506
507int
Neale Ranns28287212019-12-16 00:53:11 +0000508ipsec_tun_protect_update_out (u32 sw_if_index,
509 const ip_address_t * nh, u32 sa_out)
Neale Ranns12989b52019-09-26 16:20:19 +0000510{
511 u32 itpi, *sas_in, sai, *saip;
512 ipsec_tun_protect_t *itp;
513 ipsec_main_t *im;
514 int rv;
515
516 sas_in = NULL;
517 rv = 0;
518 im = &ipsec_main;
Neale Ranns28287212019-12-16 00:53:11 +0000519
520 itpi = ipsec_tun_protect_find (sw_if_index, nh);
Neale Ranns12989b52019-09-26 16:20:19 +0000521
522 if (INDEX_INVALID == itpi)
523 {
524 return (VNET_API_ERROR_INVALID_INTERFACE);
525 }
526
Neale Ranns28287212019-12-16 00:53:11 +0000527 itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
Neale Ranns12989b52019-09-26 16:20:19 +0000528
Neale Ranns28287212019-12-16 00:53:11 +0000529 /* *INDENT-OFF* */
530 FOR_EACH_IPSEC_PROTECT_INPUT_SAI (itp, sai,
531 ({
532 ipsec_sa_lock (sai);
533 vec_add1 (sas_in, sai);
534 }));
Neale Ranns12989b52019-09-26 16:20:19 +0000535 /* *INDENT-ON* */
536
537 sa_out = ipsec_sa_find_and_lock (sa_out);
538
539 if (~0 == sa_out)
540 {
541 rv = VNET_API_ERROR_INVALID_VALUE;
542 goto out;
543 }
544
545 ipsec_tun_protect_unconfig (im, itp);
546 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
547
548 ipsec_sa_unlock (sa_out);
549 vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
550
551out:
552 vec_free (sas_in);
553 return (rv);
554}
555
556int
Neale Ranns28287212019-12-16 00:53:11 +0000557ipsec_tun_protect_update_in (u32 sw_if_index,
558 const ip_address_t * nh, u32 sa_in)
Neale Ranns12989b52019-09-26 16:20:19 +0000559{
560 u32 itpi, *sas_in, sa_out;
561 ipsec_tun_protect_t *itp;
562 ipsec_main_t *im;
563 int rv;
564
565 sas_in = NULL;
566 rv = 0;
567 im = &ipsec_main;
Neale Ranns28287212019-12-16 00:53:11 +0000568 itpi = ipsec_tun_protect_find (sw_if_index, nh);
Neale Ranns12989b52019-09-26 16:20:19 +0000569
570 if (INDEX_INVALID == itpi)
571 {
572 return (VNET_API_ERROR_INVALID_INTERFACE);
573 }
574
575 sa_in = ipsec_sa_find_and_lock (sa_in);
576
577 if (~0 == sa_in)
578 {
579 rv = VNET_API_ERROR_INVALID_VALUE;
580 goto out;
581 }
582 vec_add1 (sas_in, sa_in);
583
Neale Ranns28287212019-12-16 00:53:11 +0000584 itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
Neale Ranns12989b52019-09-26 16:20:19 +0000585 sa_out = itp->itp_out_sa;
586
587 ipsec_sa_lock (sa_out);
588
589 ipsec_tun_protect_unconfig (im, itp);
590 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
591
592 ipsec_sa_unlock (sa_out);
593 ipsec_sa_unlock (sa_in);
594out:
595 vec_free (sas_in);
596 return (rv);
597}
598
Neale Ranns28287212019-12-16 00:53:11 +0000599static void
600ipsec_tun_protect_update_from_teib (ipsec_tun_protect_t * itp,
601 const teib_entry_t * ne)
602{
603 if (NULL != ne)
604 {
605 const fib_prefix_t *pfx;
606
607 pfx = teib_entry_get_nh (ne);
608
609 ip46_address_copy (&itp->itp_tun.dst, &pfx->fp_addr);
610 }
611 else
612 ip46_address_reset (&itp->itp_tun.dst);
613}
614
Neale Ranns12989b52019-09-26 16:20:19 +0000615int
Neale Ranns28287212019-12-16 00:53:11 +0000616ipsec_tun_protect_update (u32 sw_if_index,
617 const ip_address_t * nh, u32 sa_out, u32 * sas_in)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800618{
Neale Rannsc87b66c2019-02-07 07:26:12 -0800619 ipsec_tun_protect_t *itp;
Neale Ranns12989b52019-09-26 16:20:19 +0000620 u32 itpi, ii, *saip;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800621 ipsec_main_t *im;
622 int rv;
623
Neale Ranns28287212019-12-16 00:53:11 +0000624 ITP_DBG2 ("update: %U/%U",
625 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
626 format_ip_address, nh);
627
Matthew Smithdc3e9662020-04-10 20:27:33 -0500628 if (vec_len (sas_in) > ITP_MAX_N_SA_IN)
629 {
630 rv = VNET_API_ERROR_LIMIT_EXCEEDED;
631 goto out;
632 }
633
Neale Rannsc87b66c2019-02-07 07:26:12 -0800634 rv = 0;
635 im = &ipsec_main;
Neale Ranns28287212019-12-16 00:53:11 +0000636 if (NULL == nh)
637 nh = &IP_ADDR_ALL_0;
638 itpi = ipsec_tun_protect_find (sw_if_index, nh);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800639
640 vec_foreach_index (ii, sas_in)
641 {
Neale Ranns495d7ff2019-07-12 09:15:26 +0000642 sas_in[ii] = ipsec_sa_find_and_lock (sas_in[ii]);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800643 if (~0 == sas_in[ii])
644 {
645 rv = VNET_API_ERROR_INVALID_VALUE;
646 goto out;
647 }
648 }
649
Neale Ranns495d7ff2019-07-12 09:15:26 +0000650 sa_out = ipsec_sa_find_and_lock (sa_out);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800651
652 if (~0 == sa_out)
653 {
654 rv = VNET_API_ERROR_INVALID_VALUE;
655 goto out;
656 }
657
658 if (INDEX_INVALID == itpi)
659 {
660 vnet_device_class_t *dev_class;
661 vnet_hw_interface_t *hi;
662 vnet_main_t *vnm;
663 u8 is_l2;
664
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);
668
669 if (NULL == dev_class->ip_tun_desc)
670 {
671 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
672 goto out;
673 }
674
Neale Ranns28287212019-12-16 00:53:11 +0000675 pool_get_zero (ipsec_tun_protect_pool, itp);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800676
677 itp->itp_sw_if_index = sw_if_index;
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000678 itp->itp_ai = ADJ_INDEX_INVALID;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800679
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;
684
Neale Ranns28287212019-12-16 00:53:11 +0000685 itp->itp_key = clib_mem_alloc (sizeof (*itp->itp_key));
686 ip_address_copy (itp->itp_key, nh);
687
Neale Rannsc87b66c2019-02-07 07:26:12 -0800688 rv = dev_class->ip_tun_desc (sw_if_index,
689 &itp->itp_tun.src,
690 &itp->itp_tun.dst, &is_l2);
691
692 if (rv)
693 goto out;
694
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000695 if (ip46_address_is_zero (&itp->itp_tun.src))
696 {
697 /* must be one of thos pesky ipsec interfaces that has no encap.
698 * the encap then MUST comefrom the tunnel mode SA.
699 */
700 ipsec_sa_t *sa;
701
702 sa = ipsec_sa_get (itp->itp_out_sa);
703
704 if (!ipsec_sa_is_set_IS_TUNNEL (sa))
705 {
706 rv = VNET_API_ERROR_INVALID_DST_ADDRESS;
707 goto out;
708 }
709
710 itp->itp_flags |= IPSEC_PROTECT_ITF;
711 }
712 else if (ip46_address_is_zero (&itp->itp_tun.dst))
Neale Ranns28287212019-12-16 00:53:11 +0000713 {
714 /* tunnel has no destination address, presumably because it's p2mp
715 in which case we use the nh that this is protection for */
Neale Ranns28287212019-12-16 00:53:11 +0000716 ipsec_tun_protect_update_from_teib
Neale Rannse6b83052020-09-17 12:56:47 +0000717 (itp, teib_entry_find (sw_if_index, nh));
Neale Ranns28287212019-12-16 00:53:11 +0000718 }
719
Neale Rannsc87b66c2019-02-07 07:26:12 -0800720 if (is_l2)
721 itp->itp_flags |= IPSEC_PROTECT_L2;
722
723 /*
724 * add to the tunnel DB for ingress
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000725 * - if the SA is in trasnport mode, then the packates will arrive
Neale Rannsc87b66c2019-02-07 07:26:12 -0800726 * with the IP src,dst of the protected tunnel, in which case we can
727 * simply strip the IP header and hand the payload to the protocol
728 * appropriate input handler
729 * - if the SA is in tunnel mode then there are two IP headers present
730 * one for the crytpo tunnel endpoints (described in the SA) and one
731 * for the tunnel endpoints. The outer IP headers in the srriving
732 * packets will have the crypto endpoints. So the DB needs to contain
733 * the crpto endpoint. Once the crypto header is stripped, revealing,
734 * the tunnel-IP we have 2 choices:
735 * 1) do a tunnel lookup based on the revealed header
736 * 2) skip the tunnel lookup and assume that the packet matches the
737 * one that is protected here.
738 * If we did 1) then we would allow our peer to use the SA for tunnel
739 * X to inject traffic onto tunnel Y, this is not good. If we do 2)
740 * then we don't verify that the peer is indeed using SA for tunnel
741 * X and addressing tunnel X. So we take a compromise, once the SA
742 * matches to tunnel X we veriy that the inner IP matches the value
743 * of the tunnel we are protecting, else it's dropped.
744 */
745 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800746 }
747 else
748 {
749 /* updating SAs only */
Neale Ranns28287212019-12-16 00:53:11 +0000750 itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800751
752 ipsec_tun_protect_unconfig (im, itp);
753 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
754 }
755
Neale Ranns12989b52019-09-26 16:20:19 +0000756 ipsec_sa_unlock (sa_out);
757 vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800758 vec_free (sas_in);
Neale Ranns12989b52019-09-26 16:20:19 +0000759
Neale Rannsc87b66c2019-02-07 07:26:12 -0800760out:
761 return (rv);
762}
763
764int
Neale Ranns28287212019-12-16 00:53:11 +0000765ipsec_tun_protect_del (u32 sw_if_index, const ip_address_t * nh)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800766{
767 ipsec_tun_protect_t *itp;
768 ipsec_main_t *im;
769 index_t itpi;
770
Neale Ranns28287212019-12-16 00:53:11 +0000771 ITP_DBG2 ("delete: %U/%U",
772 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
773 format_ip_address, nh);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800774
Neale Ranns28287212019-12-16 00:53:11 +0000775 im = &ipsec_main;
776 if (NULL == nh)
777 nh = &IP_ADDR_ALL_0;
778
779 itpi = ipsec_tun_protect_find (sw_if_index, nh);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800780
781 if (INDEX_INVALID == itpi)
782 return (VNET_API_ERROR_NO_SUCH_ENTRY);
783
784 itp = ipsec_tun_protect_get (itpi);
785 ipsec_tun_protect_unconfig (im, itp);
786
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000787 if (ADJ_INDEX_INVALID != itp->itp_ai)
788 adj_unlock (itp->itp_ai);
789
Neale Ranns28287212019-12-16 00:53:11 +0000790 clib_mem_free (itp->itp_key);
791 pool_put (ipsec_tun_protect_pool, itp);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800792
Neale Rannsc87b66c2019-02-07 07:26:12 -0800793 return (0);
794}
795
796void
797ipsec_tun_protect_walk (ipsec_tun_protect_walk_cb_t fn, void *ctx)
798{
799 index_t itpi;
800
801 /* *INDENT-OFF* */
Neale Ranns28287212019-12-16 00:53:11 +0000802 pool_foreach_index(itpi, ipsec_tun_protect_pool,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800803 ({
804 fn (itpi, ctx);
805 }));
806 /* *INDENT-ON* */
807}
808
Neale Ranns28287212019-12-16 00:53:11 +0000809void
810ipsec_tun_protect_walk_itf (u32 sw_if_index,
811 ipsec_tun_protect_walk_cb_t fn, void *ctx)
812{
813 ipsec_tun_protect_itf_db_t *idi;
814 ip_address_t *key;
815 index_t itpi;
816
817 if (vec_len (itp_db.id_itf) <= sw_if_index)
818 return;
819
820 idi = &itp_db.id_itf[sw_if_index];
821
822 /* *INDENT-OFF* */
823 hash_foreach(key, itpi, idi->id_hash,
824 ({
825 fn (itpi, ctx);
826 }));
827 /* *INDENT-ON* */
828 if (INDEX_INVALID != idi->id_itp)
829 fn (idi->id_itp, ctx);
830}
831
832static void
833ipsec_tun_protect_adj_delegate_adj_deleted (adj_delegate_t * ad)
834{
835 /* remove our delegate */
Neale Ranns4ec36c52020-03-31 09:21:29 -0400836 ipsec_tun_protect_add_adj (ad->ad_adj_index, NULL);
BenoƮt Ganneea9bc282020-04-16 12:40:04 +0200837 adj_delegate_remove (ad->ad_adj_index, ipsec_tun_adj_delegate_type);
Neale Ranns28287212019-12-16 00:53:11 +0000838}
839
840static void
Neale Ranns4ec36c52020-03-31 09:21:29 -0400841ipsec_tun_protect_adj_delegate_adj_modified (adj_delegate_t * ad)
842{
843 ipsec_tun_protect_add_adj (ad->ad_adj_index,
844 ipsec_tun_protect_get (ad->ad_index));
845}
846
847static void
Neale Ranns28287212019-12-16 00:53:11 +0000848ipsec_tun_protect_adj_delegate_adj_created (adj_index_t ai)
849{
850 /* add our delegate if there is protection for this neighbour */
851 ip_address_t ip = IP_ADDRESS_V4_ALL_0S;
852 ip_adjacency_t *adj;
853 index_t itpi;
854
Neale Ranns4ec36c52020-03-31 09:21:29 -0400855 if (!adj_is_midchain (ai))
Neale Ranns28287212019-12-16 00:53:11 +0000856 return;
857
Neale Ranns4ec36c52020-03-31 09:21:29 -0400858 adj = adj_get (ai);
859
Neale Ranns28287212019-12-16 00:53:11 +0000860 ip_address_from_46 (&adj->sub_type.midchain.next_hop,
861 adj->ia_nh_proto, &ip);
862
863 itpi = ipsec_tun_protect_find (adj->rewrite_header.sw_if_index, &ip);
864
865 if (INDEX_INVALID != itpi)
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000866 ipsec_tun_protect_adj_add (ai, ipsec_tun_protect_get (itpi));
Neale Ranns28287212019-12-16 00:53:11 +0000867}
868
869static u8 *
870ipsec_tun_protect_adj_delegate_format (const adj_delegate_t * aed, u8 * s)
871{
872 const ipsec_tun_protect_t *itp;
873
874 itp = ipsec_tun_protect_from_const_base (aed);
875 s = format (s, "ipsec-tun-protect:\n%U", format_ipsec_tun_protect, itp);
876
877 return (s);
878}
879
880static void
881ipsec_tun_teib_entry_added (const teib_entry_t * ne)
882{
Neale Ranns28287212019-12-16 00:53:11 +0000883 ipsec_tun_protect_t *itp;
Neale Ranns28287212019-12-16 00:53:11 +0000884 index_t itpi;
885
Neale Rannse6b83052020-09-17 12:56:47 +0000886 itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne),
887 teib_entry_get_peer (ne));
Neale Ranns28287212019-12-16 00:53:11 +0000888
889 if (INDEX_INVALID == itpi)
890 return;
891
892 itp = ipsec_tun_protect_get (itpi);
893 ipsec_tun_protect_rx_db_remove (&ipsec_main, itp);
894 ipsec_tun_protect_update_from_teib (itp, ne);
895 ipsec_tun_protect_set_crypto_addr (itp);
896 ipsec_tun_protect_rx_db_add (&ipsec_main, itp);
897
898 ITP_DBG (itp, "teib-added");
899}
900
901static void
902ipsec_tun_teib_entry_deleted (const teib_entry_t * ne)
903{
Neale Ranns28287212019-12-16 00:53:11 +0000904 ipsec_tun_protect_t *itp;
Neale Ranns28287212019-12-16 00:53:11 +0000905 index_t itpi;
906
Neale Rannse6b83052020-09-17 12:56:47 +0000907 itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne),
908 teib_entry_get_peer (ne));
Neale Ranns28287212019-12-16 00:53:11 +0000909
910 if (INDEX_INVALID == itpi)
911 return;
912
913 itp = ipsec_tun_protect_get (itpi);
914 ipsec_tun_protect_rx_db_remove (&ipsec_main, itp);
915 ipsec_tun_protect_update_from_teib (itp, NULL);
916 ipsec_tun_protect_set_crypto_addr (itp);
917
918 ITP_DBG (itp, "teib-removed");
919}
920
921/**
922 * VFT registered with the adjacency delegate
923 */
924const static adj_delegate_vft_t ipsec_tun_adj_delegate_vft = {
925 .adv_adj_deleted = ipsec_tun_protect_adj_delegate_adj_deleted,
926 .adv_adj_created = ipsec_tun_protect_adj_delegate_adj_created,
Neale Ranns4ec36c52020-03-31 09:21:29 -0400927 .adv_adj_modified = ipsec_tun_protect_adj_delegate_adj_modified,
Neale Ranns28287212019-12-16 00:53:11 +0000928 .adv_format = ipsec_tun_protect_adj_delegate_format,
929};
930
931const static teib_vft_t ipsec_tun_teib_vft = {
932 .nv_added = ipsec_tun_teib_entry_added,
933 .nv_deleted = ipsec_tun_teib_entry_deleted,
934};
935
Neale Ranns4ec36c52020-03-31 09:21:29 -0400936
Neale Rannsc87b66c2019-02-07 07:26:12 -0800937clib_error_t *
938ipsec_tunnel_protect_init (vlib_main_t * vm)
939{
940 ipsec_main_t *im;
941
942 im = &ipsec_main;
943 im->tun6_protect_by_key = hash_create_mem (0,
944 sizeof (ipsec6_tunnel_key_t),
945 sizeof (u64));
946 im->tun4_protect_by_key = hash_create (0, sizeof (u64));
947
Neale Ranns02950402019-12-20 00:54:57 +0000948 /* set up feature nodes to drop outbound packets with no crypto alg set */
Neale Ranns4ec36c52020-03-31 09:21:29 -0400949 im->esp4_no_crypto_tun_node_index =
950 vlib_get_node_by_name (vm, (u8 *) "esp4-no-crypto")->index;
951 im->esp6_no_crypto_tun_node_index =
952 vlib_get_node_by_name (vm, (u8 *) "esp6-no-crypto")->index;
953 im->esp6_encrypt_l2_tun_node_index =
954 vlib_get_node_by_name (vm, (u8 *) "esp6-encrypt-tun")->index;
955 im->esp4_encrypt_l2_tun_node_index =
956 vlib_get_node_by_name (vm, (u8 *) "esp4-encrypt-tun")->index;
Neale Ranns02950402019-12-20 00:54:57 +0000957
Neale Ranns28287212019-12-16 00:53:11 +0000958 ipsec_tun_adj_delegate_type =
959 adj_delegate_register_new_type (&ipsec_tun_adj_delegate_vft);
960
961 ipsec_tun_protect_logger = vlib_log_register_class ("ipsec", "tun");
962
963 teib_register (&ipsec_tun_teib_vft);
964
Neale Rannsc87b66c2019-02-07 07:26:12 -0800965 return 0;
966}
967
968VLIB_INIT_FUNCTION (ipsec_tunnel_protect_init);
969
970
971/*
972 * fd.io coding-style-patch-verification: ON
973 *
974 * Local Variables:
975 * eval: (c-set-style "gnu")
976 * End:
977 */