Use thread local storage for thread index
[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_aes_cbc (ipsec_crypto_alg_t alg,
85                      u8 * in, u8 * out, size_t in_len, u8 * key, u8 * iv)
86 {
87   esp_main_t *em = &esp_main;
88   u32 thread_index = vlib_get_thread_index ();
89   EVP_CIPHER_CTX *ctx = &(em->per_thread_data[thread_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
99       (alg != em->per_thread_data[thread_index].last_decrypt_alg))
100     {
101       cipher = em->esp_crypto_algs[alg].type;
102       em->per_thread_data[thread_index].last_decrypt_alg = alg;
103     }
104
105   EVP_DecryptInit_ex (ctx, cipher, NULL, key, iv);
106
107   EVP_DecryptUpdate (ctx, out, &out_len, in, in_len);
108   EVP_DecryptFinal_ex (ctx, out + out_len, &out_len);
109 }
110
111 static uword
112 esp_decrypt_node_fn (vlib_main_t * vm,
113                      vlib_node_runtime_t * node, vlib_frame_t * from_frame)
114 {
115   u32 n_left_from, *from, next_index, *to_next;
116   ipsec_main_t *im = &ipsec_main;
117   esp_main_t *em = &esp_main;
118   u32 *recycle = 0;
119   from = vlib_frame_vector_args (from_frame);
120   n_left_from = from_frame->n_vectors;
121   u32 thread_index = vlib_get_thread_index ();
122
123   ipsec_alloc_empty_buffers (vm, im);
124
125   u32 *empty_buffers = im->empty_buffers[thread_index];
126
127   if (PREDICT_FALSE (vec_len (empty_buffers) < n_left_from))
128     {
129       vlib_node_increment_counter (vm, esp_decrypt_node.index,
130                                    ESP_DECRYPT_ERROR_NO_BUFFER, n_left_from);
131       goto free_buffers_and_exit;
132     }
133
134   next_index = node->cached_next_index;
135
136   while (n_left_from > 0)
137     {
138       u32 n_left_to_next;
139
140       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
141
142       while (n_left_from > 0 && n_left_to_next > 0)
143         {
144           u32 i_bi0, o_bi0 = (u32) ~ 0, next0;
145           vlib_buffer_t *i_b0;
146           vlib_buffer_t *o_b0 = 0;
147           esp_header_t *esp0;
148           ipsec_sa_t *sa0;
149           u32 sa_index0 = ~0;
150           u32 seq;
151           ip4_header_t *ih4 = 0, *oh4 = 0;
152           ip6_header_t *ih6 = 0, *oh6 = 0;
153           u8 tunnel_mode = 1;
154           u8 transport_ip6 = 0;
155
156
157           i_bi0 = from[0];
158           from += 1;
159           n_left_from -= 1;
160           n_left_to_next -= 1;
161
162           next0 = ESP_DECRYPT_NEXT_DROP;
163
164           i_b0 = vlib_get_buffer (vm, i_bi0);
165           esp0 = vlib_buffer_get_current (i_b0);
166
167           sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
168           sa0 = pool_elt_at_index (im->sad, sa_index0);
169
170           seq = clib_host_to_net_u32 (esp0->seq);
171
172           /* anti-replay check */
173           if (sa0->use_anti_replay)
174             {
175               int rv = 0;
176
177               if (PREDICT_TRUE (sa0->use_esn))
178                 rv = esp_replay_check_esn (sa0, seq);
179               else
180                 rv = esp_replay_check (sa0, seq);
181
182               if (PREDICT_FALSE (rv))
183                 {
184                   clib_warning ("anti-replay SPI %u seq %u", sa0->spi, seq);
185                   vlib_node_increment_counter (vm, esp_decrypt_node.index,
186                                                ESP_DECRYPT_ERROR_REPLAY, 1);
187                   o_bi0 = i_bi0;
188                   to_next[0] = o_bi0;
189                   to_next += 1;
190                   goto trace;
191                 }
192             }
193
194           sa0->total_data_size += i_b0->current_length;
195
196           if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
197             {
198               u8 sig[64];
199               int icv_size = em->esp_integ_algs[sa0->integ_alg].trunc_size;
200               memset (sig, 0, sizeof (sig));
201               u8 *icv =
202                 vlib_buffer_get_current (i_b0) + i_b0->current_length -
203                 icv_size;
204               i_b0->current_length -= icv_size;
205
206               hmac_calc (sa0->integ_alg, sa0->integ_key, sa0->integ_key_len,
207                          (u8 *) esp0, i_b0->current_length, sig, sa0->use_esn,
208                          sa0->seq_hi);
209
210               if (PREDICT_FALSE (memcmp (icv, sig, icv_size)))
211                 {
212                   vlib_node_increment_counter (vm, esp_decrypt_node.index,
213                                                ESP_DECRYPT_ERROR_INTEG_ERROR,
214                                                1);
215                   o_bi0 = i_bi0;
216                   to_next[0] = o_bi0;
217                   to_next += 1;
218                   goto trace;
219                 }
220             }
221
222           if (PREDICT_TRUE (sa0->use_anti_replay))
223             {
224               if (PREDICT_TRUE (sa0->use_esn))
225                 esp_replay_advance_esn (sa0, seq);
226               else
227                 esp_replay_advance (sa0, seq);
228             }
229
230           /* grab free buffer */
231           uword last_empty_buffer = vec_len (empty_buffers) - 1;
232           o_bi0 = empty_buffers[last_empty_buffer];
233           to_next[0] = o_bi0;
234           to_next += 1;
235           o_b0 = vlib_get_buffer (vm, o_bi0);
236           vlib_prefetch_buffer_with_index (vm,
237                                            empty_buffers[last_empty_buffer -
238                                                          1], STORE);
239           _vec_len (empty_buffers) = last_empty_buffer;
240
241           /* add old buffer to the recycle list */
242           vec_add1 (recycle, i_bi0);
243
244           if (sa0->crypto_alg >= IPSEC_CRYPTO_ALG_AES_CBC_128 &&
245               sa0->crypto_alg <= IPSEC_CRYPTO_ALG_AES_CBC_256)
246             {
247               const int BLOCK_SIZE = 16;
248               const int IV_SIZE = 16;
249               esp_footer_t *f0;
250               u8 ip_hdr_size = 0;
251
252               int blocks =
253                 (i_b0->current_length - sizeof (esp_header_t) -
254                  IV_SIZE) / BLOCK_SIZE;
255
256               o_b0->current_data = sizeof (ethernet_header_t);
257
258               /* transport mode */
259               if (PREDICT_FALSE (!sa0->is_tunnel && !sa0->is_tunnel_ip6))
260                 {
261                   tunnel_mode = 0;
262                   ih4 =
263                     (ip4_header_t *) (i_b0->data +
264                                       sizeof (ethernet_header_t));
265                   if (PREDICT_TRUE
266                       ((ih4->ip_version_and_header_length & 0xF0) != 0x40))
267                     {
268                       if (PREDICT_TRUE
269                           ((ih4->ip_version_and_header_length & 0xF0) ==
270                            0x60))
271                         {
272                           transport_ip6 = 1;
273                           ip_hdr_size = sizeof (ip6_header_t);
274                           ih6 =
275                             (ip6_header_t *) (i_b0->data +
276                                               sizeof (ethernet_header_t));
277                           oh6 = vlib_buffer_get_current (o_b0);
278                         }
279                       else
280                         {
281                           vlib_node_increment_counter (vm,
282                                                        esp_decrypt_node.index,
283                                                        ESP_DECRYPT_ERROR_NOT_IP,
284                                                        1);
285                           o_b0 = 0;
286                           goto trace;
287                         }
288                     }
289                   else
290                     {
291                       oh4 = vlib_buffer_get_current (o_b0);
292                       ip_hdr_size = sizeof (ip4_header_t);
293                     }
294                 }
295
296               esp_decrypt_aes_cbc (sa0->crypto_alg,
297                                    esp0->data + IV_SIZE,
298                                    (u8 *) vlib_buffer_get_current (o_b0) +
299                                    ip_hdr_size, BLOCK_SIZE * blocks,
300                                    sa0->crypto_key, esp0->data);
301
302               o_b0->current_length = (blocks * 16) - 2 + ip_hdr_size;
303               o_b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
304               f0 =
305                 (esp_footer_t *) ((u8 *) vlib_buffer_get_current (o_b0) +
306                                   o_b0->current_length);
307               o_b0->current_length -= f0->pad_length;
308
309               /* tunnel mode */
310               if (PREDICT_TRUE (tunnel_mode))
311                 {
312                   if (PREDICT_TRUE (f0->next_header == IP_PROTOCOL_IP_IN_IP))
313                     {
314                       next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
315                       oh4 = vlib_buffer_get_current (o_b0);
316                     }
317                   else if (f0->next_header == IP_PROTOCOL_IPV6)
318                     next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
319                   else
320                     {
321                       clib_warning ("next header: 0x%x", f0->next_header);
322                       vlib_node_increment_counter (vm, esp_decrypt_node.index,
323                                                    ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
324                                                    1);
325                       o_b0 = 0;
326                       goto trace;
327                     }
328                 }
329               /* transport mode */
330               else
331                 {
332                   if (PREDICT_FALSE (transport_ip6))
333                     {
334                       next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
335                       oh6->ip_version_traffic_class_and_flow_label =
336                         ih6->ip_version_traffic_class_and_flow_label;
337                       oh6->protocol = f0->next_header;
338                       oh6->hop_limit = ih6->hop_limit;
339                       oh6->src_address.as_u64[0] = ih6->src_address.as_u64[0];
340                       oh6->src_address.as_u64[1] = ih6->src_address.as_u64[1];
341                       oh6->dst_address.as_u64[0] = ih6->dst_address.as_u64[0];
342                       oh6->dst_address.as_u64[1] = ih6->dst_address.as_u64[1];
343                       oh6->payload_length =
344                         clib_host_to_net_u16 (vlib_buffer_length_in_chain
345                                               (vm,
346                                                o_b0) - sizeof (ip6_header_t));
347                     }
348                   else
349                     {
350                       next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
351                       oh4->ip_version_and_header_length = 0x45;
352                       oh4->tos = ih4->tos;
353                       oh4->fragment_id = 0;
354                       oh4->flags_and_fragment_offset = 0;
355                       oh4->ttl = ih4->ttl;
356                       oh4->protocol = f0->next_header;
357                       oh4->src_address.as_u32 = ih4->src_address.as_u32;
358                       oh4->dst_address.as_u32 = ih4->dst_address.as_u32;
359                       oh4->length =
360                         clib_host_to_net_u16 (vlib_buffer_length_in_chain
361                                               (vm, o_b0));
362                       oh4->checksum = ip4_header_checksum (oh4);
363                     }
364                 }
365
366               /* for IPSec-GRE tunnel next node is ipsec-gre-input */
367               if (PREDICT_FALSE
368                   ((vnet_buffer (i_b0)->ipsec.flags) &
369                    IPSEC_FLAG_IPSEC_GRE_TUNNEL))
370                 next0 = ESP_DECRYPT_NEXT_IPSEC_GRE_INPUT;
371
372               vnet_buffer (o_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
373             }
374
375         trace:
376           if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
377             {
378               if (o_b0)
379                 {
380                   o_b0->flags |= VLIB_BUFFER_IS_TRACED;
381                   o_b0->trace_index = i_b0->trace_index;
382                   esp_decrypt_trace_t *tr =
383                     vlib_add_trace (vm, node, o_b0, sizeof (*tr));
384                   tr->crypto_alg = sa0->crypto_alg;
385                   tr->integ_alg = sa0->integ_alg;
386                 }
387             }
388
389           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
390                                            n_left_to_next, o_bi0, next0);
391         }
392       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
393     }
394   vlib_node_increment_counter (vm, esp_decrypt_node.index,
395                                ESP_DECRYPT_ERROR_RX_PKTS,
396                                from_frame->n_vectors);
397
398 free_buffers_and_exit:
399   if (recycle)
400     vlib_buffer_free (vm, recycle, vec_len (recycle));
401   vec_free (recycle);
402   return from_frame->n_vectors;
403 }
404
405
406 /* *INDENT-OFF* */
407 VLIB_REGISTER_NODE (esp_decrypt_node) = {
408   .function = esp_decrypt_node_fn,
409   .name = "esp-decrypt",
410   .vector_size = sizeof (u32),
411   .format_trace = format_esp_decrypt_trace,
412   .type = VLIB_NODE_TYPE_INTERNAL,
413
414   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
415   .error_strings = esp_decrypt_error_strings,
416
417   .n_next_nodes = ESP_DECRYPT_N_NEXT,
418   .next_nodes = {
419 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
420     foreach_esp_decrypt_next
421 #undef _
422   },
423 };
424 /* *INDENT-ON* */
425
426 VLIB_NODE_FUNCTION_MULTIARCH (esp_decrypt_node, esp_decrypt_node_fn)
427 /*
428  * fd.io coding-style-patch-verification: ON
429  *
430  * Local Variables:
431  * eval: (c-set-style "gnu")
432  * End:
433  */