IPSEC-AH: fix packet drop
[vpp.git] / src / vnet / ipsec / esp_decrypt.c
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 {
34   foreach_esp_decrypt_next
35 #undef _
36     ESP_DECRYPT_N_NEXT,
37 } esp_decrypt_next_t;
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,
52   foreach_esp_decrypt_error
53 #undef _
54     ESP_DECRYPT_N_ERROR,
55 } esp_decrypt_error_t;
56
57 static char *esp_decrypt_error_strings[] = {
58 #define _(sym,string) string,
59   foreach_esp_decrypt_error
60 #undef _
61 };
62
63 typedef struct
64 {
65   ipsec_crypto_alg_t crypto_alg;
66   ipsec_integ_alg_t integ_alg;
67 } esp_decrypt_trace_t;
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",
78               format_ipsec_crypto_alg, t->crypto_alg,
79               format_ipsec_integ_alg, t->integ_alg);
80   return s;
81 }
82
83 always_inline void
84 esp_decrypt_cbc (ipsec_crypto_alg_t alg,
85                  u8 * in, u8 * out, size_t in_len, u8 * key, u8 * iv)
86 {
87   ipsec_proto_main_t *em = &ipsec_proto_main;
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
99   if (PREDICT_FALSE (em->ipsec_proto_main_crypto_algs[alg].type == 0))
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 always_inline uword
116 esp_decrypt_inline (vlib_main_t * vm,
117                     vlib_node_runtime_t * node, vlib_frame_t * from_frame,
118                     int is_ip6)
119 {
120   u32 n_left_from, *from, next_index, *to_next;
121   ipsec_main_t *im = &ipsec_main;
122   ipsec_proto_main_t *em = &ipsec_proto_main;
123   u32 *recycle = 0;
124   from = vlib_frame_vector_args (from_frame);
125   n_left_from = from_frame->n_vectors;
126   u32 thread_index = vlib_get_thread_index ();
127
128   ipsec_alloc_empty_buffers (vm, im);
129
130   u32 *empty_buffers = im->empty_buffers[thread_index];
131
132   if (PREDICT_FALSE (vec_len (empty_buffers) < n_left_from))
133     {
134       if (is_ip6)
135         vlib_node_increment_counter (vm, esp6_decrypt_node.index,
136                                      ESP_DECRYPT_ERROR_NO_BUFFER,
137                                      n_left_from);
138       else
139         vlib_node_increment_counter (vm, esp4_decrypt_node.index,
140                                      ESP_DECRYPT_ERROR_NO_BUFFER,
141                                      n_left_from);
142       goto free_buffers_and_exit;
143     }
144
145   next_index = node->cached_next_index;
146
147   while (n_left_from > 0)
148     {
149       u32 n_left_to_next;
150
151       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
152
153       while (n_left_from > 0 && n_left_to_next > 0)
154         {
155           u32 i_bi0, o_bi0 = (u32) ~ 0, next0;
156           vlib_buffer_t *i_b0;
157           vlib_buffer_t *o_b0 = 0;
158           esp_header_t *esp0;
159           ipsec_sa_t *sa0;
160           u32 sa_index0 = ~0;
161           u32 seq;
162           ip4_header_t *ih4 = 0, *oh4 = 0;
163           ip6_header_t *ih6 = 0, *oh6 = 0;
164           u8 tunnel_mode = 1;
165
166           i_bi0 = from[0];
167           from += 1;
168           n_left_from -= 1;
169           n_left_to_next -= 1;
170
171           next0 = ESP_DECRYPT_NEXT_DROP;
172
173           i_b0 = vlib_get_buffer (vm, i_bi0);
174           esp0 = vlib_buffer_get_current (i_b0);
175
176           sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
177           sa0 = pool_elt_at_index (im->sad, sa_index0);
178
179           seq = clib_host_to_net_u32 (esp0->seq);
180
181           /* anti-replay check */
182           if (sa0->use_anti_replay)
183             {
184               int rv = 0;
185
186               if (PREDICT_TRUE (sa0->use_esn))
187                 rv = esp_replay_check_esn (sa0, seq);
188               else
189                 rv = esp_replay_check (sa0, seq);
190
191               if (PREDICT_FALSE (rv))
192                 {
193                   if (is_ip6)
194                     vlib_node_increment_counter (vm,
195                                                  esp6_decrypt_node.index,
196                                                  ESP_DECRYPT_ERROR_REPLAY, 1);
197                   else
198                     vlib_node_increment_counter (vm,
199                                                  esp4_decrypt_node.index,
200                                                  ESP_DECRYPT_ERROR_REPLAY, 1);
201                   o_bi0 = i_bi0;
202                   to_next[0] = o_bi0;
203                   to_next += 1;
204                   goto trace;
205                 }
206             }
207
208           sa0->total_data_size += i_b0->current_length;
209
210           if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
211             {
212               u8 sig[64];
213               int icv_size =
214                 em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
215               clib_memset (sig, 0, sizeof (sig));
216               u8 *icv =
217                 vlib_buffer_get_current (i_b0) + i_b0->current_length -
218                 icv_size;
219               i_b0->current_length -= icv_size;
220
221               hmac_calc (sa0->integ_alg, sa0->integ_key, sa0->integ_key_len,
222                          (u8 *) esp0, i_b0->current_length, sig, sa0->use_esn,
223                          sa0->seq_hi);
224
225               if (PREDICT_FALSE (memcmp (icv, sig, icv_size)))
226                 {
227                   if (is_ip6)
228                     vlib_node_increment_counter (vm,
229                                                  esp6_decrypt_node.index,
230                                                  ESP_DECRYPT_ERROR_INTEG_ERROR,
231                                                  1);
232                   else
233                     vlib_node_increment_counter (vm,
234                                                  esp4_decrypt_node.index,
235                                                  ESP_DECRYPT_ERROR_INTEG_ERROR,
236                                                  1);
237                   o_bi0 = i_bi0;
238                   to_next[0] = o_bi0;
239                   to_next += 1;
240                   goto trace;
241                 }
242             }
243
244           if (PREDICT_TRUE (sa0->use_anti_replay))
245             {
246               if (PREDICT_TRUE (sa0->use_esn))
247                 esp_replay_advance_esn (sa0, seq);
248               else
249                 esp_replay_advance (sa0, seq);
250             }
251
252           /* grab free buffer */
253           uword last_empty_buffer = vec_len (empty_buffers) - 1;
254           o_bi0 = empty_buffers[last_empty_buffer];
255           to_next[0] = o_bi0;
256           to_next += 1;
257           o_b0 = vlib_get_buffer (vm, o_bi0);
258           vlib_prefetch_buffer_with_index (vm,
259                                            empty_buffers[last_empty_buffer -
260                                                          1], STORE);
261           _vec_len (empty_buffers) = last_empty_buffer;
262
263           /* add old buffer to the recycle list */
264           vec_add1 (recycle, i_bi0);
265
266           if ((sa0->crypto_alg >= IPSEC_CRYPTO_ALG_AES_CBC_128 &&
267                sa0->crypto_alg <= IPSEC_CRYPTO_ALG_AES_CBC_256) ||
268               (sa0->crypto_alg >= IPSEC_CRYPTO_ALG_DES_CBC &&
269                sa0->crypto_alg <= IPSEC_CRYPTO_ALG_3DES_CBC))
270             {
271               const int BLOCK_SIZE =
272                 em->ipsec_proto_main_crypto_algs[sa0->crypto_alg].block_size;;
273               const int IV_SIZE =
274                 em->ipsec_proto_main_crypto_algs[sa0->crypto_alg].iv_size;
275               esp_footer_t *f0;
276               u8 ip_hdr_size = 0;
277
278               int blocks =
279                 (i_b0->current_length - sizeof (esp_header_t) -
280                  IV_SIZE) / BLOCK_SIZE;
281
282               o_b0->current_data = sizeof (ethernet_header_t);
283
284               /* transport mode */
285               if (PREDICT_FALSE (!sa0->is_tunnel && !sa0->is_tunnel_ip6))
286                 {
287                   tunnel_mode = 0;
288
289                   if (is_ip6)
290                     {
291                       ih6 =
292                         (ip6_header_t *) ((u8 *) esp0 -
293                                           sizeof (ip6_header_t));
294                       ip_hdr_size = sizeof (ip6_header_t);
295                       oh6 = vlib_buffer_get_current (o_b0);
296                     }
297                   else
298                     {
299                       ih4 =
300                         (ip4_header_t *) ((u8 *) esp0 -
301                                           sizeof (ip4_header_t));
302                       oh4 = vlib_buffer_get_current (o_b0);
303                       ip_hdr_size = sizeof (ip4_header_t);
304                     }
305                 }
306
307               esp_decrypt_cbc (sa0->crypto_alg,
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                     {
332                       if (is_ip6)
333                         vlib_node_increment_counter (vm,
334                                                      esp6_decrypt_node.index,
335                                                      ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
336                                                      1);
337                       else
338                         vlib_node_increment_counter (vm,
339                                                      esp4_decrypt_node.index,
340                                                      ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
341                                                      1);
342                       o_b0 = 0;
343                       goto trace;
344                     }
345                 }
346               /* transport mode */
347               else
348                 {
349                   if (is_ip6)
350                     {
351                       next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
352                       oh6->ip_version_traffic_class_and_flow_label =
353                         ih6->ip_version_traffic_class_and_flow_label;
354                       oh6->protocol = f0->next_header;
355                       oh6->hop_limit = ih6->hop_limit;
356                       oh6->src_address.as_u64[0] = ih6->src_address.as_u64[0];
357                       oh6->src_address.as_u64[1] = ih6->src_address.as_u64[1];
358                       oh6->dst_address.as_u64[0] = ih6->dst_address.as_u64[0];
359                       oh6->dst_address.as_u64[1] = ih6->dst_address.as_u64[1];
360                       oh6->payload_length =
361                         clib_host_to_net_u16 (vlib_buffer_length_in_chain
362                                               (vm,
363                                                o_b0) - sizeof (ip6_header_t));
364                     }
365                   else
366                     {
367                       next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
368                       oh4->ip_version_and_header_length = 0x45;
369                       oh4->tos = ih4->tos;
370                       oh4->fragment_id = 0;
371                       oh4->flags_and_fragment_offset = 0;
372                       oh4->ttl = ih4->ttl;
373                       oh4->protocol = f0->next_header;
374                       oh4->src_address.as_u32 = ih4->src_address.as_u32;
375                       oh4->dst_address.as_u32 = ih4->dst_address.as_u32;
376                       oh4->length =
377                         clib_host_to_net_u16 (vlib_buffer_length_in_chain
378                                               (vm, o_b0));
379                       oh4->checksum = ip4_header_checksum (oh4);
380                     }
381                 }
382
383               /* for IPSec-GRE tunnel next node is ipsec-gre-input */
384               if (PREDICT_FALSE
385                   ((vnet_buffer (i_b0)->ipsec.flags) &
386                    IPSEC_FLAG_IPSEC_GRE_TUNNEL))
387                 next0 = ESP_DECRYPT_NEXT_IPSEC_GRE_INPUT;
388
389               vnet_buffer (o_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
390               vnet_buffer (o_b0)->sw_if_index[VLIB_RX] =
391                 vnet_buffer (i_b0)->sw_if_index[VLIB_RX];
392             }
393
394         trace:
395           if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
396             {
397               if (o_b0)
398                 {
399                   o_b0->flags |= VLIB_BUFFER_IS_TRACED;
400                   o_b0->trace_index = i_b0->trace_index;
401                   esp_decrypt_trace_t *tr =
402                     vlib_add_trace (vm, node, o_b0, sizeof (*tr));
403                   tr->crypto_alg = sa0->crypto_alg;
404                   tr->integ_alg = sa0->integ_alg;
405                 }
406             }
407
408           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
409                                            n_left_to_next, o_bi0, next0);
410         }
411       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
412     }
413   if (is_ip6)
414     vlib_node_increment_counter (vm, esp6_decrypt_node.index,
415                                  ESP_DECRYPT_ERROR_RX_PKTS,
416                                  from_frame->n_vectors);
417   else
418     vlib_node_increment_counter (vm, esp4_decrypt_node.index,
419                                  ESP_DECRYPT_ERROR_RX_PKTS,
420                                  from_frame->n_vectors);
421
422
423 free_buffers_and_exit:
424   if (recycle)
425     vlib_buffer_free (vm, recycle, vec_len (recycle));
426   vec_free (recycle);
427   return from_frame->n_vectors;
428 }
429
430 VLIB_NODE_FN (esp4_decrypt_node) (vlib_main_t * vm,
431                                   vlib_node_runtime_t * node,
432                                   vlib_frame_t * from_frame)
433 {
434   return esp_decrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
435 }
436
437 /* *INDENT-OFF* */
438 VLIB_REGISTER_NODE (esp4_decrypt_node) = {
439   .name = "esp4-decrypt",
440   .vector_size = sizeof (u32),
441   .format_trace = format_esp_decrypt_trace,
442   .type = VLIB_NODE_TYPE_INTERNAL,
443
444   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
445   .error_strings = esp_decrypt_error_strings,
446
447   .n_next_nodes = ESP_DECRYPT_N_NEXT,
448   .next_nodes = {
449 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
450     foreach_esp_decrypt_next
451 #undef _
452   },
453 };
454 /* *INDENT-ON* */
455
456 VLIB_NODE_FN (esp6_decrypt_node) (vlib_main_t * vm,
457                                   vlib_node_runtime_t * node,
458                                   vlib_frame_t * from_frame)
459 {
460   return esp_decrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
461 }
462
463 /* *INDENT-OFF* */
464 VLIB_REGISTER_NODE (esp6_decrypt_node) = {
465   .name = "esp6-decrypt",
466   .vector_size = sizeof (u32),
467   .format_trace = format_esp_decrypt_trace,
468   .type = VLIB_NODE_TYPE_INTERNAL,
469
470   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
471   .error_strings = esp_decrypt_error_strings,
472
473   .n_next_nodes = ESP_DECRYPT_N_NEXT,
474   .next_nodes = {
475 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
476     foreach_esp_decrypt_next
477 #undef _
478   },
479 };
480 /* *INDENT-ON* */
481
482 /*
483  * fd.io coding-style-patch-verification: ON
484  *
485  * Local Variables:
486  * eval: (c-set-style "gnu")
487  * End:
488  */