ipsec: remove dedicated IPSec tunnels
[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
22 /**
23  * Pool of tunnel protection objects
24  */
25 ipsec_tun_protect_t *ipsec_protect_pool;
26
27 /**
28  * DB of protected tunnels
29  */
30 typedef struct ipsec_protect_db_t_
31 {
32   u32 *tunnels;
33   u32 count;
34 } ipsec_protect_db_t;
35
36 static ipsec_protect_db_t ipsec_protect_db;
37
38 static int
39 ipsec_tun_protect_feature_set (ipsec_tun_protect_t * itp, u8 enable)
40 {
41   u32 sai = itp->itp_out_sa;
42   int rv;
43
44   const char *enc_node = (ip46_address_is_ip4 (&itp->itp_tun.src) ?
45                           "esp4-encrypt-tun" : "esp6-encrypt-tun");
46
47   if (itp->itp_flags & IPSEC_PROTECT_L2)
48     {
49       rv = vnet_feature_enable_disable ("ethernet-output",
50                                         enc_node,
51                                         itp->itp_sw_if_index, enable,
52                                         &sai, sizeof (sai));
53     }
54   else
55     {
56       rv = vnet_feature_enable_disable ("ip4-output",
57                                         enc_node,
58                                         itp->itp_sw_if_index, enable,
59                                         &sai, sizeof (sai));
60       rv = vnet_feature_enable_disable ("ip6-output",
61                                         enc_node,
62                                         itp->itp_sw_if_index, enable,
63                                         &sai, sizeof (sai));
64     }
65   ASSERT (!rv);
66   return (rv);
67 }
68
69 static void
70 ipsec_tun_protect_db_add (ipsec_main_t * im, const ipsec_tun_protect_t * itp)
71 {
72   const ipsec_sa_t *sa;
73   u32 sai;
74
75   /* *INDENT-OFF* */
76   FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
77   ({
78       sa = ipsec_sa_get (sai);
79
80       ipsec_tun_lkup_result_t res = {
81         .tun_index = itp - ipsec_protect_pool,
82         .sa_index = sai,
83       };
84
85       /*
86        * The key is formed from the tunnel's destination
87        * as the packet lookup is done from the packet's source
88        */
89       if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
90         {
91           ipsec4_tunnel_key_t key = {
92             .remote_ip = itp->itp_crypto.dst.ip4,
93             .spi = clib_host_to_net_u32 (sa->spi),
94           };
95           hash_set (im->tun4_protect_by_key, key.as_u64, res.as_u64);
96           if (1 == hash_elts(im->tun4_protect_by_key))
97             udp_register_dst_port (vlib_get_main(),
98                                    UDP_DST_PORT_ipsec,
99                                    ipsec4_tun_input_node.index, 1);
100         }
101       else
102         {
103           ipsec6_tunnel_key_t key = {
104             .remote_ip = itp->itp_crypto.dst.ip6,
105             .spi = clib_host_to_net_u32 (sa->spi),
106           };
107           hash_set_mem_alloc (&im->tun6_protect_by_key, &key, res.as_u64);
108         }
109   }))
110   /* *INDENT-ON* */
111 }
112
113 static void
114 ipsec_tun_protect_db_remove (ipsec_main_t * im,
115                              const ipsec_tun_protect_t * itp)
116 {
117   const ipsec_sa_t *sa;
118
119   /* *INDENT-OFF* */
120   FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
121   ({
122       if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
123         {
124           ipsec4_tunnel_key_t key = {
125             .remote_ip = itp->itp_crypto.dst.ip4,
126             .spi = clib_host_to_net_u32 (sa->spi),
127           };
128           hash_unset (im->tun4_protect_by_key, key.as_u64);
129           if (0 == hash_elts(im->tun4_protect_by_key))
130             udp_unregister_dst_port (vlib_get_main(),
131                                      UDP_DST_PORT_ipsec,
132                                      1);
133         }
134       else
135         {
136           ipsec6_tunnel_key_t key = {
137             .remote_ip = itp->itp_crypto.dst.ip6,
138             .spi = clib_host_to_net_u32 (sa->spi),
139           };
140           hash_unset_mem_free (&im->tun6_protect_by_key, &key);
141         }
142   }))
143   /* *INDENT-ON* */
144 }
145
146 static void
147 ipsec_tun_protect_config (ipsec_main_t * im,
148                           ipsec_tun_protect_t * itp, u32 sa_out, u32 * sas_in)
149 {
150   ipsec_sa_t *sa;
151   index_t sai;
152   u32 ii;
153
154   itp->itp_n_sa_in = vec_len (sas_in);
155   for (ii = 0; ii < itp->itp_n_sa_in; ii++)
156     itp->itp_in_sas[ii] = sas_in[ii];
157   itp->itp_out_sa = sa_out;
158
159   ipsec_sa_lock (itp->itp_out_sa);
160
161   /* *INDENT-OFF* */
162   FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
163   ({
164     ipsec_sa_lock(sai);
165   }));
166   FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
167   ({
168     if (ipsec_sa_is_set_IS_TUNNEL (sa))
169       {
170         itp->itp_crypto.src = sa->tunnel_dst_addr;
171         itp->itp_crypto.dst = sa->tunnel_src_addr;
172         ipsec_sa_set_IS_PROTECT (sa);
173         itp->itp_flags |= IPSEC_PROTECT_ENCAPED;
174       }
175     else
176       {
177         itp->itp_crypto.src = itp->itp_tun.src;
178         itp->itp_crypto.dst = itp->itp_tun.dst;
179         itp->itp_flags &= ~IPSEC_PROTECT_ENCAPED;
180       }
181   }));
182   /* *INDENT-ON* */
183
184   /*
185    * add to the DB against each SA
186    */
187   ipsec_tun_protect_db_add (im, itp);
188
189   /*
190    * enable the encrypt feature for egress.
191    */
192   ipsec_tun_protect_feature_set (itp, 1);
193 }
194
195 static void
196 ipsec_tun_protect_unconfig (ipsec_main_t * im, ipsec_tun_protect_t * itp)
197 {
198   ipsec_sa_t *sa;
199   index_t sai;
200
201   ipsec_tun_protect_feature_set (itp, 0);
202
203   /* *INDENT-OFF* */
204   FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
205   ({
206     ipsec_sa_unset_IS_PROTECT (sa);
207   }));
208
209   ipsec_tun_protect_db_remove (im, itp);
210
211   ipsec_sa_unlock(itp->itp_out_sa);
212
213   FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
214   ({
215     ipsec_sa_unlock(sai);
216   }));
217   /* *INDENT-ON* */
218 }
219
220 index_t
221 ipsec_tun_protect_find (u32 sw_if_index)
222 {
223   if (vec_len (ipsec_protect_db.tunnels) < sw_if_index)
224     return (INDEX_INVALID);
225
226   return (ipsec_protect_db.tunnels[sw_if_index]);
227 }
228
229 int
230 ipsec_tun_protect_update_one (u32 sw_if_index, u32 sa_out, u32 sa_in)
231 {
232   u32 *sas_in = NULL;
233   int rv;
234
235   vec_add1 (sas_in, sa_in);
236   rv = ipsec_tun_protect_update (sw_if_index, sa_out, sas_in);
237
238   return (rv);
239 }
240
241 int
242 ipsec_tun_protect_update_out (u32 sw_if_index, u32 sa_out)
243 {
244   u32 itpi, *sas_in, sai, *saip;
245   ipsec_tun_protect_t *itp;
246   ipsec_main_t *im;
247   int rv;
248
249   sas_in = NULL;
250   rv = 0;
251   im = &ipsec_main;
252   vec_validate_init_empty (ipsec_protect_db.tunnels, sw_if_index,
253                            INDEX_INVALID);
254   itpi = ipsec_protect_db.tunnels[sw_if_index];
255
256   if (INDEX_INVALID == itpi)
257     {
258       return (VNET_API_ERROR_INVALID_INTERFACE);
259     }
260
261   itp = pool_elt_at_index (ipsec_protect_pool, itpi);
262
263   /* *INDENT-0FF* */
264   FOR_EACH_IPSEC_PROTECT_INPUT_SAI (itp, sai, (
265                                                 {
266                                                 ipsec_sa_lock (sai);
267                                                 vec_add1 (sas_in, sai);
268                                                 }
269                                     ));
270   /* *INDENT-ON* */
271
272   sa_out = ipsec_sa_find_and_lock (sa_out);
273
274   if (~0 == sa_out)
275     {
276       rv = VNET_API_ERROR_INVALID_VALUE;
277       goto out;
278     }
279
280   ipsec_tun_protect_unconfig (im, itp);
281   ipsec_tun_protect_config (im, itp, sa_out, sas_in);
282
283   ipsec_sa_unlock (sa_out);
284   vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
285
286 out:
287   vec_free (sas_in);
288   return (rv);
289 }
290
291 int
292 ipsec_tun_protect_update_in (u32 sw_if_index, u32 sa_in)
293 {
294   u32 itpi, *sas_in, sa_out;
295   ipsec_tun_protect_t *itp;
296   ipsec_main_t *im;
297   int rv;
298
299   sas_in = NULL;
300   rv = 0;
301   im = &ipsec_main;
302   vec_validate_init_empty (ipsec_protect_db.tunnels, sw_if_index,
303                            INDEX_INVALID);
304   itpi = ipsec_protect_db.tunnels[sw_if_index];
305
306   if (INDEX_INVALID == itpi)
307     {
308       return (VNET_API_ERROR_INVALID_INTERFACE);
309     }
310
311   sa_in = ipsec_sa_find_and_lock (sa_in);
312
313   if (~0 == sa_in)
314     {
315       rv = VNET_API_ERROR_INVALID_VALUE;
316       goto out;
317     }
318   vec_add1 (sas_in, sa_in);
319
320   itp = pool_elt_at_index (ipsec_protect_pool, itpi);
321   sa_out = itp->itp_out_sa;
322
323   ipsec_sa_lock (sa_out);
324
325   ipsec_tun_protect_unconfig (im, itp);
326   ipsec_tun_protect_config (im, itp, sa_out, sas_in);
327
328   ipsec_sa_unlock (sa_out);
329   ipsec_sa_unlock (sa_in);
330 out:
331   vec_free (sas_in);
332   return (rv);
333 }
334
335 int
336 ipsec_tun_protect_update (u32 sw_if_index, u32 sa_out, u32 * sas_in)
337 {
338   ipsec_tun_protect_t *itp;
339   u32 itpi, ii, *saip;
340   ipsec_main_t *im;
341   int rv;
342
343   rv = 0;
344   im = &ipsec_main;
345   vec_validate_init_empty (ipsec_protect_db.tunnels, sw_if_index,
346                            INDEX_INVALID);
347   itpi = ipsec_protect_db.tunnels[sw_if_index];
348
349   vec_foreach_index (ii, sas_in)
350   {
351     sas_in[ii] = ipsec_sa_find_and_lock (sas_in[ii]);
352     if (~0 == sas_in[ii])
353       {
354         rv = VNET_API_ERROR_INVALID_VALUE;
355         goto out;
356       }
357   }
358
359   sa_out = ipsec_sa_find_and_lock (sa_out);
360
361   if (~0 == sa_out)
362     {
363       rv = VNET_API_ERROR_INVALID_VALUE;
364       goto out;
365     }
366
367   if (INDEX_INVALID == itpi)
368     {
369       vnet_device_class_t *dev_class;
370       vnet_hw_interface_t *hi;
371       vnet_main_t *vnm;
372       u8 is_l2;
373
374       vnm = vnet_get_main ();
375       hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
376       dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
377
378       if (NULL == dev_class->ip_tun_desc)
379         {
380           rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
381           goto out;
382         }
383
384       pool_get_zero (ipsec_protect_pool, itp);
385
386       itp->itp_sw_if_index = sw_if_index;
387       ipsec_protect_db.tunnels[sw_if_index] = itp - ipsec_protect_pool;
388       ipsec_protect_db.count++;
389
390       itp->itp_n_sa_in = vec_len (sas_in);
391       for (ii = 0; ii < itp->itp_n_sa_in; ii++)
392         itp->itp_in_sas[ii] = sas_in[ii];
393       itp->itp_out_sa = sa_out;
394
395       rv = dev_class->ip_tun_desc (sw_if_index,
396                                    &itp->itp_tun.src,
397                                    &itp->itp_tun.dst, &is_l2);
398
399       if (rv)
400         goto out;
401
402       if (is_l2)
403         itp->itp_flags |= IPSEC_PROTECT_L2;
404
405       /*
406        * add to the tunnel DB for ingress
407        *  - if the SA is in trasnport mode, then the packates will arrivw
408        *    with the IP src,dst of the protected tunnel, in which case we can
409        *    simply strip the IP header and hand the payload to the protocol
410        *    appropriate input handler
411        *  - if the SA is in tunnel mode then there are two IP headers present
412        *    one for the crytpo tunnel endpoints (described in the SA) and one
413        *    for the tunnel endpoints. The outer IP headers in the srriving
414        *    packets will have the crypto endpoints. So the DB needs to contain
415        *    the crpto endpoint. Once the crypto header is stripped, revealing,
416        *    the tunnel-IP we have 2 choices:
417        *     1) do a tunnel lookup based on the revealed header
418        *     2) skip the tunnel lookup and assume that the packet matches the
419        *        one that is protected here.
420        *    If we did 1) then we would allow our peer to use the SA for tunnel
421        *    X to inject traffic onto tunnel Y, this is not good. If we do 2)
422        *    then we don't verify that the peer is indeed using SA for tunnel
423        *    X and addressing tunnel X. So we take a compromise, once the SA
424        *    matches to tunnel X we veriy that the inner IP matches the value
425        *    of the tunnel we are protecting, else it's dropped.
426        */
427       ipsec_tun_protect_config (im, itp, sa_out, sas_in);
428
429       if (1 == hash_elts (im->tun4_protect_by_key))
430         ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
431                                ipsec4_tun_input_node.index);
432       if (1 == hash_elts (im->tun6_protect_by_key))
433         ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP,
434                                ipsec6_tun_input_node.index);
435     }
436   else
437     {
438       /* updating SAs only */
439       itp = pool_elt_at_index (ipsec_protect_pool, itpi);
440
441       ipsec_tun_protect_unconfig (im, itp);
442       ipsec_tun_protect_config (im, itp, sa_out, sas_in);
443     }
444
445   ipsec_sa_unlock (sa_out);
446   vec_foreach (saip, sas_in) ipsec_sa_unlock (*saip);
447   vec_free (sas_in);
448
449 out:
450   return (rv);
451 }
452
453 int
454 ipsec_tun_protect_del (u32 sw_if_index)
455 {
456   ipsec_tun_protect_t *itp;
457   ipsec_main_t *im;
458   index_t itpi;
459
460   im = &ipsec_main;
461
462   vec_validate_init_empty (ipsec_protect_db.tunnels, sw_if_index,
463                            INDEX_INVALID);
464   itpi = ipsec_protect_db.tunnels[sw_if_index];
465
466   if (INDEX_INVALID == itpi)
467     return (VNET_API_ERROR_NO_SUCH_ENTRY);
468
469   itp = ipsec_tun_protect_get (itpi);
470   ipsec_tun_protect_unconfig (im, itp);
471
472   ipsec_protect_db.tunnels[itp->itp_sw_if_index] = INDEX_INVALID;
473
474   pool_put (ipsec_protect_pool, itp);
475
476   if (0 == hash_elts (im->tun4_protect_by_key))
477     ip4_unregister_protocol (IP_PROTOCOL_IPSEC_ESP);
478   if (0 == hash_elts (im->tun6_protect_by_key))
479     ip6_unregister_protocol (IP_PROTOCOL_IPSEC_ESP);
480
481   return (0);
482 }
483
484 void
485 ipsec_tun_protect_walk (ipsec_tun_protect_walk_cb_t fn, void *ctx)
486 {
487   index_t itpi;
488
489   /* *INDENT-OFF* */
490   pool_foreach_index(itpi, ipsec_protect_pool,
491   ({
492     fn (itpi, ctx);
493   }));
494   /* *INDENT-ON* */
495 }
496
497 clib_error_t *
498 ipsec_tunnel_protect_init (vlib_main_t * vm)
499 {
500   ipsec_main_t *im;
501
502   im = &ipsec_main;
503   im->tun6_protect_by_key = hash_create_mem (0,
504                                              sizeof (ipsec6_tunnel_key_t),
505                                              sizeof (u64));
506   im->tun4_protect_by_key = hash_create (0, sizeof (u64));
507
508   return 0;
509 }
510
511 VLIB_INIT_FUNCTION (ipsec_tunnel_protect_init);
512
513
514 /*
515  * fd.io coding-style-patch-verification: ON
516  *
517  * Local Variables:
518  * eval: (c-set-style "gnu")
519  * End:
520  */