Use counters on ipsec tunnel interfaces
[vpp.git] / src / vnet / ipsec / ipsec_if.c
1 /*
2  * ipsec_if.c : IPSec interface support
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/vnet.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/ip/ip.h>
21
22 #include <vnet/ipsec/ipsec.h>
23 #include <vnet/ipsec/esp.h>
24
25 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
26
27 static u8 *
28 format_ipsec_name (u8 * s, va_list * args)
29 {
30   u32 dev_instance = va_arg (*args, u32);
31   return format (s, "ipsec%d", dev_instance);
32 }
33
34 static uword
35 dummy_interface_tx (vlib_main_t * vm,
36                     vlib_node_runtime_t * node, vlib_frame_t * frame)
37 {
38   clib_warning ("you shouldn't be here, leaking buffers...");
39   return frame->n_vectors;
40 }
41
42 static clib_error_t *
43 ipsec_admin_up_down_function (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
44 {
45   ipsec_main_t *im = &ipsec_main;
46   clib_error_t *err = 0;
47   ipsec_tunnel_if_t *t;
48   vnet_hw_interface_t *hi;
49   ipsec_sa_t *sa;
50
51   hi = vnet_get_hw_interface (vnm, hw_if_index);
52   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
53     {
54       t = pool_elt_at_index (im->tunnel_interfaces, hi->hw_instance);
55       ASSERT (im->cb.check_support_cb);
56       sa = pool_elt_at_index (im->sad, t->input_sa_index);
57       err = im->cb.check_support_cb (sa);
58       if (err)
59         return err;
60
61       sa = pool_elt_at_index (im->sad, t->output_sa_index);
62       err = im->cb.check_support_cb (sa);
63       if (err)
64         return err;
65
66       vnet_hw_interface_set_flags (vnm, hw_if_index,
67                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
68     }
69   else
70     vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
71
72   return /* no error */ 0;
73 }
74
75 /* *INDENT-OFF* */
76 VNET_DEVICE_CLASS (ipsec_device_class, static) =
77 {
78   .name = "IPSec",
79   .format_device_name = format_ipsec_name,
80   .format_tx_trace = format_ipsec_if_output_trace,
81   .tx_function = dummy_interface_tx,
82   .admin_up_down_function = ipsec_admin_up_down_function,
83 };
84 /* *INDENT-ON* */
85
86 /* *INDENT-OFF* */
87 VNET_HW_INTERFACE_CLASS (ipsec_hw_class) =
88 {
89   .name = "IPSec",
90   .build_rewrite = default_build_rewrite,
91 };
92 /* *INDENT-ON* */
93
94 static int
95 ipsec_add_del_tunnel_if_rpc_callback (ipsec_add_del_tunnel_args_t * a)
96 {
97   vnet_main_t *vnm = vnet_get_main ();
98   ASSERT (vlib_get_thread_index () == 0);
99
100   return ipsec_add_del_tunnel_if_internal (vnm, a, NULL);
101 }
102
103 int
104 ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args)
105 {
106   vl_api_rpc_call_main_thread (ipsec_add_del_tunnel_if_rpc_callback,
107                                (u8 *) args, sizeof (*args));
108   return 0;
109 }
110
111 int
112 ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
113                                   ipsec_add_del_tunnel_args_t * args,
114                                   u32 * sw_if_index)
115 {
116   ipsec_tunnel_if_t *t;
117   ipsec_main_t *im = &ipsec_main;
118   vnet_hw_interface_t *hi = NULL;
119   u32 hw_if_index = ~0;
120   uword *p;
121   ipsec_sa_t *sa;
122
123   u64 key = (u64) args->remote_ip.as_u32 << 32 | (u64) args->remote_spi;
124   p = hash_get (im->ipsec_if_pool_index_by_key, key);
125
126   if (args->is_add)
127     {
128       /* check if same src/dst pair exists */
129       if (p)
130         return VNET_API_ERROR_INVALID_VALUE;
131
132       pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
133       memset (t, 0, sizeof (*t));
134
135       pool_get (im->sad, sa);
136       memset (sa, 0, sizeof (*sa));
137       t->input_sa_index = sa - im->sad;
138       sa->spi = args->remote_spi;
139       sa->tunnel_src_addr.ip4.as_u32 = args->remote_ip.as_u32;
140       sa->tunnel_dst_addr.ip4.as_u32 = args->local_ip.as_u32;
141       sa->is_tunnel = 1;
142       sa->use_esn = args->esn;
143       sa->use_anti_replay = args->anti_replay;
144       sa->integ_alg = args->integ_alg;
145       if (args->remote_integ_key_len <= sizeof (args->remote_integ_key))
146         {
147           sa->integ_key_len = args->remote_integ_key_len;
148           clib_memcpy (sa->integ_key, args->remote_integ_key,
149                        args->remote_integ_key_len);
150         }
151       sa->crypto_alg = args->crypto_alg;
152       if (args->remote_crypto_key_len <= sizeof (args->remote_crypto_key))
153         {
154           sa->crypto_key_len = args->remote_crypto_key_len;
155           clib_memcpy (sa->crypto_key, args->remote_crypto_key,
156                        args->remote_crypto_key_len);
157         }
158
159       if (im->cb.add_del_sa_sess_cb &&
160           im->cb.add_del_sa_sess_cb (t->input_sa_index, args->is_add) < 0)
161         return VNET_API_ERROR_SYSCALL_ERROR_1;
162
163       pool_get (im->sad, sa);
164       memset (sa, 0, sizeof (*sa));
165       t->output_sa_index = sa - im->sad;
166       sa->spi = args->local_spi;
167       sa->tunnel_src_addr.ip4.as_u32 = args->local_ip.as_u32;
168       sa->tunnel_dst_addr.ip4.as_u32 = args->remote_ip.as_u32;
169       sa->is_tunnel = 1;
170       sa->seq = 1;
171       sa->use_esn = args->esn;
172       sa->use_anti_replay = args->anti_replay;
173       sa->integ_alg = args->integ_alg;
174       if (args->local_integ_key_len <= sizeof (args->local_integ_key))
175         {
176           sa->integ_key_len = args->local_integ_key_len;
177           clib_memcpy (sa->integ_key, args->local_integ_key,
178                        args->local_integ_key_len);
179         }
180       sa->crypto_alg = args->crypto_alg;
181       if (args->local_crypto_key_len <= sizeof (args->local_crypto_key))
182         {
183           sa->crypto_key_len = args->local_crypto_key_len;
184           clib_memcpy (sa->crypto_key, args->local_crypto_key,
185                        args->local_crypto_key_len);
186         }
187
188       if (im->cb.add_del_sa_sess_cb &&
189           im->cb.add_del_sa_sess_cb (t->output_sa_index, args->is_add) < 0)
190         return VNET_API_ERROR_SYSCALL_ERROR_1;
191
192       hash_set (im->ipsec_if_pool_index_by_key, key,
193                 t - im->tunnel_interfaces);
194
195       if (vec_len (im->free_tunnel_if_indices) > 0)
196         {
197           hw_if_index =
198             im->free_tunnel_if_indices[vec_len (im->free_tunnel_if_indices) -
199                                        1];
200           _vec_len (im->free_tunnel_if_indices) -= 1;
201         }
202       else
203         {
204           hw_if_index =
205             vnet_register_interface (vnm, ipsec_device_class.index,
206                                      t - im->tunnel_interfaces,
207                                      ipsec_hw_class.index,
208                                      t - im->tunnel_interfaces);
209         }
210
211       hi = vnet_get_hw_interface (vnm, hw_if_index);
212       hi->output_node_index = ipsec_if_output_node.index;
213       t->hw_if_index = hw_if_index;
214
215       /*1st interface, register protocol */
216       if (pool_elts (im->tunnel_interfaces) == 1)
217         ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
218                                ipsec_if_input_node.index);
219
220     }
221   else
222     {
223       vnet_interface_main_t *vim = &vnm->interface_main;
224
225       /* check if exists */
226       if (!p)
227         return VNET_API_ERROR_INVALID_VALUE;
228
229       t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
230       hi = vnet_get_hw_interface (vnm, t->hw_if_index);
231       vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0);    /* admin down */
232       vec_add1 (im->free_tunnel_if_indices, t->hw_if_index);
233
234       vnet_interface_counter_lock (vim);
235       vlib_zero_combined_counter (vim->combined_sw_if_counters +
236                                   VNET_INTERFACE_COUNTER_TX, hi->sw_if_index);
237       vlib_zero_combined_counter (vim->combined_sw_if_counters +
238                                   VNET_INTERFACE_COUNTER_RX, hi->sw_if_index);
239       vnet_interface_counter_unlock (vim);
240
241       /* delete input and output SA */
242       sa = pool_elt_at_index (im->sad, t->input_sa_index);
243
244       if (im->cb.add_del_sa_sess_cb &&
245           im->cb.add_del_sa_sess_cb (t->input_sa_index, args->is_add) < 0)
246         return VNET_API_ERROR_SYSCALL_ERROR_1;
247
248       pool_put (im->sad, sa);
249
250       sa = pool_elt_at_index (im->sad, t->output_sa_index);
251
252       if (im->cb.add_del_sa_sess_cb &&
253           im->cb.add_del_sa_sess_cb (t->output_sa_index, args->is_add) < 0)
254         return VNET_API_ERROR_SYSCALL_ERROR_1;
255
256       pool_put (im->sad, sa);
257
258       hash_unset (im->ipsec_if_pool_index_by_key, key);
259       pool_put (im->tunnel_interfaces, t);
260     }
261
262   if (sw_if_index)
263     *sw_if_index = hi->sw_if_index;
264
265   return 0;
266 }
267
268 int
269 ipsec_add_del_ipsec_gre_tunnel (vnet_main_t * vnm,
270                                 ipsec_add_del_ipsec_gre_tunnel_args_t * args)
271 {
272   ipsec_tunnel_if_t *t = 0;
273   ipsec_main_t *im = &ipsec_main;
274   uword *p;
275   ipsec_sa_t *sa;
276   u64 key;
277   u32 isa, osa;
278
279   p = hash_get (im->sa_index_by_sa_id, args->local_sa_id);
280   if (!p)
281     return VNET_API_ERROR_INVALID_VALUE;
282   isa = p[0];
283
284   p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id);
285   if (!p)
286     return VNET_API_ERROR_INVALID_VALUE;
287   osa = p[0];
288   sa = pool_elt_at_index (im->sad, p[0]);
289
290   if (sa->is_tunnel)
291     key = (u64) sa->tunnel_dst_addr.ip4.as_u32 << 32 | (u64) sa->spi;
292   else
293     key = (u64) args->remote_ip.as_u32 << 32 | (u64) sa->spi;
294
295   p = hash_get (im->ipsec_if_pool_index_by_key, key);
296
297   if (args->is_add)
298     {
299       /* check if same src/dst pair exists */
300       if (p)
301         return VNET_API_ERROR_INVALID_VALUE;
302
303       pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
304       memset (t, 0, sizeof (*t));
305
306       t->input_sa_index = isa;
307       t->output_sa_index = osa;
308       t->hw_if_index = ~0;
309       hash_set (im->ipsec_if_pool_index_by_key, key,
310                 t - im->tunnel_interfaces);
311
312       /*1st interface, register protocol */
313       if (pool_elts (im->tunnel_interfaces) == 1)
314         ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
315                                ipsec_if_input_node.index);
316     }
317   else
318     {
319       /* check if exists */
320       if (!p)
321         return VNET_API_ERROR_INVALID_VALUE;
322
323       t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
324       hash_unset (im->ipsec_if_pool_index_by_key, key);
325       pool_put (im->tunnel_interfaces, t);
326     }
327   return 0;
328 }
329
330 int
331 ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
332                          ipsec_if_set_key_type_t type, u8 alg, u8 * key)
333 {
334   ipsec_main_t *im = &ipsec_main;
335   vnet_hw_interface_t *hi;
336   ipsec_tunnel_if_t *t;
337   ipsec_sa_t *sa;
338
339   hi = vnet_get_hw_interface (vnm, hw_if_index);
340   t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
341
342   if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO)
343     {
344       sa = pool_elt_at_index (im->sad, t->output_sa_index);
345       sa->crypto_alg = alg;
346       sa->crypto_key_len = vec_len (key);
347       clib_memcpy (sa->crypto_key, key, vec_len (key));
348
349       if (im->cb.add_del_sa_sess_cb &&
350           im->cb.add_del_sa_sess_cb (t->output_sa_index, 0) < 0)
351         return VNET_API_ERROR_SYSCALL_ERROR_1;
352     }
353   else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG)
354     {
355       sa = pool_elt_at_index (im->sad, t->output_sa_index);
356       sa->integ_alg = alg;
357       sa->integ_key_len = vec_len (key);
358       clib_memcpy (sa->integ_key, key, vec_len (key));
359
360       if (im->cb.add_del_sa_sess_cb &&
361           im->cb.add_del_sa_sess_cb (t->output_sa_index, 0) < 0)
362         return VNET_API_ERROR_SYSCALL_ERROR_1;
363     }
364   else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO)
365     {
366       sa = pool_elt_at_index (im->sad, t->input_sa_index);
367       sa->crypto_alg = alg;
368       sa->crypto_key_len = vec_len (key);
369       clib_memcpy (sa->crypto_key, key, vec_len (key));
370
371       if (im->cb.add_del_sa_sess_cb &&
372           im->cb.add_del_sa_sess_cb (t->input_sa_index, 0) < 0)
373         return VNET_API_ERROR_SYSCALL_ERROR_1;
374     }
375   else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG)
376     {
377       sa = pool_elt_at_index (im->sad, t->input_sa_index);
378       sa->integ_alg = alg;
379       sa->integ_key_len = vec_len (key);
380       clib_memcpy (sa->integ_key, key, vec_len (key));
381
382       if (im->cb.add_del_sa_sess_cb &&
383           im->cb.add_del_sa_sess_cb (t->input_sa_index, 0) < 0)
384         return VNET_API_ERROR_SYSCALL_ERROR_1;
385     }
386   else
387     return VNET_API_ERROR_INVALID_VALUE;
388
389   return 0;
390 }
391
392
393 clib_error_t *
394 ipsec_tunnel_if_init (vlib_main_t * vm)
395 {
396   ipsec_main_t *im = &ipsec_main;
397
398   im->ipsec_if_pool_index_by_key = hash_create (0, sizeof (uword));
399
400   return 0;
401 }
402
403 VLIB_INIT_FUNCTION (ipsec_tunnel_if_init);
404
405
406 /*
407  * fd.io coding-style-patch-verification: ON
408  *
409  * Local Variables:
410  * eval: (c-set-style "gnu")
411  * End:
412  */