FD.io VPP  v18.10-34-gcce845e
Vector Packet Processing
esp_decrypt.c
Go to the documentation of this file.
1 /*
2  * esp_decrypt.c : IPSec ESP decrypt node
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 #define foreach_esp_decrypt_next \
26 _(DROP, "error-drop") \
27 _(IP4_INPUT, "ip4-input") \
28 _(IP6_INPUT, "ip6-input") \
29 _(IPSEC_GRE_INPUT, "ipsec-gre-input")
30 
31 #define _(v, s) ESP_DECRYPT_NEXT_##v,
32 typedef enum
33 {
35 #undef _
38 
39 
40 #define foreach_esp_decrypt_error \
41  _(RX_PKTS, "ESP pkts received") \
42  _(NO_BUFFER, "No buffer (packed dropped)") \
43  _(DECRYPTION_FAILED, "ESP decryption failed") \
44  _(INTEG_ERROR, "Integrity check failed") \
45  _(REPLAY, "SA replayed packet") \
46  _(NOT_IP, "Not IP packet (dropped)")
47 
48 
49 typedef enum
50 {
51 #define _(sym,str) ESP_DECRYPT_ERROR_##sym,
53 #undef _
56 
57 static char *esp_decrypt_error_strings[] = {
58 #define _(sym,string) string,
60 #undef _
61 };
62 
63 typedef struct
64 {
68 
69 /* packet trace format function */
70 static u8 *
71 format_esp_decrypt_trace (u8 * s, va_list * args)
72 {
73  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
74  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
75  esp_decrypt_trace_t *t = va_arg (*args, esp_decrypt_trace_t *);
76 
77  s = format (s, "esp: crypto %U integrity %U",
80  return s;
81 }
82 
83 always_inline void
85  u8 * in, u8 * out, size_t in_len, u8 * key, u8 * iv)
86 {
88  u32 thread_index = vlib_get_thread_index ();
89 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
90  EVP_CIPHER_CTX *ctx = em->per_thread_data[thread_index].decrypt_ctx;
91 #else
92  EVP_CIPHER_CTX *ctx = &(em->per_thread_data[thread_index].decrypt_ctx);
93 #endif
94  const EVP_CIPHER *cipher = NULL;
95  int out_len;
96 
97  ASSERT (alg < IPSEC_CRYPTO_N_ALG);
98 
100  return;
101 
102  if (PREDICT_FALSE
103  (alg != em->per_thread_data[thread_index].last_decrypt_alg))
104  {
105  cipher = em->ipsec_proto_main_crypto_algs[alg].type;
106  em->per_thread_data[thread_index].last_decrypt_alg = alg;
107  }
108 
109  EVP_DecryptInit_ex (ctx, cipher, NULL, key, iv);
110 
111  EVP_DecryptUpdate (ctx, out, &out_len, in, in_len);
112  EVP_DecryptFinal_ex (ctx, out + out_len, &out_len);
113 }
114 
115 static uword
117  vlib_node_runtime_t * node, vlib_frame_t * from_frame)
118 {
119  u32 n_left_from, *from, next_index, *to_next;
120  ipsec_main_t *im = &ipsec_main;
122  u32 *recycle = 0;
123  from = vlib_frame_vector_args (from_frame);
124  n_left_from = from_frame->n_vectors;
125  u32 thread_index = vlib_get_thread_index ();
126 
127  ipsec_alloc_empty_buffers (vm, im);
128 
129  u32 *empty_buffers = im->empty_buffers[thread_index];
130 
131  if (PREDICT_FALSE (vec_len (empty_buffers) < n_left_from))
132  {
134  ESP_DECRYPT_ERROR_NO_BUFFER, n_left_from);
135  goto free_buffers_and_exit;
136  }
137 
138  next_index = node->cached_next_index;
139 
140  while (n_left_from > 0)
141  {
142  u32 n_left_to_next;
143 
144  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
145 
146  while (n_left_from > 0 && n_left_to_next > 0)
147  {
148  u32 i_bi0, o_bi0 = (u32) ~ 0, next0;
149  vlib_buffer_t *i_b0;
150  vlib_buffer_t *o_b0 = 0;
151  esp_header_t *esp0;
152  ipsec_sa_t *sa0;
153  u32 sa_index0 = ~0;
154  u32 seq;
155  ip4_header_t *ih4 = 0, *oh4 = 0;
156  ip6_header_t *ih6 = 0, *oh6 = 0;
157  u8 tunnel_mode = 1;
158  u8 transport_ip6 = 0;
159 
160 
161  i_bi0 = from[0];
162  from += 1;
163  n_left_from -= 1;
164  n_left_to_next -= 1;
165 
166  next0 = ESP_DECRYPT_NEXT_DROP;
167 
168  i_b0 = vlib_get_buffer (vm, i_bi0);
169  esp0 = vlib_buffer_get_current (i_b0);
170 
171  sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
172  sa0 = pool_elt_at_index (im->sad, sa_index0);
173 
174  seq = clib_host_to_net_u32 (esp0->seq);
175 
176  /* anti-replay check */
177  if (sa0->use_anti_replay)
178  {
179  int rv = 0;
180 
181  if (PREDICT_TRUE (sa0->use_esn))
182  rv = esp_replay_check_esn (sa0, seq);
183  else
184  rv = esp_replay_check (sa0, seq);
185 
186  if (PREDICT_FALSE (rv))
187  {
189  ESP_DECRYPT_ERROR_REPLAY, 1);
190  o_bi0 = i_bi0;
191  to_next[0] = o_bi0;
192  to_next += 1;
193  goto trace;
194  }
195  }
196 
197  sa0->total_data_size += i_b0->current_length;
198 
199  if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
200  {
201  u8 sig[64];
202  int icv_size =
204  memset (sig, 0, sizeof (sig));
205  u8 *icv =
206  vlib_buffer_get_current (i_b0) + i_b0->current_length -
207  icv_size;
208  i_b0->current_length -= icv_size;
209 
210  hmac_calc (sa0->integ_alg, sa0->integ_key, sa0->integ_key_len,
211  (u8 *) esp0, i_b0->current_length, sig, sa0->use_esn,
212  sa0->seq_hi);
213 
214  if (PREDICT_FALSE (memcmp (icv, sig, icv_size)))
215  {
217  ESP_DECRYPT_ERROR_INTEG_ERROR,
218  1);
219  o_bi0 = i_bi0;
220  to_next[0] = o_bi0;
221  to_next += 1;
222  goto trace;
223  }
224  }
225 
226  if (PREDICT_TRUE (sa0->use_anti_replay))
227  {
228  if (PREDICT_TRUE (sa0->use_esn))
229  esp_replay_advance_esn (sa0, seq);
230  else
231  esp_replay_advance (sa0, seq);
232  }
233 
234  /* grab free buffer */
235  uword last_empty_buffer = vec_len (empty_buffers) - 1;
236  o_bi0 = empty_buffers[last_empty_buffer];
237  to_next[0] = o_bi0;
238  to_next += 1;
239  o_b0 = vlib_get_buffer (vm, o_bi0);
241  empty_buffers[last_empty_buffer -
242  1], STORE);
243  _vec_len (empty_buffers) = last_empty_buffer;
244 
245  /* add old buffer to the recycle list */
246  vec_add1 (recycle, i_bi0);
247 
248  if ((sa0->crypto_alg >= IPSEC_CRYPTO_ALG_AES_CBC_128 &&
249  sa0->crypto_alg <= IPSEC_CRYPTO_ALG_AES_CBC_256) ||
250  (sa0->crypto_alg >= IPSEC_CRYPTO_ALG_DES_CBC &&
251  sa0->crypto_alg <= IPSEC_CRYPTO_ALG_3DES_CBC))
252  {
253  const int BLOCK_SIZE =
255  const int IV_SIZE =
257  esp_footer_t *f0;
258  u8 ip_hdr_size = 0;
259 
260  int blocks =
261  (i_b0->current_length - sizeof (esp_header_t) -
262  IV_SIZE) / BLOCK_SIZE;
263 
264  o_b0->current_data = sizeof (ethernet_header_t);
265 
266  /* transport mode */
267  if (PREDICT_FALSE (!sa0->is_tunnel && !sa0->is_tunnel_ip6))
268  {
269  tunnel_mode = 0;
270 
271  if (i_b0->flags & VNET_BUFFER_F_IS_IP4)
272  ih4 =
273  (ip4_header_t *) ((u8 *) esp0 - sizeof (ip4_header_t));
274  else
275  ih4 =
276  (ip4_header_t *) ((u8 *) esp0 - sizeof (ip6_header_t));
277 
278  if (PREDICT_TRUE
279  ((ih4->ip_version_and_header_length & 0xF0) != 0x40))
280  {
281  if (PREDICT_TRUE
282  ((ih4->ip_version_and_header_length & 0xF0) ==
283  0x60))
284  {
285  transport_ip6 = 1;
286  ip_hdr_size = sizeof (ip6_header_t);
287  ih6 = (ip6_header_t *) ih4;
288  oh6 = vlib_buffer_get_current (o_b0);
289  }
290  else
291  {
293  esp_decrypt_node.index,
294  ESP_DECRYPT_ERROR_NOT_IP,
295  1);
296  o_b0 = 0;
297  goto trace;
298  }
299  }
300  else
301  {
302  oh4 = vlib_buffer_get_current (o_b0);
303  ip_hdr_size = sizeof (ip4_header_t);
304  }
305  }
306 
308  esp0->data + IV_SIZE,
309  (u8 *) vlib_buffer_get_current (o_b0) +
310  ip_hdr_size, BLOCK_SIZE * blocks,
311  sa0->crypto_key, esp0->data);
312 
313  o_b0->current_length = (blocks * BLOCK_SIZE) - 2 + ip_hdr_size;
314  o_b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
315  f0 =
316  (esp_footer_t *) ((u8 *) vlib_buffer_get_current (o_b0) +
317  o_b0->current_length);
318  o_b0->current_length -= f0->pad_length;
319 
320  /* tunnel mode */
321  if (PREDICT_TRUE (tunnel_mode))
322  {
323  if (PREDICT_TRUE (f0->next_header == IP_PROTOCOL_IP_IN_IP))
324  {
325  next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
326  oh4 = vlib_buffer_get_current (o_b0);
327  }
328  else if (f0->next_header == IP_PROTOCOL_IPV6)
329  next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
330  else
331  {
333  ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
334  1);
335  o_b0 = 0;
336  goto trace;
337  }
338  }
339  /* transport mode */
340  else
341  {
342  if (PREDICT_FALSE (transport_ip6))
343  {
344  next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
345  oh6->ip_version_traffic_class_and_flow_label =
347  oh6->protocol = f0->next_header;
348  oh6->hop_limit = ih6->hop_limit;
349  oh6->src_address.as_u64[0] = ih6->src_address.as_u64[0];
350  oh6->src_address.as_u64[1] = ih6->src_address.as_u64[1];
351  oh6->dst_address.as_u64[0] = ih6->dst_address.as_u64[0];
352  oh6->dst_address.as_u64[1] = ih6->dst_address.as_u64[1];
353  oh6->payload_length =
354  clib_host_to_net_u16 (vlib_buffer_length_in_chain
355  (vm,
356  o_b0) - sizeof (ip6_header_t));
357  }
358  else
359  {
360  next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
361  oh4->ip_version_and_header_length = 0x45;
362  oh4->tos = ih4->tos;
363  oh4->fragment_id = 0;
364  oh4->flags_and_fragment_offset = 0;
365  oh4->ttl = ih4->ttl;
366  oh4->protocol = f0->next_header;
367  oh4->src_address.as_u32 = ih4->src_address.as_u32;
368  oh4->dst_address.as_u32 = ih4->dst_address.as_u32;
369  oh4->length =
370  clib_host_to_net_u16 (vlib_buffer_length_in_chain
371  (vm, o_b0));
372  oh4->checksum = ip4_header_checksum (oh4);
373  }
374  }
375 
376  /* for IPSec-GRE tunnel next node is ipsec-gre-input */
377  if (PREDICT_FALSE
378  ((vnet_buffer (i_b0)->ipsec.flags) &
380  next0 = ESP_DECRYPT_NEXT_IPSEC_GRE_INPUT;
381 
382  vnet_buffer (o_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
383  vnet_buffer (o_b0)->sw_if_index[VLIB_RX] =
384  vnet_buffer (i_b0)->sw_if_index[VLIB_RX];
385  }
386 
387  trace:
388  if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
389  {
390  if (o_b0)
391  {
392  o_b0->flags |= VLIB_BUFFER_IS_TRACED;
393  o_b0->trace_index = i_b0->trace_index;
394  esp_decrypt_trace_t *tr =
395  vlib_add_trace (vm, node, o_b0, sizeof (*tr));
396  tr->crypto_alg = sa0->crypto_alg;
397  tr->integ_alg = sa0->integ_alg;
398  }
399  }
400 
401  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
402  n_left_to_next, o_bi0, next0);
403  }
404  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
405  }
407  ESP_DECRYPT_ERROR_RX_PKTS,
408  from_frame->n_vectors);
409 
410 free_buffers_and_exit:
411  if (recycle)
412  vlib_buffer_free (vm, recycle, vec_len (recycle));
413  vec_free (recycle);
414  return from_frame->n_vectors;
415 }
416 
417 
418 /* *INDENT-OFF* */
420  .function = esp_decrypt_node_fn,
421  .name = "esp-decrypt",
422  .vector_size = sizeof (u32),
423  .format_trace = format_esp_decrypt_trace,
424  .type = VLIB_NODE_TYPE_INTERNAL,
425 
427  .error_strings = esp_decrypt_error_strings,
428 
429  .n_next_nodes = ESP_DECRYPT_N_NEXT,
430  .next_nodes = {
431 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
433 #undef _
434  },
435 };
436 /* *INDENT-ON* */
437 
439 /*
440  * fd.io coding-style-patch-verification: ON
441  *
442  * Local Variables:
443  * eval: (c-set-style "gnu")
444  * End:
445  */
static vlib_cli_command_t trace
(constructor) VLIB_CLI_COMMAND (trace)
Definition: vlib_api_cli.c:862
static u8 * format_esp_decrypt_trace(u8 *s, va_list *args)
Definition: esp_decrypt.c:71
#define CLIB_UNUSED(x)
Definition: clib.h:81
static char * esp_decrypt_error_strings[]
Definition: esp_decrypt.c:57
static void vlib_buffer_free(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers Frees the entire buffer chain for each buffer.
Definition: buffer_funcs.h:547
ip4_address_t src_address
Definition: ip4_packet.h:169
ipsec_proto_main_integ_alg_t * ipsec_proto_main_integ_algs
Definition: esp.h:101
#define PREDICT_TRUE(x)
Definition: clib.h:108
u64 as_u64[2]
Definition: ip6_packet.h:51
static void esp_replay_advance(ipsec_sa_t *sa, u32 seq)
Definition: esp.h:175
#define NULL
Definition: clib.h:57
ipsec_crypto_alg_t last_decrypt_alg
Definition: esp.h:94
static unsigned int hmac_calc(ipsec_integ_alg_t alg, u8 *key, int key_len, u8 *data, int data_len, u8 *signature, u8 use_esn, u32 seq_hi)
Definition: esp.h:331
ipsec_integ_alg_t integ_alg
Definition: ipsec.h:121
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:523
ipsec_crypto_alg_t crypto_alg
Definition: esp_decrypt.c:65
u8 is_tunnel
Definition: ipsec.h:128
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
const EVP_CIPHER * type
Definition: esp.h:62
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:263
ipsec_proto_main_crypto_alg_t * ipsec_proto_main_crypto_algs
Definition: esp.h:100
ip6_address_t src_address
Definition: ip6_packet.h:378
esp_decrypt_next_t
Definition: esp_decrypt.c:32
unsigned char u8
Definition: types.h:56
u8 crypto_key[128]
Definition: ipsec.h:119
u32 seq_hi
Definition: ipsec.h:138
memset(h->entries, 0, sizeof(h->entries[0])*entries)
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:109
u8 integ_key[128]
Definition: ipsec.h:123
#define vlib_prefetch_buffer_with_index(vm, bi, type)
Prefetch buffer metadata by buffer index The first 64 bytes of buffer contains most header informatio...
Definition: buffer_funcs.h:337
static void esp_replay_advance_esn(ipsec_sa_t *sa, u32 seq)
Definition: esp.h:196
ipsec_main_t ipsec_main
Definition: ipsec.c:30
#define always_inline
Definition: clib.h:94
ip4_address_t dst_address
Definition: ip4_packet.h:169
u8 use_esn
Definition: ipsec.h:125
static void esp_decrypt_cbc(ipsec_crypto_alg_t alg, u8 *in, u8 *out, size_t in_len, u8 *key, u8 *iv)
Definition: esp_decrypt.c:84
u8 data[0]
Definition: esp.h:29
unsigned int u32
Definition: types.h:88
#define foreach_esp_decrypt_error
Definition: esp_decrypt.c:40
u8 * format_ipsec_crypto_alg(u8 *s, va_list *args)
Definition: ipsec_format.c:58
#define VLIB_NODE_FUNCTION_MULTIARCH(node, fn)
Definition: node.h:209
ipsec_proto_main_per_thread_data_t * per_thread_data
Definition: esp.h:102
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:464
ipsec_integ_alg_t
Definition: ipsec.h:97
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
u8 is_tunnel_ip6
Definition: ipsec.h:129
static int esp_replay_check_esn(ipsec_sa_t *sa, u32 seq)
Definition: esp.h:131
long ctx[MAX_CONNS]
Definition: main.c:144
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:205
#define PREDICT_FALSE(x)
Definition: clib.h:107
vlib_node_registration_t esp_decrypt_node
(constructor) VLIB_REGISTER_NODE (esp_decrypt_node)
Definition: esp_decrypt.c:419
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:218
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:364
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1176
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:155
u16 n_vectors
Definition: node.h:401
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:211
vlib_main_t * vm
Definition: buffer.c:294
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:339
static void ipsec_alloc_empty_buffers(vlib_main_t *vm, ipsec_main_t *im)
Definition: ipsec.h:356
EVP_CIPHER_CTX decrypt_ctx
Definition: esp.h:85
#define ARRAY_LEN(x)
Definition: clib.h:61
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:455
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:513
#define ASSERT(truth)
static int esp_replay_check(ipsec_sa_t *sa, u32 seq)
Definition: esp.h:113
esp_decrypt_error_t
Definition: esp_decrypt.c:49
static uword esp_decrypt_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: esp_decrypt.c:116
ipsec_sa_t * sad
Definition: ipsec.h:265
ipsec_crypto_alg_t
Definition: ipsec.h:80
u64 total_data_size
Definition: ipsec.h:144
u8 integ_key_len
Definition: ipsec.h:122
u32 seq
Definition: esp.h:28
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:57
u8 * format_ipsec_integ_alg(u8 *s, va_list *args)
Definition: ipsec_format.c:90
u32 ip_version_traffic_class_and_flow_label
Definition: ip6_packet.h:365
Definition: defs.h:47
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:267
#define vnet_buffer(b)
Definition: buffer.h:344
#define foreach_esp_decrypt_next
Definition: esp_decrypt.c:25
ipsec_crypto_alg_t crypto_alg
Definition: ipsec.h:117
ipsec_proto_main_t ipsec_proto_main
Definition: esp_encrypt.c:26
u8 ip_version_and_header_length
Definition: ip4_packet.h:137
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:116
u32 ** empty_buffers
Definition: ipsec.h:271
u8 use_anti_replay
Definition: ipsec.h:126
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:58
u32 trace_index
Specifies index into trace buffer if VLIB_PACKET_IS_TRACED flag is set.
Definition: buffer.h:150
#define IPSEC_FLAG_IPSEC_GRE_TUNNEL
Definition: ipsec.h:21
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:246
ipsec_integ_alg_t integ_alg
Definition: esp_decrypt.c:66
Definition: defs.h:46
ip6_address_t dst_address
Definition: ip6_packet.h:378