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