ipsec: keep crypto data inside SA
[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/fib/fib_table.h>
18
19 /**
20  * @brief
21  * SA packet & bytes counters
22  */
23 vlib_combined_counter_main_t ipsec_sa_counters = {
24   .name = "SA",
25   .stat_segment_name = "/net/ipsec/sa",
26 };
27
28
29 static clib_error_t *
30 ipsec_call_add_del_callbacks (ipsec_main_t * im, ipsec_sa_t * sa,
31                               u32 sa_index, int is_add)
32 {
33   ipsec_ah_backend_t *ab;
34   ipsec_esp_backend_t *eb;
35   switch (sa->protocol)
36     {
37     case IPSEC_PROTOCOL_AH:
38       ab = pool_elt_at_index (im->ah_backends, im->ah_current_backend);
39       if (ab->add_del_sa_sess_cb)
40         return ab->add_del_sa_sess_cb (sa_index, is_add);
41       break;
42     case IPSEC_PROTOCOL_ESP:
43       eb = pool_elt_at_index (im->esp_backends, im->esp_current_backend);
44       if (eb->add_del_sa_sess_cb)
45         return eb->add_del_sa_sess_cb (sa_index, is_add);
46       break;
47     }
48   return 0;
49 }
50
51 void
52 ipsec_mk_key (ipsec_key_t * key, const u8 * data, u8 len)
53 {
54   memset (key, 0, sizeof (*key));
55
56   if (len > sizeof (key->data))
57     key->len = sizeof (key->data);
58   else
59     key->len = len;
60
61   memcpy (key->data, data, key->len);
62 }
63
64 /**
65  * 'stack' (resolve the recursion for) the SA tunnel destination
66  */
67 void
68 ipsec_sa_stack (ipsec_sa_t * sa)
69 {
70   ipsec_main_t *im = &ipsec_main;
71   fib_forward_chain_type_t fct;
72   dpo_id_t tmp = DPO_INVALID;
73
74   fct = fib_forw_chain_type_from_fib_proto ((sa->is_tunnel_ip6 ?
75                                              FIB_PROTOCOL_IP6 :
76                                              FIB_PROTOCOL_IP4));
77
78   fib_entry_contribute_forwarding (sa->fib_entry_index, fct, &tmp);
79
80   dpo_stack_from_node ((sa->is_tunnel_ip6 ?
81                         im->ah6_encrypt_node_index :
82                         im->ah4_encrypt_node_index),
83                        &sa->dpo[IPSEC_PROTOCOL_AH], &tmp);
84   dpo_stack_from_node ((sa->is_tunnel_ip6 ?
85                         im->esp6_encrypt_node_index :
86                         im->esp4_encrypt_node_index),
87                        &sa->dpo[IPSEC_PROTOCOL_ESP], &tmp);
88   dpo_reset (&tmp);
89 }
90
91 void
92 ipsec_sa_set_crypto_alg (ipsec_sa_t * sa, ipsec_crypto_alg_t crypto_alg)
93 {
94   ipsec_main_t *im = &ipsec_main;
95   sa->crypto_alg = crypto_alg;
96   sa->crypto_iv_size = im->crypto_algs[crypto_alg].iv_size;
97   sa->crypto_block_size = im->crypto_algs[crypto_alg].block_size;
98   sa->crypto_enc_op_type = im->crypto_algs[crypto_alg].enc_op_type;
99   sa->crypto_dec_op_type = im->crypto_algs[crypto_alg].dec_op_type;
100 }
101
102 void
103 ipsec_sa_set_integ_alg (ipsec_sa_t * sa, ipsec_integ_alg_t integ_alg)
104 {
105   ipsec_main_t *im = &ipsec_main;
106   sa->integ_alg = integ_alg;
107   sa->integ_trunc_size = im->integ_algs[integ_alg].trunc_size;
108   sa->integ_op_type = im->integ_algs[integ_alg].op_type;
109 }
110
111 int
112 ipsec_sa_add (u32 id,
113               u32 spi,
114               ipsec_protocol_t proto,
115               ipsec_crypto_alg_t crypto_alg,
116               const ipsec_key_t * ck,
117               ipsec_integ_alg_t integ_alg,
118               const ipsec_key_t * ik,
119               ipsec_sa_flags_t flags,
120               u32 tx_table_id,
121               const ip46_address_t * tun_src,
122               const ip46_address_t * tun_dst, u32 * sa_out_index)
123 {
124   ipsec_main_t *im = &ipsec_main;
125   clib_error_t *err;
126   ipsec_sa_t *sa;
127   u32 sa_index;
128   uword *p;
129
130   p = hash_get (im->sa_index_by_sa_id, id);
131   if (p)
132     return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
133
134   pool_get_zero (im->sad, sa);
135
136   fib_node_init (&sa->node, FIB_NODE_TYPE_IPSEC_SA);
137   sa_index = sa - im->sad;
138
139   vlib_validate_combined_counter (&ipsec_sa_counters, sa_index);
140   vlib_zero_combined_counter (&ipsec_sa_counters, sa_index);
141
142   sa->id = id;
143   sa->spi = spi;
144   sa->stat_index = sa_index;
145   sa->protocol = proto;
146   ipsec_sa_set_crypto_alg (sa, crypto_alg);
147   clib_memcpy (&sa->crypto_key, ck, sizeof (sa->crypto_key));
148   ipsec_sa_set_integ_alg (sa, integ_alg);
149   clib_memcpy (&sa->integ_key, ik, sizeof (sa->integ_key));
150   ip46_address_copy (&sa->tunnel_src_addr, tun_src);
151   ip46_address_copy (&sa->tunnel_dst_addr, tun_dst);
152
153   if (flags & IPSEC_SA_FLAG_USE_EXTENDED_SEQ_NUM)
154     sa->use_esn = 1;
155   if (flags & IPSEC_SA_FLAG_USE_ANTI_REPLAY)
156     sa->use_anti_replay = 1;
157   if (flags & IPSEC_SA_FLAG_IS_TUNNEL)
158     sa->is_tunnel = 1;
159   if (flags & IPSEC_SA_FLAG_IS_TUNNEL_V6)
160     sa->is_tunnel_ip6 = 1;
161   if (flags & IPSEC_SA_FLAG_UDP_ENCAP)
162     sa->udp_encap = 1;
163
164   err = ipsec_check_support_cb (im, sa);
165   if (err)
166     {
167       clib_warning ("%s", err->what);
168       pool_put (im->sad, sa);
169       return VNET_API_ERROR_UNIMPLEMENTED;
170     }
171
172   err = ipsec_call_add_del_callbacks (im, sa, sa_index, 1);
173   if (err)
174     {
175       pool_put (im->sad, sa);
176       return VNET_API_ERROR_SYSCALL_ERROR_1;
177     }
178
179   if (sa->is_tunnel)
180     {
181       fib_protocol_t fproto = (sa->is_tunnel_ip6 ?
182                                FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
183       fib_prefix_t pfx = {
184         .fp_addr = sa->tunnel_dst_addr,
185         .fp_len = (sa->is_tunnel_ip6 ? 128 : 32),
186         .fp_proto = fproto,
187       };
188       sa->tx_fib_index = fib_table_find (fproto, tx_table_id);
189       if (sa->tx_fib_index == ~((u32) 0))
190         {
191           pool_put (im->sad, sa);
192           return VNET_API_ERROR_NO_SUCH_FIB;
193         }
194
195       sa->fib_entry_index = fib_table_entry_special_add (sa->tx_fib_index,
196                                                          &pfx,
197                                                          FIB_SOURCE_RR,
198                                                          FIB_ENTRY_FLAG_NONE);
199       sa->sibling = fib_entry_child_add (sa->fib_entry_index,
200                                          FIB_NODE_TYPE_IPSEC_SA, sa_index);
201       ipsec_sa_stack (sa);
202     }
203   hash_set (im->sa_index_by_sa_id, sa->id, sa_index);
204
205   if (sa_out_index)
206     *sa_out_index = sa_index;
207
208   return (0);
209 }
210
211 u32
212 ipsec_sa_del (u32 id)
213 {
214   ipsec_main_t *im = &ipsec_main;
215   ipsec_sa_t *sa = 0;
216   uword *p;
217   u32 sa_index;
218   clib_error_t *err;
219
220   p = hash_get (im->sa_index_by_sa_id, id);
221
222   if (!p)
223     return VNET_API_ERROR_NO_SUCH_ENTRY;
224
225   sa_index = p[0];
226   sa = pool_elt_at_index (im->sad, sa_index);
227   if (ipsec_is_sa_used (sa_index))
228     {
229       clib_warning ("sa_id %u used in policy", sa->id);
230       /* sa used in policy */
231       return VNET_API_ERROR_SYSCALL_ERROR_1;
232     }
233   hash_unset (im->sa_index_by_sa_id, sa->id);
234   err = ipsec_call_add_del_callbacks (im, sa, sa_index, 0);
235   if (err)
236     return VNET_API_ERROR_SYSCALL_ERROR_1;
237   if (sa->is_tunnel)
238     {
239       fib_entry_child_remove (sa->fib_entry_index, sa->sibling);
240       fib_table_entry_special_remove
241         (sa->tx_fib_index,
242          fib_entry_get_prefix (sa->fib_entry_index), FIB_SOURCE_RR);
243       dpo_reset (&sa->dpo[IPSEC_PROTOCOL_AH]);
244       dpo_reset (&sa->dpo[IPSEC_PROTOCOL_ESP]);
245     }
246   pool_put (im->sad, sa);
247   return 0;
248 }
249
250 u8
251 ipsec_is_sa_used (u32 sa_index)
252 {
253   ipsec_main_t *im = &ipsec_main;
254   ipsec_tunnel_if_t *t;
255   ipsec_policy_t *p;
256
257   /* *INDENT-OFF* */
258   pool_foreach(p, im->policies, ({
259      if (p->policy == IPSEC_POLICY_ACTION_PROTECT)
260        {
261          if (p->sa_index == sa_index)
262            return 1;
263        }
264   }));
265
266   pool_foreach(t, im->tunnel_interfaces, ({
267     if (t->input_sa_index == sa_index)
268       return 1;
269     if (t->output_sa_index == sa_index)
270       return 1;
271   }));
272   /* *INDENT-ON* */
273
274   return 0;
275 }
276
277 int
278 ipsec_set_sa_key (u32 id, const ipsec_key_t * ck, const ipsec_key_t * ik)
279 {
280   ipsec_main_t *im = &ipsec_main;
281   uword *p;
282   u32 sa_index;
283   ipsec_sa_t *sa = 0;
284   clib_error_t *err;
285
286   p = hash_get (im->sa_index_by_sa_id, id);
287   if (!p)
288     return VNET_API_ERROR_SYSCALL_ERROR_1;      /* no such sa-id */
289
290   sa_index = p[0];
291   sa = pool_elt_at_index (im->sad, sa_index);
292
293   /* new crypto key */
294   if (ck)
295     {
296       clib_memcpy (&sa->crypto_key, ck, sizeof (sa->crypto_key));
297     }
298
299   /* new integ key */
300   if (ik)
301     {
302       clib_memcpy (&sa->integ_key, 0, sizeof (sa->integ_key));
303     }
304
305   if (ck || ik)
306     {
307       err = ipsec_call_add_del_callbacks (im, sa, sa_index, 0);
308       if (err)
309         {
310           clib_error_free (err);
311           return VNET_API_ERROR_SYSCALL_ERROR_1;
312         }
313     }
314
315   return 0;
316 }
317
318 u32
319 ipsec_get_sa_index_by_sa_id (u32 sa_id)
320 {
321   ipsec_main_t *im = &ipsec_main;
322   uword *p = hash_get (im->sa_index_by_sa_id, sa_id);
323   if (!p)
324     return ~0;
325
326   return p[0];
327 }
328
329 void
330 ipsec_sa_walk (ipsec_sa_walk_cb_t cb, void *ctx)
331 {
332   ipsec_main_t *im = &ipsec_main;
333   ipsec_sa_t *sa;
334
335   /* *INDENT-OFF* */
336   pool_foreach (sa, im->sad,
337   ({
338     if (WALK_CONTINUE != cb(sa, ctx))
339       break;
340   }));
341   /* *INDENT-ON* */
342 }
343
344 /**
345  * Function definition to get a FIB node from its index
346  */
347 static fib_node_t *
348 ipsec_sa_fib_node_get (fib_node_index_t index)
349 {
350   ipsec_main_t *im;
351   ipsec_sa_t *sa;
352
353   im = &ipsec_main;
354   sa = pool_elt_at_index (im->sad, index);
355
356   return (&sa->node);
357 }
358
359 /**
360  * Function definition to inform the FIB node that its last lock has gone.
361  */
362 static void
363 ipsec_sa_last_lock_gone (fib_node_t * node)
364 {
365   /*
366    * The ipsec SA is a root of the graph. As such
367    * it never has children and thus is never locked.
368    */
369   ASSERT (0);
370 }
371
372 static ipsec_sa_t *
373 ipsec_sa_from_fib_node (fib_node_t * node)
374 {
375   ASSERT (FIB_NODE_TYPE_IPSEC_SA == node->fn_type);
376   return ((ipsec_sa_t *) (((char *) node) -
377                           STRUCT_OFFSET_OF (ipsec_sa_t, node)));
378
379 }
380
381 /**
382  * Function definition to backwalk a FIB node
383  */
384 static fib_node_back_walk_rc_t
385 ipsec_sa_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
386 {
387   ipsec_sa_stack (ipsec_sa_from_fib_node (node));
388
389   return (FIB_NODE_BACK_WALK_CONTINUE);
390 }
391
392 /*
393  * Virtual function table registered by MPLS GRE tunnels
394  * for participation in the FIB object graph.
395  */
396 const static fib_node_vft_t ipsec_sa_vft = {
397   .fnv_get = ipsec_sa_fib_node_get,
398   .fnv_last_lock = ipsec_sa_last_lock_gone,
399   .fnv_back_walk = ipsec_sa_back_walk,
400 };
401
402 /* force inclusion from application's main.c */
403 clib_error_t *
404 ipsec_sa_interface_init (vlib_main_t * vm)
405 {
406   fib_node_register_type (FIB_NODE_TYPE_IPSEC_SA, &ipsec_sa_vft);
407
408   return 0;
409 }
410
411 VLIB_INIT_FUNCTION (ipsec_sa_interface_init);
412
413 /*
414  * fd.io coding-style-patch-verification: ON
415  *
416  * Local Variables:
417  * eval: (c-set-style "gnu")
418  * End:
419  */