blob: 9b76d311bc25f374591f8ec17de52af86da11551 [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>
19#include <vnet/ipsec/esp.h>
20#include <vnet/udp/udp.h>
Neale Ranns28287212019-12-16 00:53:11 +000021#include <vnet/adj/adj_delegate.h>
Neale Ranns4ec36c52020-03-31 09:21:29 -040022#include <vnet/adj/adj_midchain.h>
Neale Ranns28287212019-12-16 00:53:11 +000023#include <vnet/teib/teib.h>
24
25/**
26 * The logger
27 */
28vlib_log_class_t ipsec_tun_protect_logger;
Neale Rannsc87b66c2019-02-07 07:26:12 -080029
30/**
31 * Pool of tunnel protection objects
32 */
Neale Ranns28287212019-12-16 00:53:11 +000033ipsec_tun_protect_t *ipsec_tun_protect_pool;
Neale Rannsc87b66c2019-02-07 07:26:12 -080034
35/**
Neale Ranns28287212019-12-16 00:53:11 +000036 * Adj delegate registered type
Neale Rannsc87b66c2019-02-07 07:26:12 -080037 */
Neale Ranns28287212019-12-16 00:53:11 +000038static adj_delegate_type_t ipsec_tun_adj_delegate_type;
Neale Rannsc87b66c2019-02-07 07:26:12 -080039
Neale Ranns28287212019-12-16 00:53:11 +000040/**
41 * Adj index to TX SA mapping
42 */
43index_t *ipsec_tun_protect_sa_by_adj_index;
44
45const ip_address_t IP_ADDR_ALL_0 = IP_ADDRESS_V4_ALL_0S;
46
47/**
48 * The DB of all added per-nh tunnel protectiond
49 */
50typedef struct ipsec_tun_protect_itf_db_t_
51{
52 /** A hash table key'd on IP (4 or 6) address */
53 uword *id_hash;
54 /** If the interface is P2P then there is only one protect
55 * object associated with the auto-adj for each NH proto */
56 index_t id_itp;
57} ipsec_tun_protect_itf_db_t;
58
59typedef struct ipsec_tun_protect_db_t_
60{
61 /** Per-interface vector */
62 ipsec_tun_protect_itf_db_t *id_itf;
63} ipsec_tun_protect_db_t;
64
65static ipsec_tun_protect_db_t itp_db;
66
67const static ipsec_tun_protect_itf_db_t IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY = {
68 .id_itp = INDEX_INVALID,
69};
70
71#define ITP_DBG(_itp, _fmt, _args...) \
72{ \
73 vlib_log_debug(ipsec_tun_protect_logger, \
74 "[%U]: " _fmt, \
75 format_ipsec_tun_protect, \
76 _itp, ##_args); \
77}
78
79#define ITP_DBG2(_fmt, _args...) \
80{ \
81 vlib_log_debug(ipsec_tun_protect_logger, \
82 _fmt, ##_args); \
83}
84
Neale Ranns8d6d74c2020-02-20 09:45:16 +000085static u32 ipsec_tun_node_regs[N_AF];
86
87void
88ipsec_tun_register_nodes (ip_address_family_t af)
89{
90 if (0 == ipsec_tun_node_regs[af]++)
91 {
92 if (AF_IP4 == af)
93 {
Neale Rannsabc56602020-04-01 09:45:23 +000094 ipsec_register_udp_port (UDP_DST_PORT_ipsec);
Neale Ranns8d6d74c2020-02-20 09:45:16 +000095 ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
96 ipsec4_tun_input_node.index);
97 }
98 else
99 ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP,
100 ipsec6_tun_input_node.index);
101 }
102}
103
104void
105ipsec_tun_unregister_nodes (ip_address_family_t af)
106{
107 ASSERT (0 != ipsec_tun_node_regs[af]);
108 if (0 == --ipsec_tun_node_regs[af])
109 {
110 if (AF_IP4 == af)
111 {
Neale Rannsabc56602020-04-01 09:45:23 +0000112 ipsec_unregister_udp_port (UDP_DST_PORT_ipsec);
Neale Ranns8d6d74c2020-02-20 09:45:16 +0000113 ip4_unregister_protocol (IP_PROTOCOL_IPSEC_ESP);
114 }
115 else
116 ip6_unregister_protocol (IP_PROTOCOL_IPSEC_ESP);
117 }
118}
119
Neale Ranns28287212019-12-16 00:53:11 +0000120static inline const ipsec_tun_protect_t *
121ipsec_tun_protect_from_const_base (const adj_delegate_t * ad)
122{
123 if (ad == NULL)
124 return (NULL);
125 return (pool_elt_at_index (ipsec_tun_protect_pool, ad->ad_index));
126}
Neale Rannsc87b66c2019-02-07 07:26:12 -0800127
Neale Ranns4ec36c52020-03-31 09:21:29 -0400128static u32
129ipsec_tun_protect_get_adj_next (const ipsec_tun_protect_t * itp)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800130{
Neale Ranns4ec36c52020-03-31 09:21:29 -0400131 ipsec_main_t *im;
132 ipsec_sa_t *sa;
133 bool is_ip4;
134 u32 next;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800135
Neale Ranns4ec36c52020-03-31 09:21:29 -0400136 is_ip4 = ip46_address_is_ip4 (&itp->itp_tun.src);
137 sa = ipsec_sa_get (itp->itp_out_sa);
138 im = &ipsec_main;
139
140 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_NONE &&
141 sa->integ_alg == IPSEC_INTEG_ALG_NONE)
142 next = (is_ip4 ?
143 im->esp4_no_crypto_tun_node_index :
144 im->esp6_no_crypto_tun_node_index);
145 else if (itp->itp_flags & IPSEC_PROTECT_L2)
146 next = (is_ip4 ?
147 im->esp4_encrypt_l2_tun_node_index :
148 im->esp6_encrypt_l2_tun_node_index);
149 else
150 next = (is_ip4 ?
151 im->esp4_encrypt_tun_node_index :
152 im->esp6_encrypt_tun_node_index);
153
154 return (next);
155}
156
157static void
158ipsec_tun_protect_add_adj (adj_index_t ai, const ipsec_tun_protect_t * itp)
159{
160 vec_validate_init_empty (ipsec_tun_protect_sa_by_adj_index, ai,
161 INDEX_INVALID);
162
163 if (NULL == itp)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800164 {
Neale Ranns4ec36c52020-03-31 09:21:29 -0400165 ipsec_tun_protect_sa_by_adj_index[ai] = INDEX_INVALID;
166 adj_nbr_midchain_reset_next_node (ai);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800167 }
168 else
169 {
Neale Ranns4ec36c52020-03-31 09:21:29 -0400170 ipsec_tun_protect_sa_by_adj_index[ai] = itp->itp_out_sa;
171 adj_nbr_midchain_update_next_node
172 (ai, ipsec_tun_protect_get_adj_next (itp));
Neale Rannsc87b66c2019-02-07 07:26:12 -0800173 }
Neale Rannsc87b66c2019-02-07 07:26:12 -0800174}
175
Neale Ranns28287212019-12-16 00:53:11 +0000176static index_t
177ipsec_tun_protect_find (u32 sw_if_index, const ip_address_t * nh)
178{
179 ipsec_tun_protect_itf_db_t *idi;
180 uword *p;
181
182 if (vec_len (itp_db.id_itf) <= sw_if_index)
183 return INDEX_INVALID;
184
185 if (vnet_sw_interface_is_p2p (vnet_get_main (), sw_if_index))
186 return (itp_db.id_itf[sw_if_index].id_itp);
187
188 idi = &itp_db.id_itf[sw_if_index];
189 p = hash_get_mem (idi->id_hash, nh);
190
191 if (NULL == p)
192 {
193 return INDEX_INVALID;
194 }
195 return (p[0]);
196}
197
Neale Rannsc87b66c2019-02-07 07:26:12 -0800198static void
Neale Ranns28287212019-12-16 00:53:11 +0000199ipsec_tun_protect_rx_db_add (ipsec_main_t * im,
200 const ipsec_tun_protect_t * itp)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800201{
202 const ipsec_sa_t *sa;
203 u32 sai;
204
Neale Ranns28287212019-12-16 00:53:11 +0000205 if (ip46_address_is_zero (&itp->itp_crypto.dst))
206 return;
207
Neale Rannsc87b66c2019-02-07 07:26:12 -0800208 /* *INDENT-OFF* */
209 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
210 ({
211 sa = ipsec_sa_get (sai);
212
213 ipsec_tun_lkup_result_t res = {
Neale Ranns28287212019-12-16 00:53:11 +0000214 .tun_index = itp - ipsec_tun_protect_pool,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800215 .sa_index = sai,
216 };
217
218 /*
219 * The key is formed from the tunnel's destination
220 * as the packet lookup is done from the packet's source
221 */
222 if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
223 {
224 ipsec4_tunnel_key_t key = {
Neale Ranns41afb332019-07-16 06:19:35 -0700225 .remote_ip = itp->itp_crypto.dst.ip4,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800226 .spi = clib_host_to_net_u32 (sa->spi),
227 };
228 hash_set (im->tun4_protect_by_key, key.as_u64, res.as_u64);
Neale Ranns8d6d74c2020-02-20 09:45:16 +0000229 ipsec_tun_register_nodes(AF_IP4);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800230 }
231 else
232 {
233 ipsec6_tunnel_key_t key = {
234 .remote_ip = itp->itp_crypto.dst.ip6,
235 .spi = clib_host_to_net_u32 (sa->spi),
236 };
237 hash_set_mem_alloc (&im->tun6_protect_by_key, &key, res.as_u64);
Neale Ranns8d6d74c2020-02-20 09:45:16 +0000238 ipsec_tun_register_nodes(AF_IP6);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800239 }
240 }))
241 /* *INDENT-ON* */
242}
243
Neale Ranns28287212019-12-16 00:53:11 +0000244static adj_walk_rc_t
245ipsec_tun_protect_adj_add (adj_index_t ai, void *arg)
246{
247 ipsec_tun_protect_t *itp = arg;
248 adj_delegate_add (adj_get (ai), ipsec_tun_adj_delegate_type,
249 itp - ipsec_tun_protect_pool);
Neale Ranns4ec36c52020-03-31 09:21:29 -0400250 ipsec_tun_protect_add_adj (ai, itp);
Neale Ranns28287212019-12-16 00:53:11 +0000251
252 return (ADJ_WALK_RC_CONTINUE);
253}
254
Neale Rannsc87b66c2019-02-07 07:26:12 -0800255static void
Neale Ranns28287212019-12-16 00:53:11 +0000256ipsec_tun_protect_tx_db_add (ipsec_tun_protect_t * itp)
257{
258 /*
259 * add the delegate to the adj
260 */
261 ipsec_tun_protect_itf_db_t *idi;
262 fib_protocol_t nh_proto;
263 ip46_address_t nh;
264
265 vec_validate_init_empty (itp_db.id_itf,
266 itp->itp_sw_if_index,
267 IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY);
268
269 idi = &itp_db.id_itf[itp->itp_sw_if_index];
270
271 if (vnet_sw_interface_is_p2p (vnet_get_main (), itp->itp_sw_if_index))
272 {
273 if (INDEX_INVALID == idi->id_itp)
274 {
Neale Ranns4ec36c52020-03-31 09:21:29 -0400275 // ipsec_tun_protect_feature_set (itp, 1);
Neale Ranns28287212019-12-16 00:53:11 +0000276 }
277 idi->id_itp = itp - ipsec_tun_protect_pool;
278
279 FOR_EACH_FIB_IP_PROTOCOL (nh_proto)
280 adj_nbr_walk (itp->itp_sw_if_index,
281 nh_proto, ipsec_tun_protect_adj_add, itp);
282 }
283 else
284 {
285 if (NULL == idi->id_hash)
286 {
287 idi->id_hash =
288 hash_create_mem (0, sizeof (ip_address_t), sizeof (uword));
289 /*
290 * enable the encrypt feature for egress if this is the first addition
291 * on this interface
292 */
Neale Ranns4ec36c52020-03-31 09:21:29 -0400293 // ipsec_tun_protect_feature_set (itp, 1);
Neale Ranns28287212019-12-16 00:53:11 +0000294 }
295
296 hash_set_mem (idi->id_hash, itp->itp_key, itp - ipsec_tun_protect_pool);
297
298 /*
299 * walk all the adjs with the same nh on this interface
300 * to associate them with this protection
301 */
302 nh_proto = ip_address_to_46 (itp->itp_key, &nh);
303
304 adj_nbr_walk_nh (itp->itp_sw_if_index,
305 nh_proto, &nh, ipsec_tun_protect_adj_add, itp);
Neale Ranns8d6d74c2020-02-20 09:45:16 +0000306
307 ipsec_tun_register_nodes (FIB_PROTOCOL_IP6 == nh_proto ?
308 AF_IP6 : AF_IP4);
Neale Ranns28287212019-12-16 00:53:11 +0000309 }
310}
311
312static void
313ipsec_tun_protect_rx_db_remove (ipsec_main_t * im,
314 const ipsec_tun_protect_t * itp)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800315{
316 const ipsec_sa_t *sa;
317
318 /* *INDENT-OFF* */
319 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
320 ({
Neale Ranns4ec36c52020-03-31 09:21:29 -0400321 if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
322 {
323 ipsec4_tunnel_key_t key = {
324 .remote_ip = itp->itp_crypto.dst.ip4,
325 .spi = clib_host_to_net_u32 (sa->spi),
326 };
327 if (hash_get(im->tun4_protect_by_key, key.as_u64))
328 {
329 hash_unset (im->tun4_protect_by_key, key.as_u64);
330 ipsec_tun_unregister_nodes(AF_IP4);
331 }
332 }
333 else
334 {
335 ipsec6_tunnel_key_t key = {
336 .remote_ip = itp->itp_crypto.dst.ip6,
337 .spi = clib_host_to_net_u32 (sa->spi),
338 };
339 if (hash_get_mem(im->tun6_protect_by_key, &key))
340 {
341 hash_unset_mem_free (&im->tun6_protect_by_key, &key);
342 ipsec_tun_unregister_nodes(AF_IP6);
343 }
344 }
345 }));
Neale Rannsc87b66c2019-02-07 07:26:12 -0800346 /* *INDENT-ON* */
347}
348
Neale Ranns28287212019-12-16 00:53:11 +0000349static adj_walk_rc_t
350ipsec_tun_protect_adj_remove (adj_index_t ai, void *arg)
351{
352 adj_delegate_remove (ai, ipsec_tun_adj_delegate_type);
Neale Ranns4ec36c52020-03-31 09:21:29 -0400353 ipsec_tun_protect_add_adj (ai, NULL);
Neale Ranns28287212019-12-16 00:53:11 +0000354
355 return (ADJ_WALK_RC_CONTINUE);
356}
357
Neale Rannsc87b66c2019-02-07 07:26:12 -0800358static void
Neale Ranns28287212019-12-16 00:53:11 +0000359ipsec_tun_protect_tx_db_remove (ipsec_tun_protect_t * itp)
360{
361 ipsec_tun_protect_itf_db_t *idi;
362 fib_protocol_t nh_proto;
363 ip46_address_t nh;
364
365 nh_proto = ip_address_to_46 (itp->itp_key, &nh);
366 idi = &itp_db.id_itf[itp->itp_sw_if_index];
367
368 if (vnet_sw_interface_is_p2p (vnet_get_main (), itp->itp_sw_if_index))
369 {
Neale Ranns4ec36c52020-03-31 09:21:29 -0400370 // ipsec_tun_protect_feature_set (itp, 0);
Neale Ranns28287212019-12-16 00:53:11 +0000371 idi->id_itp = INDEX_INVALID;
372
373 FOR_EACH_FIB_IP_PROTOCOL (nh_proto)
374 adj_nbr_walk (itp->itp_sw_if_index,
375 nh_proto, ipsec_tun_protect_adj_remove, itp);
376 }
377 else
378 {
379 adj_nbr_walk_nh (itp->itp_sw_if_index,
380 nh_proto, &nh, ipsec_tun_protect_adj_remove, itp);
381
382 hash_unset_mem (idi->id_hash, itp->itp_key);
383
384 if (0 == hash_elts (idi->id_hash))
385 {
Neale Ranns4ec36c52020-03-31 09:21:29 -0400386 // ipsec_tun_protect_feature_set (itp, 0);
Neale Ranns28287212019-12-16 00:53:11 +0000387 hash_free (idi->id_hash);
388 idi->id_hash = NULL;
389 }
Neale Ranns8d6d74c2020-02-20 09:45:16 +0000390 ipsec_tun_unregister_nodes (FIB_PROTOCOL_IP6 == nh_proto ?
391 AF_IP6 : AF_IP4);
Neale Ranns28287212019-12-16 00:53:11 +0000392 }
393}
394
395static void
396ipsec_tun_protect_set_crypto_addr (ipsec_tun_protect_t * itp)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800397{
398 ipsec_sa_t *sa;
Neale Ranns12989b52019-09-26 16:20:19 +0000399
Neale Rannsc87b66c2019-02-07 07:26:12 -0800400 /* *INDENT-OFF* */
401 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
402 ({
403 if (ipsec_sa_is_set_IS_TUNNEL (sa))
404 {
405 itp->itp_crypto.src = sa->tunnel_dst_addr;
406 itp->itp_crypto.dst = sa->tunnel_src_addr;
407 ipsec_sa_set_IS_PROTECT (sa);
408 itp->itp_flags |= IPSEC_PROTECT_ENCAPED;
409 }
410 else
411 {
412 itp->itp_crypto.src = itp->itp_tun.src;
413 itp->itp_crypto.dst = itp->itp_tun.dst;
414 itp->itp_flags &= ~IPSEC_PROTECT_ENCAPED;
415 }
416 }));
417 /* *INDENT-ON* */
Neale Ranns28287212019-12-16 00:53:11 +0000418}
419
420static void
421ipsec_tun_protect_config (ipsec_main_t * im,
422 ipsec_tun_protect_t * itp, u32 sa_out, u32 * sas_in)
423{
424 index_t sai;
425 u32 ii;
426
427 itp->itp_n_sa_in = vec_len (sas_in);
428 for (ii = 0; ii < itp->itp_n_sa_in; ii++)
429 itp->itp_in_sas[ii] = sas_in[ii];
430 itp->itp_out_sa = sa_out;
431
432 ipsec_sa_lock (itp->itp_out_sa);
433
434 /* *INDENT-OFF* */
435 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
436 ({
437 ipsec_sa_lock(sai);
438 }));
439 ipsec_tun_protect_set_crypto_addr(itp);
440 /* *INDENT-ON* */
Neale Rannsc87b66c2019-02-07 07:26:12 -0800441
442 /*
443 * add to the DB against each SA
444 */
Neale Ranns28287212019-12-16 00:53:11 +0000445 ipsec_tun_protect_rx_db_add (im, itp);
446 ipsec_tun_protect_tx_db_add (itp);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800447
Neale Ranns28287212019-12-16 00:53:11 +0000448 ITP_DBG (itp, "configured");
Neale Rannsc87b66c2019-02-07 07:26:12 -0800449}
450
451static void
452ipsec_tun_protect_unconfig (ipsec_main_t * im, ipsec_tun_protect_t * itp)
453{
454 ipsec_sa_t *sa;
Neale Ranns495d7ff2019-07-12 09:15:26 +0000455 index_t sai;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800456
Neale Rannsc87b66c2019-02-07 07:26:12 -0800457 /* *INDENT-OFF* */
458 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
459 ({
460 ipsec_sa_unset_IS_PROTECT (sa);
461 }));
Neale Rannsc87b66c2019-02-07 07:26:12 -0800462
Neale Ranns28287212019-12-16 00:53:11 +0000463 ipsec_tun_protect_rx_db_remove (im, itp);
464 ipsec_tun_protect_tx_db_remove (itp);
Neale Ranns495d7ff2019-07-12 09:15:26 +0000465
466 ipsec_sa_unlock(itp->itp_out_sa);
467
468 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
469 ({
470 ipsec_sa_unlock(sai);
471 }));
472 /* *INDENT-ON* */
Neale Ranns28287212019-12-16 00:53:11 +0000473 ITP_DBG (itp, "unconfigured");
Neale Rannsc87b66c2019-02-07 07:26:12 -0800474}
475
476int
Neale Ranns28287212019-12-16 00:53:11 +0000477ipsec_tun_protect_update_one (u32 sw_if_index,
478 const ip_address_t * nh, u32 sa_out, u32 sa_in)
Neale Ranns12989b52019-09-26 16:20:19 +0000479{
480 u32 *sas_in = NULL;
481 int rv;
482
483 vec_add1 (sas_in, sa_in);
Neale Ranns28287212019-12-16 00:53:11 +0000484 rv = ipsec_tun_protect_update (sw_if_index, nh, sa_out, sas_in);
Neale Ranns12989b52019-09-26 16:20:19 +0000485
486 return (rv);
487}
488
489int
Neale Ranns28287212019-12-16 00:53:11 +0000490ipsec_tun_protect_update_out (u32 sw_if_index,
491 const ip_address_t * nh, u32 sa_out)
Neale Ranns12989b52019-09-26 16:20:19 +0000492{
493 u32 itpi, *sas_in, sai, *saip;
494 ipsec_tun_protect_t *itp;
495 ipsec_main_t *im;
496 int rv;
497
498 sas_in = NULL;
499 rv = 0;
500 im = &ipsec_main;
Neale Ranns28287212019-12-16 00:53:11 +0000501
502 itpi = ipsec_tun_protect_find (sw_if_index, nh);
Neale Ranns12989b52019-09-26 16:20:19 +0000503
504 if (INDEX_INVALID == itpi)
505 {
506 return (VNET_API_ERROR_INVALID_INTERFACE);
507 }
508
Neale Ranns28287212019-12-16 00:53:11 +0000509 itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
Neale Ranns12989b52019-09-26 16:20:19 +0000510
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 vec_add1 (sas_in, sai);
516 }));
Neale Ranns12989b52019-09-26 16:20:19 +0000517 /* *INDENT-ON* */
518
519 sa_out = ipsec_sa_find_and_lock (sa_out);
520
521 if (~0 == sa_out)
522 {
523 rv = VNET_API_ERROR_INVALID_VALUE;
524 goto out;
525 }
526
527 ipsec_tun_protect_unconfig (im, itp);
528 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
529
530 ipsec_sa_unlock (sa_out);
531 vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
532
533out:
534 vec_free (sas_in);
535 return (rv);
536}
537
538int
Neale Ranns28287212019-12-16 00:53:11 +0000539ipsec_tun_protect_update_in (u32 sw_if_index,
540 const ip_address_t * nh, u32 sa_in)
Neale Ranns12989b52019-09-26 16:20:19 +0000541{
542 u32 itpi, *sas_in, sa_out;
543 ipsec_tun_protect_t *itp;
544 ipsec_main_t *im;
545 int rv;
546
547 sas_in = NULL;
548 rv = 0;
549 im = &ipsec_main;
Neale Ranns28287212019-12-16 00:53:11 +0000550 itpi = ipsec_tun_protect_find (sw_if_index, nh);
Neale Ranns12989b52019-09-26 16:20:19 +0000551
552 if (INDEX_INVALID == itpi)
553 {
554 return (VNET_API_ERROR_INVALID_INTERFACE);
555 }
556
557 sa_in = ipsec_sa_find_and_lock (sa_in);
558
559 if (~0 == sa_in)
560 {
561 rv = VNET_API_ERROR_INVALID_VALUE;
562 goto out;
563 }
564 vec_add1 (sas_in, sa_in);
565
Neale Ranns28287212019-12-16 00:53:11 +0000566 itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
Neale Ranns12989b52019-09-26 16:20:19 +0000567 sa_out = itp->itp_out_sa;
568
569 ipsec_sa_lock (sa_out);
570
571 ipsec_tun_protect_unconfig (im, itp);
572 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
573
574 ipsec_sa_unlock (sa_out);
575 ipsec_sa_unlock (sa_in);
576out:
577 vec_free (sas_in);
578 return (rv);
579}
580
Neale Ranns28287212019-12-16 00:53:11 +0000581static void
582ipsec_tun_protect_update_from_teib (ipsec_tun_protect_t * itp,
583 const teib_entry_t * ne)
584{
585 if (NULL != ne)
586 {
587 const fib_prefix_t *pfx;
588
589 pfx = teib_entry_get_nh (ne);
590
591 ip46_address_copy (&itp->itp_tun.dst, &pfx->fp_addr);
592 }
593 else
594 ip46_address_reset (&itp->itp_tun.dst);
595}
596
Neale Ranns12989b52019-09-26 16:20:19 +0000597int
Neale Ranns28287212019-12-16 00:53:11 +0000598ipsec_tun_protect_update (u32 sw_if_index,
599 const ip_address_t * nh, u32 sa_out, u32 * sas_in)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800600{
Neale Rannsc87b66c2019-02-07 07:26:12 -0800601 ipsec_tun_protect_t *itp;
Neale Ranns12989b52019-09-26 16:20:19 +0000602 u32 itpi, ii, *saip;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800603 ipsec_main_t *im;
604 int rv;
605
Neale Ranns28287212019-12-16 00:53:11 +0000606 ITP_DBG2 ("update: %U/%U",
607 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
608 format_ip_address, nh);
609
Matthew Smithdc3e9662020-04-10 20:27:33 -0500610 if (vec_len (sas_in) > ITP_MAX_N_SA_IN)
611 {
612 rv = VNET_API_ERROR_LIMIT_EXCEEDED;
613 goto out;
614 }
615
Neale Rannsc87b66c2019-02-07 07:26:12 -0800616 rv = 0;
617 im = &ipsec_main;
Neale Ranns28287212019-12-16 00:53:11 +0000618 if (NULL == nh)
619 nh = &IP_ADDR_ALL_0;
620 itpi = ipsec_tun_protect_find (sw_if_index, nh);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800621
622 vec_foreach_index (ii, sas_in)
623 {
Neale Ranns495d7ff2019-07-12 09:15:26 +0000624 sas_in[ii] = ipsec_sa_find_and_lock (sas_in[ii]);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800625 if (~0 == sas_in[ii])
626 {
627 rv = VNET_API_ERROR_INVALID_VALUE;
628 goto out;
629 }
630 }
631
Neale Ranns495d7ff2019-07-12 09:15:26 +0000632 sa_out = ipsec_sa_find_and_lock (sa_out);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800633
634 if (~0 == sa_out)
635 {
636 rv = VNET_API_ERROR_INVALID_VALUE;
637 goto out;
638 }
639
640 if (INDEX_INVALID == itpi)
641 {
642 vnet_device_class_t *dev_class;
643 vnet_hw_interface_t *hi;
644 vnet_main_t *vnm;
645 u8 is_l2;
646
647 vnm = vnet_get_main ();
648 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
649 dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
650
651 if (NULL == dev_class->ip_tun_desc)
652 {
653 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
654 goto out;
655 }
656
Neale Ranns28287212019-12-16 00:53:11 +0000657 pool_get_zero (ipsec_tun_protect_pool, itp);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800658
659 itp->itp_sw_if_index = sw_if_index;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800660
661 itp->itp_n_sa_in = vec_len (sas_in);
662 for (ii = 0; ii < itp->itp_n_sa_in; ii++)
663 itp->itp_in_sas[ii] = sas_in[ii];
664 itp->itp_out_sa = sa_out;
665
Neale Ranns28287212019-12-16 00:53:11 +0000666 itp->itp_key = clib_mem_alloc (sizeof (*itp->itp_key));
667 ip_address_copy (itp->itp_key, nh);
668
Neale Rannsc87b66c2019-02-07 07:26:12 -0800669 rv = dev_class->ip_tun_desc (sw_if_index,
670 &itp->itp_tun.src,
671 &itp->itp_tun.dst, &is_l2);
672
673 if (rv)
674 goto out;
675
Neale Ranns28287212019-12-16 00:53:11 +0000676 if (ip46_address_is_zero (&itp->itp_tun.dst))
677 {
678 /* tunnel has no destination address, presumably because it's p2mp
679 in which case we use the nh that this is protection for */
680 ip46_address_t peer;
681
682 ip_address_to_46 (nh, &peer);
683
684 ipsec_tun_protect_update_from_teib
685 (itp, teib_entry_find (sw_if_index, &peer));
686 }
687
Neale Rannsc87b66c2019-02-07 07:26:12 -0800688 if (is_l2)
689 itp->itp_flags |= IPSEC_PROTECT_L2;
690
691 /*
692 * add to the tunnel DB for ingress
693 * - if the SA is in trasnport mode, then the packates will arrivw
694 * with the IP src,dst of the protected tunnel, in which case we can
695 * simply strip the IP header and hand the payload to the protocol
696 * appropriate input handler
697 * - if the SA is in tunnel mode then there are two IP headers present
698 * one for the crytpo tunnel endpoints (described in the SA) and one
699 * for the tunnel endpoints. The outer IP headers in the srriving
700 * packets will have the crypto endpoints. So the DB needs to contain
701 * the crpto endpoint. Once the crypto header is stripped, revealing,
702 * the tunnel-IP we have 2 choices:
703 * 1) do a tunnel lookup based on the revealed header
704 * 2) skip the tunnel lookup and assume that the packet matches the
705 * one that is protected here.
706 * If we did 1) then we would allow our peer to use the SA for tunnel
707 * X to inject traffic onto tunnel Y, this is not good. If we do 2)
708 * then we don't verify that the peer is indeed using SA for tunnel
709 * X and addressing tunnel X. So we take a compromise, once the SA
710 * matches to tunnel X we veriy that the inner IP matches the value
711 * of the tunnel we are protecting, else it's dropped.
712 */
713 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800714 }
715 else
716 {
717 /* updating SAs only */
Neale Ranns28287212019-12-16 00:53:11 +0000718 itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800719
720 ipsec_tun_protect_unconfig (im, itp);
721 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
722 }
723
Neale Ranns12989b52019-09-26 16:20:19 +0000724 ipsec_sa_unlock (sa_out);
725 vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800726 vec_free (sas_in);
Neale Ranns12989b52019-09-26 16:20:19 +0000727
Neale Rannsc87b66c2019-02-07 07:26:12 -0800728out:
729 return (rv);
730}
731
732int
Neale Ranns28287212019-12-16 00:53:11 +0000733ipsec_tun_protect_del (u32 sw_if_index, const ip_address_t * nh)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800734{
735 ipsec_tun_protect_t *itp;
736 ipsec_main_t *im;
737 index_t itpi;
738
Neale Ranns28287212019-12-16 00:53:11 +0000739 ITP_DBG2 ("delete: %U/%U",
740 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
741 format_ip_address, nh);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800742
Neale Ranns28287212019-12-16 00:53:11 +0000743 im = &ipsec_main;
744 if (NULL == nh)
745 nh = &IP_ADDR_ALL_0;
746
747 itpi = ipsec_tun_protect_find (sw_if_index, nh);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800748
749 if (INDEX_INVALID == itpi)
750 return (VNET_API_ERROR_NO_SUCH_ENTRY);
751
752 itp = ipsec_tun_protect_get (itpi);
753 ipsec_tun_protect_unconfig (im, itp);
754
Neale Ranns28287212019-12-16 00:53:11 +0000755 clib_mem_free (itp->itp_key);
756 pool_put (ipsec_tun_protect_pool, itp);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800757
Neale Rannsc87b66c2019-02-07 07:26:12 -0800758 return (0);
759}
760
761void
762ipsec_tun_protect_walk (ipsec_tun_protect_walk_cb_t fn, void *ctx)
763{
764 index_t itpi;
765
766 /* *INDENT-OFF* */
Neale Ranns28287212019-12-16 00:53:11 +0000767 pool_foreach_index(itpi, ipsec_tun_protect_pool,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800768 ({
769 fn (itpi, ctx);
770 }));
771 /* *INDENT-ON* */
772}
773
Neale Ranns28287212019-12-16 00:53:11 +0000774void
775ipsec_tun_protect_walk_itf (u32 sw_if_index,
776 ipsec_tun_protect_walk_cb_t fn, void *ctx)
777{
778 ipsec_tun_protect_itf_db_t *idi;
779 ip_address_t *key;
780 index_t itpi;
781
782 if (vec_len (itp_db.id_itf) <= sw_if_index)
783 return;
784
785 idi = &itp_db.id_itf[sw_if_index];
786
787 /* *INDENT-OFF* */
788 hash_foreach(key, itpi, idi->id_hash,
789 ({
790 fn (itpi, ctx);
791 }));
792 /* *INDENT-ON* */
793 if (INDEX_INVALID != idi->id_itp)
794 fn (idi->id_itp, ctx);
795}
796
797static void
798ipsec_tun_protect_adj_delegate_adj_deleted (adj_delegate_t * ad)
799{
800 /* remove our delegate */
Neale Ranns4ec36c52020-03-31 09:21:29 -0400801 ipsec_tun_protect_add_adj (ad->ad_adj_index, NULL);
BenoƮt Ganneea9bc282020-04-16 12:40:04 +0200802 adj_delegate_remove (ad->ad_adj_index, ipsec_tun_adj_delegate_type);
Neale Ranns28287212019-12-16 00:53:11 +0000803}
804
805static void
Neale Ranns4ec36c52020-03-31 09:21:29 -0400806ipsec_tun_protect_adj_delegate_adj_modified (adj_delegate_t * ad)
807{
808 ipsec_tun_protect_add_adj (ad->ad_adj_index,
809 ipsec_tun_protect_get (ad->ad_index));
810}
811
812static void
Neale Ranns28287212019-12-16 00:53:11 +0000813ipsec_tun_protect_adj_delegate_adj_created (adj_index_t ai)
814{
815 /* add our delegate if there is protection for this neighbour */
816 ip_address_t ip = IP_ADDRESS_V4_ALL_0S;
817 ip_adjacency_t *adj;
818 index_t itpi;
819
Neale Ranns4ec36c52020-03-31 09:21:29 -0400820 if (!adj_is_midchain (ai))
Neale Ranns28287212019-12-16 00:53:11 +0000821 return;
822
Neale Ranns4ec36c52020-03-31 09:21:29 -0400823 adj = adj_get (ai);
824
Neale Ranns28287212019-12-16 00:53:11 +0000825 ip_address_from_46 (&adj->sub_type.midchain.next_hop,
826 adj->ia_nh_proto, &ip);
827
828 itpi = ipsec_tun_protect_find (adj->rewrite_header.sw_if_index, &ip);
829
830 if (INDEX_INVALID != itpi)
831 {
832 const ipsec_tun_protect_t *itp;
833
834 itp = ipsec_tun_protect_get (itpi);
835 adj_delegate_add (adj_get (ai), ipsec_tun_adj_delegate_type, itpi);
Neale Ranns4ec36c52020-03-31 09:21:29 -0400836 ipsec_tun_protect_add_adj (ai, itp);
Neale Ranns28287212019-12-16 00:53:11 +0000837 }
838}
839
840static u8 *
841ipsec_tun_protect_adj_delegate_format (const adj_delegate_t * aed, u8 * s)
842{
843 const ipsec_tun_protect_t *itp;
844
845 itp = ipsec_tun_protect_from_const_base (aed);
846 s = format (s, "ipsec-tun-protect:\n%U", format_ipsec_tun_protect, itp);
847
848 return (s);
849}
850
851static void
852ipsec_tun_teib_entry_added (const teib_entry_t * ne)
853{
854 const ip46_address_t *peer46;
855 ipsec_tun_protect_t *itp;
856 ip_address_t peer;
857 index_t itpi;
858
859 peer46 = teib_entry_get_peer (ne);
860 ip_address_from_46 (peer46,
861 (ip46_address_is_ip4 (peer46) ?
862 FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6), &peer);
863
864 itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne), &peer);
865
866 if (INDEX_INVALID == itpi)
867 return;
868
869 itp = ipsec_tun_protect_get (itpi);
870 ipsec_tun_protect_rx_db_remove (&ipsec_main, itp);
871 ipsec_tun_protect_update_from_teib (itp, ne);
872 ipsec_tun_protect_set_crypto_addr (itp);
873 ipsec_tun_protect_rx_db_add (&ipsec_main, itp);
874
875 ITP_DBG (itp, "teib-added");
876}
877
878static void
879ipsec_tun_teib_entry_deleted (const teib_entry_t * ne)
880{
881 const ip46_address_t *peer46;
882 ipsec_tun_protect_t *itp;
883 ip_address_t peer;
884 index_t itpi;
885
886 peer46 = teib_entry_get_peer (ne);
887 ip_address_from_46 (peer46,
888 (ip46_address_is_ip4 (peer46) ?
889 FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6), &peer);
890
891 itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne), &peer);
892
893 if (INDEX_INVALID == itpi)
894 return;
895
896 itp = ipsec_tun_protect_get (itpi);
897 ipsec_tun_protect_rx_db_remove (&ipsec_main, itp);
898 ipsec_tun_protect_update_from_teib (itp, NULL);
899 ipsec_tun_protect_set_crypto_addr (itp);
900
901 ITP_DBG (itp, "teib-removed");
902}
903
904/**
905 * VFT registered with the adjacency delegate
906 */
907const static adj_delegate_vft_t ipsec_tun_adj_delegate_vft = {
908 .adv_adj_deleted = ipsec_tun_protect_adj_delegate_adj_deleted,
909 .adv_adj_created = ipsec_tun_protect_adj_delegate_adj_created,
Neale Ranns4ec36c52020-03-31 09:21:29 -0400910 .adv_adj_modified = ipsec_tun_protect_adj_delegate_adj_modified,
Neale Ranns28287212019-12-16 00:53:11 +0000911 .adv_format = ipsec_tun_protect_adj_delegate_format,
912};
913
914const static teib_vft_t ipsec_tun_teib_vft = {
915 .nv_added = ipsec_tun_teib_entry_added,
916 .nv_deleted = ipsec_tun_teib_entry_deleted,
917};
918
Neale Ranns4ec36c52020-03-31 09:21:29 -0400919
Neale Rannsc87b66c2019-02-07 07:26:12 -0800920clib_error_t *
921ipsec_tunnel_protect_init (vlib_main_t * vm)
922{
923 ipsec_main_t *im;
924
925 im = &ipsec_main;
926 im->tun6_protect_by_key = hash_create_mem (0,
927 sizeof (ipsec6_tunnel_key_t),
928 sizeof (u64));
929 im->tun4_protect_by_key = hash_create (0, sizeof (u64));
930
Neale Ranns02950402019-12-20 00:54:57 +0000931 /* set up feature nodes to drop outbound packets with no crypto alg set */
Neale Ranns4ec36c52020-03-31 09:21:29 -0400932 im->esp4_no_crypto_tun_node_index =
933 vlib_get_node_by_name (vm, (u8 *) "esp4-no-crypto")->index;
934 im->esp6_no_crypto_tun_node_index =
935 vlib_get_node_by_name (vm, (u8 *) "esp6-no-crypto")->index;
936 im->esp6_encrypt_l2_tun_node_index =
937 vlib_get_node_by_name (vm, (u8 *) "esp6-encrypt-tun")->index;
938 im->esp4_encrypt_l2_tun_node_index =
939 vlib_get_node_by_name (vm, (u8 *) "esp4-encrypt-tun")->index;
Neale Ranns02950402019-12-20 00:54:57 +0000940
Neale Ranns28287212019-12-16 00:53:11 +0000941 ipsec_tun_adj_delegate_type =
942 adj_delegate_register_new_type (&ipsec_tun_adj_delegate_vft);
943
944 ipsec_tun_protect_logger = vlib_log_register_class ("ipsec", "tun");
945
946 teib_register (&ipsec_tun_teib_vft);
947
Neale Rannsc87b66c2019-02-07 07:26:12 -0800948 return 0;
949}
950
951VLIB_INIT_FUNCTION (ipsec_tunnel_protect_init);
952
953
954/*
955 * fd.io coding-style-patch-verification: ON
956 *
957 * Local Variables:
958 * eval: (c-set-style "gnu")
959 * End:
960 */