ipsec: Reference count the SAs
[vpp.git] / src / vnet / ipsec / ipsec_sa.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vnet/ipsec/ipsec.h>
17 #include <vnet/ipsec/esp.h>
18 #include <vnet/udp/udp.h>
19 #include <vnet/fib/fib_table.h>
20 #include <vnet/ipsec/ipsec_tun.h>
21
22 /**
23  * @brief
24  * SA packet & bytes counters
25  */
26 vlib_combined_counter_main_t ipsec_sa_counters = {
27   .name = "SA",
28   .stat_segment_name = "/net/ipsec/sa",
29 };
30
31
32 static clib_error_t *
33 ipsec_call_add_del_callbacks (ipsec_main_t * im, ipsec_sa_t * sa,
34                               u32 sa_index, int is_add)
35 {
36   ipsec_ah_backend_t *ab;
37   ipsec_esp_backend_t *eb;
38   switch (sa->protocol)
39     {
40     case IPSEC_PROTOCOL_AH:
41       ab = pool_elt_at_index (im->ah_backends, im->ah_current_backend);
42       if (ab->add_del_sa_sess_cb)
43         return ab->add_del_sa_sess_cb (sa_index, is_add);
44       break;
45     case IPSEC_PROTOCOL_ESP:
46       eb = pool_elt_at_index (im->esp_backends, im->esp_current_backend);
47       if (eb->add_del_sa_sess_cb)
48         return eb->add_del_sa_sess_cb (sa_index, is_add);
49       break;
50     }
51   return 0;
52 }
53
54 void
55 ipsec_mk_key (ipsec_key_t * key, const u8 * data, u8 len)
56 {
57   memset (key, 0, sizeof (*key));
58
59   if (len > sizeof (key->data))
60     key->len = sizeof (key->data);
61   else
62     key->len = len;
63
64   memcpy (key->data, data, key->len);
65 }
66
67 /**
68  * 'stack' (resolve the recursion for) the SA tunnel destination
69  */
70 static void
71 ipsec_sa_stack (ipsec_sa_t * sa)
72 {
73   ipsec_main_t *im = &ipsec_main;
74   fib_forward_chain_type_t fct;
75   dpo_id_t tmp = DPO_INVALID;
76
77   fct =
78     fib_forw_chain_type_from_fib_proto ((ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ?
79                                          FIB_PROTOCOL_IP6 :
80                                          FIB_PROTOCOL_IP4));
81
82   fib_entry_contribute_forwarding (sa->fib_entry_index, fct, &tmp);
83
84   if (IPSEC_PROTOCOL_AH == sa->protocol)
85     dpo_stack_from_node ((ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ?
86                           im->ah6_encrypt_node_index :
87                           im->ah4_encrypt_node_index), &sa->dpo, &tmp);
88   else
89     dpo_stack_from_node ((ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ?
90                           im->esp6_encrypt_node_index :
91                           im->esp4_encrypt_node_index), &sa->dpo, &tmp);
92   dpo_reset (&tmp);
93 }
94
95 void
96 ipsec_sa_set_crypto_alg (ipsec_sa_t * sa, ipsec_crypto_alg_t crypto_alg)
97 {
98   ipsec_main_t *im = &ipsec_main;
99   sa->crypto_alg = crypto_alg;
100   sa->crypto_iv_size = im->crypto_algs[crypto_alg].iv_size;
101   sa->crypto_block_size = im->crypto_algs[crypto_alg].block_size;
102   sa->crypto_enc_op_id = im->crypto_algs[crypto_alg].enc_op_id;
103   sa->crypto_dec_op_id = im->crypto_algs[crypto_alg].dec_op_id;
104   sa->crypto_calg = im->crypto_algs[crypto_alg].alg;
105   ASSERT (sa->crypto_iv_size <= ESP_MAX_IV_SIZE);
106   ASSERT (sa->crypto_block_size <= ESP_MAX_BLOCK_SIZE);
107   if (IPSEC_CRYPTO_ALG_IS_GCM (crypto_alg))
108     {
109       sa->integ_icv_size = im->crypto_algs[crypto_alg].icv_size;
110       ipsec_sa_set_IS_AEAD (sa);
111     }
112 }
113
114 void
115 ipsec_sa_set_integ_alg (ipsec_sa_t * sa, ipsec_integ_alg_t integ_alg)
116 {
117   ipsec_main_t *im = &ipsec_main;
118   sa->integ_alg = integ_alg;
119   sa->integ_icv_size = im->integ_algs[integ_alg].icv_size;
120   sa->integ_op_id = im->integ_algs[integ_alg].op_id;
121   sa->integ_calg = im->integ_algs[integ_alg].alg;
122   ASSERT (sa->integ_icv_size <= ESP_MAX_ICV_SIZE);
123 }
124
125 int
126 ipsec_sa_add_and_lock (u32 id,
127                        u32 spi,
128                        ipsec_protocol_t proto,
129                        ipsec_crypto_alg_t crypto_alg,
130                        const ipsec_key_t * ck,
131                        ipsec_integ_alg_t integ_alg,
132                        const ipsec_key_t * ik,
133                        ipsec_sa_flags_t flags,
134                        u32 tx_table_id,
135                        u32 salt,
136                        const ip46_address_t * tun_src,
137                        const ip46_address_t * tun_dst, u32 * sa_out_index)
138 {
139   vlib_main_t *vm = vlib_get_main ();
140   ipsec_main_t *im = &ipsec_main;
141   clib_error_t *err;
142   ipsec_sa_t *sa;
143   u32 sa_index;
144   uword *p;
145
146   p = hash_get (im->sa_index_by_sa_id, id);
147   if (p)
148     return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
149
150   pool_get_aligned_zero (im->sad, sa, CLIB_CACHE_LINE_BYTES);
151
152   fib_node_init (&sa->node, FIB_NODE_TYPE_IPSEC_SA);
153   fib_node_lock (&sa->node);
154   sa_index = sa - im->sad;
155
156   vlib_validate_combined_counter (&ipsec_sa_counters, sa_index);
157   vlib_zero_combined_counter (&ipsec_sa_counters, sa_index);
158
159   sa->id = id;
160   sa->spi = spi;
161   sa->stat_index = sa_index;
162   sa->protocol = proto;
163   sa->flags = flags;
164   sa->salt = salt;
165   ipsec_sa_set_integ_alg (sa, integ_alg);
166   clib_memcpy (&sa->integ_key, ik, sizeof (sa->integ_key));
167   ipsec_sa_set_crypto_alg (sa, crypto_alg);
168   clib_memcpy (&sa->crypto_key, ck, sizeof (sa->crypto_key));
169   ip46_address_copy (&sa->tunnel_src_addr, tun_src);
170   ip46_address_copy (&sa->tunnel_dst_addr, tun_dst);
171
172   sa->crypto_key_index = vnet_crypto_key_add (vm,
173                                               im->crypto_algs[crypto_alg].alg,
174                                               (u8 *) ck->data, ck->len);
175   if (~0 == sa->crypto_key_index)
176     {
177       pool_put (im->sad, sa);
178       return VNET_API_ERROR_KEY_LENGTH;
179     }
180
181   sa->integ_key_index = vnet_crypto_key_add (vm,
182                                              im->integ_algs[integ_alg].alg,
183                                              (u8 *) ik->data, ik->len);
184   if (~0 == sa->integ_key_index)
185     {
186       pool_put (im->sad, sa);
187       return VNET_API_ERROR_KEY_LENGTH;
188     }
189
190   err = ipsec_check_support_cb (im, sa);
191   if (err)
192     {
193       clib_warning ("%s", err->what);
194       pool_put (im->sad, sa);
195       return VNET_API_ERROR_UNIMPLEMENTED;
196     }
197
198   err = ipsec_call_add_del_callbacks (im, sa, sa_index, 1);
199   if (err)
200     {
201       pool_put (im->sad, sa);
202       return VNET_API_ERROR_SYSCALL_ERROR_1;
203     }
204
205   if (ipsec_sa_is_set_IS_TUNNEL (sa) && !ipsec_sa_is_set_IS_INBOUND (sa))
206     {
207       fib_protocol_t fproto = (ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ?
208                                FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
209       fib_prefix_t pfx = {
210         .fp_addr = sa->tunnel_dst_addr,
211         .fp_len = (ipsec_sa_is_set_IS_TUNNEL_V6 (sa) ? 128 : 32),
212         .fp_proto = fproto,
213       };
214       sa->tx_fib_index = fib_table_find (fproto, tx_table_id);
215       if (sa->tx_fib_index == ~((u32) 0))
216         {
217           pool_put (im->sad, sa);
218           return VNET_API_ERROR_NO_SUCH_FIB;
219         }
220
221       sa->fib_entry_index = fib_table_entry_special_add (sa->tx_fib_index,
222                                                          &pfx,
223                                                          FIB_SOURCE_RR,
224                                                          FIB_ENTRY_FLAG_NONE);
225       sa->sibling = fib_entry_child_add (sa->fib_entry_index,
226                                          FIB_NODE_TYPE_IPSEC_SA, sa_index);
227       ipsec_sa_stack (sa);
228
229       /* generate header templates */
230       if (ipsec_sa_is_set_IS_TUNNEL_V6 (sa))
231         {
232           sa->ip6_hdr.ip_version_traffic_class_and_flow_label = 0x60;
233           sa->ip6_hdr.hop_limit = 254;
234           sa->ip6_hdr.src_address.as_u64[0] =
235             sa->tunnel_src_addr.ip6.as_u64[0];
236           sa->ip6_hdr.src_address.as_u64[1] =
237             sa->tunnel_src_addr.ip6.as_u64[1];
238           sa->ip6_hdr.dst_address.as_u64[0] =
239             sa->tunnel_dst_addr.ip6.as_u64[0];
240           sa->ip6_hdr.dst_address.as_u64[1] =
241             sa->tunnel_dst_addr.ip6.as_u64[1];
242           if (ipsec_sa_is_set_UDP_ENCAP (sa))
243             sa->ip6_hdr.protocol = IP_PROTOCOL_UDP;
244           else
245             sa->ip6_hdr.protocol = IP_PROTOCOL_IPSEC_ESP;
246         }
247       else
248         {
249           sa->ip4_hdr.ip_version_and_header_length = 0x45;
250           sa->ip4_hdr.ttl = 254;
251           sa->ip4_hdr.src_address.as_u32 = sa->tunnel_src_addr.ip4.as_u32;
252           sa->ip4_hdr.dst_address.as_u32 = sa->tunnel_dst_addr.ip4.as_u32;
253
254           if (ipsec_sa_is_set_UDP_ENCAP (sa))
255             sa->ip4_hdr.protocol = IP_PROTOCOL_UDP;
256           else
257             sa->ip4_hdr.protocol = IP_PROTOCOL_IPSEC_ESP;
258           sa->ip4_hdr.checksum = ip4_header_checksum (&sa->ip4_hdr);
259         }
260     }
261
262   if (ipsec_sa_is_set_UDP_ENCAP (sa))
263     {
264       sa->udp_hdr.src_port = clib_host_to_net_u16 (UDP_DST_PORT_ipsec);
265       sa->udp_hdr.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_ipsec);
266     }
267
268   hash_set (im->sa_index_by_sa_id, sa->id, sa_index);
269
270   if (sa_out_index)
271     *sa_out_index = sa_index;
272
273   return (0);
274 }
275
276 static void
277 ipsec_sa_del (ipsec_sa_t * sa)
278 {
279   vlib_main_t *vm = vlib_get_main ();
280   ipsec_main_t *im = &ipsec_main;
281   u32 sa_index;
282
283   sa_index = sa - im->sad;
284   hash_unset (im->sa_index_by_sa_id, sa->id);
285
286   /* no recovery possible when deleting an SA */
287   (void) ipsec_call_add_del_callbacks (im, sa, sa_index, 0);
288
289   if (ipsec_sa_is_set_IS_TUNNEL (sa) && !ipsec_sa_is_set_IS_INBOUND (sa))
290     {
291       fib_entry_child_remove (sa->fib_entry_index, sa->sibling);
292       fib_table_entry_special_remove
293         (sa->tx_fib_index,
294          fib_entry_get_prefix (sa->fib_entry_index), FIB_SOURCE_RR);
295       dpo_reset (&sa->dpo);
296     }
297   vnet_crypto_key_del (vm, sa->crypto_key_index);
298   vnet_crypto_key_del (vm, sa->integ_key_index);
299   pool_put (im->sad, sa);
300 }
301
302 void
303 ipsec_sa_unlock (index_t sai)
304 {
305   ipsec_main_t *im = &ipsec_main;
306   ipsec_sa_t *sa;
307
308   if (INDEX_INVALID == sai)
309     return;
310
311   sa = pool_elt_at_index (im->sad, sai);
312
313   fib_node_unlock (&sa->node);
314 }
315
316 index_t
317 ipsec_sa_find_and_lock (u32 id)
318 {
319   ipsec_main_t *im = &ipsec_main;
320   ipsec_sa_t *sa;
321   uword *p;
322
323   p = hash_get (im->sa_index_by_sa_id, id);
324
325   if (!p)
326     return INDEX_INVALID;
327
328   sa = pool_elt_at_index (im->sad, p[0]);
329
330   fib_node_lock (&sa->node);
331
332   return (p[0]);
333 }
334
335 int
336 ipsec_sa_unlock_id (u32 id)
337 {
338   ipsec_main_t *im = &ipsec_main;
339   uword *p;
340
341   p = hash_get (im->sa_index_by_sa_id, id);
342
343   if (!p)
344     return VNET_API_ERROR_NO_SUCH_ENTRY;
345
346   ipsec_sa_unlock (p[0]);
347
348   return (0);
349 }
350
351 void
352 ipsec_sa_clear (index_t sai)
353 {
354   vlib_zero_combined_counter (&ipsec_sa_counters, sai);
355 }
356
357 void
358 ipsec_sa_walk (ipsec_sa_walk_cb_t cb, void *ctx)
359 {
360   ipsec_main_t *im = &ipsec_main;
361   ipsec_sa_t *sa;
362
363   /* *INDENT-OFF* */
364   pool_foreach (sa, im->sad,
365   ({
366     if (WALK_CONTINUE != cb(sa, ctx))
367       break;
368   }));
369   /* *INDENT-ON* */
370 }
371
372 /**
373  * Function definition to get a FIB node from its index
374  */
375 static fib_node_t *
376 ipsec_sa_fib_node_get (fib_node_index_t index)
377 {
378   ipsec_main_t *im;
379   ipsec_sa_t *sa;
380
381   im = &ipsec_main;
382   sa = pool_elt_at_index (im->sad, index);
383
384   return (&sa->node);
385 }
386
387 static ipsec_sa_t *
388 ipsec_sa_from_fib_node (fib_node_t * node)
389 {
390   ASSERT (FIB_NODE_TYPE_IPSEC_SA == node->fn_type);
391   return ((ipsec_sa_t *) (((char *) node) -
392                           STRUCT_OFFSET_OF (ipsec_sa_t, node)));
393
394 }
395
396 /**
397  * Function definition to inform the FIB node that its last lock has gone.
398  */
399 static void
400 ipsec_sa_last_lock_gone (fib_node_t * node)
401 {
402   /*
403    * The ipsec SA is a root of the graph. As such
404    * it never has children and thus is never locked.
405    */
406   ipsec_sa_del (ipsec_sa_from_fib_node (node));
407 }
408
409 /**
410  * Function definition to backwalk a FIB node
411  */
412 static fib_node_back_walk_rc_t
413 ipsec_sa_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
414 {
415   ipsec_sa_stack (ipsec_sa_from_fib_node (node));
416
417   return (FIB_NODE_BACK_WALK_CONTINUE);
418 }
419
420 /*
421  * Virtual function table registered by SAs
422  * for participation in the FIB object graph.
423  */
424 const static fib_node_vft_t ipsec_sa_vft = {
425   .fnv_get = ipsec_sa_fib_node_get,
426   .fnv_last_lock = ipsec_sa_last_lock_gone,
427   .fnv_back_walk = ipsec_sa_back_walk,
428 };
429
430 /* force inclusion from application's main.c */
431 clib_error_t *
432 ipsec_sa_interface_init (vlib_main_t * vm)
433 {
434   fib_node_register_type (FIB_NODE_TYPE_IPSEC_SA, &ipsec_sa_vft);
435
436   return 0;
437 }
438
439 VLIB_INIT_FUNCTION (ipsec_sa_interface_init);
440
441 /*
442  * fd.io coding-style-patch-verification: ON
443  *
444  * Local Variables:
445  * eval: (c-set-style "gnu")
446  * End:
447  */