2 * esp_decrypt.c : IPSec ESP Decrypt node using DPDK Cryptodev
4 * Copyright (c) 2017 Intel 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 opy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vnet/vnet.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/ip/ip.h>
22 #include <vnet/ipsec/ipsec.h>
23 #include <vnet/ipsec/esp.h>
24 #include <dpdk/ipsec/ipsec.h>
25 #include <dpdk/device/dpdk.h>
26 #include <dpdk/device/dpdk_priv.h>
28 #define foreach_esp_decrypt_next \
29 _(DROP, "error-drop") \
30 _(IP4_INPUT, "ip4-input-no-checksum") \
31 _(IP6_INPUT, "ip6-input")
33 #define _(v, s) ESP_DECRYPT_NEXT_##v,
35 foreach_esp_decrypt_next
40 #define foreach_esp_decrypt_error \
41 _(RX_PKTS, "ESP pkts received") \
42 _(DECRYPTION_FAILED, "ESP decryption failed") \
43 _(REPLAY, "SA replayed packet") \
44 _(NOT_IP, "Not IP packet (dropped)") \
45 _(ENQ_FAIL, "Enqueue failed (buffer full)") \
46 _(DISCARD, "Not enough crypto operations, discarding frame") \
47 _(BAD_LEN, "Invalid ciphertext length") \
48 _(SESSION, "Failed to get crypto session") \
49 _(NOSUP, "Cipher/Auth not supported")
53 #define _(sym,str) ESP_DECRYPT_ERROR_##sym,
54 foreach_esp_decrypt_error
57 } esp_decrypt_error_t;
59 static char * esp_decrypt_error_strings[] = {
60 #define _(sym,string) string,
61 foreach_esp_decrypt_error
65 vlib_node_registration_t dpdk_esp_decrypt_node;
68 ipsec_crypto_alg_t crypto_alg;
69 ipsec_integ_alg_t integ_alg;
71 } esp_decrypt_trace_t;
73 /* packet trace format function */
74 static u8 * format_esp_decrypt_trace (u8 * s, va_list * args)
76 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
77 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
78 esp_decrypt_trace_t * t = va_arg (*args, esp_decrypt_trace_t *);
79 u32 indent = format_get_indent (s);
81 s = format (s, "cipher %U auth %U\n",
82 format_ipsec_crypto_alg, t->crypto_alg,
83 format_ipsec_integ_alg, t->integ_alg);
84 s = format (s, "%U%U",
85 format_white_space, indent,
86 format_esp_header, t->packet_data);
91 dpdk_esp_decrypt_node_fn (vlib_main_t * vm,
92 vlib_node_runtime_t * node,
93 vlib_frame_t * from_frame)
95 u32 n_left_from, *from, *to_next, next_index;
96 ipsec_main_t *im = &ipsec_main;
97 u32 thread_idx = vlib_get_thread_index();
98 dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
99 crypto_resource_t *res = 0;
101 crypto_alg_t *cipher_alg = 0, *auth_alg = 0;
102 struct rte_cryptodev_sym_session *session = 0;
103 u32 ret, last_sa_index = ~0;
104 u8 numa = rte_socket_id ();
106 crypto_worker_main_t *cwm =
107 vec_elt_at_index (dcm->workers_main, thread_idx);
108 struct rte_crypto_op **ops = cwm->ops;
110 from = vlib_frame_vector_args (from_frame);
111 n_left_from = from_frame->n_vectors;
113 ret = crypto_alloc_ops (numa, ops, n_left_from);
116 vlib_node_increment_counter (vm, dpdk_esp_decrypt_node.index,
117 ESP_DECRYPT_ERROR_DISCARD, 1);
118 /* Discard whole frame */
122 next_index = ESP_DECRYPT_NEXT_DROP;
124 while (n_left_from > 0)
128 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
130 while (n_left_from > 0 && n_left_to_next > 0)
133 u32 bi0, sa_index0, seq, iv_size;
137 struct rte_mbuf *mb0;
138 struct rte_crypto_op *op;
145 b0 = vlib_get_buffer (vm, bi0);
146 mb0 = rte_mbuf_from_vlib_buffer(b0);
147 esp0 = vlib_buffer_get_current (b0);
150 CLIB_PREFETCH (esp0, sizeof (esp0[0]) + 16, LOAD);
152 CLIB_PREFETCH (mb0, CLIB_CACHE_LINE_BYTES, STORE);
156 ASSERT (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED);
158 dpdk_op_priv_t *priv = crypto_op_get_priv (op);
161 sizeof (op[0]) + sizeof (op[0].sym[0]) + sizeof (priv[0]);
162 CLIB_PREFETCH (op, op_len, STORE);
164 sa_index0 = vnet_buffer(b0)->ipsec.sad_index;
166 if (sa_index0 != last_sa_index)
168 sa0 = pool_elt_at_index (im->sad, sa_index0);
170 cipher_alg = vec_elt_at_index (dcm->cipher_algs, sa0->crypto_alg);
171 auth_alg = vec_elt_at_index (dcm->auth_algs, sa0->integ_alg);
173 is_aead = (cipher_alg->type == RTE_CRYPTO_SYM_XFORM_AEAD);
175 auth_alg = cipher_alg;
177 res_idx = get_resource (cwm, sa0);
179 if (PREDICT_FALSE (res_idx == (u16) ~0))
181 clib_warning ("unsupported SA by thread index %u", thread_idx);
182 vlib_node_increment_counter (vm, dpdk_esp_decrypt_node.index,
183 ESP_DECRYPT_ERROR_NOSUP, 1);
189 res = vec_elt_at_index (dcm->resource, res_idx);
191 error = crypto_get_session (&session, sa_index0, res, cwm, 0);
192 if (PREDICT_FALSE (error || !session))
194 clib_warning ("failed to get crypto session");
195 vlib_node_increment_counter (vm, dpdk_esp_decrypt_node.index,
196 ESP_DECRYPT_ERROR_SESSION, 1);
203 last_sa_index = sa_index0;
206 /* anti-replay check */
207 if (sa0->use_anti_replay)
211 seq = clib_net_to_host_u32 (esp0->seq);
213 if (PREDICT_TRUE(sa0->use_esn))
214 rv = esp_replay_check_esn (sa0, seq);
216 rv = esp_replay_check (sa0, seq);
218 if (PREDICT_FALSE (rv))
220 clib_warning ("failed anti-replay check");
221 vlib_node_increment_counter (vm, dpdk_esp_decrypt_node.index,
222 ESP_DECRYPT_ERROR_REPLAY, 1);
230 priv->next = DPDK_CRYPTO_INPUT_NEXT_DECRYPT_POST;
232 /* FIXME multi-seg */
233 sa0->total_data_size += b0->current_length;
235 res->ops[res->n_ops] = op;
236 res->bi[res->n_ops] = bi0;
239 /* Convert vlib buffer to mbuf */
240 mb0->data_len = b0->current_length;
241 mb0->pkt_len = b0->current_length;
242 mb0->data_off = RTE_PKTMBUF_HEADROOM + b0->current_data;
244 trunc_size = auth_alg->trunc_size;
245 iv_size = cipher_alg->iv_len;
247 /* Outer IP header has already been stripped */
249 b0->current_length - sizeof (esp_header_t) - iv_size - trunc_size;
251 ASSERT (payload_len >= 4);
253 if (payload_len & (cipher_alg->boundary - 1))
255 clib_warning ("payload %u not multiple of %d\n",
256 payload_len, cipher_alg->boundary);
257 vlib_node_increment_counter (vm, dpdk_esp_decrypt_node.index,
258 ESP_DECRYPT_ERROR_BAD_LEN, 1);
266 u32 cipher_off, cipher_len;
270 u8 *iv = (u8 *) (esp0 + 1);
272 dpdk_gcm_cnt_blk *icb = &priv->cb;
274 cipher_off = sizeof (esp_header_t) + iv_size;
275 cipher_len = payload_len;
277 u8 *digest = vlib_buffer_get_tail (b0) - trunc_size;
279 mb0->buf_physaddr + digest - ((u8 *) mb0->buf_addr);
281 if (!is_aead && cipher_alg->alg == RTE_CRYPTO_CIPHER_AES_CBC)
282 clib_memcpy(icb, iv, 16);
285 u32 *_iv = (u32 *) iv;
287 crypto_set_icb (icb, sa0->salt, _iv[0], _iv[1]);
293 u32 * _aad = (u32 *) aad;
294 clib_memcpy (aad, esp0, 8);
296 /* _aad[3] should always be 0 */
297 if (PREDICT_FALSE (sa0->use_esn))
298 _aad[2] = clib_host_to_net_u32 (sa0->seq_hi);
304 auth_len = sizeof(esp_header_t) + iv_size + payload_len;
308 clib_memcpy (priv->icv, digest, trunc_size);
309 u32 *_digest = (u32 *) digest;
310 _digest[0] = clib_host_to_net_u32 (sa0->seq_hi);
311 auth_len += sizeof(sa0->seq_hi);
315 op->phys_addr + (uintptr_t) priv->icv - (uintptr_t) op;
319 crypto_op_setup (is_aead, mb0, op, session, cipher_off, cipher_len,
320 0, auth_len, aad, digest, digest_paddr);
322 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
324 esp_decrypt_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
325 tr->crypto_alg = sa0->crypto_alg;
326 tr->integ_alg = sa0->integ_alg;
327 clib_memcpy (tr->packet_data, vlib_buffer_get_current (b0),
328 sizeof (esp_header_t));
331 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
334 vlib_node_increment_counter (vm, dpdk_esp_decrypt_node.index,
335 ESP_DECRYPT_ERROR_RX_PKTS,
336 from_frame->n_vectors);
338 crypto_enqueue_ops (vm, cwm, 0, dpdk_esp_decrypt_node.index,
339 ESP_DECRYPT_ERROR_ENQ_FAIL, numa);
341 crypto_free_ops (numa, ops, cwm->ops + from_frame->n_vectors - ops);
343 return from_frame->n_vectors;
347 VLIB_REGISTER_NODE (dpdk_esp_decrypt_node) = {
348 .function = dpdk_esp_decrypt_node_fn,
349 .name = "dpdk-esp-decrypt",
350 .vector_size = sizeof (u32),
351 .format_trace = format_esp_decrypt_trace,
352 .type = VLIB_NODE_TYPE_INTERNAL,
354 .n_errors = ARRAY_LEN(esp_decrypt_error_strings),
355 .error_strings = esp_decrypt_error_strings,
357 .n_next_nodes = ESP_DECRYPT_N_NEXT,
359 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
360 foreach_esp_decrypt_next
366 VLIB_NODE_FUNCTION_MULTIARCH (dpdk_esp_decrypt_node, dpdk_esp_decrypt_node_fn)
372 #define foreach_esp_decrypt_post_error \
373 _(PKTS, "ESP post pkts")
376 #define _(sym,str) ESP_DECRYPT_POST_ERROR_##sym,
377 foreach_esp_decrypt_post_error
379 ESP_DECRYPT_POST_N_ERROR,
380 } esp_decrypt_post_error_t;
382 static char * esp_decrypt_post_error_strings[] = {
383 #define _(sym,string) string,
384 foreach_esp_decrypt_post_error
388 vlib_node_registration_t dpdk_esp_decrypt_post_node;
390 static u8 * format_esp_decrypt_post_trace (u8 * s, va_list * args)
392 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
393 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
394 esp_decrypt_trace_t * t = va_arg (*args, esp_decrypt_trace_t *);
395 u32 indent = format_get_indent (s);
397 s = format (s, "cipher %U auth %U\n",
398 format_ipsec_crypto_alg, t->crypto_alg,
399 format_ipsec_integ_alg, t->integ_alg);
401 ip4_header_t *ih4 = (ip4_header_t *) t->packet_data;
402 if ((ih4->ip_version_and_header_length & 0xF0) == 0x60)
403 s = format (s, "%U%U", format_white_space, indent, format_ip6_header, ih4);
405 s = format (s, "%U%U", format_white_space, indent, format_ip4_header, ih4);
411 dpdk_esp_decrypt_post_node_fn (vlib_main_t * vm,
412 vlib_node_runtime_t * node,
413 vlib_frame_t * from_frame)
415 u32 n_left_from, *from, *to_next = 0, next_index;
418 ipsec_main_t *im = &ipsec_main;
419 dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
421 from = vlib_frame_vector_args (from_frame);
422 n_left_from = from_frame->n_vectors;
424 next_index = node->cached_next_index;
426 while (n_left_from > 0)
430 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
432 while (n_left_from > 0 && n_left_to_next > 0)
435 u32 bi0, iv_size, next0;
436 vlib_buffer_t * b0 = 0;
437 ip4_header_t *ih4 = 0, *oh4 = 0;
438 ip6_header_t *ih6 = 0, *oh6 = 0;
439 crypto_alg_t *cipher_alg, *auth_alg;
441 u8 trunc_size, is_aead;
442 u16 udp_encap_adv = 0;
444 next0 = ESP_DECRYPT_NEXT_DROP;
451 b0 = vlib_get_buffer (vm, bi0);
452 esp0 = vlib_buffer_get_current (b0);
454 sa_index0 = vnet_buffer(b0)->ipsec.sad_index;
455 sa0 = pool_elt_at_index (im->sad, sa_index0);
460 cipher_alg = vec_elt_at_index (dcm->cipher_algs, sa0->crypto_alg);
461 auth_alg = vec_elt_at_index (dcm->auth_algs, sa0->integ_alg);
462 is_aead = cipher_alg->type == RTE_CRYPTO_SYM_XFORM_AEAD;
464 auth_alg = cipher_alg;
466 trunc_size = auth_alg->trunc_size;
468 iv_size = cipher_alg->iv_len;
470 if (sa0->use_anti_replay)
473 seq = clib_host_to_net_u32(esp0->seq);
474 if (PREDICT_TRUE(sa0->use_esn))
475 esp_replay_advance_esn(sa0, seq);
477 esp_replay_advance(sa0, seq);
480 /* if UDP encapsulation is used adjust the address of the IP header */
481 if (sa0->udp_encap && (b0->flags & VNET_BUFFER_F_IS_IP4))
483 udp_encap_adv = sizeof (udp_header_t);
486 if (b0->flags & VNET_BUFFER_F_IS_IP4)
487 ih4 = (ip4_header_t *)
488 ((u8 *) esp0 - udp_encap_adv - sizeof (ip4_header_t));
491 (ip4_header_t *) ((u8 *) esp0 - sizeof (ip6_header_t));
493 vlib_buffer_advance (b0, sizeof (esp_header_t) + iv_size);
495 b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
496 f0 = (esp_footer_t *) (vlib_buffer_get_tail (b0) - trunc_size - 2);
497 b0->current_length -= (f0->pad_length + trunc_size + 2);
500 const u8 *padding = vlib_buffer_get_tail (b0);
501 if (PREDICT_FALSE (memcmp (padding, pad_data, f0->pad_length)))
503 clib_warning("bad padding");
504 vlib_node_increment_counter (vm, dpdk_esp_decrypt_node.index,
505 ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
512 if (f0->next_header == IP_PROTOCOL_IP_IN_IP)
513 next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
514 else if (sa0->is_tunnel_ip6 && f0->next_header == IP_PROTOCOL_IPV6)
515 next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
518 clib_warning("next header: 0x%x", f0->next_header);
519 vlib_node_increment_counter (vm, dpdk_esp_decrypt_node.index,
520 ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
525 else /* transport mode */
527 if ((ih4->ip_version_and_header_length & 0xF0) == 0x40)
529 u16 ih4_len = ip4_header_bytes (ih4);
530 vlib_buffer_advance (b0, - ih4_len - udp_encap_adv);
531 next0 = ESP_DECRYPT_NEXT_IP4_INPUT;
534 oh4 = vlib_buffer_get_current (b0);
535 memmove(oh4, ih4, ih4_len);
536 oh4->protocol = f0->next_header;
537 oh4->length = clib_host_to_net_u16 (b0->current_length);
538 oh4->checksum = ip4_header_checksum(oh4);
541 else if ((ih4->ip_version_and_header_length & 0xF0) == 0x60)
543 ih6 = (ip6_header_t *) ih4;
544 vlib_buffer_advance (b0, -sizeof(ip6_header_t));
545 oh6 = vlib_buffer_get_current (b0);
546 memmove(oh6, ih6, sizeof(ip6_header_t));
548 next0 = ESP_DECRYPT_NEXT_IP6_INPUT;
549 oh6->protocol = f0->next_header;
550 u16 len = b0->current_length - sizeof (ip6_header_t);
551 oh6->payload_length = clib_host_to_net_u16 (len);
555 clib_warning("next header: 0x%x", f0->next_header);
556 vlib_node_increment_counter (vm, dpdk_esp_decrypt_node.index,
557 ESP_DECRYPT_ERROR_DECRYPTION_FAILED,
563 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32)~0;
566 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
568 esp_decrypt_trace_t *tr = vlib_add_trace (vm, node, b0, sizeof (*tr));
569 tr->crypto_alg = sa0->crypto_alg;
570 tr->integ_alg = sa0->integ_alg;
571 ih4 = vlib_buffer_get_current (b0);
572 clib_memcpy (tr->packet_data, ih4, sizeof (ip6_header_t));
575 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
576 to_next, n_left_to_next, bi0, next0);
578 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
581 vlib_node_increment_counter (vm, dpdk_esp_decrypt_post_node.index,
582 ESP_DECRYPT_POST_ERROR_PKTS,
583 from_frame->n_vectors);
585 return from_frame->n_vectors;
589 VLIB_REGISTER_NODE (dpdk_esp_decrypt_post_node) = {
590 .function = dpdk_esp_decrypt_post_node_fn,
591 .name = "dpdk-esp-decrypt-post",
592 .vector_size = sizeof (u32),
593 .format_trace = format_esp_decrypt_post_trace,
594 .type = VLIB_NODE_TYPE_INTERNAL,
596 .n_errors = ARRAY_LEN(esp_decrypt_post_error_strings),
597 .error_strings = esp_decrypt_post_error_strings,
599 .n_next_nodes = ESP_DECRYPT_N_NEXT,
601 #define _(s,n) [ESP_DECRYPT_NEXT_##s] = n,
602 foreach_esp_decrypt_next
608 VLIB_NODE_FUNCTION_MULTIARCH (dpdk_esp_decrypt_post_node, dpdk_esp_decrypt_post_node_fn)