blob: 07dd9ea409bf22abe1bdc3d495db38a46cd0ae07 [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>
22#include <vnet/teib/teib.h>
23
24/**
25 * The logger
26 */
27vlib_log_class_t ipsec_tun_protect_logger;
Neale Rannsc87b66c2019-02-07 07:26:12 -080028
29/**
30 * Pool of tunnel protection objects
31 */
Neale Ranns28287212019-12-16 00:53:11 +000032ipsec_tun_protect_t *ipsec_tun_protect_pool;
Neale Rannsc87b66c2019-02-07 07:26:12 -080033
34/**
Neale Ranns28287212019-12-16 00:53:11 +000035 * Adj delegate registered type
Neale Rannsc87b66c2019-02-07 07:26:12 -080036 */
Neale Ranns28287212019-12-16 00:53:11 +000037static adj_delegate_type_t ipsec_tun_adj_delegate_type;
Neale Rannsc87b66c2019-02-07 07:26:12 -080038
Neale Ranns28287212019-12-16 00:53:11 +000039/**
40 * Adj index to TX SA mapping
41 */
42index_t *ipsec_tun_protect_sa_by_adj_index;
43
44const ip_address_t IP_ADDR_ALL_0 = IP_ADDRESS_V4_ALL_0S;
45
46/**
47 * The DB of all added per-nh tunnel protectiond
48 */
49typedef struct ipsec_tun_protect_itf_db_t_
50{
51 /** A hash table key'd on IP (4 or 6) address */
52 uword *id_hash;
53 /** If the interface is P2P then there is only one protect
54 * object associated with the auto-adj for each NH proto */
55 index_t id_itp;
56} ipsec_tun_protect_itf_db_t;
57
58typedef struct ipsec_tun_protect_db_t_
59{
60 /** Per-interface vector */
61 ipsec_tun_protect_itf_db_t *id_itf;
62} ipsec_tun_protect_db_t;
63
64static ipsec_tun_protect_db_t itp_db;
65
66const static ipsec_tun_protect_itf_db_t IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY = {
67 .id_itp = INDEX_INVALID,
68};
69
70#define ITP_DBG(_itp, _fmt, _args...) \
71{ \
72 vlib_log_debug(ipsec_tun_protect_logger, \
73 "[%U]: " _fmt, \
74 format_ipsec_tun_protect, \
75 _itp, ##_args); \
76}
77
78#define ITP_DBG2(_fmt, _args...) \
79{ \
80 vlib_log_debug(ipsec_tun_protect_logger, \
81 _fmt, ##_args); \
82}
83
Neale Ranns8d6d74c2020-02-20 09:45:16 +000084static u32 ipsec_tun_node_regs[N_AF];
85
86void
87ipsec_tun_register_nodes (ip_address_family_t af)
88{
89 if (0 == ipsec_tun_node_regs[af]++)
90 {
91 if (AF_IP4 == af)
92 {
93 udp_register_dst_port (vlib_get_main (),
94 UDP_DST_PORT_ipsec,
95 ipsec4_tun_input_node.index, 1);
96 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 {
113 udp_unregister_dst_port (vlib_get_main (), UDP_DST_PORT_ipsec, 1);
114 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 void
122ipsec_tun_protect_add_adj (adj_index_t ai, index_t sai)
123{
124 vec_validate_init_empty (ipsec_tun_protect_sa_by_adj_index, ai,
125 INDEX_INVALID);
126
127 ipsec_tun_protect_sa_by_adj_index[ai] = sai;
128}
129
130static inline const ipsec_tun_protect_t *
131ipsec_tun_protect_from_const_base (const adj_delegate_t * ad)
132{
133 if (ad == NULL)
134 return (NULL);
135 return (pool_elt_at_index (ipsec_tun_protect_pool, ad->ad_index));
136}
Neale Rannsc87b66c2019-02-07 07:26:12 -0800137
Neale Ranns02950402019-12-20 00:54:57 +0000138static void
Neale Rannsc87b66c2019-02-07 07:26:12 -0800139ipsec_tun_protect_feature_set (ipsec_tun_protect_t * itp, u8 enable)
140{
Neale Ranns28287212019-12-16 00:53:11 +0000141 ITP_DBG2 ("%s on %U", (enable ? "enable" : "disable"),
142 format_vnet_sw_if_index_name, vnet_get_main (),
143 itp->itp_sw_if_index);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800144
Neale Rannsb3259832019-09-27 13:32:02 +0000145 if (itp->itp_flags & IPSEC_PROTECT_L2)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800146 {
Neale Ranns02950402019-12-20 00:54:57 +0000147 /* l2-GRE only supported by the vnet ipsec code */
148 vnet_feature_enable_disable ("ethernet-output",
149 (ip46_address_is_ip4 (&itp->itp_tun.src) ?
150 "esp4-encrypt-tun" :
151 "esp6-encrypt-tun"),
Neale Ranns28287212019-12-16 00:53:11 +0000152 itp->itp_sw_if_index, enable, NULL, 0);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800153 }
154 else
155 {
Neale Ranns28287212019-12-16 00:53:11 +0000156 u32 fi4, fi6, sai;
Neale Ranns02950402019-12-20 00:54:57 +0000157 ipsec_main_t *im;
158 ipsec_sa_t *sa;
Neale Ranns02950402019-12-20 00:54:57 +0000159
160 im = &ipsec_main;
Neale Ranns28287212019-12-16 00:53:11 +0000161 sai = itp->itp_out_sa;
Neale Ranns02950402019-12-20 00:54:57 +0000162 sa = ipsec_sa_get (sai);
163
164 if (sa->crypto_alg == IPSEC_CRYPTO_ALG_NONE &&
165 sa->integ_alg == IPSEC_INTEG_ALG_NONE)
166 {
167 fi4 = im->esp4_no_crypto_tun_feature_index;
168 fi6 = im->esp6_no_crypto_tun_feature_index;
169 }
170 else
171 {
172 if (ip46_address_is_ip4 (&itp->itp_tun.src))
173 {
174 /* tunnel destination is v4 so we need the Xo4 indexes */
175 fi4 = im->esp44_encrypt_tun_feature_index;
176 fi6 = im->esp64_encrypt_tun_feature_index;
177 }
178 else
179 {
180 /* tunnel destination is v6 so we need the Xo6 indexes */
181 fi4 = im->esp46_encrypt_tun_feature_index;
182 fi6 = im->esp66_encrypt_tun_feature_index;
183 }
184 }
185
186 vnet_feature_enable_disable_with_index
187 (vnet_get_feature_arc_index ("ip4-output"),
Neale Ranns28287212019-12-16 00:53:11 +0000188 fi4, itp->itp_sw_if_index, enable, NULL, 0);
Neale Ranns02950402019-12-20 00:54:57 +0000189 vnet_feature_enable_disable_with_index
190 (vnet_get_feature_arc_index ("ip6-output"),
Neale Ranns28287212019-12-16 00:53:11 +0000191 fi6, itp->itp_sw_if_index, enable, NULL, 0);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800192 }
Neale Rannsc87b66c2019-02-07 07:26:12 -0800193}
194
Neale Ranns28287212019-12-16 00:53:11 +0000195static index_t
196ipsec_tun_protect_find (u32 sw_if_index, const ip_address_t * nh)
197{
198 ipsec_tun_protect_itf_db_t *idi;
199 uword *p;
200
201 if (vec_len (itp_db.id_itf) <= sw_if_index)
202 return INDEX_INVALID;
203
204 if (vnet_sw_interface_is_p2p (vnet_get_main (), sw_if_index))
205 return (itp_db.id_itf[sw_if_index].id_itp);
206
207 idi = &itp_db.id_itf[sw_if_index];
208 p = hash_get_mem (idi->id_hash, nh);
209
210 if (NULL == p)
211 {
212 return INDEX_INVALID;
213 }
214 return (p[0]);
215}
216
Neale Rannsc87b66c2019-02-07 07:26:12 -0800217static void
Neale Ranns28287212019-12-16 00:53:11 +0000218ipsec_tun_protect_rx_db_add (ipsec_main_t * im,
219 const ipsec_tun_protect_t * itp)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800220{
221 const ipsec_sa_t *sa;
222 u32 sai;
223
Neale Ranns28287212019-12-16 00:53:11 +0000224 if (ip46_address_is_zero (&itp->itp_crypto.dst))
225 return;
226
Neale Rannsc87b66c2019-02-07 07:26:12 -0800227 /* *INDENT-OFF* */
228 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
229 ({
230 sa = ipsec_sa_get (sai);
231
232 ipsec_tun_lkup_result_t res = {
Neale Ranns28287212019-12-16 00:53:11 +0000233 .tun_index = itp - ipsec_tun_protect_pool,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800234 .sa_index = sai,
235 };
236
237 /*
238 * The key is formed from the tunnel's destination
239 * as the packet lookup is done from the packet's source
240 */
241 if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
242 {
243 ipsec4_tunnel_key_t key = {
Neale Ranns41afb332019-07-16 06:19:35 -0700244 .remote_ip = itp->itp_crypto.dst.ip4,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800245 .spi = clib_host_to_net_u32 (sa->spi),
246 };
247 hash_set (im->tun4_protect_by_key, key.as_u64, res.as_u64);
Neale Ranns8d6d74c2020-02-20 09:45:16 +0000248 ipsec_tun_register_nodes(AF_IP4);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800249 }
250 else
251 {
252 ipsec6_tunnel_key_t key = {
253 .remote_ip = itp->itp_crypto.dst.ip6,
254 .spi = clib_host_to_net_u32 (sa->spi),
255 };
256 hash_set_mem_alloc (&im->tun6_protect_by_key, &key, res.as_u64);
Neale Ranns8d6d74c2020-02-20 09:45:16 +0000257 ipsec_tun_register_nodes(AF_IP6);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800258 }
259 }))
260 /* *INDENT-ON* */
261}
262
Neale Ranns28287212019-12-16 00:53:11 +0000263static adj_walk_rc_t
264ipsec_tun_protect_adj_add (adj_index_t ai, void *arg)
265{
266 ipsec_tun_protect_t *itp = arg;
267 adj_delegate_add (adj_get (ai), ipsec_tun_adj_delegate_type,
268 itp - ipsec_tun_protect_pool);
269 ipsec_tun_protect_add_adj (ai, itp->itp_out_sa);
270
271 return (ADJ_WALK_RC_CONTINUE);
272}
273
Neale Rannsc87b66c2019-02-07 07:26:12 -0800274static void
Neale Ranns28287212019-12-16 00:53:11 +0000275ipsec_tun_protect_tx_db_add (ipsec_tun_protect_t * itp)
276{
277 /*
278 * add the delegate to the adj
279 */
280 ipsec_tun_protect_itf_db_t *idi;
281 fib_protocol_t nh_proto;
282 ip46_address_t nh;
283
284 vec_validate_init_empty (itp_db.id_itf,
285 itp->itp_sw_if_index,
286 IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY);
287
288 idi = &itp_db.id_itf[itp->itp_sw_if_index];
289
290 if (vnet_sw_interface_is_p2p (vnet_get_main (), itp->itp_sw_if_index))
291 {
292 if (INDEX_INVALID == idi->id_itp)
293 {
294 ipsec_tun_protect_feature_set (itp, 1);
295 }
296 idi->id_itp = itp - ipsec_tun_protect_pool;
297
298 FOR_EACH_FIB_IP_PROTOCOL (nh_proto)
299 adj_nbr_walk (itp->itp_sw_if_index,
300 nh_proto, ipsec_tun_protect_adj_add, itp);
301 }
302 else
303 {
304 if (NULL == idi->id_hash)
305 {
306 idi->id_hash =
307 hash_create_mem (0, sizeof (ip_address_t), sizeof (uword));
308 /*
309 * enable the encrypt feature for egress if this is the first addition
310 * on this interface
311 */
312 ipsec_tun_protect_feature_set (itp, 1);
313 }
314
315 hash_set_mem (idi->id_hash, itp->itp_key, itp - ipsec_tun_protect_pool);
316
317 /*
318 * walk all the adjs with the same nh on this interface
319 * to associate them with this protection
320 */
321 nh_proto = ip_address_to_46 (itp->itp_key, &nh);
322
323 adj_nbr_walk_nh (itp->itp_sw_if_index,
324 nh_proto, &nh, ipsec_tun_protect_adj_add, itp);
Neale Ranns8d6d74c2020-02-20 09:45:16 +0000325
326 ipsec_tun_register_nodes (FIB_PROTOCOL_IP6 == nh_proto ?
327 AF_IP6 : AF_IP4);
Neale Ranns28287212019-12-16 00:53:11 +0000328 }
329}
330
331static void
332ipsec_tun_protect_rx_db_remove (ipsec_main_t * im,
333 const ipsec_tun_protect_t * itp)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800334{
335 const ipsec_sa_t *sa;
336
337 /* *INDENT-OFF* */
338 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
339 ({
340 if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
341 {
342 ipsec4_tunnel_key_t key = {
Neale Ranns41afb332019-07-16 06:19:35 -0700343 .remote_ip = itp->itp_crypto.dst.ip4,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800344 .spi = clib_host_to_net_u32 (sa->spi),
345 };
Neale Ranns8d6d74c2020-02-20 09:45:16 +0000346 if (hash_get(im->tun4_protect_by_key, key.as_u64))
347 {
348 hash_unset (im->tun4_protect_by_key, key.as_u64);
349 ipsec_tun_unregister_nodes(AF_IP4);
350 }
Neale Rannsc87b66c2019-02-07 07:26:12 -0800351 }
352 else
353 {
354 ipsec6_tunnel_key_t key = {
355 .remote_ip = itp->itp_crypto.dst.ip6,
356 .spi = clib_host_to_net_u32 (sa->spi),
357 };
Neale Ranns8d6d74c2020-02-20 09:45:16 +0000358 if (hash_get_mem(im->tun6_protect_by_key, &key))
359 {
360 hash_unset_mem_free (&im->tun6_protect_by_key, &key);
361 ipsec_tun_unregister_nodes(AF_IP6);
362 }
Neale Rannsc87b66c2019-02-07 07:26:12 -0800363 }
364 }))
365 /* *INDENT-ON* */
366}
367
Neale Ranns28287212019-12-16 00:53:11 +0000368static adj_walk_rc_t
369ipsec_tun_protect_adj_remove (adj_index_t ai, void *arg)
370{
371 adj_delegate_remove (ai, ipsec_tun_adj_delegate_type);
372 ipsec_tun_protect_add_adj (ai, INDEX_INVALID);
373
374 return (ADJ_WALK_RC_CONTINUE);
375}
376
Neale Rannsc87b66c2019-02-07 07:26:12 -0800377static void
Neale Ranns28287212019-12-16 00:53:11 +0000378ipsec_tun_protect_tx_db_remove (ipsec_tun_protect_t * itp)
379{
380 ipsec_tun_protect_itf_db_t *idi;
381 fib_protocol_t nh_proto;
382 ip46_address_t nh;
383
384 nh_proto = ip_address_to_46 (itp->itp_key, &nh);
385 idi = &itp_db.id_itf[itp->itp_sw_if_index];
386
387 if (vnet_sw_interface_is_p2p (vnet_get_main (), itp->itp_sw_if_index))
388 {
389 ipsec_tun_protect_feature_set (itp, 0);
390 idi->id_itp = INDEX_INVALID;
391
392 FOR_EACH_FIB_IP_PROTOCOL (nh_proto)
393 adj_nbr_walk (itp->itp_sw_if_index,
394 nh_proto, ipsec_tun_protect_adj_remove, itp);
395 }
396 else
397 {
398 adj_nbr_walk_nh (itp->itp_sw_if_index,
399 nh_proto, &nh, ipsec_tun_protect_adj_remove, itp);
400
401 hash_unset_mem (idi->id_hash, itp->itp_key);
402
403 if (0 == hash_elts (idi->id_hash))
404 {
405 ipsec_tun_protect_feature_set (itp, 0);
406 hash_free (idi->id_hash);
407 idi->id_hash = NULL;
408 }
Neale Ranns8d6d74c2020-02-20 09:45:16 +0000409 ipsec_tun_unregister_nodes (FIB_PROTOCOL_IP6 == nh_proto ?
410 AF_IP6 : AF_IP4);
Neale Ranns28287212019-12-16 00:53:11 +0000411 }
412}
413
414static void
415ipsec_tun_protect_set_crypto_addr (ipsec_tun_protect_t * itp)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800416{
417 ipsec_sa_t *sa;
Neale Ranns12989b52019-09-26 16:20:19 +0000418
Neale Rannsc87b66c2019-02-07 07:26:12 -0800419 /* *INDENT-OFF* */
420 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
421 ({
422 if (ipsec_sa_is_set_IS_TUNNEL (sa))
423 {
424 itp->itp_crypto.src = sa->tunnel_dst_addr;
425 itp->itp_crypto.dst = sa->tunnel_src_addr;
426 ipsec_sa_set_IS_PROTECT (sa);
427 itp->itp_flags |= IPSEC_PROTECT_ENCAPED;
428 }
429 else
430 {
431 itp->itp_crypto.src = itp->itp_tun.src;
432 itp->itp_crypto.dst = itp->itp_tun.dst;
433 itp->itp_flags &= ~IPSEC_PROTECT_ENCAPED;
434 }
435 }));
436 /* *INDENT-ON* */
Neale Ranns28287212019-12-16 00:53:11 +0000437}
438
439static void
440ipsec_tun_protect_config (ipsec_main_t * im,
441 ipsec_tun_protect_t * itp, u32 sa_out, u32 * sas_in)
442{
443 index_t sai;
444 u32 ii;
445
446 itp->itp_n_sa_in = vec_len (sas_in);
447 for (ii = 0; ii < itp->itp_n_sa_in; ii++)
448 itp->itp_in_sas[ii] = sas_in[ii];
449 itp->itp_out_sa = sa_out;
450
451 ipsec_sa_lock (itp->itp_out_sa);
452
453 /* *INDENT-OFF* */
454 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
455 ({
456 ipsec_sa_lock(sai);
457 }));
458 ipsec_tun_protect_set_crypto_addr(itp);
459 /* *INDENT-ON* */
Neale Rannsc87b66c2019-02-07 07:26:12 -0800460
461 /*
462 * add to the DB against each SA
463 */
Neale Ranns28287212019-12-16 00:53:11 +0000464 ipsec_tun_protect_rx_db_add (im, itp);
465 ipsec_tun_protect_tx_db_add (itp);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800466
Neale Ranns28287212019-12-16 00:53:11 +0000467 ITP_DBG (itp, "configured");
Neale Rannsc87b66c2019-02-07 07:26:12 -0800468}
469
470static void
471ipsec_tun_protect_unconfig (ipsec_main_t * im, ipsec_tun_protect_t * itp)
472{
473 ipsec_sa_t *sa;
Neale Ranns495d7ff2019-07-12 09:15:26 +0000474 index_t sai;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800475
Neale Rannsc87b66c2019-02-07 07:26:12 -0800476 /* *INDENT-OFF* */
477 FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
478 ({
479 ipsec_sa_unset_IS_PROTECT (sa);
480 }));
Neale Rannsc87b66c2019-02-07 07:26:12 -0800481
Neale Ranns28287212019-12-16 00:53:11 +0000482 ipsec_tun_protect_rx_db_remove (im, itp);
483 ipsec_tun_protect_tx_db_remove (itp);
Neale Ranns495d7ff2019-07-12 09:15:26 +0000484
485 ipsec_sa_unlock(itp->itp_out_sa);
486
487 FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
488 ({
489 ipsec_sa_unlock(sai);
490 }));
491 /* *INDENT-ON* */
Neale Ranns28287212019-12-16 00:53:11 +0000492 ITP_DBG (itp, "unconfigured");
Neale Rannsc87b66c2019-02-07 07:26:12 -0800493}
494
495int
Neale Ranns28287212019-12-16 00:53:11 +0000496ipsec_tun_protect_update_one (u32 sw_if_index,
497 const ip_address_t * nh, u32 sa_out, u32 sa_in)
Neale Ranns12989b52019-09-26 16:20:19 +0000498{
499 u32 *sas_in = NULL;
500 int rv;
501
502 vec_add1 (sas_in, sa_in);
Neale Ranns28287212019-12-16 00:53:11 +0000503 rv = ipsec_tun_protect_update (sw_if_index, nh, sa_out, sas_in);
Neale Ranns12989b52019-09-26 16:20:19 +0000504
505 return (rv);
506}
507
508int
Neale Ranns28287212019-12-16 00:53:11 +0000509ipsec_tun_protect_update_out (u32 sw_if_index,
510 const ip_address_t * nh, u32 sa_out)
Neale Ranns12989b52019-09-26 16:20:19 +0000511{
512 u32 itpi, *sas_in, sai, *saip;
513 ipsec_tun_protect_t *itp;
514 ipsec_main_t *im;
515 int rv;
516
517 sas_in = NULL;
518 rv = 0;
519 im = &ipsec_main;
Neale Ranns28287212019-12-16 00:53:11 +0000520
521 itpi = ipsec_tun_protect_find (sw_if_index, nh);
Neale Ranns12989b52019-09-26 16:20:19 +0000522
523 if (INDEX_INVALID == itpi)
524 {
525 return (VNET_API_ERROR_INVALID_INTERFACE);
526 }
527
Neale Ranns28287212019-12-16 00:53:11 +0000528 itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
Neale Ranns12989b52019-09-26 16:20:19 +0000529
Neale Ranns28287212019-12-16 00:53:11 +0000530 /* *INDENT-OFF* */
531 FOR_EACH_IPSEC_PROTECT_INPUT_SAI (itp, sai,
532 ({
533 ipsec_sa_lock (sai);
534 vec_add1 (sas_in, sai);
535 }));
Neale Ranns12989b52019-09-26 16:20:19 +0000536 /* *INDENT-ON* */
537
538 sa_out = ipsec_sa_find_and_lock (sa_out);
539
540 if (~0 == sa_out)
541 {
542 rv = VNET_API_ERROR_INVALID_VALUE;
543 goto out;
544 }
545
546 ipsec_tun_protect_unconfig (im, itp);
547 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
548
549 ipsec_sa_unlock (sa_out);
550 vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
551
552out:
553 vec_free (sas_in);
554 return (rv);
555}
556
557int
Neale Ranns28287212019-12-16 00:53:11 +0000558ipsec_tun_protect_update_in (u32 sw_if_index,
559 const ip_address_t * nh, u32 sa_in)
Neale Ranns12989b52019-09-26 16:20:19 +0000560{
561 u32 itpi, *sas_in, sa_out;
562 ipsec_tun_protect_t *itp;
563 ipsec_main_t *im;
564 int rv;
565
566 sas_in = NULL;
567 rv = 0;
568 im = &ipsec_main;
Neale Ranns28287212019-12-16 00:53:11 +0000569 itpi = ipsec_tun_protect_find (sw_if_index, nh);
Neale Ranns12989b52019-09-26 16:20:19 +0000570
571 if (INDEX_INVALID == itpi)
572 {
573 return (VNET_API_ERROR_INVALID_INTERFACE);
574 }
575
576 sa_in = ipsec_sa_find_and_lock (sa_in);
577
578 if (~0 == sa_in)
579 {
580 rv = VNET_API_ERROR_INVALID_VALUE;
581 goto out;
582 }
583 vec_add1 (sas_in, sa_in);
584
Neale Ranns28287212019-12-16 00:53:11 +0000585 itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
Neale Ranns12989b52019-09-26 16:20:19 +0000586 sa_out = itp->itp_out_sa;
587
588 ipsec_sa_lock (sa_out);
589
590 ipsec_tun_protect_unconfig (im, itp);
591 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
592
593 ipsec_sa_unlock (sa_out);
594 ipsec_sa_unlock (sa_in);
595out:
596 vec_free (sas_in);
597 return (rv);
598}
599
Neale Ranns28287212019-12-16 00:53:11 +0000600static void
601ipsec_tun_protect_update_from_teib (ipsec_tun_protect_t * itp,
602 const teib_entry_t * ne)
603{
604 if (NULL != ne)
605 {
606 const fib_prefix_t *pfx;
607
608 pfx = teib_entry_get_nh (ne);
609
610 ip46_address_copy (&itp->itp_tun.dst, &pfx->fp_addr);
611 }
612 else
613 ip46_address_reset (&itp->itp_tun.dst);
614}
615
Neale Ranns12989b52019-09-26 16:20:19 +0000616int
Neale Ranns28287212019-12-16 00:53:11 +0000617ipsec_tun_protect_update (u32 sw_if_index,
618 const ip_address_t * nh, u32 sa_out, u32 * sas_in)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800619{
Neale Rannsc87b66c2019-02-07 07:26:12 -0800620 ipsec_tun_protect_t *itp;
Neale Ranns12989b52019-09-26 16:20:19 +0000621 u32 itpi, ii, *saip;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800622 ipsec_main_t *im;
623 int rv;
624
Neale Ranns28287212019-12-16 00:53:11 +0000625 ITP_DBG2 ("update: %U/%U",
626 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
627 format_ip_address, nh);
628
Neale Rannsc87b66c2019-02-07 07:26:12 -0800629 rv = 0;
630 im = &ipsec_main;
Neale Ranns28287212019-12-16 00:53:11 +0000631 if (NULL == nh)
632 nh = &IP_ADDR_ALL_0;
633 itpi = ipsec_tun_protect_find (sw_if_index, nh);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800634
635 vec_foreach_index (ii, sas_in)
636 {
Neale Ranns495d7ff2019-07-12 09:15:26 +0000637 sas_in[ii] = ipsec_sa_find_and_lock (sas_in[ii]);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800638 if (~0 == sas_in[ii])
639 {
640 rv = VNET_API_ERROR_INVALID_VALUE;
641 goto out;
642 }
643 }
644
Neale Ranns495d7ff2019-07-12 09:15:26 +0000645 sa_out = ipsec_sa_find_and_lock (sa_out);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800646
647 if (~0 == sa_out)
648 {
649 rv = VNET_API_ERROR_INVALID_VALUE;
650 goto out;
651 }
652
653 if (INDEX_INVALID == itpi)
654 {
655 vnet_device_class_t *dev_class;
656 vnet_hw_interface_t *hi;
657 vnet_main_t *vnm;
658 u8 is_l2;
659
660 vnm = vnet_get_main ();
661 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
662 dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
663
664 if (NULL == dev_class->ip_tun_desc)
665 {
666 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
667 goto out;
668 }
669
Neale Ranns28287212019-12-16 00:53:11 +0000670 pool_get_zero (ipsec_tun_protect_pool, itp);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800671
672 itp->itp_sw_if_index = sw_if_index;
Neale Rannsc87b66c2019-02-07 07:26:12 -0800673
674 itp->itp_n_sa_in = vec_len (sas_in);
675 for (ii = 0; ii < itp->itp_n_sa_in; ii++)
676 itp->itp_in_sas[ii] = sas_in[ii];
677 itp->itp_out_sa = sa_out;
678
Neale Ranns28287212019-12-16 00:53:11 +0000679 itp->itp_key = clib_mem_alloc (sizeof (*itp->itp_key));
680 ip_address_copy (itp->itp_key, nh);
681
Neale Rannsc87b66c2019-02-07 07:26:12 -0800682 rv = dev_class->ip_tun_desc (sw_if_index,
683 &itp->itp_tun.src,
684 &itp->itp_tun.dst, &is_l2);
685
686 if (rv)
687 goto out;
688
Neale Ranns28287212019-12-16 00:53:11 +0000689 if (ip46_address_is_zero (&itp->itp_tun.dst))
690 {
691 /* tunnel has no destination address, presumably because it's p2mp
692 in which case we use the nh that this is protection for */
693 ip46_address_t peer;
694
695 ip_address_to_46 (nh, &peer);
696
697 ipsec_tun_protect_update_from_teib
698 (itp, teib_entry_find (sw_if_index, &peer));
699 }
700
Neale Rannsc87b66c2019-02-07 07:26:12 -0800701 if (is_l2)
702 itp->itp_flags |= IPSEC_PROTECT_L2;
703
704 /*
705 * add to the tunnel DB for ingress
706 * - if the SA is in trasnport mode, then the packates will arrivw
707 * with the IP src,dst of the protected tunnel, in which case we can
708 * simply strip the IP header and hand the payload to the protocol
709 * appropriate input handler
710 * - if the SA is in tunnel mode then there are two IP headers present
711 * one for the crytpo tunnel endpoints (described in the SA) and one
712 * for the tunnel endpoints. The outer IP headers in the srriving
713 * packets will have the crypto endpoints. So the DB needs to contain
714 * the crpto endpoint. Once the crypto header is stripped, revealing,
715 * the tunnel-IP we have 2 choices:
716 * 1) do a tunnel lookup based on the revealed header
717 * 2) skip the tunnel lookup and assume that the packet matches the
718 * one that is protected here.
719 * If we did 1) then we would allow our peer to use the SA for tunnel
720 * X to inject traffic onto tunnel Y, this is not good. If we do 2)
721 * then we don't verify that the peer is indeed using SA for tunnel
722 * X and addressing tunnel X. So we take a compromise, once the SA
723 * matches to tunnel X we veriy that the inner IP matches the value
724 * of the tunnel we are protecting, else it's dropped.
725 */
726 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800727 }
728 else
729 {
730 /* updating SAs only */
Neale Ranns28287212019-12-16 00:53:11 +0000731 itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800732
733 ipsec_tun_protect_unconfig (im, itp);
734 ipsec_tun_protect_config (im, itp, sa_out, sas_in);
735 }
736
Neale Ranns12989b52019-09-26 16:20:19 +0000737 ipsec_sa_unlock (sa_out);
738 vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800739 vec_free (sas_in);
Neale Ranns12989b52019-09-26 16:20:19 +0000740
Neale Rannsc87b66c2019-02-07 07:26:12 -0800741out:
742 return (rv);
743}
744
745int
Neale Ranns28287212019-12-16 00:53:11 +0000746ipsec_tun_protect_del (u32 sw_if_index, const ip_address_t * nh)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800747{
748 ipsec_tun_protect_t *itp;
749 ipsec_main_t *im;
750 index_t itpi;
751
Neale Ranns28287212019-12-16 00:53:11 +0000752 ITP_DBG2 ("delete: %U/%U",
753 format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
754 format_ip_address, nh);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800755
Neale Ranns28287212019-12-16 00:53:11 +0000756 im = &ipsec_main;
757 if (NULL == nh)
758 nh = &IP_ADDR_ALL_0;
759
760 itpi = ipsec_tun_protect_find (sw_if_index, nh);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800761
762 if (INDEX_INVALID == itpi)
763 return (VNET_API_ERROR_NO_SUCH_ENTRY);
764
765 itp = ipsec_tun_protect_get (itpi);
766 ipsec_tun_protect_unconfig (im, itp);
767
Neale Ranns28287212019-12-16 00:53:11 +0000768 clib_mem_free (itp->itp_key);
769 pool_put (ipsec_tun_protect_pool, itp);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800770
Neale Rannsc87b66c2019-02-07 07:26:12 -0800771 return (0);
772}
773
774void
775ipsec_tun_protect_walk (ipsec_tun_protect_walk_cb_t fn, void *ctx)
776{
777 index_t itpi;
778
779 /* *INDENT-OFF* */
Neale Ranns28287212019-12-16 00:53:11 +0000780 pool_foreach_index(itpi, ipsec_tun_protect_pool,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800781 ({
782 fn (itpi, ctx);
783 }));
784 /* *INDENT-ON* */
785}
786
Neale Ranns28287212019-12-16 00:53:11 +0000787void
788ipsec_tun_protect_walk_itf (u32 sw_if_index,
789 ipsec_tun_protect_walk_cb_t fn, void *ctx)
790{
791 ipsec_tun_protect_itf_db_t *idi;
792 ip_address_t *key;
793 index_t itpi;
794
795 if (vec_len (itp_db.id_itf) <= sw_if_index)
796 return;
797
798 idi = &itp_db.id_itf[sw_if_index];
799
800 /* *INDENT-OFF* */
801 hash_foreach(key, itpi, idi->id_hash,
802 ({
803 fn (itpi, ctx);
804 }));
805 /* *INDENT-ON* */
806 if (INDEX_INVALID != idi->id_itp)
807 fn (idi->id_itp, ctx);
808}
809
810static void
811ipsec_tun_protect_adj_delegate_adj_deleted (adj_delegate_t * ad)
812{
813 /* remove our delegate */
814 adj_delegate_remove (ad->ad_adj_index, ipsec_tun_adj_delegate_type);
815 ipsec_tun_protect_add_adj (ad->ad_adj_index, INDEX_INVALID);
816}
817
818static void
819ipsec_tun_protect_adj_delegate_adj_created (adj_index_t ai)
820{
821 /* add our delegate if there is protection for this neighbour */
822 ip_address_t ip = IP_ADDRESS_V4_ALL_0S;
823 ip_adjacency_t *adj;
824 index_t itpi;
825
826 adj = adj_get (ai);
827
828 if (adj->lookup_next_index != IP_LOOKUP_NEXT_MIDCHAIN)
829 return;
830
831 ip_address_from_46 (&adj->sub_type.midchain.next_hop,
832 adj->ia_nh_proto, &ip);
833
834 itpi = ipsec_tun_protect_find (adj->rewrite_header.sw_if_index, &ip);
835
836 if (INDEX_INVALID != itpi)
837 {
838 const ipsec_tun_protect_t *itp;
839
840 itp = ipsec_tun_protect_get (itpi);
841 adj_delegate_add (adj_get (ai), ipsec_tun_adj_delegate_type, itpi);
842 ipsec_tun_protect_add_adj (ai, itp->itp_out_sa);
843 }
844}
845
846static u8 *
847ipsec_tun_protect_adj_delegate_format (const adj_delegate_t * aed, u8 * s)
848{
849 const ipsec_tun_protect_t *itp;
850
851 itp = ipsec_tun_protect_from_const_base (aed);
852 s = format (s, "ipsec-tun-protect:\n%U", format_ipsec_tun_protect, itp);
853
854 return (s);
855}
856
857static void
858ipsec_tun_teib_entry_added (const teib_entry_t * ne)
859{
860 const ip46_address_t *peer46;
861 ipsec_tun_protect_t *itp;
862 ip_address_t peer;
863 index_t itpi;
864
865 peer46 = teib_entry_get_peer (ne);
866 ip_address_from_46 (peer46,
867 (ip46_address_is_ip4 (peer46) ?
868 FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6), &peer);
869
870 itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne), &peer);
871
872 if (INDEX_INVALID == itpi)
873 return;
874
875 itp = ipsec_tun_protect_get (itpi);
876 ipsec_tun_protect_rx_db_remove (&ipsec_main, itp);
877 ipsec_tun_protect_update_from_teib (itp, ne);
878 ipsec_tun_protect_set_crypto_addr (itp);
879 ipsec_tun_protect_rx_db_add (&ipsec_main, itp);
880
881 ITP_DBG (itp, "teib-added");
882}
883
884static void
885ipsec_tun_teib_entry_deleted (const teib_entry_t * ne)
886{
887 const ip46_address_t *peer46;
888 ipsec_tun_protect_t *itp;
889 ip_address_t peer;
890 index_t itpi;
891
892 peer46 = teib_entry_get_peer (ne);
893 ip_address_from_46 (peer46,
894 (ip46_address_is_ip4 (peer46) ?
895 FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6), &peer);
896
897 itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne), &peer);
898
899 if (INDEX_INVALID == itpi)
900 return;
901
902 itp = ipsec_tun_protect_get (itpi);
903 ipsec_tun_protect_rx_db_remove (&ipsec_main, itp);
904 ipsec_tun_protect_update_from_teib (itp, NULL);
905 ipsec_tun_protect_set_crypto_addr (itp);
906
907 ITP_DBG (itp, "teib-removed");
908}
909
910/**
911 * VFT registered with the adjacency delegate
912 */
913const static adj_delegate_vft_t ipsec_tun_adj_delegate_vft = {
914 .adv_adj_deleted = ipsec_tun_protect_adj_delegate_adj_deleted,
915 .adv_adj_created = ipsec_tun_protect_adj_delegate_adj_created,
916 .adv_format = ipsec_tun_protect_adj_delegate_format,
917};
918
919const static teib_vft_t ipsec_tun_teib_vft = {
920 .nv_added = ipsec_tun_teib_entry_added,
921 .nv_deleted = ipsec_tun_teib_entry_deleted,
922};
923
Neale Rannsc87b66c2019-02-07 07:26:12 -0800924clib_error_t *
925ipsec_tunnel_protect_init (vlib_main_t * vm)
926{
927 ipsec_main_t *im;
928
929 im = &ipsec_main;
930 im->tun6_protect_by_key = hash_create_mem (0,
931 sizeof (ipsec6_tunnel_key_t),
932 sizeof (u64));
933 im->tun4_protect_by_key = hash_create (0, sizeof (u64));
934
Neale Ranns02950402019-12-20 00:54:57 +0000935 /* set up feature nodes to drop outbound packets with no crypto alg set */
936 ipsec_add_feature ("ip4-output", "esp4-no-crypto",
937 &im->esp4_no_crypto_tun_feature_index);
938 ipsec_add_feature ("ip6-output", "esp6-no-crypto",
939 &im->esp6_no_crypto_tun_feature_index);
940
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 */