VPP-256 - Coding style cleanup vnet/vnet/ipsec
[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 VNET_HW_INTERFACE_CLASS (ipsec_hw_class) =
47 {
48 .name = "IPSec",};
49
50
51 static int
52 ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
53                                   ipsec_add_del_tunnel_args_t * args);
54
55 static int
56 ipsec_add_del_tunnel_if_rpc_callback (ipsec_add_del_tunnel_args_t * a)
57 {
58   vnet_main_t *vnm = vnet_get_main ();
59   ASSERT (os_get_cpu_number () == 0);
60
61   return ipsec_add_del_tunnel_if_internal (vnm, a);
62 }
63
64 int
65 ipsec_add_del_tunnel_if (ipsec_add_del_tunnel_args_t * args)
66 {
67   vl_api_rpc_call_main_thread (ipsec_add_del_tunnel_if_rpc_callback,
68                                (u8 *) args, sizeof (*args));
69   return 0;
70 }
71
72 int
73 ipsec_add_del_tunnel_if_internal (vnet_main_t * vnm,
74                                   ipsec_add_del_tunnel_args_t * args)
75 {
76   ipsec_tunnel_if_t *t;
77   ipsec_main_t *im = &ipsec_main;
78   vnet_hw_interface_t *hi;
79   u32 hw_if_index = ~0;
80   uword *p;
81   ipsec_sa_t *sa;
82
83   u64 key = (u64) args->remote_ip.as_u32 << 32 | (u64) args->remote_spi;
84   p = hash_get (im->ipsec_if_pool_index_by_key, key);
85
86   if (args->is_add)
87     {
88       /* check if same src/dst pair exists */
89       if (p)
90         return VNET_API_ERROR_INVALID_VALUE;
91
92       pool_get_aligned (im->tunnel_interfaces, t, CLIB_CACHE_LINE_BYTES);
93       memset (t, 0, sizeof (*t));
94
95       pool_get (im->sad, sa);
96       memset (sa, 0, sizeof (*sa));
97       t->input_sa_index = sa - im->sad;
98       sa->spi = args->remote_spi;
99       sa->tunnel_src_addr.ip4.as_u32 = args->remote_ip.as_u32;
100       sa->tunnel_dst_addr.ip4.as_u32 = args->local_ip.as_u32;
101       sa->is_tunnel = 1;
102       sa->use_esn = args->esn;
103       sa->use_anti_replay = args->anti_replay;
104       sa->integ_alg = args->integ_alg;
105       if (args->remote_integ_key_len <= sizeof (args->remote_integ_key))
106         {
107           sa->integ_key_len = args->remote_integ_key_len;
108           clib_memcpy (sa->integ_key, args->remote_integ_key,
109                        args->remote_integ_key_len);
110         }
111       sa->crypto_alg = args->crypto_alg;
112       if (args->remote_crypto_key_len <= sizeof (args->remote_crypto_key))
113         {
114           sa->crypto_key_len = args->remote_crypto_key_len;
115           clib_memcpy (sa->crypto_key, args->remote_crypto_key,
116                        args->remote_crypto_key_len);
117         }
118
119       pool_get (im->sad, sa);
120       memset (sa, 0, sizeof (*sa));
121       t->output_sa_index = sa - im->sad;
122       sa->spi = args->local_spi;
123       sa->tunnel_src_addr.ip4.as_u32 = args->local_ip.as_u32;
124       sa->tunnel_dst_addr.ip4.as_u32 = args->remote_ip.as_u32;
125       sa->is_tunnel = 1;
126       sa->seq = 1;
127       sa->use_esn = args->esn;
128       sa->use_anti_replay = args->anti_replay;
129       sa->integ_alg = args->integ_alg;
130       if (args->local_integ_key_len <= sizeof (args->local_integ_key))
131         {
132           sa->integ_key_len = args->local_integ_key_len;
133           clib_memcpy (sa->integ_key, args->local_integ_key,
134                        args->local_integ_key_len);
135         }
136       sa->crypto_alg = args->crypto_alg;
137       if (args->local_crypto_key_len <= sizeof (args->local_crypto_key))
138         {
139           sa->crypto_key_len = args->local_crypto_key_len;
140           clib_memcpy (sa->crypto_key, args->local_crypto_key,
141                        args->local_crypto_key_len);
142         }
143
144       hash_set (im->ipsec_if_pool_index_by_key, key,
145                 t - im->tunnel_interfaces);
146
147       if (vec_len (im->free_tunnel_if_indices) > 0)
148         {
149           hw_if_index =
150             im->free_tunnel_if_indices[vec_len (im->free_tunnel_if_indices) -
151                                        1];
152           _vec_len (im->free_tunnel_if_indices) -= 1;
153         }
154       else
155         {
156           hw_if_index =
157             vnet_register_interface (vnm, ipsec_device_class.index,
158                                      t - im->tunnel_interfaces,
159                                      ipsec_hw_class.index,
160                                      t - im->tunnel_interfaces);
161
162           hi = vnet_get_hw_interface (vnm, hw_if_index);
163           hi->output_node_index = ipsec_if_output_node.index;
164         }
165       t->hw_if_index = hw_if_index;
166
167       /*1st interface, register protocol */
168       if (pool_elts (im->tunnel_interfaces) == 1)
169         ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
170                                ipsec_if_input_node.index);
171
172       return hw_if_index;
173     }
174   else
175     {
176       /* check if exists */
177       if (!p)
178         return VNET_API_ERROR_INVALID_VALUE;
179
180       t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
181       hi = vnet_get_hw_interface (vnm, t->hw_if_index);
182       vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0);    /* admin down */
183       vec_add1 (im->free_tunnel_if_indices, t->hw_if_index);
184
185       /* delete input and output SA */
186       sa = pool_elt_at_index (im->sad, t->input_sa_index);
187       pool_put (im->sad, sa);
188       sa = pool_elt_at_index (im->sad, t->output_sa_index);
189       pool_put (im->sad, sa);
190
191       hash_unset (im->ipsec_if_pool_index_by_key, key);
192       pool_put (im->tunnel_interfaces, t);
193     }
194   return 0;
195 }
196
197 int
198 ipsec_set_interface_key (vnet_main_t * vnm, u32 hw_if_index,
199                          ipsec_if_set_key_type_t type, u8 alg, u8 * key)
200 {
201   ipsec_main_t *im = &ipsec_main;
202   vnet_hw_interface_t *hi;
203   ipsec_tunnel_if_t *t;
204   ipsec_sa_t *sa;
205
206   hi = vnet_get_hw_interface (vnm, hw_if_index);
207   t = pool_elt_at_index (im->tunnel_interfaces, hi->dev_instance);
208
209   if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_CRYPTO)
210     {
211       sa = pool_elt_at_index (im->sad, t->output_sa_index);
212       sa->crypto_alg = alg;
213       sa->crypto_key_len = vec_len (key);
214       clib_memcpy (sa->crypto_key, key, vec_len (key));
215     }
216   else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG)
217     {
218       sa = pool_elt_at_index (im->sad, t->output_sa_index);
219       sa->integ_alg = alg;
220       sa->integ_key_len = vec_len (key);
221       clib_memcpy (sa->integ_key, key, vec_len (key));
222     }
223   else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO)
224     {
225       sa = pool_elt_at_index (im->sad, t->input_sa_index);
226       sa->crypto_alg = alg;
227       sa->crypto_key_len = vec_len (key);
228       clib_memcpy (sa->crypto_key, key, vec_len (key));
229     }
230   else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG)
231     {
232       sa = pool_elt_at_index (im->sad, t->input_sa_index);
233       sa->integ_alg = alg;
234       sa->integ_key_len = vec_len (key);
235       clib_memcpy (sa->integ_key, key, vec_len (key));
236     }
237   else
238     return VNET_API_ERROR_INVALID_VALUE;
239
240   return 0;
241 }
242
243
244 clib_error_t *
245 ipsec_tunnel_if_init (vlib_main_t * vm)
246 {
247   ipsec_main_t *im = &ipsec_main;
248
249   im->ipsec_if_pool_index_by_key = hash_create (0, sizeof (uword));
250
251   return 0;
252 }
253
254 VLIB_INIT_FUNCTION (ipsec_tunnel_if_init);
255
256
257 /*
258  * fd.io coding-style-patch-verification: ON
259  *
260  * Local Variables:
261  * eval: (c-set-style "gnu")
262  * End:
263  */