ipsec: ipsec-tun protect
[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 is_ip4, is_l2, rv;
43
44   is_ip4 = ip46_address_is_ip4 (&itp->itp_tun.src);
45   is_l2 = itp->itp_flags & IPSEC_PROTECT_L2;
46
47   if (is_ip4)
48     {
49       if (is_l2)
50         rv = vnet_feature_enable_disable ("ethernet-output",
51                                           "esp4-encrypt-tun",
52                                           itp->itp_sw_if_index, enable,
53                                           &sai, sizeof (sai));
54       else
55         rv = vnet_feature_enable_disable ("ip4-output",
56                                           "esp4-encrypt-tun",
57                                           itp->itp_sw_if_index, enable,
58                                           &sai, sizeof (sai));
59     }
60   else
61     {
62       if (is_l2)
63         rv = vnet_feature_enable_disable ("ethernet-output",
64                                           "esp6-encrypt-tun",
65                                           itp->itp_sw_if_index, enable,
66                                           &sai, sizeof (sai));
67       else
68         rv = vnet_feature_enable_disable ("ip6-output",
69                                           "esp6-encrypt-tun",
70                                           itp->itp_sw_if_index, enable,
71                                           &sai, sizeof (sai));
72     }
73
74   ASSERT (!rv);
75   return (rv);
76 }
77
78 static void
79 ipsec_tun_protect_db_add (ipsec_main_t * im, const ipsec_tun_protect_t * itp)
80 {
81   const ipsec_sa_t *sa;
82   u32 sai;
83
84   /* *INDENT-OFF* */
85   FOR_EACH_IPSEC_PROTECT_INPUT_SAI(itp, sai,
86   ({
87       sa = ipsec_sa_get (sai);
88
89       ipsec_tun_lkup_result_t res = {
90         .tun_index = itp - ipsec_protect_pool,
91         .sa_index = sai,
92       };
93
94       /*
95        * The key is formed from the tunnel's destination
96        * as the packet lookup is done from the packet's source
97        */
98       if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
99         {
100           ipsec4_tunnel_key_t key = {
101             .remote_ip = itp->itp_crypto.dst.ip4.as_u32,
102             .spi = clib_host_to_net_u32 (sa->spi),
103           };
104           hash_set (im->tun4_protect_by_key, key.as_u64, res.as_u64);
105         }
106       else
107         {
108           ipsec6_tunnel_key_t key = {
109             .remote_ip = itp->itp_crypto.dst.ip6,
110             .spi = clib_host_to_net_u32 (sa->spi),
111           };
112           hash_set_mem_alloc (&im->tun6_protect_by_key, &key, res.as_u64);
113         }
114   }))
115   /* *INDENT-ON* */
116 }
117
118 static void
119 ipsec_tun_protect_db_remove (ipsec_main_t * im,
120                              const ipsec_tun_protect_t * itp)
121 {
122   const ipsec_sa_t *sa;
123
124   /* *INDENT-OFF* */
125   FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
126   ({
127       if (ip46_address_is_ip4 (&itp->itp_crypto.dst))
128         {
129           ipsec4_tunnel_key_t key = {
130             .remote_ip = itp->itp_crypto.dst.ip4.as_u32,
131             .spi = clib_host_to_net_u32 (sa->spi),
132           };
133           hash_unset (im->tun4_protect_by_key, &key);
134         }
135       else
136         {
137           ipsec6_tunnel_key_t key = {
138             .remote_ip = itp->itp_crypto.dst.ip6,
139             .spi = clib_host_to_net_u32 (sa->spi),
140           };
141           hash_unset_mem_free (&im->tun6_protect_by_key, &key);
142         }
143   }))
144   /* *INDENT-ON* */
145 }
146
147 static void
148 ipsec_tun_protect_config (ipsec_main_t * im,
149                           ipsec_tun_protect_t * itp, u32 sa_out, u32 * sas_in)
150 {
151   ipsec_sa_t *sa;
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   /* *INDENT-OFF* */
160   FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
161   ({
162     if (ipsec_sa_is_set_IS_TUNNEL (sa))
163       {
164         itp->itp_crypto.src = sa->tunnel_dst_addr;
165         itp->itp_crypto.dst = sa->tunnel_src_addr;
166         ipsec_sa_set_IS_PROTECT (sa);
167         itp->itp_flags |= IPSEC_PROTECT_ENCAPED;
168       }
169     else
170       {
171         itp->itp_crypto.src = itp->itp_tun.src;
172         itp->itp_crypto.dst = itp->itp_tun.dst;
173         itp->itp_flags &= ~IPSEC_PROTECT_ENCAPED;
174       }
175   }));
176   /* *INDENT-ON* */
177
178   /*
179    * add to the DB against each SA
180    */
181   ipsec_tun_protect_db_add (im, itp);
182
183   /*
184    * enable the encrypt feature for egress.
185    */
186   ipsec_tun_protect_feature_set (itp, 1);
187
188 }
189
190 static void
191 ipsec_tun_protect_unconfig (ipsec_main_t * im, ipsec_tun_protect_t * itp)
192 {
193   ipsec_sa_t *sa;
194
195   ipsec_tun_protect_feature_set (itp, 0);
196
197   /* *INDENT-OFF* */
198   FOR_EACH_IPSEC_PROTECT_INPUT_SA(itp, sa,
199   ({
200     ipsec_sa_unset_IS_PROTECT (sa);
201   }));
202   /* *INDENT-ON* */
203
204   ipsec_tun_protect_db_remove (im, itp);
205 }
206
207 index_t
208 ipsec_tun_protect_find (u32 sw_if_index)
209 {
210   if (vec_len (ipsec_protect_db.tunnels) < sw_if_index)
211     return (INDEX_INVALID);
212
213   return (ipsec_protect_db.tunnels[sw_if_index]);
214 }
215
216 int
217 ipsec_tun_protect_update (u32 sw_if_index, u32 sa_out, u32 * sas_in)
218 {
219   u32 itpi, ii;
220   ipsec_tun_protect_t *itp;
221   ipsec_main_t *im;
222   int rv;
223
224   rv = 0;
225   im = &ipsec_main;
226   vec_validate_init_empty (ipsec_protect_db.tunnels, sw_if_index,
227                            INDEX_INVALID);
228   itpi = ipsec_protect_db.tunnels[sw_if_index];
229
230   vec_foreach_index (ii, sas_in)
231   {
232     sas_in[ii] = ipsec_get_sa_index_by_sa_id (sas_in[ii]);
233     if (~0 == sas_in[ii])
234       {
235         rv = VNET_API_ERROR_INVALID_VALUE;
236         goto out;
237       }
238   }
239
240   sa_out = ipsec_get_sa_index_by_sa_id (sa_out);
241
242   if (~0 == sa_out)
243     {
244       rv = VNET_API_ERROR_INVALID_VALUE;
245       goto out;
246     }
247
248   if (INDEX_INVALID == itpi)
249     {
250       vnet_device_class_t *dev_class;
251       vnet_hw_interface_t *hi;
252       vnet_main_t *vnm;
253       u8 is_l2;
254
255       vnm = vnet_get_main ();
256       hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
257       dev_class = vnet_get_device_class (vnm, hi->dev_class_index);
258
259       if (NULL == dev_class->ip_tun_desc)
260         {
261           rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
262           goto out;
263         }
264
265       pool_get_zero (ipsec_protect_pool, itp);
266
267       itp->itp_sw_if_index = sw_if_index;
268       ipsec_protect_db.tunnels[sw_if_index] = itp - ipsec_protect_pool;
269       ipsec_protect_db.count++;
270
271       itp->itp_n_sa_in = vec_len (sas_in);
272       for (ii = 0; ii < itp->itp_n_sa_in; ii++)
273         itp->itp_in_sas[ii] = sas_in[ii];
274       itp->itp_out_sa = sa_out;
275
276       rv = dev_class->ip_tun_desc (sw_if_index,
277                                    &itp->itp_tun.src,
278                                    &itp->itp_tun.dst, &is_l2);
279
280       if (rv)
281         goto out;
282
283       if (is_l2)
284         itp->itp_flags |= IPSEC_PROTECT_L2;
285
286       /*
287        * add to the tunnel DB for ingress
288        *  - if the SA is in trasnport mode, then the packates will arrivw
289        *    with the IP src,dst of the protected tunnel, in which case we can
290        *    simply strip the IP header and hand the payload to the protocol
291        *    appropriate input handler
292        *  - if the SA is in tunnel mode then there are two IP headers present
293        *    one for the crytpo tunnel endpoints (described in the SA) and one
294        *    for the tunnel endpoints. The outer IP headers in the srriving
295        *    packets will have the crypto endpoints. So the DB needs to contain
296        *    the crpto endpoint. Once the crypto header is stripped, revealing,
297        *    the tunnel-IP we have 2 choices:
298        *     1) do a tunnel lookup based on the revealed header
299        *     2) skip the tunnel lookup and assume that the packet matches the
300        *        one that is protected here.
301        *    If we did 1) then we would allow our peer to use the SA for tunnel
302        *    X to inject traffic onto tunnel Y, this is not good. If we do 2)
303        *    then we don't verify that the peer is indeed using SA for tunnel
304        *    X and addressing tunnel X. So we take a compromise, once the SA
305        *    matches to tunnel X we veriy that the inner IP matches the value
306        *    of the tunnel we are protecting, else it's dropped.
307        */
308       ipsec_tun_protect_config (im, itp, sa_out, sas_in);
309
310       if (1 == hash_elts (im->tun4_protect_by_key))
311         ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
312                                ipsec4_tun_input_node.index);
313       if (1 == hash_elts (im->tun6_protect_by_key))
314         ip6_register_protocol (IP_PROTOCOL_IPSEC_ESP,
315                                ipsec6_tun_input_node.index);
316     }
317   else
318     {
319       /* updating SAs only */
320       itp = pool_elt_at_index (ipsec_protect_pool, itpi);
321
322       ipsec_tun_protect_unconfig (im, itp);
323       ipsec_tun_protect_config (im, itp, sa_out, sas_in);
324     }
325
326   vec_free (sas_in);
327 out:
328   return (rv);
329 }
330
331 int
332 ipsec_tun_protect_del (u32 sw_if_index)
333 {
334   ipsec_tun_protect_t *itp;
335   ipsec_main_t *im;
336   index_t itpi;
337
338   im = &ipsec_main;
339
340   vec_validate_init_empty (ipsec_protect_db.tunnels, sw_if_index,
341                            INDEX_INVALID);
342   itpi = ipsec_protect_db.tunnels[sw_if_index];
343
344   if (INDEX_INVALID == itpi)
345     return (VNET_API_ERROR_NO_SUCH_ENTRY);
346
347   itp = ipsec_tun_protect_get (itpi);
348   ipsec_tun_protect_unconfig (im, itp);
349
350   ipsec_protect_db.tunnels[itp->itp_sw_if_index] = INDEX_INVALID;
351
352   pool_put (ipsec_protect_pool, itp);
353
354   /* if (0 == hash_elts (im->tun4_protect_by_key)) */
355   /*   ip4_unregister_protocol (IP_PROTOCOL_IPSEC_ESP); */
356   /* if (0 == hash_elts (im->tun6_protect_by_key)) */
357   /*   ip6_unregister_protocol (IP_PROTOCOL_IPSEC_ESP); */
358
359   return (0);
360 }
361
362 void
363 ipsec_tun_protect_walk (ipsec_tun_protect_walk_cb_t fn, void *ctx)
364 {
365   index_t itpi;
366
367   /* *INDENT-OFF* */
368   pool_foreach_index(itpi, ipsec_protect_pool,
369   ({
370     fn (itpi, ctx);
371   }));
372   /* *INDENT-ON* */
373 }
374
375 clib_error_t *
376 ipsec_tunnel_protect_init (vlib_main_t * vm)
377 {
378   ipsec_main_t *im;
379
380   im = &ipsec_main;
381   im->tun6_protect_by_key = hash_create_mem (0,
382                                              sizeof (ipsec6_tunnel_key_t),
383                                              sizeof (u64));
384   im->tun4_protect_by_key = hash_create (0, sizeof (u64));
385
386   return 0;
387 }
388
389 VLIB_INIT_FUNCTION (ipsec_tunnel_protect_init);
390
391
392 /*
393  * fd.io coding-style-patch-verification: ON
394  *
395  * Local Variables:
396  * eval: (c-set-style "gnu")
397  * End:
398  */