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