Fixes for buliding for 32bit targets:
[vpp.git] / src / plugins / ipsecmb / esp_decrypt.c
1 /*
2  * esp_decrypt.c : ipsecmb 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 #include <ipsecmb/ipsecmb.h>
25
26 #define foreach_esp_decrypt_next                \
27 _(DROP, "error-drop")                           \
28 _(IP4_INPUT, "ip4-input")                       \
29 _(IP6_INPUT, "ip6-input")                       \
30 _(IPSEC_GRE_INPUT, "ipsec-gre-input")
31
32 #define _(v, s) ESP_DECRYPT_NEXT_##v,
33 typedef enum
34 {
35   foreach_esp_decrypt_next
36 #undef _
37     ESP_DECRYPT_N_NEXT,
38 } esp_decrypt_next_t;
39
40
41 #define foreach_esp_decrypt_error                   \
42  _(RX_PKTS, "ESP pkts received")                    \
43  _(NO_BUFFER, "No buffer (packed dropped)")         \
44  _(DECRYPTION_FAILED, "ESP decryption failed")      \
45  _(INTEG_ERROR, "Integrity check failed")           \
46  _(REPLAY, "SA replayed packet")                    \
47  _(NOT_IP, "Not IP packet (dropped)")
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 #ifdef CLIB_MARCH_VARIANT
84 always_inline void
85 esp_finish_decrypt (vlib_main_t * vm, vlib_node_runtime_t * node,
86                     JOB_AES_HMAC * job, u32 * next0, ipsec_sa_t * sa0,
87                     int is_ip6)
88 {
89   u32 bi0 = (uintptr_t) job->user_data;
90   vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0);
91   esp_footer_t *f0;
92   ip4_header_t *ih4 = vlib_buffer_get_current (b0);
93   ip6_header_t *ih6 = vlib_buffer_get_current (b0);
94
95   if (NULL_HASH != job->hash_alg)
96     {
97       if (0 !=
98           memcmp (job->auth_tag_output,
99                   job->auth_tag_output - job->auth_tag_output_len_in_bytes,
100                   job->auth_tag_output_len_in_bytes))
101         {
102           vlib_node_increment_counter (vm, node->node_index,
103                                        ESP_DECRYPT_ERROR_INTEG_ERROR, 1);
104           *next0 = ESP_DECRYPT_NEXT_DROP;
105           return;
106         }
107     }
108
109   f0 = (esp_footer_t *) ((u8 *) vlib_buffer_get_current (b0) +
110                          b0->current_length);
111   b0->current_length -= f0->pad_length;
112
113   /* tunnel mode */
114   if (sa0->is_tunnel)
115     {
116       if (f0->next_header == IP_PROTOCOL_IP_IN_IP)
117         {
118           *next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
119         }
120       else if (f0->next_header == IP_PROTOCOL_IPV6)
121         {
122           *next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
123         }
124       else
125         {
126           vlib_node_increment_counter (vm, node->node_index,
127                                        ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
128                                        1);
129           *next0 = ESP_DECRYPT_NEXT_DROP;
130           return;
131         }
132     }
133   /* transport mode */
134   else
135     {
136       if (is_ip6)
137         {
138           *next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
139           ih6->protocol = f0->next_header;
140           ih6->payload_length =
141             clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
142                                   sizeof (ip6_header_t));
143         }
144       else
145         {
146           *next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
147           ih4->fragment_id = 0;
148           ih4->flags_and_fragment_offset = 0;
149           ih4->protocol = f0->next_header;
150           ih4->length =
151             clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
152           ih4->checksum = ip4_header_checksum (ih4);
153         }
154     }
155
156   /* for IPSec-GRE tunnel next node is ipsec-gre-input */
157   if ((vnet_buffer (b0)->ipsec.flags & IPSEC_FLAG_IPSEC_GRE_TUNNEL))
158     {
159       *next0 = ESP_DECRYPT_NEXT_IPSEC_GRE_INPUT;
160     }
161
162   vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
163   if (sa0->use_anti_replay)
164     {
165       if (sa0->use_esn)
166         esp_replay_advance_esn (sa0, vnet_buffer (b0)->ipsec.seq);
167       else
168         esp_replay_advance (sa0, vnet_buffer (b0)->ipsec.seq);
169     }
170 }
171
172 always_inline uword
173 esp_decrypt_ipsecmb_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
174                             vlib_frame_t * from_frame, int is_ip6)
175 {
176   u32 n_left_from, *from, next_index, *to_next;
177   u32 packets_in_flight = 0;
178   ipsec_main_t *im = &ipsec_main;
179   ipsecmb_main_t *imbm = &ipsecmb_main;
180   ipsec_proto_main_t *em = &ipsec_proto_main;
181   from = vlib_frame_vector_args (from_frame);
182   n_left_from = from_frame->n_vectors;
183   u32 thread_index = vlib_get_thread_index ();
184   MB_MGR *mgr = imbm->mb_mgr[thread_index];
185   u32 *to_be_freed = NULL;
186
187   next_index = node->cached_next_index;
188
189   while (n_left_from > 0 || packets_in_flight > 0)
190     {
191       u32 n_left_to_next;
192
193       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
194
195       while (n_left_from > 0 && n_left_to_next > 0)
196         {
197           u32 bi0, next0;
198           vlib_buffer_t *b0;
199           esp_header_t *esp0;
200           ipsec_sa_t *sa0;
201           ipsecmb_sa_t *samb0;
202           u32 sa_index0 = ~0;
203           u32 seq;
204
205           bi0 = from[0];
206           from += 1;
207           n_left_from -= 1;
208
209           next0 = ESP_DECRYPT_NEXT_DROP;
210
211           b0 = vlib_get_buffer (vm, bi0);
212           esp0 = vlib_buffer_get_current (b0);
213
214           sa_index0 = vnet_buffer (b0)->ipsec.sad_index;
215           sa0 = pool_elt_at_index (im->sad, sa_index0);
216           samb0 = pool_elt_at_index (imbm->sad, sa_index0);
217
218           vnet_buffer (b0)->ipsec.seq = seq =
219             clib_host_to_net_u32 (esp0->seq);
220
221           /* anti-replay check */
222           if (sa0->use_anti_replay)
223             {
224               int rv = 0;
225
226               if (sa0->use_esn)
227                 rv = esp_replay_check_esn (sa0, seq);
228               else
229                 rv = esp_replay_check (sa0, seq);
230
231               if (PREDICT_FALSE (rv))
232                 {
233                   vlib_node_increment_counter (vm, node->node_index,
234                                                ESP_DECRYPT_ERROR_REPLAY, 1);
235                   goto trace;
236                 }
237             }
238
239           sa0->total_data_size += b0->current_length;
240
241           if (PREDICT_FALSE (b0->n_add_refs > 0))
242             {
243               vec_add1 (to_be_freed, bi0);
244               b0 = vlib_buffer_copy (vm, b0);
245               bi0 = vlib_get_buffer_index (vm, b0);
246             }
247
248           JOB_AES_HMAC *job = IPSECMB_FUNC (get_next_job) (mgr);
249           int trunc_size = 0;
250           if (sa0->integ_alg != IPSEC_INTEG_ALG_NONE)
251             {
252               trunc_size =
253                 em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
254               // put calculated auth tag after in-packet auth tag
255               job->auth_tag_output =
256                 vlib_buffer_get_current (b0) + b0->current_length;
257               b0->current_length -= trunc_size;
258               job->msg_len_to_hash_in_bytes = b0->current_length;
259               job->auth_tag_output_len_in_bytes = trunc_size;
260               job->u.HMAC._hashed_auth_key_xor_ipad = samb0->ipad_hash;
261               job->u.HMAC._hashed_auth_key_xor_opad = samb0->opad_hash;
262             }
263
264           job->hash_alg = imbm->integ_algs[sa0->integ_alg].hash_alg;
265           u8 ip_hdr_size = 0;
266
267           if ((sa0->crypto_alg >= IPSEC_CRYPTO_ALG_AES_CBC_128 &&
268                sa0->crypto_alg <= IPSEC_CRYPTO_ALG_AES_CBC_256) ||
269               (sa0->crypto_alg >= IPSEC_CRYPTO_ALG_DES_CBC &&
270                sa0->crypto_alg <= IPSEC_CRYPTO_ALG_3DES_CBC))
271             {
272               const int block_size =
273                 em->ipsec_proto_main_crypto_algs[sa0->crypto_alg].block_size;
274               const int iv_size =
275                 em->ipsec_proto_main_crypto_algs[sa0->crypto_alg].iv_size;
276
277               int blocks =
278                 (b0->current_length - sizeof (esp_header_t) -
279                  iv_size) / block_size;
280               if (b0->current_length - sizeof (esp_header_t) - iv_size <
281                   block_size || blocks <= 0)
282                 {
283                   vlib_node_increment_counter (vm, node->node_index,
284                                                ESP_DECRYPT_ERROR_INTEG_ERROR,
285                                                1);
286                   goto trace;
287                 }
288
289               /* transport mode */
290               if (!sa0->is_tunnel)
291                 {
292                   if (b0->flags & VNET_BUFFER_F_L3_HDR_OFFSET_VALID)
293                     {
294                       if (is_ip6)
295                         {
296                           ip_hdr_size = sizeof (ip6_header_t);
297                         }
298                       else
299                         {
300                           ip_hdr_size = sizeof (ip4_header_t);
301                         }
302                     }
303                   else
304                     {
305                       vlib_node_increment_counter (vm, node->node_index,
306                                                    ESP_DECRYPT_ERROR_NOT_IP,
307                                                    1);
308                       goto trace;
309                     }
310                 }
311
312               job->chain_order = HASH_CIPHER;
313               job->cipher_direction = DECRYPT;
314               job->src = (u8 *) esp0;
315               job->dst = (u8 *) esp0;
316               vlib_buffer_advance (b0, -ip_hdr_size);
317               job->cipher_mode =
318                 imbm->crypto_algs[sa0->crypto_alg].cipher_mode;
319               job->aes_enc_key_expanded = samb0->aes_enc_key_expanded;
320               job->aes_dec_key_expanded = samb0->aes_dec_key_expanded;
321               job->aes_key_len_in_bytes = sa0->crypto_key_len;
322               job->iv = esp0->data;
323               job->iv_len_in_bytes = iv_size;
324               job->msg_len_to_cipher_in_bytes = blocks * block_size;
325               job->cipher_start_src_offset_in_bytes =
326                 sizeof (esp_header_t) + iv_size;
327               job->hash_start_src_offset_in_bytes = 0;
328
329               job->user_data = (void *) (uintptr_t) bi0;
330               job->user_data2 = sa0;
331               b0->current_length =
332                 (blocks * block_size) - sizeof (esp_footer_t) + ip_hdr_size;
333               ASSERT ((u8 *) vlib_buffer_get_current (b0) +
334                       b0->current_length < job->auth_tag_output);
335               b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
336
337               job = IPSECMB_FUNC (submit_job) (mgr);
338               ++packets_in_flight;
339
340               if (!job)
341                 {
342                   continue;
343                 }
344
345               --packets_in_flight;
346
347               sa0 = job->user_data2;
348               bi0 = (uintptr_t) job->user_data;
349               b0 = vlib_get_buffer (vm, bi0);
350               esp_finish_decrypt (vm, node, job, &next0, sa0, is_ip6);
351
352             trace:
353               to_next[0] = bi0;
354               to_next += 1;
355               n_left_to_next -= 1;
356
357               if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
358                 {
359                   esp_decrypt_trace_t *tr =
360                     vlib_add_trace (vm, node, b0, sizeof (*tr));
361                   tr->crypto_alg = sa0->crypto_alg;
362                   tr->integ_alg = sa0->integ_alg;
363                 }
364
365               vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
366                                                n_left_to_next, bi0, next0);
367             }
368         }
369
370       if (PREDICT_FALSE (n_left_from == 0 && packets_in_flight > 0))
371         {
372           JOB_AES_HMAC *job = NULL;
373           while (n_left_to_next > 0 && (job = IPSECMB_FUNC (flush_job) (mgr)))
374             {
375               --packets_in_flight;
376               u32 bi0, next0;
377               bi0 = (uintptr_t) job->user_data;
378               vlib_buffer_t *i_b0 = vlib_get_buffer (vm, bi0);
379
380               ipsec_sa_t *sa0 = job->user_data2;
381               esp_finish_decrypt (vm, node, job, &next0, sa0, is_ip6);
382
383               to_next[0] = bi0;
384               to_next += 1;
385               n_left_to_next -= 1;
386
387               if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
388                 {
389                   esp_decrypt_trace_t *tr =
390                     vlib_add_trace (vm, node, i_b0, sizeof (*tr));
391                   tr->crypto_alg = sa0->crypto_alg;
392                   tr->integ_alg = sa0->integ_alg;
393                 }
394
395               vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
396                                                n_left_to_next, bi0, next0);
397             }
398         }
399
400       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
401     }
402   vlib_node_increment_counter (vm, node->node_index,
403                                ESP_DECRYPT_ERROR_RX_PKTS,
404                                from_frame->n_vectors);
405
406   if (to_be_freed)
407     vlib_buffer_free (vm, to_be_freed, vec_len (to_be_freed));
408   vec_free (to_be_freed);
409   return from_frame->n_vectors;
410 }
411
412 VLIB_NODE_FN (esp4_decrypt_ipsecmb_node) (vlib_main_t * vm,
413                                           vlib_node_runtime_t * node,
414                                           vlib_frame_t * from_frame)
415 {
416   return esp_decrypt_ipsecmb_inline (vm, node, from_frame, 0 /*is_ip6 */ );
417 }
418
419 VLIB_NODE_FN (esp6_decrypt_ipsecmb_node) (vlib_main_t * vm,
420                                           vlib_node_runtime_t * node,
421                                           vlib_frame_t * from_frame)
422 {
423   return esp_decrypt_ipsecmb_inline (vm, node, from_frame, 1 /*is_ip6 */ );
424 }
425 #endif
426
427 /* *INDENT-OFF* */
428 VLIB_REGISTER_NODE (esp4_decrypt_ipsecmb_node) = {
429   .name = "esp4-decrypt-ipsecmb",
430   .vector_size = sizeof (u32),
431   .format_trace = format_esp_decrypt_trace,
432   .type = VLIB_NODE_TYPE_INTERNAL,
433
434   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
435   .error_strings = esp_decrypt_error_strings,
436
437   .n_next_nodes = ESP_DECRYPT_N_NEXT,
438   .next_nodes = {
439 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
440     foreach_esp_decrypt_next
441 #undef _
442   },
443 };
444 /* *INDENT-ON* */
445
446 /* *INDENT-OFF* */
447 VLIB_REGISTER_NODE (esp6_decrypt_ipsecmb_node) = {
448   .name = "esp6-decrypt-ipsecmb",
449   .vector_size = sizeof (u32),
450   .format_trace = format_esp_decrypt_trace,
451   .type = VLIB_NODE_TYPE_INTERNAL,
452
453   .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
454   .error_strings = esp_decrypt_error_strings,
455
456   .n_next_nodes = ESP_DECRYPT_N_NEXT,
457   .next_nodes = {
458 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
459     foreach_esp_decrypt_next
460 #undef _
461   },
462 };
463 /* *INDENT-ON* */
464
465 /*
466  * fd.io coding-style-patch-verification: ON
467  *
468  * Local Variables:
469  * eval: (c-set-style "gnu")
470  * End:
471  */