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