IPSEC: no second lookup after tunnel encap
[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       vlib_node_increment_counter (vm, node->node_index,
135                                    ESP_DECRYPT_ERROR_NO_BUFFER, n_left_from);
136       goto free_buffers_and_exit;
137     }
138
139   next_index = node->cached_next_index;
140
141   while (n_left_from > 0)
142     {
143       u32 n_left_to_next;
144
145       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
146
147       while (n_left_from > 0 && n_left_to_next > 0)
148         {
149           u32 i_bi0, o_bi0 = (u32) ~ 0, next0;
150           vlib_buffer_t *i_b0;
151           vlib_buffer_t *o_b0 = 0;
152           esp_header_t *esp0;
153           ipsec_sa_t *sa0;
154           u32 sa_index0 = ~0;
155           u32 seq;
156           ip4_header_t *ih4 = 0, *oh4 = 0;
157           ip6_header_t *ih6 = 0, *oh6 = 0;
158           u8 tunnel_mode = 1;
159
160           i_bi0 = from[0];
161           from += 1;
162           n_left_from -= 1;
163           n_left_to_next -= 1;
164
165           next0 = ESP_DECRYPT_NEXT_DROP;
166
167           i_b0 = vlib_get_buffer (vm, i_bi0);
168           esp0 = vlib_buffer_get_current (i_b0);
169
170           sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
171           sa0 = pool_elt_at_index (im->sad, sa_index0);
172
173           seq = clib_host_to_net_u32 (esp0->seq);
174
175           /* anti-replay check */
176           if (sa0->use_anti_replay)
177             {
178               int rv = 0;
179
180               if (PREDICT_TRUE (sa0->use_esn))
181                 rv = esp_replay_check_esn (sa0, seq);
182               else
183                 rv = esp_replay_check (sa0, seq);
184
185               if (PREDICT_FALSE (rv))
186                 {
187                   vlib_node_increment_counter (vm, node->node_index,
188                                                ESP_DECRYPT_ERROR_REPLAY, 1);
189                   o_bi0 = i_bi0;
190                   to_next[0] = o_bi0;
191                   to_next += 1;
192                   goto trace;
193                 }
194             }
195
196           sa0->total_data_size += i_b0->current_length;
197
198           if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
199             {
200               u8 sig[64];
201               int icv_size =
202                 em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
203               clib_memset (sig, 0, sizeof (sig));
204               u8 *icv =
205                 vlib_buffer_get_current (i_b0) + i_b0->current_length -
206                 icv_size;
207               i_b0->current_length -= icv_size;
208
209               hmac_calc (sa0->integ_alg, sa0->integ_key.data,
210                          sa0->integ_key.len, (u8 *) esp0,
211                          i_b0->current_length, sig, sa0->use_esn,
212                          sa0->seq_hi);
213
214               if (PREDICT_FALSE (memcmp (icv, sig, icv_size)))
215                 {
216                   vlib_node_increment_counter (vm, node->node_index,
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);
240           vlib_prefetch_buffer_with_index (vm,
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 =
254                 em->ipsec_proto_main_crypto_algs[sa0->crypto_alg].block_size;;
255               const int IV_SIZE =
256                 em->ipsec_proto_main_crypto_algs[sa0->crypto_alg].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 (is_ip6)
272                     {
273                       ih6 =
274                         (ip6_header_t *) ((u8 *) esp0 -
275                                           sizeof (ip6_header_t));
276                       ip_hdr_size = sizeof (ip6_header_t);
277                       oh6 = vlib_buffer_get_current (o_b0);
278                     }
279                   else
280                     {
281                       if (sa0->udp_encap)
282                         {
283                           ih4 =
284                             (ip4_header_t *) ((u8 *) esp0 -
285                                               sizeof (udp_header_t) -
286                                               sizeof (ip4_header_t));
287                         }
288                       else
289                         {
290                           ih4 =
291                             (ip4_header_t *) ((u8 *) esp0 -
292                                               sizeof (ip4_header_t));
293                         }
294                       oh4 = vlib_buffer_get_current (o_b0);
295                       ip_hdr_size = sizeof (ip4_header_t);
296                     }
297                 }
298
299               esp_decrypt_cbc (sa0->crypto_alg,
300                                esp0->data + IV_SIZE,
301                                (u8 *) vlib_buffer_get_current (o_b0) +
302                                ip_hdr_size, BLOCK_SIZE * blocks,
303                                sa0->crypto_key.data, esp0->data);
304
305               o_b0->current_length = (blocks * BLOCK_SIZE) - 2 + ip_hdr_size;
306               o_b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
307               f0 =
308                 (esp_footer_t *) ((u8 *) vlib_buffer_get_current (o_b0) +
309                                   o_b0->current_length);
310               o_b0->current_length -= f0->pad_length;
311
312               /* tunnel mode */
313               if (PREDICT_TRUE (tunnel_mode))
314                 {
315                   if (PREDICT_TRUE (f0->next_header == IP_PROTOCOL_IP_IN_IP))
316                     {
317                       next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
318                       oh4 = vlib_buffer_get_current (o_b0);
319                     }
320                   else if (f0->next_header == IP_PROTOCOL_IPV6)
321                     next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
322                   else
323                     {
324                       vlib_node_increment_counter (vm, node->node_index,
325                                                    ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
326                                                    1);
327                       o_b0 = 0;
328                       goto trace;
329                     }
330                 }
331               /* transport mode */
332               else
333                 {
334                   if (is_ip6)
335                     {
336                       next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
337                       oh6->ip_version_traffic_class_and_flow_label =
338                         ih6->ip_version_traffic_class_and_flow_label;
339                       oh6->protocol = f0->next_header;
340                       oh6->hop_limit = ih6->hop_limit;
341                       oh6->src_address.as_u64[0] = ih6->src_address.as_u64[0];
342                       oh6->src_address.as_u64[1] = ih6->src_address.as_u64[1];
343                       oh6->dst_address.as_u64[0] = ih6->dst_address.as_u64[0];
344                       oh6->dst_address.as_u64[1] = ih6->dst_address.as_u64[1];
345                       oh6->payload_length =
346                         clib_host_to_net_u16 (vlib_buffer_length_in_chain
347                                               (vm,
348                                                o_b0) - sizeof (ip6_header_t));
349                     }
350                   else
351                     {
352                       next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
353                       oh4->ip_version_and_header_length = 0x45;
354                       oh4->tos = ih4->tos;
355                       oh4->fragment_id = 0;
356                       oh4->flags_and_fragment_offset = 0;
357                       oh4->ttl = ih4->ttl;
358                       oh4->protocol = f0->next_header;
359                       oh4->src_address.as_u32 = ih4->src_address.as_u32;
360                       oh4->dst_address.as_u32 = ih4->dst_address.as_u32;
361                       oh4->length =
362                         clib_host_to_net_u16 (vlib_buffer_length_in_chain
363                                               (vm, o_b0));
364                       oh4->checksum = ip4_header_checksum (oh4);
365                     }
366                 }
367
368               /* for IPSec-GRE tunnel next node is ipsec-gre-input */
369               if (PREDICT_FALSE
370                   ((vnet_buffer (i_b0)->ipsec.flags) &
371                    IPSEC_FLAG_IPSEC_GRE_TUNNEL))
372                 next0 = ESP_DECRYPT_NEXT_IPSEC_GRE_INPUT;
373
374               vnet_buffer (o_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
375               vnet_buffer (o_b0)->sw_if_index[VLIB_RX] =
376                 vnet_buffer (i_b0)->sw_if_index[VLIB_RX];
377             }
378
379         trace:
380           if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
381             {
382               if (o_b0)
383                 {
384                   o_b0->flags |= VLIB_BUFFER_IS_TRACED;
385                   o_b0->trace_index = i_b0->trace_index;
386                   esp_decrypt_trace_t *tr =
387                     vlib_add_trace (vm, node, o_b0, sizeof (*tr));
388                   tr->crypto_alg = sa0->crypto_alg;
389                   tr->integ_alg = sa0->integ_alg;
390                 }
391             }
392
393           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
394                                            n_left_to_next, o_bi0, next0);
395         }
396       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
397     }
398   vlib_node_increment_counter (vm, node->node_index,
399                                ESP_DECRYPT_ERROR_RX_PKTS,
400                                from_frame->n_vectors);
401
402
403 free_buffers_and_exit:
404   if (recycle)
405     vlib_buffer_free (vm, recycle, vec_len (recycle));
406   vec_free (recycle);
407   return from_frame->n_vectors;
408 }
409
410 VLIB_NODE_FN (esp4_decrypt_node) (vlib_main_t * vm,
411                                   vlib_node_runtime_t * node,
412                                   vlib_frame_t * from_frame)
413 {
414   return esp_decrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
415 }
416
417 /* *INDENT-OFF* */
418 VLIB_REGISTER_NODE (esp4_decrypt_node) = {
419   .name = "esp4-decrypt",
420   .vector_size = sizeof (u32),
421   .format_trace = format_esp_decrypt_trace,
422   .type = VLIB_NODE_TYPE_INTERNAL,
423
424   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
425   .error_strings = esp_decrypt_error_strings,
426
427   .n_next_nodes = ESP_DECRYPT_N_NEXT,
428   .next_nodes = {
429 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
430     foreach_esp_decrypt_next
431 #undef _
432   },
433 };
434 /* *INDENT-ON* */
435
436 VLIB_NODE_FN (esp6_decrypt_node) (vlib_main_t * vm,
437                                   vlib_node_runtime_t * node,
438                                   vlib_frame_t * from_frame)
439 {
440   return esp_decrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
441 }
442
443 /* *INDENT-OFF* */
444 VLIB_REGISTER_NODE (esp6_decrypt_node) = {
445   .name = "esp6-decrypt",
446   .vector_size = sizeof (u32),
447   .format_trace = format_esp_decrypt_trace,
448   .type = VLIB_NODE_TYPE_INTERNAL,
449
450   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
451   .error_strings = esp_decrypt_error_strings,
452
453   .n_next_nodes = ESP_DECRYPT_N_NEXT,
454   .next_nodes = {
455 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
456     foreach_esp_decrypt_next
457 #undef _
458   },
459 };
460 /* *INDENT-ON* */
461
462 /*
463  * fd.io coding-style-patch-verification: ON
464  *
465  * Local Variables:
466  * eval: (c-set-style "gnu")
467  * End:
468  */