268f7783ebf363dba0994d1236d107d6c2853e64
[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 u32 ipsec_tun_node_regs[N_AF];
85
86 void
87 ipsec_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
105 void
106 ipsec_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
121 static void
122 ipsec_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
130 static inline const ipsec_tun_protect_t *
131 ipsec_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 }
137
138 static void
139 ipsec_tun_protect_feature_set (ipsec_tun_protect_t * itp, u8 enable)
140 {
141   ITP_DBG2 ("%s on %U", (enable ? "enable" : "disable"),
142             format_vnet_sw_if_index_name, vnet_get_main (),
143             itp->itp_sw_if_index);
144
145   if (itp->itp_flags & IPSEC_PROTECT_L2)
146     {
147       /* 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"),
152                                    itp->itp_sw_if_index, enable, NULL, 0);
153     }
154   else
155     {
156       u32 fi4, fi6, sai;
157       ipsec_main_t *im;
158       ipsec_sa_t *sa;
159
160       im = &ipsec_main;
161       sai = itp->itp_out_sa;
162       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"),
188          fi4, itp->itp_sw_if_index, enable, NULL, 0);
189       vnet_feature_enable_disable_with_index
190         (vnet_get_feature_arc_index ("ip6-output"),
191          fi6, itp->itp_sw_if_index, enable, NULL, 0);
192     }
193 }
194
195 static index_t
196 ipsec_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
217 static void
218 ipsec_tun_protect_rx_db_add (ipsec_main_t * im,
219                              const ipsec_tun_protect_t * itp)
220 {
221   const ipsec_sa_t *sa;
222   u32 sai;
223
224   if (ip46_address_is_zero (&itp->itp_crypto.dst))
225     return;
226
227   /* *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 = {
233         .tun_index = itp - ipsec_tun_protect_pool,
234         .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 = {
244             .remote_ip = itp->itp_crypto.dst.ip4,
245             .spi = clib_host_to_net_u32 (sa->spi),
246           };
247           hash_set (im->tun4_protect_by_key, key.as_u64, res.as_u64);
248           ipsec_tun_register_nodes(AF_IP4);
249         }
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);
257           ipsec_tun_register_nodes(AF_IP6);
258         }
259   }))
260   /* *INDENT-ON* */
261 }
262
263 static adj_walk_rc_t
264 ipsec_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
274 static void
275 ipsec_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);
325
326       ipsec_tun_register_nodes (FIB_PROTOCOL_IP6 == nh_proto ?
327                                 AF_IP6 : AF_IP4);
328     }
329 }
330
331 static void
332 ipsec_tun_protect_rx_db_remove (ipsec_main_t * im,
333                                 const ipsec_tun_protect_t * itp)
334 {
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 = {
343             .remote_ip = itp->itp_crypto.dst.ip4,
344             .spi = clib_host_to_net_u32 (sa->spi),
345           };
346           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             }
351         }
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           };
358           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             }
363         }
364   }))
365   /* *INDENT-ON* */
366 }
367
368 static adj_walk_rc_t
369 ipsec_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
377 static void
378 ipsec_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         }
409       ipsec_tun_unregister_nodes (FIB_PROTOCOL_IP6 == nh_proto ?
410                                   AF_IP6 : AF_IP4);
411     }
412 }
413
414 static void
415 ipsec_tun_protect_set_crypto_addr (ipsec_tun_protect_t * itp)
416 {
417   ipsec_sa_t *sa;
418
419   /* *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* */
437 }
438
439 static void
440 ipsec_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* */
460
461   /*
462    * add to the DB against each SA
463    */
464   ipsec_tun_protect_rx_db_add (im, itp);
465   ipsec_tun_protect_tx_db_add (itp);
466
467   ITP_DBG (itp, "configured");
468 }
469
470 static void
471 ipsec_tun_protect_unconfig (ipsec_main_t * im, ipsec_tun_protect_t * itp)
472 {
473   ipsec_sa_t *sa;
474   index_t sai;
475
476   /* *INDENT-OFF* */
477   FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
478   ({
479     ipsec_sa_unset_IS_PROTECT (sa);
480   }));
481
482   ipsec_tun_protect_rx_db_remove (im, itp);
483   ipsec_tun_protect_tx_db_remove (itp);
484
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* */
492   ITP_DBG (itp, "unconfigured");
493 }
494
495 int
496 ipsec_tun_protect_update_one (u32 sw_if_index,
497                               const ip_address_t * nh, u32 sa_out, u32 sa_in)
498 {
499   u32 *sas_in = NULL;
500   int rv;
501
502   vec_add1 (sas_in, sa_in);
503   rv = ipsec_tun_protect_update (sw_if_index, nh, sa_out, sas_in);
504
505   return (rv);
506 }
507
508 int
509 ipsec_tun_protect_update_out (u32 sw_if_index,
510                               const ip_address_t * nh, u32 sa_out)
511 {
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;
520
521   itpi = ipsec_tun_protect_find (sw_if_index, nh);
522
523   if (INDEX_INVALID == itpi)
524     {
525       return (VNET_API_ERROR_INVALID_INTERFACE);
526     }
527
528   itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
529
530   /* *INDENT-OFF* */
531   FOR_EACH_IPSEC_PROTECT_INPUT_SAI (itp, sai,
532   ({
533     ipsec_sa_lock (sai);
534     vec_add1 (sas_in, sai);
535   }));
536   /* *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
552 out:
553   vec_free (sas_in);
554   return (rv);
555 }
556
557 int
558 ipsec_tun_protect_update_in (u32 sw_if_index,
559                              const ip_address_t * nh, u32 sa_in)
560 {
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;
569   itpi = ipsec_tun_protect_find (sw_if_index, nh);
570
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
585   itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
586   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);
595 out:
596   vec_free (sas_in);
597   return (rv);
598 }
599
600 static void
601 ipsec_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
616 int
617 ipsec_tun_protect_update (u32 sw_if_index,
618                           const ip_address_t * nh, u32 sa_out, u32 * sas_in)
619 {
620   ipsec_tun_protect_t *itp;
621   u32 itpi, ii, *saip;
622   ipsec_main_t *im;
623   int rv;
624
625   ITP_DBG2 ("update: %U/%U",
626             format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
627             format_ip_address, nh);
628
629   if (vec_len (sas_in) > ITP_MAX_N_SA_IN)
630     {
631       rv = VNET_API_ERROR_LIMIT_EXCEEDED;
632       goto out;
633     }
634
635   rv = 0;
636   im = &ipsec_main;
637   if (NULL == nh)
638     nh = &IP_ADDR_ALL_0;
639   itpi = ipsec_tun_protect_find (sw_if_index, nh);
640
641   vec_foreach_index (ii, sas_in)
642   {
643     sas_in[ii] = ipsec_sa_find_and_lock (sas_in[ii]);
644     if (~0 == sas_in[ii])
645       {
646         rv = VNET_API_ERROR_INVALID_VALUE;
647         goto out;
648       }
649   }
650
651   sa_out = ipsec_sa_find_and_lock (sa_out);
652
653   if (~0 == sa_out)
654     {
655       rv = VNET_API_ERROR_INVALID_VALUE;
656       goto out;
657     }
658
659   if (INDEX_INVALID == itpi)
660     {
661       vnet_device_class_t *dev_class;
662       vnet_hw_interface_t *hi;
663       vnet_main_t *vnm;
664       u8 is_l2;
665
666       vnm = vnet_get_main ();
667       hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
668       dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
669
670       if (NULL == dev_class->ip_tun_desc)
671         {
672           rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
673           goto out;
674         }
675
676       pool_get_zero (ipsec_tun_protect_pool, itp);
677
678       itp->itp_sw_if_index = sw_if_index;
679
680       itp->itp_n_sa_in = vec_len (sas_in);
681       for (ii = 0; ii < itp->itp_n_sa_in; ii++)
682         itp->itp_in_sas[ii] = sas_in[ii];
683       itp->itp_out_sa = sa_out;
684
685       itp->itp_key = clib_mem_alloc (sizeof (*itp->itp_key));
686       ip_address_copy (itp->itp_key, nh);
687
688       rv = dev_class->ip_tun_desc (sw_if_index,
689                                    &itp->itp_tun.src,
690                                    &itp->itp_tun.dst, &is_l2);
691
692       if (rv)
693         goto out;
694
695       if (ip46_address_is_zero (&itp->itp_tun.dst))
696         {
697           /* tunnel has no destination address, presumably because it's p2mp
698              in which case we use the nh that this is protection for */
699           ip46_address_t peer;
700
701           ip_address_to_46 (nh, &peer);
702
703           ipsec_tun_protect_update_from_teib
704             (itp, teib_entry_find (sw_if_index, &peer));
705         }
706
707       if (is_l2)
708         itp->itp_flags |= IPSEC_PROTECT_L2;
709
710       /*
711        * add to the tunnel DB for ingress
712        *  - if the SA is in trasnport mode, then the packates will arrivw
713        *    with the IP src,dst of the protected tunnel, in which case we can
714        *    simply strip the IP header and hand the payload to the protocol
715        *    appropriate input handler
716        *  - if the SA is in tunnel mode then there are two IP headers present
717        *    one for the crytpo tunnel endpoints (described in the SA) and one
718        *    for the tunnel endpoints. The outer IP headers in the srriving
719        *    packets will have the crypto endpoints. So the DB needs to contain
720        *    the crpto endpoint. Once the crypto header is stripped, revealing,
721        *    the tunnel-IP we have 2 choices:
722        *     1) do a tunnel lookup based on the revealed header
723        *     2) skip the tunnel lookup and assume that the packet matches the
724        *        one that is protected here.
725        *    If we did 1) then we would allow our peer to use the SA for tunnel
726        *    X to inject traffic onto tunnel Y, this is not good. If we do 2)
727        *    then we don't verify that the peer is indeed using SA for tunnel
728        *    X and addressing tunnel X. So we take a compromise, once the SA
729        *    matches to tunnel X we veriy that the inner IP matches the value
730        *    of the tunnel we are protecting, else it's dropped.
731        */
732       ipsec_tun_protect_config (im, itp, sa_out, sas_in);
733     }
734   else
735     {
736       /* updating SAs only */
737       itp = pool_elt_at_index (ipsec_tun_protect_pool, itpi);
738
739       ipsec_tun_protect_unconfig (im, itp);
740       ipsec_tun_protect_config (im, itp, sa_out, sas_in);
741     }
742
743   ipsec_sa_unlock (sa_out);
744   vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
745   vec_free (sas_in);
746
747 out:
748   return (rv);
749 }
750
751 int
752 ipsec_tun_protect_del (u32 sw_if_index, const ip_address_t * nh)
753 {
754   ipsec_tun_protect_t *itp;
755   ipsec_main_t *im;
756   index_t itpi;
757
758   ITP_DBG2 ("delete: %U/%U",
759             format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index,
760             format_ip_address, nh);
761
762   im = &ipsec_main;
763   if (NULL == nh)
764     nh = &IP_ADDR_ALL_0;
765
766   itpi = ipsec_tun_protect_find (sw_if_index, nh);
767
768   if (INDEX_INVALID == itpi)
769     return (VNET_API_ERROR_NO_SUCH_ENTRY);
770
771   itp = ipsec_tun_protect_get (itpi);
772   ipsec_tun_protect_unconfig (im, itp);
773
774   clib_mem_free (itp->itp_key);
775   pool_put (ipsec_tun_protect_pool, itp);
776
777   return (0);
778 }
779
780 void
781 ipsec_tun_protect_walk (ipsec_tun_protect_walk_cb_t fn, void *ctx)
782 {
783   index_t itpi;
784
785   /* *INDENT-OFF* */
786   pool_foreach_index(itpi, ipsec_tun_protect_pool,
787   ({
788     fn (itpi, ctx);
789   }));
790   /* *INDENT-ON* */
791 }
792
793 void
794 ipsec_tun_protect_walk_itf (u32 sw_if_index,
795                             ipsec_tun_protect_walk_cb_t fn, void *ctx)
796 {
797   ipsec_tun_protect_itf_db_t *idi;
798   ip_address_t *key;
799   index_t itpi;
800
801   if (vec_len (itp_db.id_itf) <= sw_if_index)
802     return;
803
804   idi = &itp_db.id_itf[sw_if_index];
805
806   /* *INDENT-OFF* */
807   hash_foreach(key, itpi, idi->id_hash,
808   ({
809     fn (itpi, ctx);
810   }));
811   /* *INDENT-ON* */
812   if (INDEX_INVALID != idi->id_itp)
813     fn (idi->id_itp, ctx);
814 }
815
816 static void
817 ipsec_tun_protect_adj_delegate_adj_deleted (adj_delegate_t * ad)
818 {
819   /* remove our delegate */
820   adj_delegate_remove (ad->ad_adj_index, ipsec_tun_adj_delegate_type);
821   ipsec_tun_protect_add_adj (ad->ad_adj_index, INDEX_INVALID);
822 }
823
824 static void
825 ipsec_tun_protect_adj_delegate_adj_created (adj_index_t ai)
826 {
827   /* add our delegate if there is protection for this neighbour */
828   ip_address_t ip = IP_ADDRESS_V4_ALL_0S;
829   ip_adjacency_t *adj;
830   index_t itpi;
831
832   adj = adj_get (ai);
833
834   if (adj->lookup_next_index != IP_LOOKUP_NEXT_MIDCHAIN)
835     return;
836
837   ip_address_from_46 (&adj->sub_type.midchain.next_hop,
838                       adj->ia_nh_proto, &ip);
839
840   itpi = ipsec_tun_protect_find (adj->rewrite_header.sw_if_index, &ip);
841
842   if (INDEX_INVALID != itpi)
843     {
844       const ipsec_tun_protect_t *itp;
845
846       itp = ipsec_tun_protect_get (itpi);
847       adj_delegate_add (adj_get (ai), ipsec_tun_adj_delegate_type, itpi);
848       ipsec_tun_protect_add_adj (ai, itp->itp_out_sa);
849     }
850 }
851
852 static u8 *
853 ipsec_tun_protect_adj_delegate_format (const adj_delegate_t * aed, u8 * s)
854 {
855   const ipsec_tun_protect_t *itp;
856
857   itp = ipsec_tun_protect_from_const_base (aed);
858   s = format (s, "ipsec-tun-protect:\n%U", format_ipsec_tun_protect, itp);
859
860   return (s);
861 }
862
863 static void
864 ipsec_tun_teib_entry_added (const teib_entry_t * ne)
865 {
866   const ip46_address_t *peer46;
867   ipsec_tun_protect_t *itp;
868   ip_address_t peer;
869   index_t itpi;
870
871   peer46 = teib_entry_get_peer (ne);
872   ip_address_from_46 (peer46,
873                       (ip46_address_is_ip4 (peer46) ?
874                        FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6), &peer);
875
876   itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne), &peer);
877
878   if (INDEX_INVALID == itpi)
879     return;
880
881   itp = ipsec_tun_protect_get (itpi);
882   ipsec_tun_protect_rx_db_remove (&ipsec_main, itp);
883   ipsec_tun_protect_update_from_teib (itp, ne);
884   ipsec_tun_protect_set_crypto_addr (itp);
885   ipsec_tun_protect_rx_db_add (&ipsec_main, itp);
886
887   ITP_DBG (itp, "teib-added");
888 }
889
890 static void
891 ipsec_tun_teib_entry_deleted (const teib_entry_t * ne)
892 {
893   const ip46_address_t *peer46;
894   ipsec_tun_protect_t *itp;
895   ip_address_t peer;
896   index_t itpi;
897
898   peer46 = teib_entry_get_peer (ne);
899   ip_address_from_46 (peer46,
900                       (ip46_address_is_ip4 (peer46) ?
901                        FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6), &peer);
902
903   itpi = ipsec_tun_protect_find (teib_entry_get_sw_if_index (ne), &peer);
904
905   if (INDEX_INVALID == itpi)
906     return;
907
908   itp = ipsec_tun_protect_get (itpi);
909   ipsec_tun_protect_rx_db_remove (&ipsec_main, itp);
910   ipsec_tun_protect_update_from_teib (itp, NULL);
911   ipsec_tun_protect_set_crypto_addr (itp);
912
913   ITP_DBG (itp, "teib-removed");
914 }
915
916 /**
917  * VFT registered with the adjacency delegate
918  */
919 const static adj_delegate_vft_t ipsec_tun_adj_delegate_vft = {
920   .adv_adj_deleted = ipsec_tun_protect_adj_delegate_adj_deleted,
921   .adv_adj_created = ipsec_tun_protect_adj_delegate_adj_created,
922   .adv_format = ipsec_tun_protect_adj_delegate_format,
923 };
924
925 const static teib_vft_t ipsec_tun_teib_vft = {
926   .nv_added = ipsec_tun_teib_entry_added,
927   .nv_deleted = ipsec_tun_teib_entry_deleted,
928 };
929
930 clib_error_t *
931 ipsec_tunnel_protect_init (vlib_main_t * vm)
932 {
933   ipsec_main_t *im;
934
935   im = &ipsec_main;
936   im->tun6_protect_by_key = hash_create_mem (0,
937                                              sizeof (ipsec6_tunnel_key_t),
938                                              sizeof (u64));
939   im->tun4_protect_by_key = hash_create (0, sizeof (u64));
940
941   /* set up feature nodes to drop outbound packets with no crypto alg set */
942   ipsec_add_feature ("ip4-output", "esp4-no-crypto",
943                      &im->esp4_no_crypto_tun_feature_index);
944   ipsec_add_feature ("ip6-output", "esp6-no-crypto",
945                      &im->esp6_no_crypto_tun_feature_index);
946
947   ipsec_tun_adj_delegate_type =
948     adj_delegate_register_new_type (&ipsec_tun_adj_delegate_vft);
949
950   ipsec_tun_protect_logger = vlib_log_register_class ("ipsec", "tun");
951
952   teib_register (&ipsec_tun_teib_vft);
953
954   return 0;
955 }
956
957 VLIB_INIT_FUNCTION (ipsec_tunnel_protect_init);
958
959
960 /*
961  * fd.io coding-style-patch-verification: ON
962  *
963  * Local Variables:
964  * eval: (c-set-style "gnu")
965  * End:
966  */