769229721f69c1711d0853304fb834d88a006b58
[vpp.git] / src / vnet / ipsec / ipsec_tun.c
1 /*
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>
21 #include <vnet/adj/adj_delegate.h>
22 #include <vnet/teib/teib.h>
23
24 /**
25  * The logger
26  */
27 vlib_log_class_t ipsec_tun_protect_logger;
28
29 /**
30  * Pool of tunnel protection objects
31  */
32 ipsec_tun_protect_t *ipsec_tun_protect_pool;
33
34 /**
35  * Adj delegate registered type
36  */
37 static adj_delegate_type_t ipsec_tun_adj_delegate_type;
38
39 /**
40  * Adj index to TX SA mapping
41  */
42 index_t *ipsec_tun_protect_sa_by_adj_index;
43
44 const 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  */
49 typedef 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
58 typedef 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
64 static ipsec_tun_protect_db_t itp_db;
65
66 const 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
84 static void
85 ipsec_tun_protect_add_adj (adj_index_t ai, index_t sai)
86 {
87   vec_validate_init_empty (ipsec_tun_protect_sa_by_adj_index, ai,
88                            INDEX_INVALID);
89
90   ipsec_tun_protect_sa_by_adj_index[ai] = sai;
91 }
92
93 static inline const ipsec_tun_protect_t *
94 ipsec_tun_protect_from_const_base (const adj_delegate_t * ad)
95 {
96   if (ad == NULL)
97     return (NULL);
98   return (pool_elt_at_index (ipsec_tun_protect_pool, ad->ad_index));
99 }
100
101 static void
102 ipsec_tun_protect_feature_set (ipsec_tun_protect_t * itp, u8 enable)
103 {
104   ITP_DBG2 ("%s on %U", (enable ? "enable" : "disable"),
105             format_vnet_sw_if_index_name, vnet_get_main (),
106             itp->itp_sw_if_index);
107
108   if (itp->itp_flags & IPSEC_PROTECT_L2)
109     {
110       /* l2-GRE only supported by the vnet ipsec code */
111       vnet_feature_enable_disable ("ethernet-output",
112                                    (ip46_address_is_ip4 (&itp->itp_tun.src) ?
113                                     "esp4-encrypt-tun" :
114                                     "esp6-encrypt-tun"),
115                                    itp->itp_sw_if_index, enable, NULL, 0);
116     }
117   else
118     {
119       u32 fi4, fi6, sai;
120       ipsec_main_t *im;
121       ipsec_sa_t *sa;
122
123       im = &ipsec_main;
124       sai = itp->itp_out_sa;
125       sa = ipsec_sa_get (sai);
126
127       if (sa->crypto_alg == IPSEC_CRYPTO_ALG_NONE &&
128           sa->integ_alg == IPSEC_INTEG_ALG_NONE)
129         {
130           fi4 = im->esp4_no_crypto_tun_feature_index;
131           fi6 = im->esp6_no_crypto_tun_feature_index;
132         }
133       else
134         {
135           if (ip46_address_is_ip4 (&itp->itp_tun.src))
136             {
137               /* tunnel destination is v4 so we need the Xo4 indexes */
138               fi4 = im->esp44_encrypt_tun_feature_index;
139               fi6 = im->esp64_encrypt_tun_feature_index;
140             }
141           else
142             {
143               /* tunnel destination is v6 so we need the Xo6 indexes */
144               fi4 = im->esp46_encrypt_tun_feature_index;
145               fi6 = im->esp66_encrypt_tun_feature_index;
146             }
147         }
148
149       vnet_feature_enable_disable_with_index
150         (vnet_get_feature_arc_index ("ip4-output"),
151          fi4, itp->itp_sw_if_index, enable, NULL, 0);
152       vnet_feature_enable_disable_with_index
153         (vnet_get_feature_arc_index ("ip6-output"),
154          fi6, itp->itp_sw_if_index, enable, NULL, 0);
155     }
156 }
157
158 static index_t
159 ipsec_tun_protect_find (u32 sw_if_index, const ip_address_t * nh)
160 {
161   ipsec_tun_protect_itf_db_t *idi;
162   uword *p;
163
164   if (vec_len (itp_db.id_itf) <= sw_if_index)
165     return INDEX_INVALID;
166
167   if (vnet_sw_interface_is_p2p (vnet_get_main (), sw_if_index))
168     return (itp_db.id_itf[sw_if_index].id_itp);
169
170   idi = &itp_db.id_itf[sw_if_index];
171   p = hash_get_mem (idi->id_hash, nh);
172
173   if (NULL == p)
174     {
175       return INDEX_INVALID;
176     }
177   return (p[0]);
178 }
179
180 static void
181 ipsec_tun_protect_rx_db_add (ipsec_main_t * im,
182                              const ipsec_tun_protect_t * itp)
183 {
184   const ipsec_sa_t *sa;
185   u32 sai;
186
187   if (ip46_address_is_zero (&itp->itp_crypto.dst))
188     return;
189
190   /* *INDENT-OFF* */
191   FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
192   ({
193       sa = ipsec_sa_get (sai);
194
195       ipsec_tun_lkup_result_t res = {
196         .tun_index = itp - ipsec_tun_protect_pool,
197         .sa_index = sai,
198       };
199
200       /*
201        * The key is formed from the tunnel's destination
202        * as the packet lookup is done from the packet's source
203        */
204       if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
205         {
206           ipsec4_tunnel_key_t key = {
207             .remote_ip = itp->itp_crypto.dst.ip4,
208             .spi = clib_host_to_net_u32 (sa->spi),
209           };
210           hash_set (im->tun4_protect_by_key, key.as_u64, res.as_u64);
211           if (1 == hash_elts(im->tun4_protect_by_key))
212             {
213               udp_register_dst_port (vlib_get_main(),
214                                      UDP_DST_PORT_ipsec,
215                                      ipsec4_tun_input_node.index, 1);
216               ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
217                                      ipsec4_tun_input_node.index);
218             }
219         }
220       else
221         {
222           ipsec6_tunnel_key_t key = {
223             .remote_ip = itp->itp_crypto.dst.ip6,
224             .spi = clib_host_to_net_u32 (sa->spi),
225           };
226           hash_set_mem_alloc (&im->tun6_protect_by_key, &key, res.as_u64);
227
228           if (1 == hash_elts (im->tun6_protect_by_key))
229             ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP,
230                                    ipsec6_tun_input_node.index);
231         }
232   }))
233   /* *INDENT-ON* */
234 }
235
236 static adj_walk_rc_t
237 ipsec_tun_protect_adj_add (adj_index_t ai, void *arg)
238 {
239   ipsec_tun_protect_t *itp = arg;
240   adj_delegate_add (adj_get (ai), ipsec_tun_adj_delegate_type,
241                     itp - ipsec_tun_protect_pool);
242   ipsec_tun_protect_add_adj (ai, itp->itp_out_sa);
243
244   return (ADJ_WALK_RC_CONTINUE);
245 }
246
247 static void
248 ipsec_tun_protect_tx_db_add (ipsec_tun_protect_t * itp)
249 {
250   /*
251    * add the delegate to the adj
252    */
253   ipsec_tun_protect_itf_db_t *idi;
254   fib_protocol_t nh_proto;
255   ip46_address_t nh;
256
257   vec_validate_init_empty (itp_db.id_itf,
258                            itp->itp_sw_if_index,
259                            IPSEC_TUN_PROTECT_DEFAULT_DB_ENTRY);
260
261   idi = &itp_db.id_itf[itp->itp_sw_if_index];
262
263   if (vnet_sw_interface_is_p2p (vnet_get_main (), itp->itp_sw_if_index))
264     {
265       if (INDEX_INVALID == idi->id_itp)
266         {
267           ipsec_tun_protect_feature_set (itp, 1);
268         }
269       idi->id_itp = itp - ipsec_tun_protect_pool;
270
271       FOR_EACH_FIB_IP_PROTOCOL (nh_proto)
272         adj_nbr_walk (itp->itp_sw_if_index,
273                       nh_proto, ipsec_tun_protect_adj_add, itp);
274     }
275   else
276     {
277       if (NULL == idi->id_hash)
278         {
279           idi->id_hash =
280             hash_create_mem (0, sizeof (ip_address_t), sizeof (uword));
281           /*
282            * enable the encrypt feature for egress if this is the first addition
283            * on this interface
284            */
285           ipsec_tun_protect_feature_set (itp, 1);
286         }
287
288       hash_set_mem (idi->id_hash, itp->itp_key, itp - ipsec_tun_protect_pool);
289
290       /*
291        * walk all the adjs with the same nh on this interface
292        * to associate them with this protection
293        */
294       nh_proto = ip_address_to_46 (itp->itp_key, &nh);
295
296       adj_nbr_walk_nh (itp->itp_sw_if_index,
297                        nh_proto, &nh, ipsec_tun_protect_adj_add, itp);
298     }
299 }
300
301 static void
302 ipsec_tun_protect_rx_db_remove (ipsec_main_t * im,
303                                 const ipsec_tun_protect_t * itp)
304 {
305   const ipsec_sa_t *sa;
306
307   /* *INDENT-OFF* */
308   FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
309   ({
310       if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
311         {
312           ipsec4_tunnel_key_t key = {
313             .remote_ip = itp->itp_crypto.dst.ip4,
314             .spi = clib_host_to_net_u32 (sa->spi),
315           };
316           hash_unset (im->tun4_protect_by_key, key.as_u64);
317           if (0 == hash_elts(im->tun4_protect_by_key))
318             udp_unregister_dst_port (vlib_get_main(),
319                                      UDP_DST_PORT_ipsec,
320                                      1);
321         }
322       else
323         {
324           ipsec6_tunnel_key_t key = {
325             .remote_ip = itp->itp_crypto.dst.ip6,
326             .spi = clib_host_to_net_u32 (sa->spi),
327           };
328           hash_unset_mem_free (&im->tun6_protect_by_key, &key);
329         }
330   }))
331   /* *INDENT-ON* */
332 }
333
334 static adj_walk_rc_t
335 ipsec_tun_protect_adj_remove (adj_index_t ai, void *arg)
336 {
337   adj_delegate_remove (ai, ipsec_tun_adj_delegate_type);
338   ipsec_tun_protect_add_adj (ai, INDEX_INVALID);
339
340   return (ADJ_WALK_RC_CONTINUE);
341 }
342
343 static void
344 ipsec_tun_protect_tx_db_remove (ipsec_tun_protect_t * itp)
345 {
346   ipsec_tun_protect_itf_db_t *idi;
347   fib_protocol_t nh_proto;
348   ip46_address_t nh;
349
350   nh_proto = ip_address_to_46 (itp->itp_key, &nh);
351   idi = &itp_db.id_itf[itp->itp_sw_if_index];
352
353   if (vnet_sw_interface_is_p2p (vnet_get_main (), itp->itp_sw_if_index))
354     {
355       ipsec_tun_protect_feature_set (itp, 0);
356       idi->id_itp = INDEX_INVALID;
357
358       FOR_EACH_FIB_IP_PROTOCOL (nh_proto)
359         adj_nbr_walk (itp->itp_sw_if_index,
360                       nh_proto, ipsec_tun_protect_adj_remove, itp);
361     }
362   else
363     {
364       adj_nbr_walk_nh (itp->itp_sw_if_index,
365                        nh_proto, &nh, ipsec_tun_protect_adj_remove, itp);
366
367       hash_unset_mem (idi->id_hash, itp->itp_key);
368
369       if (0 == hash_elts (idi->id_hash))
370         {
371           ipsec_tun_protect_feature_set (itp, 0);
372           hash_free (idi->id_hash);
373           idi->id_hash = NULL;
374         }
375     }
376 }
377
378 static void
379 ipsec_tun_protect_set_crypto_addr (ipsec_tun_protect_t * itp)
380 {
381   ipsec_sa_t *sa;
382
383   /* *INDENT-OFF* */
384   FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
385   ({
386     if (ipsec_sa_is_set_IS_TUNNEL (sa))
387       {
388         itp->itp_crypto.src = sa->tunnel_dst_addr;
389         itp->itp_crypto.dst = sa->tunnel_src_addr;
390         ipsec_sa_set_IS_PROTECT (sa);
391         itp->itp_flags |= IPSEC_PROTECT_ENCAPED;
392       }
393     else
394       {
395         itp->itp_crypto.src = itp->itp_tun.src;
396         itp->itp_crypto.dst = itp->itp_tun.dst;
397         itp->itp_flags &= ~IPSEC_PROTECT_ENCAPED;
398       }
399   }));
400   /* *INDENT-ON* */
401 }
402
403 static void
404 ipsec_tun_protect_config (ipsec_main_t * im,
405                           ipsec_tun_protect_t * itp, u32 sa_out, u32 * sas_in)
406 {
407   index_t sai;
408   u32 ii;
409
410   itp->itp_n_sa_in = vec_len (sas_in);
411   for (ii = 0; ii < itp->itp_n_sa_in; ii++)
412     itp->itp_in_sas[ii] = sas_in[ii];
413   itp->itp_out_sa = sa_out;
414
415   ipsec_sa_lock (itp->itp_out_sa);
416
417   /* *INDENT-OFF* */
418   FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
419   ({
420     ipsec_sa_lock(sai);
421   }));
422   ipsec_tun_protect_set_crypto_addr(itp);
423   /* *INDENT-ON* */
424
425   /*
426    * add to the DB against each SA
427    */
428   ipsec_tun_protect_rx_db_add (im, itp);
429   ipsec_tun_protect_tx_db_add (itp);
430
431   ITP_DBG (itp, "configured");
432 }
433
434 static void
435 ipsec_tun_protect_unconfig (ipsec_main_t * im, ipsec_tun_protect_t * itp)
436 {
437   ipsec_sa_t *sa;
438   index_t sai;
439
440   /* *INDENT-OFF* */
441   FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
442   ({
443     ipsec_sa_unset_IS_PROTECT (sa);
444   }));
445
446   ipsec_tun_protect_rx_db_remove (im, itp);
447   ipsec_tun_protect_tx_db_remove (itp);
448
449   ipsec_sa_unlock(itp->itp_out_sa);
450
451   FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
452   ({
453     ipsec_sa_unlock(sai);
454   }));
455   /* *INDENT-ON* */
456   ITP_DBG (itp, "unconfigured");
457 }
458
459 int
460 ipsec_tun_protect_update_one (u32 sw_if_index,
461                               const ip_address_t * nh, u32 sa_out, u32 sa_in)
462 {
463   u32 *sas_in = NULL;
464   int rv;
465
466   vec_add1 (sas_in, sa_in);
467   rv = ipsec_tun_protect_update (sw_if_index, nh, sa_out, sas_in);
468
469   return (rv);
470 }
471
472 int
473 ipsec_tun_protect_update_out (u32 sw_if_index,
474                               const ip_address_t * nh, u32 sa_out)
475 {
476   u32 itpi, *sas_in, sai, *saip;
477   ipsec_tun_protect_t *itp;
478   ipsec_main_t *im;
479   int rv;
480
481   sas_in = NULL;
482   rv = 0;
483   im = &ipsec_main;
484
485   itpi = ipsec_tun_protect_find (sw_if_index, nh);
486
487   if (INDEX_INVALID == itpi)
488     {
489       return (VNET_API_ERROR_INVALID_INTERFACE);
490     }
491
492   itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
493
494   /* *INDENT-OFF* */
495   FOR_EACH_IPSEC_PROTECT_INPUT_SAI (itp, sai,
496   ({
497     ipsec_sa_lock (sai);
498     vec_add1 (sas_in, sai);
499   }));
500   /* *INDENT-ON* */
501
502   sa_out = ipsec_sa_find_and_lock (sa_out);
503
504   if (~0 == sa_out)
505     {
506       rv = VNET_API_ERROR_INVALID_VALUE;
507       goto out;
508     }
509
510   ipsec_tun_protect_unconfig (im, itp);
511   ipsec_tun_protect_config (im, itp, sa_out, sas_in);
512
513   ipsec_sa_unlock (sa_out);
514   vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
515
516 out:
517   vec_free (sas_in);
518   return (rv);
519 }
520
521 int
522 ipsec_tun_protect_update_in (u32 sw_if_index,
523                              const ip_address_t * nh, u32 sa_in)
524 {
525   u32 itpi, *sas_in, sa_out;
526   ipsec_tun_protect_t *itp;
527   ipsec_main_t *im;
528   int rv;
529
530   sas_in = NULL;
531   rv = 0;
532   im = &ipsec_main;
533   itpi = ipsec_tun_protect_find (sw_if_index, nh);
534
535   if (INDEX_INVALID == itpi)
536     {
537       return (VNET_API_ERROR_INVALID_INTERFACE);
538     }
539
540   sa_in = ipsec_sa_find_and_lock (sa_in);
541
542   if (~0 == sa_in)
543     {
544       rv = VNET_API_ERROR_INVALID_VALUE;
545       goto out;
546     }
547   vec_add1 (sas_in, sa_in);
548
549   itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
550   sa_out = itp->itp_out_sa;
551
552   ipsec_sa_lock (sa_out);
553
554   ipsec_tun_protect_unconfig (im, itp);
555   ipsec_tun_protect_config (im, itp, sa_out, sas_in);
556
557   ipsec_sa_unlock (sa_out);
558   ipsec_sa_unlock (sa_in);
559 out:
560   vec_free (sas_in);
561   return (rv);
562 }
563
564 static void
565 ipsec_tun_protect_update_from_teib (ipsec_tun_protect_t * itp,
566                                     const teib_entry_t * ne)
567 {
568   if (NULL != ne)
569     {
570       const fib_prefix_t *pfx;
571
572       pfx = teib_entry_get_nh (ne);
573
574       ip46_address_copy (&itp->itp_tun.dst, &pfx->fp_addr);
575     }
576   else
577     ip46_address_reset (&itp->itp_tun.dst);
578 }
579
580 int
581 ipsec_tun_protect_update (u32 sw_if_index,
582                           const ip_address_t * nh, u32 sa_out, u32 * sas_in)
583 {
584   ipsec_tun_protect_t *itp;
585   u32 itpi, ii, *saip;
586   ipsec_main_t *im;
587   int rv;
588
589   ITP_DBG2 ("update: %U/%U",
590             format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
591             format_ip_address, nh);
592
593   rv = 0;
594   im = &ipsec_main;
595   if (NULL == nh)
596     nh = &IP_ADDR_ALL_0;
597   itpi = ipsec_tun_protect_find (sw_if_index, nh);
598
599   vec_foreach_index (ii, sas_in)
600   {
601     sas_in[ii] = ipsec_sa_find_and_lock (sas_in[ii]);
602     if (~0 == sas_in[ii])
603       {
604         rv = VNET_API_ERROR_INVALID_VALUE;
605         goto out;
606       }
607   }
608
609   sa_out = ipsec_sa_find_and_lock (sa_out);
610
611   if (~0 == sa_out)
612     {
613       rv = VNET_API_ERROR_INVALID_VALUE;
614       goto out;
615     }
616
617   if (INDEX_INVALID == itpi)
618     {
619       vnet_device_class_t *dev_class;
620       vnet_hw_interface_t *hi;
621       vnet_main_t *vnm;
622       u8 is_l2;
623
624       vnm = vnet_get_main ();
625       hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
626       dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
627
628       if (NULL == dev_class->ip_tun_desc)
629         {
630           rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
631           goto out;
632         }
633
634       pool_get_zero (ipsec_tun_protect_pool, itp);
635
636       itp->itp_sw_if_index = sw_if_index;
637
638       itp->itp_n_sa_in = vec_len (sas_in);
639       for (ii = 0; ii < itp->itp_n_sa_in; ii++)
640         itp->itp_in_sas[ii] = sas_in[ii];
641       itp->itp_out_sa = sa_out;
642
643       itp->itp_key = clib_mem_alloc (sizeof (*itp->itp_key));
644       ip_address_copy (itp->itp_key, nh);
645
646       rv = dev_class->ip_tun_desc (sw_if_index,
647                                    &itp->itp_tun.src,
648                                    &itp->itp_tun.dst, &is_l2);
649
650       if (rv)
651         goto out;
652
653       if (ip46_address_is_zero (&itp->itp_tun.dst))
654         {
655           /* tunnel has no destination address, presumably because it's p2mp
656              in which case we use the nh that this is protection for */
657           ip46_address_t peer;
658
659           ip_address_to_46 (nh, &peer);
660
661           ipsec_tun_protect_update_from_teib
662             (itp, teib_entry_find (sw_if_index, &peer));
663         }
664
665       if (is_l2)
666         itp->itp_flags |= IPSEC_PROTECT_L2;
667
668       /*
669        * add to the tunnel DB for ingress
670        *  - if the SA is in trasnport mode, then the packates will arrivw
671        *    with the IP src,dst of the protected tunnel, in which case we can
672        *    simply strip the IP header and hand the payload to the protocol
673        *    appropriate input handler
674        *  - if the SA is in tunnel mode then there are two IP headers present
675        *    one for the crytpo tunnel endpoints (described in the SA) and one
676        *    for the tunnel endpoints. The outer IP headers in the srriving
677        *    packets will have the crypto endpoints. So the DB needs to contain
678        *    the crpto endpoint. Once the crypto header is stripped, revealing,
679        *    the tunnel-IP we have 2 choices:
680        *     1) do a tunnel lookup based on the revealed header
681        *     2) skip the tunnel lookup and assume that the packet matches the
682        *        one that is protected here.
683        *    If we did 1) then we would allow our peer to use the SA for tunnel
684        *    X to inject traffic onto tunnel Y, this is not good. If we do 2)
685        *    then we don't verify that the peer is indeed using SA for tunnel
686        *    X and addressing tunnel X. So we take a compromise, once the SA
687        *    matches to tunnel X we veriy that the inner IP matches the value
688        *    of the tunnel we are protecting, else it's dropped.
689        */
690       ipsec_tun_protect_config (im, itp, sa_out, sas_in);
691     }
692   else
693     {
694       /* updating SAs only */
695       itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
696
697       ipsec_tun_protect_unconfig (im, itp);
698       ipsec_tun_protect_config (im, itp, sa_out, sas_in);
699     }
700
701   ipsec_sa_unlock (sa_out);
702   vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
703   vec_free (sas_in);
704
705 out:
706   return (rv);
707 }
708
709 int
710 ipsec_tun_protect_del (u32 sw_if_index, const ip_address_t * nh)
711 {
712   ipsec_tun_protect_t *itp;
713   ipsec_main_t *im;
714   index_t itpi;
715
716   ITP_DBG2 ("delete: %U/%U",
717             format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
718             format_ip_address, nh);
719
720   im = &ipsec_main;
721   if (NULL == nh)
722     nh = &IP_ADDR_ALL_0;
723
724   itpi = ipsec_tun_protect_find (sw_if_index, nh);
725
726   if (INDEX_INVALID == itpi)
727     return (VNET_API_ERROR_NO_SUCH_ENTRY);
728
729   itp = ipsec_tun_protect_get (itpi);
730   ipsec_tun_protect_unconfig (im, itp);
731
732   clib_mem_free (itp->itp_key);
733   pool_put (ipsec_tun_protect_pool, itp);
734
735   if (0 == hash_elts (im->tun4_protect_by_key))
736     ip4_unregister_protocol (IP_PROTOCOL_IPSEC_ESP);
737   if (0 == hash_elts (im->tun6_protect_by_key))
738     ip6_unregister_protocol (IP_PROTOCOL_IPSEC_ESP);
739
740   return (0);
741 }
742
743 void
744 ipsec_tun_protect_walk (ipsec_tun_protect_walk_cb_t fn, void *ctx)
745 {
746   index_t itpi;
747
748   /* *INDENT-OFF* */
749   pool_foreach_index(itpi, ipsec_tun_protect_pool,
750   ({
751     fn (itpi, ctx);
752   }));
753   /* *INDENT-ON* */
754 }
755
756 void
757 ipsec_tun_protect_walk_itf (u32 sw_if_index,
758                             ipsec_tun_protect_walk_cb_t fn, void *ctx)
759 {
760   ipsec_tun_protect_itf_db_t *idi;
761   ip_address_t *key;
762   index_t itpi;
763
764   if (vec_len (itp_db.id_itf) <= sw_if_index)
765     return;
766
767   idi = &itp_db.id_itf[sw_if_index];
768
769   /* *INDENT-OFF* */
770   hash_foreach(key, itpi, idi->id_hash,
771   ({
772     fn (itpi, ctx);
773   }));
774   /* *INDENT-ON* */
775   if (INDEX_INVALID != idi->id_itp)
776     fn (idi->id_itp, ctx);
777 }
778
779 static void
780 ipsec_tun_protect_adj_delegate_adj_deleted (adj_delegate_t * ad)
781 {
782   /* remove our delegate */
783   adj_delegate_remove (ad->ad_adj_index, ipsec_tun_adj_delegate_type);
784   ipsec_tun_protect_add_adj (ad->ad_adj_index, INDEX_INVALID);
785 }
786
787 static void
788 ipsec_tun_protect_adj_delegate_adj_created (adj_index_t ai)
789 {
790   /* add our delegate if there is protection for this neighbour */
791   ip_address_t ip = IP_ADDRESS_V4_ALL_0S;
792   ip_adjacency_t *adj;
793   index_t itpi;
794
795   adj = adj_get (ai);
796
797   if (adj->lookup_next_index != IP_LOOKUP_NEXT_MIDCHAIN)
798     return;
799
800   ip_address_from_46 (&adj->sub_type.midchain.next_hop,
801                       adj->ia_nh_proto, &ip);
802
803   itpi = ipsec_tun_protect_find (adj->rewrite_header.sw_if_index, &ip);
804
805   if (INDEX_INVALID != itpi)
806     {
807       const ipsec_tun_protect_t *itp;
808
809       itp = ipsec_tun_protect_get (itpi);
810       adj_delegate_add (adj_get (ai), ipsec_tun_adj_delegate_type, itpi);
811       ipsec_tun_protect_add_adj (ai, itp->itp_out_sa);
812     }
813 }
814
815 static u8 *
816 ipsec_tun_protect_adj_delegate_format (const adj_delegate_t * aed, u8 * s)
817 {
818   const ipsec_tun_protect_t *itp;
819
820   itp = ipsec_tun_protect_from_const_base (aed);
821   s = format (s, "ipsec-tun-protect:\n%U", format_ipsec_tun_protect, itp);
822
823   return (s);
824 }
825
826 static void
827 ipsec_tun_teib_entry_added (const teib_entry_t * ne)
828 {
829   const ip46_address_t *peer46;
830   ipsec_tun_protect_t *itp;
831   ip_address_t peer;
832   index_t itpi;
833
834   peer46 = teib_entry_get_peer (ne);
835   ip_address_from_46 (peer46,
836                       (ip46_address_is_ip4 (peer46) ?
837                        FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6), &peer);
838
839   itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne), &peer);
840
841   if (INDEX_INVALID == itpi)
842     return;
843
844   itp = ipsec_tun_protect_get (itpi);
845   ipsec_tun_protect_rx_db_remove (&ipsec_main, itp);
846   ipsec_tun_protect_update_from_teib (itp, ne);
847   ipsec_tun_protect_set_crypto_addr (itp);
848   ipsec_tun_protect_rx_db_add (&ipsec_main, itp);
849
850   ITP_DBG (itp, "teib-added");
851 }
852
853 static void
854 ipsec_tun_teib_entry_deleted (const teib_entry_t * ne)
855 {
856   const ip46_address_t *peer46;
857   ipsec_tun_protect_t *itp;
858   ip_address_t peer;
859   index_t itpi;
860
861   peer46 = teib_entry_get_peer (ne);
862   ip_address_from_46 (peer46,
863                       (ip46_address_is_ip4 (peer46) ?
864                        FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6), &peer);
865
866   itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne), &peer);
867
868   if (INDEX_INVALID == itpi)
869     return;
870
871   itp = ipsec_tun_protect_get (itpi);
872   ipsec_tun_protect_rx_db_remove (&ipsec_main, itp);
873   ipsec_tun_protect_update_from_teib (itp, NULL);
874   ipsec_tun_protect_set_crypto_addr (itp);
875
876   ITP_DBG (itp, "teib-removed");
877 }
878
879 /**
880  * VFT registered with the adjacency delegate
881  */
882 const static adj_delegate_vft_t ipsec_tun_adj_delegate_vft = {
883   .adv_adj_deleted = ipsec_tun_protect_adj_delegate_adj_deleted,
884   .adv_adj_created = ipsec_tun_protect_adj_delegate_adj_created,
885   .adv_format = ipsec_tun_protect_adj_delegate_format,
886 };
887
888 const static teib_vft_t ipsec_tun_teib_vft = {
889   .nv_added = ipsec_tun_teib_entry_added,
890   .nv_deleted = ipsec_tun_teib_entry_deleted,
891 };
892
893 clib_error_t *
894 ipsec_tunnel_protect_init (vlib_main_t * vm)
895 {
896   ipsec_main_t *im;
897
898   im = &ipsec_main;
899   im->tun6_protect_by_key = hash_create_mem (0,
900                                              sizeof (ipsec6_tunnel_key_t),
901                                              sizeof (u64));
902   im->tun4_protect_by_key = hash_create (0, sizeof (u64));
903
904   /* set up feature nodes to drop outbound packets with no crypto alg set */
905   ipsec_add_feature ("ip4-output", "esp4-no-crypto",
906                      &im->esp4_no_crypto_tun_feature_index);
907   ipsec_add_feature ("ip6-output", "esp6-no-crypto",
908                      &im->esp6_no_crypto_tun_feature_index);
909
910   ipsec_tun_adj_delegate_type =
911     adj_delegate_register_new_type (&ipsec_tun_adj_delegate_vft);
912
913   ipsec_tun_protect_logger = vlib_log_register_class ("ipsec", "tun");
914
915   teib_register (&ipsec_tun_teib_vft);
916
917   return 0;
918 }
919
920 VLIB_INIT_FUNCTION (ipsec_tunnel_protect_init);
921
922
923 /*
924  * fd.io coding-style-patch-verification: ON
925  *
926  * Local Variables:
927  * eval: (c-set-style "gnu")
928  * End:
929  */