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