7947a8e9d02bfc9c40b0ac60922852319f19703c
[vpp.git] / src / plugins / dpdk / ipsec / esp_encrypt.c
1 /*
2  * esp_encrypt.c : IPSec ESP encrypt node using DPDK Cryptodev
3  *
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 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 <vnet/udp/udp.h>
25 #include <dpdk/ipsec/ipsec.h>
26 #include <dpdk/device/dpdk.h>
27 #include <dpdk/device/dpdk_priv.h>
28
29 #define foreach_esp_encrypt_next                   \
30 _(DROP, "error-drop")                              \
31 _(IP4_LOOKUP, "ip4-lookup")                        \
32 _(IP6_LOOKUP, "ip6-lookup")                        \
33 _(INTERFACE_OUTPUT, "interface-output")
34
35 #define _(v, s) ESP_ENCRYPT_NEXT_##v,
36 typedef enum
37 {
38   foreach_esp_encrypt_next
39 #undef _
40     ESP_ENCRYPT_N_NEXT,
41 } esp_encrypt_next_t;
42
43 #define foreach_esp_encrypt_error                   \
44  _(RX_PKTS, "ESP pkts received")                    \
45  _(SEQ_CYCLED, "Sequence number cycled")            \
46  _(ENQ_FAIL, "Enqueue failed to crypto device")     \
47  _(DISCARD, "Not enough crypto operations, discarding frame")  \
48  _(SESSION, "Failed to get crypto session")         \
49  _(NOSUP, "Cipher/Auth not supported")
50
51
52 typedef enum
53 {
54 #define _(sym,str) ESP_ENCRYPT_ERROR_##sym,
55   foreach_esp_encrypt_error
56 #undef _
57     ESP_ENCRYPT_N_ERROR,
58 } esp_encrypt_error_t;
59
60 static char *esp_encrypt_error_strings[] = {
61 #define _(sym,string) string,
62   foreach_esp_encrypt_error
63 #undef _
64 };
65
66 vlib_node_registration_t dpdk_esp_encrypt_node;
67
68 typedef struct
69 {
70   ipsec_crypto_alg_t crypto_alg;
71   ipsec_integ_alg_t integ_alg;
72   u8 packet_data[64];
73 } esp_encrypt_trace_t;
74
75 /* packet trace format function */
76 static u8 *
77 format_esp_encrypt_trace (u8 * s, va_list * args)
78 {
79   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
80   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
81   esp_encrypt_trace_t *t = va_arg (*args, esp_encrypt_trace_t *);
82   ip4_header_t *ih4 = (ip4_header_t *) t->packet_data;
83   u32 indent = format_get_indent (s), offset;
84
85   s = format (s, "cipher %U auth %U\n",
86               format_ipsec_crypto_alg, t->crypto_alg,
87               format_ipsec_integ_alg, t->integ_alg);
88
89   if ((ih4->ip_version_and_header_length & 0xF0) == 0x60)
90     {
91       s = format (s, "%U%U", format_white_space, indent,
92                   format_ip6_header, ih4);
93       offset = sizeof (ip6_header_t);
94     }
95   else
96     {
97       s = format (s, "%U%U", format_white_space, indent,
98                   format_ip4_header, ih4);
99       offset = ip4_header_bytes (ih4);
100     }
101
102   s = format (s, "\n%U%U", format_white_space, indent,
103               format_esp_header, t->packet_data + offset);
104
105   return s;
106 }
107
108 static uword
109 dpdk_esp_encrypt_node_fn (vlib_main_t * vm,
110                           vlib_node_runtime_t * node,
111                           vlib_frame_t * from_frame)
112 {
113   u32 n_left_from, *from, *to_next, next_index;
114   ipsec_main_t *im = &ipsec_main;
115   u32 thread_idx = vlib_get_thread_index ();
116   dpdk_crypto_main_t *dcm = &dpdk_crypto_main;
117   crypto_resource_t *res = 0;
118   ipsec_sa_t *sa0 = 0;
119   crypto_alg_t *cipher_alg = 0, *auth_alg = 0;
120   struct rte_cryptodev_sym_session *session = 0;
121   u32 ret, last_sa_index = ~0;
122   u8 numa = rte_socket_id ();
123   u8 is_aead = 0;
124   crypto_worker_main_t *cwm =
125     vec_elt_at_index (dcm->workers_main, thread_idx);
126   struct rte_crypto_op **ops = cwm->ops;
127
128   from = vlib_frame_vector_args (from_frame);
129   n_left_from = from_frame->n_vectors;
130
131   ret = crypto_alloc_ops (numa, ops, n_left_from);
132   if (ret)
133     {
134       vlib_node_increment_counter (vm, dpdk_esp_encrypt_node.index,
135                                    ESP_ENCRYPT_ERROR_DISCARD, 1);
136       /* Discard whole frame */
137       return n_left_from;
138     }
139
140   next_index = ESP_ENCRYPT_NEXT_DROP;
141
142   while (n_left_from > 0)
143     {
144       u32 n_left_to_next;
145
146       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
147
148       while (n_left_from > 0 && n_left_to_next > 0)
149         {
150           clib_error_t *error;
151           u32 bi0;
152           vlib_buffer_t *b0 = 0;
153           u32 sa_index0;
154           ip4_and_esp_header_t *ih0, *oh0 = 0;
155           ip6_and_esp_header_t *ih6_0, *oh6_0 = 0;
156           ip4_and_udp_and_esp_header_t *ouh0 = 0;
157           esp_header_t *esp0;
158           esp_footer_t *f0;
159           u8 is_ipv6, next_hdr_type;
160           u32 iv_size;
161           u16 orig_sz;
162           u8 trunc_size;
163           u16 rewrite_len;
164           u16 udp_encap_adv = 0;
165           struct rte_mbuf *mb0 = 0;
166           struct rte_crypto_op *op;
167           u16 res_idx;
168
169           bi0 = from[0];
170           from += 1;
171           n_left_from -= 1;
172
173           b0 = vlib_get_buffer (vm, bi0);
174           ih0 = vlib_buffer_get_current (b0);
175           mb0 = rte_mbuf_from_vlib_buffer (b0);
176
177           /* ih0/ih6_0 */
178           CLIB_PREFETCH (ih0, sizeof (ih6_0[0]), LOAD);
179           /* f0 */
180           CLIB_PREFETCH (vlib_buffer_get_tail (b0), 20, STORE);
181           /* mb0 */
182           CLIB_PREFETCH (mb0, CLIB_CACHE_LINE_BYTES, STORE);
183
184           op = ops[0];
185           ops += 1;
186           ASSERT (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED);
187
188           dpdk_op_priv_t *priv = crypto_op_get_priv (op);
189
190           u16 op_len =
191             sizeof (op[0]) + sizeof (op[0].sym[0]) + sizeof (priv[0]);
192           CLIB_PREFETCH (op, op_len, STORE);
193
194           sa_index0 = vnet_buffer (b0)->ipsec.sad_index;
195
196           if (sa_index0 != last_sa_index)
197             {
198               sa0 = pool_elt_at_index (im->sad, sa_index0);
199
200               cipher_alg =
201                 vec_elt_at_index (dcm->cipher_algs, sa0->crypto_alg);
202               auth_alg = vec_elt_at_index (dcm->auth_algs, sa0->integ_alg);
203
204               is_aead = (cipher_alg->type == RTE_CRYPTO_SYM_XFORM_AEAD);
205
206               if (is_aead)
207                 auth_alg = cipher_alg;
208
209               res_idx = get_resource (cwm, sa0);
210
211               if (PREDICT_FALSE (res_idx == (u16) ~ 0))
212                 {
213                   clib_warning ("unsupported SA by thread index %u",
214                                 thread_idx);
215                   vlib_node_increment_counter (vm,
216                                                dpdk_esp_encrypt_node.index,
217                                                ESP_ENCRYPT_ERROR_NOSUP, 1);
218                   to_next[0] = bi0;
219                   to_next += 1;
220                   n_left_to_next -= 1;
221                   goto trace;
222                 }
223               res = vec_elt_at_index (dcm->resource, res_idx);
224
225               error = crypto_get_session (&session, sa_index0, res, cwm, 1);
226               if (PREDICT_FALSE (error || !session))
227                 {
228                   clib_warning ("failed to get crypto session");
229                   vlib_node_increment_counter (vm,
230                                                dpdk_esp_encrypt_node.index,
231                                                ESP_ENCRYPT_ERROR_SESSION, 1);
232                   to_next[0] = bi0;
233                   to_next += 1;
234                   n_left_to_next -= 1;
235                   goto trace;
236                 }
237
238               last_sa_index = sa_index0;
239             }
240
241           if (PREDICT_FALSE (esp_seq_advance (sa0)))
242             {
243               clib_warning ("sequence number counter has cycled SPI %u",
244                             sa0->spi);
245               vlib_node_increment_counter (vm, dpdk_esp_encrypt_node.index,
246                                            ESP_ENCRYPT_ERROR_SEQ_CYCLED, 1);
247               //TODO: rekey SA
248               to_next[0] = bi0;
249               to_next += 1;
250               n_left_to_next -= 1;
251               goto trace;
252             }
253
254           orig_sz = b0->current_length;
255
256           /* TODO multi-seg support - total_length_not_including_first_buffer */
257           sa0->total_data_size += b0->current_length;
258
259           res->ops[res->n_ops] = op;
260           res->bi[res->n_ops] = bi0;
261           res->n_ops += 1;
262
263           dpdk_gcm_cnt_blk *icb = &priv->cb;
264
265           crypto_set_icb (icb, sa0->salt, sa0->seq, sa0->seq_hi);
266
267           is_ipv6 = (ih0->ip4.ip_version_and_header_length & 0xF0) == 0x60;
268
269           iv_size = cipher_alg->iv_len;
270           trunc_size = auth_alg->trunc_size;
271
272           /* if UDP encapsulation is used adjust the address of the IP header */
273           if (sa0->udp_encap && !is_ipv6)
274             udp_encap_adv = sizeof (udp_header_t);
275
276           if (sa0->is_tunnel)
277             {
278               rewrite_len = 0;
279               if (!is_ipv6 && !sa0->is_tunnel_ip6)      /* ip4inip4 */
280                 {
281                   /* in tunnel mode send it back to FIB */
282                   priv->next = DPDK_CRYPTO_INPUT_NEXT_IP4_LOOKUP;
283                   u8 adv = sizeof (ip4_header_t) + udp_encap_adv +
284                     sizeof (esp_header_t) + iv_size;
285                   vlib_buffer_advance (b0, -adv);
286                   oh0 = vlib_buffer_get_current (b0);
287                   ouh0 = vlib_buffer_get_current (b0);
288                   next_hdr_type = IP_PROTOCOL_IP_IN_IP;
289                   /*
290                    * oh0->ip4.ip_version_and_header_length = 0x45;
291                    * oh0->ip4.tos = ih0->ip4.tos;
292                    * oh0->ip4.fragment_id = 0;
293                    * oh0->ip4.flags_and_fragment_offset = 0;
294                    */
295                   oh0->ip4.checksum_data_64[0] =
296                     clib_host_to_net_u64 (0x45ULL << 56);
297                   /*
298                    * oh0->ip4.ttl = 254;
299                    * oh0->ip4.protocol = IP_PROTOCOL_IPSEC_ESP;
300                    */
301                   oh0->ip4.checksum_data_32[2] =
302                     clib_host_to_net_u32 (0xfe320000);
303
304                   oh0->ip4.src_address.as_u32 =
305                     sa0->tunnel_src_addr.ip4.as_u32;
306                   oh0->ip4.dst_address.as_u32 =
307                     sa0->tunnel_dst_addr.ip4.as_u32;
308
309                   if (sa0->udp_encap)
310                     {
311                       oh0->ip4.protocol = IP_PROTOCOL_UDP;
312                       esp0 = &ouh0->esp;
313                     }
314                   else
315                     esp0 = &oh0->esp;
316                   esp0->spi = clib_host_to_net_u32 (sa0->spi);
317                   esp0->seq = clib_host_to_net_u32 (sa0->seq);
318                 }
319               else if (is_ipv6 && sa0->is_tunnel_ip6)   /* ip6inip6 */
320                 {
321                   /* in tunnel mode send it back to FIB */
322                   priv->next = DPDK_CRYPTO_INPUT_NEXT_IP6_LOOKUP;
323
324                   u8 adv =
325                     sizeof (ip6_header_t) + sizeof (esp_header_t) + iv_size;
326                   vlib_buffer_advance (b0, -adv);
327                   ih6_0 = (ip6_and_esp_header_t *) ih0;
328                   oh6_0 = vlib_buffer_get_current (b0);
329
330                   next_hdr_type = IP_PROTOCOL_IPV6;
331
332                   oh6_0->ip6.ip_version_traffic_class_and_flow_label =
333                     ih6_0->ip6.ip_version_traffic_class_and_flow_label;
334
335                   oh6_0->ip6.protocol = IP_PROTOCOL_IPSEC_ESP;
336                   oh6_0->ip6.hop_limit = 254;
337                   oh6_0->ip6.src_address.as_u64[0] =
338                     sa0->tunnel_src_addr.ip6.as_u64[0];
339                   oh6_0->ip6.src_address.as_u64[1] =
340                     sa0->tunnel_src_addr.ip6.as_u64[1];
341                   oh6_0->ip6.dst_address.as_u64[0] =
342                     sa0->tunnel_dst_addr.ip6.as_u64[0];
343                   oh6_0->ip6.dst_address.as_u64[1] =
344                     sa0->tunnel_dst_addr.ip6.as_u64[1];
345                   esp0 = &oh6_0->esp;
346                   oh6_0->esp.spi = clib_host_to_net_u32 (sa0->spi);
347                   oh6_0->esp.seq = clib_host_to_net_u32 (sa0->seq);
348                 }
349               else              /* unsupported ip4inip6, ip6inip4 */
350                 {
351                   vlib_node_increment_counter (vm,
352                                                dpdk_esp_encrypt_node.index,
353                                                ESP_ENCRYPT_ERROR_NOSUP, 1);
354                   to_next[0] = bi0;
355                   to_next += 1;
356                   n_left_to_next -= 1;
357                   goto trace;
358                 }
359               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
360             }
361           else                  /* transport mode */
362             {
363               priv->next = DPDK_CRYPTO_INPUT_NEXT_INTERFACE_OUTPUT;
364               rewrite_len = vnet_buffer (b0)->ip.save_rewrite_length;
365               u16 adv = sizeof (esp_header_t) + iv_size + udp_encap_adv;
366               vlib_buffer_advance (b0, -adv - rewrite_len);
367               u8 *src = ((u8 *) ih0) - rewrite_len;
368               u8 *dst = vlib_buffer_get_current (b0);
369               oh0 = vlib_buffer_get_current (b0) + rewrite_len;
370
371               if (is_ipv6)
372                 {
373                   orig_sz -= sizeof (ip6_header_t);
374                   ih6_0 = (ip6_and_esp_header_t *) ih0;
375                   next_hdr_type = ih6_0->ip6.protocol;
376                   memmove (dst, src, rewrite_len + sizeof (ip6_header_t));
377                   oh6_0 = (ip6_and_esp_header_t *) oh0;
378                   oh6_0->ip6.protocol = IP_PROTOCOL_IPSEC_ESP;
379                   esp0 = &oh6_0->esp;
380                 }
381               else              /* ipv4 */
382                 {
383                   u16 ip_size = ip4_header_bytes (&ih0->ip4);
384                   orig_sz -= ip_size;
385                   next_hdr_type = ih0->ip4.protocol;
386                   memmove (dst, src, rewrite_len + ip_size);
387                   oh0->ip4.protocol = IP_PROTOCOL_IPSEC_ESP;
388                   esp0 = (esp_header_t *) (((u8 *) oh0) + ip_size);
389                   if (sa0->udp_encap)
390                     {
391                       oh0->ip4.protocol = IP_PROTOCOL_UDP;
392                       esp0 = (esp_header_t *)
393                         (((u8 *) oh0) + ip_size + udp_encap_adv);
394                     }
395                   else
396                     {
397                       oh0->ip4.protocol = IP_PROTOCOL_IPSEC_ESP;
398                       esp0 = (esp_header_t *) (((u8 *) oh0) + ip_size);
399                     }
400                 }
401               esp0->spi = clib_host_to_net_u32 (sa0->spi);
402               esp0->seq = clib_host_to_net_u32 (sa0->seq);
403             }
404
405           if (sa0->udp_encap && ouh0)
406             {
407               ouh0->udp.src_port = clib_host_to_net_u16 (UDP_DST_PORT_ipsec);
408               ouh0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_ipsec);
409               ouh0->udp.checksum = 0;
410             }
411           ASSERT (is_pow2 (cipher_alg->boundary));
412           u16 mask = cipher_alg->boundary - 1;
413           u16 pad_payload_len = ((orig_sz + 2) + mask) & ~mask;
414           u8 pad_bytes = pad_payload_len - 2 - orig_sz;
415
416           u8 *padding =
417             vlib_buffer_put_uninit (b0, pad_bytes + 2 + trunc_size);
418
419           /* The extra pad bytes would be overwritten by the digest */
420           if (pad_bytes)
421             clib_memcpy (padding, pad_data, 16);
422
423           f0 = (esp_footer_t *) (padding + pad_bytes);
424           f0->pad_length = pad_bytes;
425           f0->next_header = next_hdr_type;
426
427           if (is_ipv6)
428             {
429               u16 len = b0->current_length - sizeof (ip6_header_t);
430               oh6_0->ip6.payload_length =
431                 clib_host_to_net_u16 (len - rewrite_len);
432             }
433           else
434             {
435               oh0->ip4.length =
436                 clib_host_to_net_u16 (b0->current_length - rewrite_len);
437               oh0->ip4.checksum = ip4_header_checksum (&oh0->ip4);
438               if (sa0->udp_encap && ouh0)
439                 {
440                   ouh0->udp.length =
441                     clib_host_to_net_u16 (clib_net_to_host_u16
442                                           (ouh0->ip4.length) -
443                                           ip4_header_bytes (&ouh0->ip4));
444                 }
445             }
446
447           vnet_buffer (b0)->sw_if_index[VLIB_RX] =
448             vnet_buffer (b0)->sw_if_index[VLIB_RX];
449           b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
450
451           /* mbuf packet starts at ESP header */
452           mb0->data_len = vlib_buffer_get_tail (b0) - ((u8 *) esp0);
453           mb0->pkt_len = vlib_buffer_get_tail (b0) - ((u8 *) esp0);
454           mb0->data_off = ((void *) esp0) - mb0->buf_addr;
455
456           u32 cipher_off, cipher_len, auth_len = 0;
457           u32 *aad = NULL;
458
459           u8 *digest = vlib_buffer_get_tail (b0) - trunc_size;
460           u64 digest_paddr =
461             mb0->buf_physaddr + digest - ((u8 *) mb0->buf_addr);
462
463           if (!is_aead && cipher_alg->alg == RTE_CRYPTO_CIPHER_AES_CBC)
464             {
465               cipher_off = sizeof (esp_header_t);
466               cipher_len = iv_size + pad_payload_len;
467             }
468           else                  /* CTR/GCM */
469             {
470               u32 *esp_iv = (u32 *) (esp0 + 1);
471               esp_iv[0] = sa0->seq;
472               esp_iv[1] = sa0->seq_hi;
473
474               cipher_off = sizeof (esp_header_t) + iv_size;
475               cipher_len = pad_payload_len;
476             }
477
478           if (is_aead)
479             {
480               aad = (u32 *) priv->aad;
481               aad[0] = clib_host_to_net_u32 (sa0->spi);
482               aad[1] = clib_host_to_net_u32 (sa0->seq);
483
484               /* aad[3] should always be 0 */
485               if (PREDICT_FALSE (sa0->use_esn))
486                 aad[2] = clib_host_to_net_u32 (sa0->seq_hi);
487               else
488                 aad[2] = 0;
489             }
490           else
491             {
492               auth_len =
493                 vlib_buffer_get_tail (b0) - ((u8 *) esp0) - trunc_size;
494               if (sa0->use_esn)
495                 {
496                   u32 *_digest = (u32 *) digest;
497                   _digest[0] = clib_host_to_net_u32 (sa0->seq_hi);
498                   auth_len += 4;
499                 }
500             }
501
502           crypto_op_setup (is_aead, mb0, op, session, cipher_off, cipher_len,
503                            0, auth_len, (u8 *) aad, digest, digest_paddr);
504
505         trace:
506           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
507             {
508               esp_encrypt_trace_t *tr =
509                 vlib_add_trace (vm, node, b0, sizeof (*tr));
510               tr->crypto_alg = sa0->crypto_alg;
511               tr->integ_alg = sa0->integ_alg;
512               u8 *p = vlib_buffer_get_current (b0);
513               if (!sa0->is_tunnel)
514                 p += vnet_buffer (b0)->ip.save_rewrite_length;
515               clib_memcpy (tr->packet_data, p, sizeof (tr->packet_data));
516             }
517         }
518       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
519     }
520   vlib_node_increment_counter (vm, dpdk_esp_encrypt_node.index,
521                                ESP_ENCRYPT_ERROR_RX_PKTS,
522                                from_frame->n_vectors);
523
524   crypto_enqueue_ops (vm, cwm, 1, dpdk_esp_encrypt_node.index,
525                       ESP_ENCRYPT_ERROR_ENQ_FAIL, numa);
526
527   crypto_free_ops (numa, ops, cwm->ops + from_frame->n_vectors - ops);
528
529   return from_frame->n_vectors;
530 }
531
532 /* *INDENT-OFF* */
533 VLIB_REGISTER_NODE (dpdk_esp_encrypt_node) = {
534   .function = dpdk_esp_encrypt_node_fn,
535   .name = "dpdk-esp-encrypt",
536   .flags = VLIB_NODE_FLAG_IS_OUTPUT,
537   .vector_size = sizeof (u32),
538   .format_trace = format_esp_encrypt_trace,
539   .n_errors = ARRAY_LEN (esp_encrypt_error_strings),
540   .error_strings = esp_encrypt_error_strings,
541   .n_next_nodes = 1,
542   .next_nodes =
543     {
544       [ESP_ENCRYPT_NEXT_DROP] = "error-drop",
545     }
546 };
547 /* *INDENT-ON* */
548
549 VLIB_NODE_FUNCTION_MULTIARCH (dpdk_esp_encrypt_node, dpdk_esp_encrypt_node_fn)
550 /*
551  * fd.io coding-style-patch-verification: ON
552  *
553  * Local Variables:
554  * eval: (c-set-style "gnu")
555  * End:
556  */