FD.io VPP  v16.09
Vector Packet Processing
ipsec_if.c
Go to the documentation of this file.
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
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 =
45 
46 VNET_HW_INTERFACE_CLASS (ipsec_hw_class) =
47 {
48 .name = "IPSec",};
49 
50 
51 static int
54 
55 static int
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
66 {
68  (u8 *) args, sizeof (*args));
69  return 0;
70 }
71 
72 int
75 {
77  ipsec_main_t *im = &ipsec_main;
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 
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  {
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  {
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  {
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  {
141  args->local_crypto_key_len);
142  }
143 
145  t - im->tunnel_interfaces);
146 
147  if (vec_len (im->free_tunnel_if_indices) > 0)
148  {
149  hw_if_index =
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);
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 */
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 
192  pool_put (im->tunnel_interfaces, t);
193  }
194  return 0;
195 }
196 
197 int
200 {
201  ipsec_tunnel_if_t *t = 0;
202  ipsec_main_t *im = &ipsec_main;
203  uword *p;
204  ipsec_sa_t *sa;
205  u64 key;
206  u32 isa, osa;
207 
208  p = hash_get (im->sa_index_by_sa_id, args->local_sa_id);
209  if (!p)
210  return VNET_API_ERROR_INVALID_VALUE;
211  isa = p[0];
212 
213  p = hash_get (im->sa_index_by_sa_id, args->remote_sa_id);
214  if (!p)
215  return VNET_API_ERROR_INVALID_VALUE;
216  osa = p[0];
217  sa = pool_elt_at_index (im->sad, p[0]);
218 
219  if (sa->is_tunnel)
220  key = (u64) sa->tunnel_dst_addr.ip4.as_u32 << 32 | (u64) sa->spi;
221  else
222  key = (u64) args->remote_ip.as_u32 << 32 | (u64) sa->spi;
223 
224  p = hash_get (im->ipsec_if_pool_index_by_key, key);
225 
226  if (args->is_add)
227  {
228  /* check if same src/dst pair exists */
229  if (p)
230  return VNET_API_ERROR_INVALID_VALUE;
231 
233  memset (t, 0, sizeof (*t));
234 
235  t->input_sa_index = isa;
236  t->output_sa_index = osa;
237  t->hw_if_index = ~0;
239  t - im->tunnel_interfaces);
240 
241  /*1st interface, register protocol */
242  if (pool_elts (im->tunnel_interfaces) == 1)
243  ip4_register_protocol (IP_PROTOCOL_IPSEC_ESP,
244  ipsec_if_input_node.index);
245  }
246  else
247  {
248  /* check if exists */
249  if (!p)
250  return VNET_API_ERROR_INVALID_VALUE;
251 
252  t = pool_elt_at_index (im->tunnel_interfaces, p[0]);
254  pool_put (im->tunnel_interfaces, t);
255  }
256  return 0;
257 }
258 
259 int
261  ipsec_if_set_key_type_t type, u8 alg, u8 * key)
262 {
263  ipsec_main_t *im = &ipsec_main;
266  ipsec_sa_t *sa;
267 
268  hi = vnet_get_hw_interface (vnm, hw_if_index);
270 
272  {
273  sa = pool_elt_at_index (im->sad, t->output_sa_index);
274  sa->crypto_alg = alg;
275  sa->crypto_key_len = vec_len (key);
276  clib_memcpy (sa->crypto_key, key, vec_len (key));
277  }
278  else if (type == IPSEC_IF_SET_KEY_TYPE_LOCAL_INTEG)
279  {
280  sa = pool_elt_at_index (im->sad, t->output_sa_index);
281  sa->integ_alg = alg;
282  sa->integ_key_len = vec_len (key);
283  clib_memcpy (sa->integ_key, key, vec_len (key));
284  }
285  else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_CRYPTO)
286  {
287  sa = pool_elt_at_index (im->sad, t->input_sa_index);
288  sa->crypto_alg = alg;
289  sa->crypto_key_len = vec_len (key);
290  clib_memcpy (sa->crypto_key, key, vec_len (key));
291  }
292  else if (type == IPSEC_IF_SET_KEY_TYPE_REMOTE_INTEG)
293  {
294  sa = pool_elt_at_index (im->sad, t->input_sa_index);
295  sa->integ_alg = alg;
296  sa->integ_key_len = vec_len (key);
297  clib_memcpy (sa->integ_key, key, vec_len (key));
298  }
299  else
300  return VNET_API_ERROR_INVALID_VALUE;
301 
302  return 0;
303 }
304 
305 
306 clib_error_t *
308 {
309  ipsec_main_t *im = &ipsec_main;
310 
311  im->ipsec_if_pool_index_by_key = hash_create (0, sizeof (uword));
312 
313  return 0;
314 }
315 
317 
318 
319 /*
320  * fd.io coding-style-patch-verification: ON
321  *
322  * Local Variables:
323  * eval: (c-set-style "gnu")
324  * End:
325  */
vmrglw vmrglh hi
VNET_DEVICE_CLASS(ipsec_device_class, static)
#define hash_set(h, key, value)
Definition: hash.h:254
ipsec_tunnel_if_t * tunnel_interfaces
Definition: ipsec.h:211
ip46_address_t tunnel_src_addr
Definition: ipsec.h:91
#define hash_unset(h, key)
Definition: hash.h:260
a
Definition: bitmap.h:516
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
ipsec_integ_alg_t integ_alg
Definition: ipsec.h:82
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:482
ipsec_main_t ipsec_main
Definition: ipsec.h:238
int ipsec_add_del_tunnel_if(ipsec_add_del_tunnel_args_t *args)
Definition: ipsec_if.c:65
static int ipsec_add_del_tunnel_if_internal(vnet_main_t *vnm, ipsec_add_del_tunnel_args_t *args)
Definition: ipsec_if.c:73
u8 is_tunnel
Definition: ipsec.h:89
static uword dummy_interface_tx(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: ipsec_if.c:34
ipsec_if_set_key_type_t
Definition: ipsec.h:141
void ip4_register_protocol(u32 protocol, u32 node_index)
Definition: ip4_forward.c:2320
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:200
uword * ipsec_if_pool_index_by_key
Definition: ipsec.h:229
u8 crypto_key[128]
Definition: ipsec.h:80
u32 spi
Definition: ipsec.h:75
vlib_node_registration_t ipsec_if_input_node
(constructor) VLIB_REGISTER_NODE (ipsec_if_input_node)
Definition: ipsec_if_in.c:144
clib_error_t * ipsec_tunnel_if_init(vlib_main_t *vm)
Definition: ipsec_if.c:307
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
u8 integ_key[128]
Definition: ipsec.h:84
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
u8 use_esn
Definition: ipsec.h:86
ip4_address_t remote_ip
Definition: ipsec.h:117
#define clib_warning(format, args...)
Definition: error.h:59
unsigned long u64
Definition: types.h:89
int ipsec_set_interface_key(vnet_main_t *vnm, u32 hw_if_index, ipsec_if_set_key_type_t type, u8 alg, u8 *key)
Definition: ipsec_if.c:260
u32 vnet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, u32 hw_class_index, u32 hw_instance)
Definition: interface.c:650
#define hash_get(h, key)
Definition: hash.h:248
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:369
uword os_get_cpu_number(void)
Definition: unix-misc.c:224
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:214
u8 * format_ipsec_if_output_trace(u8 *s, va_list *args)
Definition: ipsec_if_out.c:58
ip46_address_t tunnel_dst_addr
Definition: ipsec.h:92
#define pool_get_aligned(P, E, A)
Allocate an object E from a pool P (general version).
Definition: pool.h:169
vlib_node_registration_t ipsec_if_output_node
(constructor) VLIB_REGISTER_NODE (ipsec_if_output_node)
Definition: ipsec_if_out.c:131
ipsec_crypto_alg_t crypto_alg
Definition: ipsec.h:120
u16 n_vectors
Definition: node.h:344
void vl_api_rpc_call_main_thread(void *fp, u8 *data, u32 data_length)
Definition: memory_vlib.c:1321
#define clib_memcpy(a, b, c)
Definition: string.h:63
uword * sa_index_by_sa_id
Definition: ipsec.h:228
u32 output_sa_index
Definition: ipsec.h:200
int ipsec_add_del_ipsec_gre_tunnel(vnet_main_t *vnm, ipsec_add_del_ipsec_gre_tunnel_args_t *args)
Definition: ipsec_if.c:198
#define hash_create(elts, value_bytes)
Definition: hash.h:647
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
static int ipsec_add_del_tunnel_if_rpc_callback(ipsec_add_del_tunnel_args_t *a)
Definition: ipsec_if.c:56
ipsec_integ_alg_t integ_alg
Definition: ipsec.h:125
ip4_address_t local_ip
Definition: ipsec.h:117
ipsec_sa_t * sad
Definition: ipsec.h:208
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:418
u8 integ_key_len
Definition: ipsec.h:83
u32 input_sa_index
Definition: ipsec.h:199
u32 seq
Definition: ipsec.h:95
u64 uword
Definition: types.h:112
u8 crypto_key_len
Definition: ipsec.h:79
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
VNET_HW_INTERFACE_CLASS(ipsec_hw_class)
u32 * free_tunnel_if_indices
Definition: ipsec.h:212
ipsec_crypto_alg_t crypto_alg
Definition: ipsec.h:78
static u8 * format_ipsec_name(u8 *s, va_list *args)
Definition: ipsec_if.c:27
clib_error_t * vnet_sw_interface_set_flags(vnet_main_t *vnm, u32 sw_if_index, u32 flags)
Definition: interface.c:521
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u8 use_anti_replay
Definition: ipsec.h:87
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:109