FD.io VPP  v17.04.2-2-ga8f93f8
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 {
87  esp_main_t *em = &esp_main;
88  u32 cpu_index = os_get_cpu_number ();
89  EVP_CIPHER_CTX *ctx = &(em->per_thread_data[cpu_index].decrypt_ctx);
90  const EVP_CIPHER *cipher = NULL;
91  int out_len;
92 
93  ASSERT (alg < IPSEC_CRYPTO_N_ALG);
94 
95  if (PREDICT_FALSE (em->esp_crypto_algs[alg].type == 0))
96  return;
97 
98  if (PREDICT_FALSE (alg != em->per_thread_data[cpu_index].last_decrypt_alg))
99  {
100  cipher = em->esp_crypto_algs[alg].type;
101  em->per_thread_data[cpu_index].last_decrypt_alg = alg;
102  }
103 
104  EVP_DecryptInit_ex (ctx, cipher, NULL, key, iv);
105 
106  EVP_DecryptUpdate (ctx, out, &out_len, in, in_len);
107  EVP_DecryptFinal_ex (ctx, out + out_len, &out_len);
108 }
109 
110 static uword
112  vlib_node_runtime_t * node, vlib_frame_t * from_frame)
113 {
114  u32 n_left_from, *from, next_index, *to_next;
115  ipsec_main_t *im = &ipsec_main;
116  esp_main_t *em = &esp_main;
117  u32 *recycle = 0;
118  from = vlib_frame_vector_args (from_frame);
119  n_left_from = from_frame->n_vectors;
120  u32 cpu_index = os_get_cpu_number ();
121 
122  ipsec_alloc_empty_buffers (vm, im);
123 
124  u32 *empty_buffers = im->empty_buffers[cpu_index];
125 
126  if (PREDICT_FALSE (vec_len (empty_buffers) < n_left_from))
127  {
129  ESP_DECRYPT_ERROR_NO_BUFFER, n_left_from);
130  goto free_buffers_and_exit;
131  }
132 
133  next_index = node->cached_next_index;
134 
135  while (n_left_from > 0)
136  {
137  u32 n_left_to_next;
138 
139  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
140 
141  while (n_left_from > 0 && n_left_to_next > 0)
142  {
143  u32 i_bi0, o_bi0 = (u32) ~ 0, next0;
144  vlib_buffer_t *i_b0;
145  vlib_buffer_t *o_b0 = 0;
146  esp_header_t *esp0;
147  ipsec_sa_t *sa0;
148  u32 sa_index0 = ~0;
149  u32 seq;
150  ip4_header_t *ih4 = 0, *oh4 = 0;
151  ip6_header_t *ih6 = 0, *oh6 = 0;
152  u8 tunnel_mode = 1;
153  u8 transport_ip6 = 0;
154 
155 
156  i_bi0 = from[0];
157  from += 1;
158  n_left_from -= 1;
159  n_left_to_next -= 1;
160 
161  next0 = ESP_DECRYPT_NEXT_DROP;
162 
163  i_b0 = vlib_get_buffer (vm, i_bi0);
164  esp0 = vlib_buffer_get_current (i_b0);
165 
166  sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
167  sa0 = pool_elt_at_index (im->sad, sa_index0);
168 
169  seq = clib_host_to_net_u32 (esp0->seq);
170 
171  /* anti-replay check */
172  if (sa0->use_anti_replay)
173  {
174  int rv = 0;
175 
176  if (PREDICT_TRUE (sa0->use_esn))
177  rv = esp_replay_check_esn (sa0, seq);
178  else
179  rv = esp_replay_check (sa0, seq);
180 
181  if (PREDICT_FALSE (rv))
182  {
183  clib_warning ("anti-replay SPI %u seq %u", sa0->spi, seq);
185  ESP_DECRYPT_ERROR_REPLAY, 1);
186  o_bi0 = i_bi0;
187  to_next[0] = o_bi0;
188  to_next += 1;
189  goto trace;
190  }
191  }
192 
193  sa0->total_data_size += i_b0->current_length;
194 
195  if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
196  {
197  u8 sig[64];
198  int icv_size = em->esp_integ_algs[sa0->integ_alg].trunc_size;
199  memset (sig, 0, sizeof (sig));
200  u8 *icv =
201  vlib_buffer_get_current (i_b0) + i_b0->current_length -
202  icv_size;
203  i_b0->current_length -= icv_size;
204 
205  hmac_calc (sa0->integ_alg, sa0->integ_key, sa0->integ_key_len,
206  (u8 *) esp0, i_b0->current_length, sig, sa0->use_esn,
207  sa0->seq_hi);
208 
209  if (PREDICT_FALSE (memcmp (icv, sig, icv_size)))
210  {
212  ESP_DECRYPT_ERROR_INTEG_ERROR,
213  1);
214  o_bi0 = i_bi0;
215  to_next[0] = o_bi0;
216  to_next += 1;
217  goto trace;
218  }
219  }
220 
221  if (PREDICT_TRUE (sa0->use_anti_replay))
222  {
223  if (PREDICT_TRUE (sa0->use_esn))
224  esp_replay_advance_esn (sa0, seq);
225  else
226  esp_replay_advance (sa0, seq);
227  }
228 
229  /* grab free buffer */
230  uword last_empty_buffer = vec_len (empty_buffers) - 1;
231  o_bi0 = empty_buffers[last_empty_buffer];
232  to_next[0] = o_bi0;
233  to_next += 1;
234  o_b0 = vlib_get_buffer (vm, o_bi0);
236  empty_buffers[last_empty_buffer -
237  1], STORE);
238  _vec_len (empty_buffers) = last_empty_buffer;
239 
240  /* add old buffer to the recycle list */
241  vec_add1 (recycle, i_bi0);
242 
243  if (sa0->crypto_alg >= IPSEC_CRYPTO_ALG_AES_CBC_128 &&
244  sa0->crypto_alg <= IPSEC_CRYPTO_ALG_AES_CBC_256)
245  {
246  const int BLOCK_SIZE = 16;
247  const int IV_SIZE = 16;
248  esp_footer_t *f0;
249  u8 ip_hdr_size = 0;
250 
251  int blocks =
252  (i_b0->current_length - sizeof (esp_header_t) -
253  IV_SIZE) / BLOCK_SIZE;
254 
255  o_b0->current_data = sizeof (ethernet_header_t);
256 
257  /* transport mode */
258  if (PREDICT_FALSE (!sa0->is_tunnel && !sa0->is_tunnel_ip6))
259  {
260  tunnel_mode = 0;
261  ih4 =
262  (ip4_header_t *) (i_b0->data +
263  sizeof (ethernet_header_t));
264  if (PREDICT_TRUE
265  ((ih4->ip_version_and_header_length & 0xF0) != 0x40))
266  {
267  if (PREDICT_TRUE
268  ((ih4->ip_version_and_header_length & 0xF0) ==
269  0x60))
270  {
271  transport_ip6 = 1;
272  ip_hdr_size = sizeof (ip6_header_t);
273  ih6 =
274  (ip6_header_t *) (i_b0->data +
275  sizeof (ethernet_header_t));
276  oh6 = vlib_buffer_get_current (o_b0);
277  }
278  else
279  {
281  esp_decrypt_node.index,
282  ESP_DECRYPT_ERROR_NOT_IP,
283  1);
284  o_b0 = 0;
285  goto trace;
286  }
287  }
288  else
289  {
290  oh4 = vlib_buffer_get_current (o_b0);
291  ip_hdr_size = sizeof (ip4_header_t);
292  }
293  }
294 
296  esp0->data + IV_SIZE,
297  (u8 *) vlib_buffer_get_current (o_b0) +
298  ip_hdr_size, BLOCK_SIZE * blocks,
299  sa0->crypto_key, esp0->data);
300 
301  o_b0->current_length = (blocks * 16) - 2 + ip_hdr_size;
303  f0 =
304  (esp_footer_t *) ((u8 *) vlib_buffer_get_current (o_b0) +
305  o_b0->current_length);
306  o_b0->current_length -= f0->pad_length;
307 
308  /* tunnel mode */
309  if (PREDICT_TRUE (tunnel_mode))
310  {
311  if (PREDICT_TRUE (f0->next_header == IP_PROTOCOL_IP_IN_IP))
312  {
313  next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
314  oh4 = vlib_buffer_get_current (o_b0);
315  }
316  else if (f0->next_header == IP_PROTOCOL_IPV6)
317  next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
318  else
319  {
320  clib_warning ("next header: 0x%x", f0->next_header);
322  ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
323  1);
324  o_b0 = 0;
325  goto trace;
326  }
327  }
328  /* transport mode */
329  else
330  {
331  if (PREDICT_FALSE (transport_ip6))
332  {
333  next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
334  oh6->ip_version_traffic_class_and_flow_label =
336  oh6->protocol = f0->next_header;
337  oh6->hop_limit = ih6->hop_limit;
338  oh6->src_address.as_u64[0] = ih6->src_address.as_u64[0];
339  oh6->src_address.as_u64[1] = ih6->src_address.as_u64[1];
340  oh6->dst_address.as_u64[0] = ih6->dst_address.as_u64[0];
341  oh6->dst_address.as_u64[1] = ih6->dst_address.as_u64[1];
342  oh6->payload_length =
343  clib_host_to_net_u16 (vlib_buffer_length_in_chain
344  (vm,
345  o_b0) - sizeof (ip6_header_t));
346  }
347  else
348  {
349  next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
350  oh4->ip_version_and_header_length = 0x45;
351  oh4->tos = ih4->tos;
352  oh4->fragment_id = 0;
353  oh4->flags_and_fragment_offset = 0;
354  oh4->ttl = ih4->ttl;
355  oh4->protocol = f0->next_header;
356  oh4->src_address.as_u32 = ih4->src_address.as_u32;
357  oh4->dst_address.as_u32 = ih4->dst_address.as_u32;
358  oh4->length =
359  clib_host_to_net_u16 (vlib_buffer_length_in_chain
360  (vm, o_b0));
361  oh4->checksum = ip4_header_checksum (oh4);
362  }
363  }
364 
365  /* for IPSec-GRE tunnel next node is ipsec-gre-input */
366  if (PREDICT_FALSE
367  ((vnet_buffer (i_b0)->ipsec.flags) &
369  next0 = ESP_DECRYPT_NEXT_IPSEC_GRE_INPUT;
370 
371  vnet_buffer (o_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
372  }
373 
374  trace:
376  {
377  if (o_b0)
378  {
379  o_b0->flags |= VLIB_BUFFER_IS_TRACED;
380  o_b0->trace_index = i_b0->trace_index;
381  esp_decrypt_trace_t *tr =
382  vlib_add_trace (vm, node, o_b0, sizeof (*tr));
383  tr->crypto_alg = sa0->crypto_alg;
384  tr->integ_alg = sa0->integ_alg;
385  }
386  }
387 
388  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
389  n_left_to_next, o_bi0, next0);
390  }
391  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
392  }
394  ESP_DECRYPT_ERROR_RX_PKTS,
395  from_frame->n_vectors);
396 
397 free_buffers_and_exit:
398  if (recycle)
399  vlib_buffer_free (vm, recycle, vec_len (recycle));
400  vec_free (recycle);
401  return from_frame->n_vectors;
402 }
403 
404 
405 /* *INDENT-OFF* */
407  .function = esp_decrypt_node_fn,
408  .name = "esp-decrypt",
409  .vector_size = sizeof (u32),
410  .format_trace = format_esp_decrypt_trace,
411  .type = VLIB_NODE_TYPE_INTERNAL,
412 
414  .error_strings = esp_decrypt_error_strings,
415 
416  .n_next_nodes = ESP_DECRYPT_N_NEXT,
417  .next_nodes = {
418 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
420 #undef _
421  },
422 };
423 /* *INDENT-ON* */
424 
426 /*
427  * fd.io coding-style-patch-verification: ON
428  *
429  * Local Variables:
430  * eval: (c-set-style "gnu")
431  * End:
432  */
ipsec_crypto_alg_t last_decrypt_alg
Definition: esp.h:69
static u8 * format_esp_decrypt_trace(u8 *s, va_list *args)
Definition: esp_decrypt.c:71
static void esp_replay_advance(ipsec_sa_t *sa, u32 seq)
Definition: esp.h:149
#define CLIB_UNUSED(x)
Definition: clib.h:79
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:279
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:290
static vlib_cli_command_t trace
(constructor) VLIB_CLI_COMMAND (trace)
Definition: memory_vlib.c:1172
ip4_address_t src_address
Definition: ip4_packet.h:163
#define PREDICT_TRUE(x)
Definition: clib.h:98
u64 as_u64[2]
Definition: ip6_packet.h:51
#define NULL
Definition: clib.h:55
static int esp_replay_check(ipsec_sa_t *sa, u32 seq)
Definition: esp.h:87
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:459
ipsec_integ_alg_t integ_alg
Definition: ipsec.h:110
const EVP_CIPHER * type
Definition: esp.h:51
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
ipsec_crypto_alg_t crypto_alg
Definition: esp_decrypt.c:65
u8 is_tunnel
Definition: ipsec.h:117
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
esp_main_t esp_main
Definition: esp.h:80
esp_crypto_alg_t * esp_crypto_algs
Definition: esp.h:75
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:100
esp_integ_alg_t * esp_integ_algs
Definition: esp.h:76
ip6_address_t src_address
Definition: ip6_packet.h:341
esp_decrypt_next_t
Definition: esp_decrypt.c:32
static void esp_replay_advance_esn(ipsec_sa_t *sa, u32 seq)
Definition: esp.h:170
u8 crypto_key[128]
Definition: ipsec.h:108
u32 spi
Definition: ipsec.h:103
u32 seq_hi
Definition: ipsec.h:126
u8 trunc_size
Definition: esp.h:57
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:67
u8 integ_key[128]
Definition: ipsec.h:112
#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:170
#define always_inline
Definition: clib.h:84
ip4_address_t dst_address
Definition: ip4_packet.h:163
ipsec_main_t ipsec_main
Definition: ipsec.h:282
u8 use_esn
Definition: ipsec.h:114
#define VLIB_BUFFER_TOTAL_LENGTH_VALID
Definition: buffer.h:89
u8 data[0]
Definition: esp.h:26
#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 pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:397
ipsec_integ_alg_t
Definition: ipsec.h:86
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:71
u8 is_tunnel_ip6
Definition: ipsec.h:118
uword os_get_cpu_number(void)
Definition: unix-misc.c:224
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:188
#define PREDICT_FALSE(x)
Definition: clib.h:97
vlib_node_registration_t esp_decrypt_node
(constructor) VLIB_REGISTER_NODE (esp_decrypt_node)
Definition: esp_decrypt.c:406
#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:216
#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:350
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1115
static int esp_replay_check_esn(ipsec_sa_t *sa, u32 seq)
Definition: esp.h:105
u16 n_vectors
Definition: node.h:344
vlib_main_t * vm
Definition: buffer.c:276
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:340
#define clib_warning(format, args...)
Definition: error.h:59
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:85
static void esp_decrypt_aes_cbc(ipsec_crypto_alg_t alg, u8 *in, u8 *out, size_t in_len, u8 *key, u8 *iv)
Definition: esp_decrypt.c:84
static void ipsec_alloc_empty_buffers(vlib_main_t *vm, ipsec_main_t *im)
Definition: ipsec.h:325
#define ARRAY_LEN(x)
Definition: clib.h:59
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:455
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
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:111
ipsec_sa_t * sad
Definition: ipsec.h:247
ipsec_crypto_alg_t
Definition: ipsec.h:68
u64 total_data_size
Definition: ipsec.h:132
u8 integ_key_len
Definition: ipsec.h:111
u32 seq
Definition: esp.h:25
u64 uword
Definition: types.h:112
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:55
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:328
Definition: defs.h:47
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:253
#define vnet_buffer(b)
Definition: buffer.h:294
#define VLIB_NODE_FUNCTION_MULTIARCH(node, fn)
Definition: node.h:158
#define foreach_esp_decrypt_next
Definition: esp_decrypt.c:25
ipsec_crypto_alg_t crypto_alg
Definition: ipsec.h:106
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u8 data[0]
Packet data.
Definition: buffer.h:152
Definition: esp.h:73
u8 ip_version_and_header_length
Definition: ip4_packet.h:131
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:74
u32 ** empty_buffers
Definition: ipsec.h:253
u8 use_anti_replay
Definition: ipsec.h:115
esp_main_per_thread_data_t * per_thread_data
Definition: esp.h:77
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
u32 trace_index
Specifies index into trace buffer if VLIB_PACKET_IS_TRACED flag is set.
Definition: buffer.h:136
#define IPSEC_FLAG_IPSEC_GRE_TUNNEL
Definition: ipsec.h:18
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:238
EVP_CIPHER_CTX decrypt_ctx
Definition: esp.h:65
ipsec_integ_alg_t integ_alg
Definition: esp_decrypt.c:66
ip6_address_t dst_address
Definition: ip6_packet.h:341