8e3ecbc6412ec44a4fd53a255c50da310a9ed47d
[dpdk.git] / lib / librte_ipsec / esp_inb.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <rte_ipsec.h>
6 #include <rte_esp.h>
7 #include <rte_ip.h>
8 #include <rte_errno.h>
9 #include <rte_cryptodev.h>
10
11 #include "sa.h"
12 #include "ipsec_sqn.h"
13 #include "crypto.h"
14 #include "iph.h"
15 #include "misc.h"
16 #include "pad.h"
17
18 typedef uint16_t (*esp_inb_process_t)(const struct rte_ipsec_sa *sa,
19         struct rte_mbuf *mb[], uint32_t sqn[], uint32_t dr[], uint16_t num,
20         uint8_t sqh_len);
21
22 /*
23  * helper function to fill crypto_sym op for cipher+auth algorithms.
24  * used by inb_cop_prepare(), see below.
25  */
26 static inline void
27 sop_ciph_auth_prepare(struct rte_crypto_sym_op *sop,
28         const struct rte_ipsec_sa *sa, const union sym_op_data *icv,
29         uint32_t pofs, uint32_t plen)
30 {
31         sop->cipher.data.offset = pofs + sa->ctp.cipher.offset;
32         sop->cipher.data.length = plen - sa->ctp.cipher.length;
33         sop->auth.data.offset = pofs + sa->ctp.auth.offset;
34         sop->auth.data.length = plen - sa->ctp.auth.length;
35         sop->auth.digest.data = icv->va;
36         sop->auth.digest.phys_addr = icv->pa;
37 }
38
39 /*
40  * helper function to fill crypto_sym op for aead algorithms
41  * used by inb_cop_prepare(), see below.
42  */
43 static inline void
44 sop_aead_prepare(struct rte_crypto_sym_op *sop,
45         const struct rte_ipsec_sa *sa, const union sym_op_data *icv,
46         uint32_t pofs, uint32_t plen)
47 {
48         sop->aead.data.offset = pofs + sa->ctp.cipher.offset;
49         sop->aead.data.length = plen - sa->ctp.cipher.length;
50         sop->aead.digest.data = icv->va;
51         sop->aead.digest.phys_addr = icv->pa;
52         sop->aead.aad.data = icv->va + sa->icv_len;
53         sop->aead.aad.phys_addr = icv->pa + sa->icv_len;
54 }
55
56 /*
57  * setup crypto op and crypto sym op for ESP inbound packet.
58  */
59 static inline void
60 inb_cop_prepare(struct rte_crypto_op *cop,
61         const struct rte_ipsec_sa *sa, struct rte_mbuf *mb,
62         const union sym_op_data *icv, uint32_t pofs, uint32_t plen)
63 {
64         struct rte_crypto_sym_op *sop;
65         struct aead_gcm_iv *gcm;
66         struct aesctr_cnt_blk *ctr;
67         uint64_t *ivc, *ivp;
68         uint32_t algo;
69
70         algo = sa->algo_type;
71         ivp = rte_pktmbuf_mtod_offset(mb, uint64_t *,
72                 pofs + sizeof(struct rte_esp_hdr));
73
74         /* fill sym op fields */
75         sop = cop->sym;
76
77         switch (algo) {
78         case ALGO_TYPE_AES_GCM:
79                 sop_aead_prepare(sop, sa, icv, pofs, plen);
80
81                 /* fill AAD IV (located inside crypto op) */
82                 gcm = rte_crypto_op_ctod_offset(cop, struct aead_gcm_iv *,
83                         sa->iv_ofs);
84                 aead_gcm_iv_fill(gcm, ivp[0], sa->salt);
85                 break;
86         case ALGO_TYPE_AES_CBC:
87         case ALGO_TYPE_3DES_CBC:
88                 sop_ciph_auth_prepare(sop, sa, icv, pofs, plen);
89
90                 /* copy iv from the input packet to the cop */
91                 ivc = rte_crypto_op_ctod_offset(cop, uint64_t *, sa->iv_ofs);
92                 copy_iv(ivc, ivp, sa->iv_len);
93                 break;
94         case ALGO_TYPE_AES_CTR:
95                 sop_ciph_auth_prepare(sop, sa, icv, pofs, plen);
96
97                 /* fill CTR block (located inside crypto op) */
98                 ctr = rte_crypto_op_ctod_offset(cop, struct aesctr_cnt_blk *,
99                         sa->iv_ofs);
100                 aes_ctr_cnt_blk_fill(ctr, ivp[0], sa->salt);
101                 break;
102         case ALGO_TYPE_NULL:
103                 sop_ciph_auth_prepare(sop, sa, icv, pofs, plen);
104                 break;
105         }
106 }
107
108 /*
109  * Helper function for prepare() to deal with situation when
110  * ICV is spread by two segments. Tries to move ICV completely into the
111  * last segment.
112  */
113 static struct rte_mbuf *
114 move_icv(struct rte_mbuf *ml, uint32_t ofs)
115 {
116         uint32_t n;
117         struct rte_mbuf *ms;
118         const void *prev;
119         void *new;
120
121         ms = ml->next;
122         n = ml->data_len - ofs;
123
124         prev = rte_pktmbuf_mtod_offset(ml, const void *, ofs);
125         new = rte_pktmbuf_prepend(ms, n);
126         if (new == NULL)
127                 return NULL;
128
129         /* move n ICV bytes from ml into ms */
130         rte_memcpy(new, prev, n);
131         ml->data_len -= n;
132
133         return ms;
134 }
135
136 /*
137  * for pure cryptodev (lookaside none) depending on SA settings,
138  * we might have to write some extra data to the packet.
139  */
140 static inline void
141 inb_pkt_xprepare(const struct rte_ipsec_sa *sa, rte_be64_t sqc,
142         const union sym_op_data *icv)
143 {
144         struct aead_gcm_aad *aad;
145
146         /* insert SQN.hi between ESP trailer and ICV */
147         if (sa->sqh_len != 0)
148                 insert_sqh(sqn_hi32(sqc), icv->va, sa->icv_len);
149
150         /*
151          * fill AAD fields, if any (aad fields are placed after icv),
152          * right now we support only one AEAD algorithm: AES-GCM.
153          */
154         if (sa->aad_len != 0) {
155                 aad = (struct aead_gcm_aad *)(icv->va + sa->icv_len);
156                 aead_gcm_aad_fill(aad, sa->spi, sqc, IS_ESN(sa));
157         }
158 }
159
160 /*
161  * setup/update packet data and metadata for ESP inbound tunnel case.
162  */
163 static inline int32_t
164 inb_pkt_prepare(const struct rte_ipsec_sa *sa, const struct replay_sqn *rsn,
165         struct rte_mbuf *mb, uint32_t hlen, union sym_op_data *icv)
166 {
167         int32_t rc;
168         uint64_t sqn;
169         uint32_t clen, icv_len, icv_ofs, plen;
170         struct rte_mbuf *ml;
171         struct rte_esp_hdr *esph;
172
173         esph = rte_pktmbuf_mtod_offset(mb, struct rte_esp_hdr *, hlen);
174
175         /*
176          * retrieve and reconstruct SQN, then check it, then
177          * convert it back into network byte order.
178          */
179         sqn = rte_be_to_cpu_32(esph->seq);
180         if (IS_ESN(sa))
181                 sqn = reconstruct_esn(rsn->sqn, sqn, sa->replay.win_sz);
182
183         rc = esn_inb_check_sqn(rsn, sa, sqn);
184         if (rc != 0)
185                 return rc;
186
187         sqn = rte_cpu_to_be_64(sqn);
188
189         /* start packet manipulation */
190         plen = mb->pkt_len;
191         plen = plen - hlen;
192
193         /* check that packet has a valid length */
194         clen = plen - sa->ctp.cipher.length;
195         if ((int32_t)clen < 0 || (clen & (sa->pad_align - 1)) != 0)
196                 return -EBADMSG;
197
198         /* find ICV location */
199         icv_len = sa->icv_len;
200         icv_ofs = mb->pkt_len - icv_len;
201
202         ml = mbuf_get_seg_ofs(mb, &icv_ofs);
203
204         /*
205          * if ICV is spread by two segments, then try to
206          * move ICV completely into the last segment.
207          */
208         if (ml->data_len < icv_ofs + icv_len) {
209
210                 ml = move_icv(ml, icv_ofs);
211                 if (ml == NULL)
212                         return -ENOSPC;
213
214                 /* new ICV location */
215                 icv_ofs = 0;
216         }
217
218         icv_ofs += sa->sqh_len;
219
220         /* we have to allocate space for AAD somewhere,
221          * right now - just use free trailing space at the last segment.
222          * Would probably be more convenient to reserve space for AAD
223          * inside rte_crypto_op itself
224          * (again for IV space is already reserved inside cop).
225          */
226         if (sa->aad_len + sa->sqh_len > rte_pktmbuf_tailroom(ml))
227                 return -ENOSPC;
228
229         icv->va = rte_pktmbuf_mtod_offset(ml, void *, icv_ofs);
230         icv->pa = rte_pktmbuf_iova_offset(ml, icv_ofs);
231
232         /*
233          * if esn is used then high-order 32 bits are also used in ICV
234          * calculation but are not transmitted, update packet length
235          * to be consistent with auth data length and offset, this will
236          * be subtracted from packet length in post crypto processing
237          */
238         mb->pkt_len += sa->sqh_len;
239         ml->data_len += sa->sqh_len;
240
241         inb_pkt_xprepare(sa, sqn, icv);
242         return plen;
243 }
244
245 /*
246  * setup/update packets and crypto ops for ESP inbound case.
247  */
248 uint16_t
249 esp_inb_pkt_prepare(const struct rte_ipsec_session *ss, struct rte_mbuf *mb[],
250         struct rte_crypto_op *cop[], uint16_t num)
251 {
252         int32_t rc;
253         uint32_t i, k, hl;
254         struct rte_ipsec_sa *sa;
255         struct rte_cryptodev_sym_session *cs;
256         struct replay_sqn *rsn;
257         union sym_op_data icv;
258         uint32_t dr[num];
259
260         sa = ss->sa;
261         cs = ss->crypto.ses;
262         rsn = rsn_acquire(sa);
263
264         k = 0;
265         for (i = 0; i != num; i++) {
266
267                 hl = mb[i]->l2_len + mb[i]->l3_len;
268                 rc = inb_pkt_prepare(sa, rsn, mb[i], hl, &icv);
269                 if (rc >= 0) {
270                         lksd_none_cop_prepare(cop[k], cs, mb[i]);
271                         inb_cop_prepare(cop[k], sa, mb[i], &icv, hl, rc);
272                         k++;
273                 } else
274                         dr[i - k] = i;
275         }
276
277         rsn_release(sa, rsn);
278
279         /* copy not prepared mbufs beyond good ones */
280         if (k != num && k != 0) {
281                 move_bad_mbufs(mb, dr, num, num - k);
282                 rte_errno = EBADMSG;
283         }
284
285         return k;
286 }
287
288 /*
289  * Start with processing inbound packet.
290  * This is common part for both tunnel and transport mode.
291  * Extract information that will be needed later from mbuf metadata and
292  * actual packet data:
293  * - mbuf for packet's last segment
294  * - length of the L2/L3 headers
295  * - esp tail structure
296  */
297 static inline void
298 process_step1(struct rte_mbuf *mb, uint32_t tlen, struct rte_mbuf **ml,
299         struct esp_tail *espt, uint32_t *hlen, uint32_t *tofs)
300 {
301         const struct esp_tail *pt;
302         uint32_t ofs;
303
304         ofs = mb->pkt_len - tlen;
305         hlen[0] = mb->l2_len + mb->l3_len;
306         ml[0] = mbuf_get_seg_ofs(mb, &ofs);
307         pt = rte_pktmbuf_mtod_offset(ml[0], const struct esp_tail *, ofs);
308         tofs[0] = ofs;
309         espt[0] = pt[0];
310 }
311
312 /*
313  * Helper function to check pad bytes values.
314  * Note that pad bytes can be spread across multiple segments.
315  */
316 static inline int
317 check_pad_bytes(struct rte_mbuf *mb, uint32_t ofs, uint32_t len)
318 {
319         const uint8_t *pd;
320         uint32_t k, n;
321
322         for (n = 0; n != len; n += k, mb = mb->next) {
323                 k = mb->data_len - ofs;
324                 k = RTE_MIN(k, len - n);
325                 pd = rte_pktmbuf_mtod_offset(mb, const uint8_t *, ofs);
326                 if (memcmp(pd, esp_pad_bytes + n, k) != 0)
327                         break;
328                 ofs = 0;
329         }
330
331         return len - n;
332 }
333
334 /*
335  * packet checks for transport mode:
336  * - no reported IPsec related failures in ol_flags
337  * - tail and header lengths are valid
338  * - padding bytes are valid
339  * apart from checks, function also updates tail offset (and segment)
340  * by taking into account pad length.
341  */
342 static inline int32_t
343 trs_process_check(struct rte_mbuf *mb, struct rte_mbuf **ml,
344         uint32_t *tofs, struct esp_tail espt, uint32_t hlen, uint32_t tlen)
345 {
346         if ((mb->ol_flags & PKT_RX_SEC_OFFLOAD_FAILED) != 0 ||
347                         tlen + hlen > mb->pkt_len)
348                 return -EBADMSG;
349
350         /* padding bytes are spread over multiple segments */
351         if (tofs[0] < espt.pad_len) {
352                 tofs[0] = mb->pkt_len - tlen;
353                 ml[0] = mbuf_get_seg_ofs(mb, tofs);
354         } else
355                 tofs[0] -= espt.pad_len;
356
357         return check_pad_bytes(ml[0], tofs[0], espt.pad_len);
358 }
359
360 /*
361  * packet checks for tunnel mode:
362  * - same as for trasnport mode
363  * - esp tail next proto contains expected for that SA value
364  */
365 static inline int32_t
366 tun_process_check(struct rte_mbuf *mb, struct rte_mbuf **ml,
367         uint32_t *tofs, struct esp_tail espt, uint32_t hlen, uint32_t tlen,
368         uint8_t proto)
369 {
370         return (trs_process_check(mb, ml, tofs, espt, hlen, tlen) ||
371                 espt.next_proto != proto);
372 }
373
374 /*
375  * step two for tunnel mode:
376  * - read SQN value (for future use)
377  * - cut of ICV, ESP tail and padding bytes
378  * - cut of ESP header and IV, also if needed - L2/L3 headers
379  *   (controlled by *adj* value)
380  */
381 static inline void *
382 tun_process_step2(struct rte_mbuf *mb, struct rte_mbuf *ml, uint32_t hlen,
383         uint32_t adj, uint32_t tofs, uint32_t tlen, uint32_t *sqn)
384 {
385         const struct rte_esp_hdr *ph;
386
387         /* read SQN value */
388         ph = rte_pktmbuf_mtod_offset(mb, const struct rte_esp_hdr *, hlen);
389         sqn[0] = ph->seq;
390
391         /* cut of ICV, ESP tail and padding bytes */
392         mbuf_cut_seg_ofs(mb, ml, tofs, tlen);
393
394         /* cut of L2/L3 headers, ESP header and IV */
395         return rte_pktmbuf_adj(mb, adj);
396 }
397
398 /*
399  * step two for transport mode:
400  * - read SQN value (for future use)
401  * - cut of ICV, ESP tail and padding bytes
402  * - cut of ESP header and IV
403  * - move L2/L3 header to fill the gap after ESP header removal
404  */
405 static inline void *
406 trs_process_step2(struct rte_mbuf *mb, struct rte_mbuf *ml, uint32_t hlen,
407         uint32_t adj, uint32_t tofs, uint32_t tlen, uint32_t *sqn)
408 {
409         char *np, *op;
410
411         /* get start of the packet before modifications */
412         op = rte_pktmbuf_mtod(mb, char *);
413
414         /* cut off ESP header and IV */
415         np = tun_process_step2(mb, ml, hlen, adj, tofs, tlen, sqn);
416
417         /* move header bytes to fill the gap after ESP header removal */
418         remove_esph(np, op, hlen);
419         return np;
420 }
421
422 /*
423  * step three for transport mode:
424  * update mbuf metadata:
425  * - packet_type
426  * - ol_flags
427  */
428 static inline void
429 trs_process_step3(struct rte_mbuf *mb)
430 {
431         /* reset mbuf packet type */
432         mb->packet_type &= (RTE_PTYPE_L2_MASK | RTE_PTYPE_L3_MASK);
433
434         /* clear the PKT_RX_SEC_OFFLOAD flag if set */
435         mb->ol_flags &= ~PKT_RX_SEC_OFFLOAD;
436 }
437
438 /*
439  * step three for tunnel mode:
440  * update mbuf metadata:
441  * - packet_type
442  * - ol_flags
443  * - tx_offload
444  */
445 static inline void
446 tun_process_step3(struct rte_mbuf *mb, uint64_t txof_msk, uint64_t txof_val)
447 {
448         /* reset mbuf metatdata: L2/L3 len, packet type */
449         mb->packet_type = RTE_PTYPE_UNKNOWN;
450         mb->tx_offload = (mb->tx_offload & txof_msk) | txof_val;
451
452         /* clear the PKT_RX_SEC_OFFLOAD flag if set */
453         mb->ol_flags &= ~PKT_RX_SEC_OFFLOAD;
454 }
455
456 /*
457  * *process* function for tunnel packets
458  */
459 static inline uint16_t
460 tun_process(const struct rte_ipsec_sa *sa, struct rte_mbuf *mb[],
461             uint32_t sqn[], uint32_t dr[], uint16_t num, uint8_t sqh_len)
462 {
463         uint32_t adj, i, k, tl;
464         uint32_t hl[num], to[num];
465         struct esp_tail espt[num];
466         struct rte_mbuf *ml[num];
467         const void *outh;
468         void *inh;
469
470         /*
471          * remove icv, esp trailer and high-order
472          * 32 bits of esn from packet length
473          */
474         const uint32_t tlen = sa->icv_len + sizeof(espt[0]) + sqh_len;
475         const uint32_t cofs = sa->ctp.cipher.offset;
476
477         /*
478          * to minimize stalls due to load latency,
479          * read mbufs metadata and esp tail first.
480          */
481         for (i = 0; i != num; i++)
482                 process_step1(mb[i], tlen, &ml[i], &espt[i], &hl[i], &to[i]);
483
484         k = 0;
485         for (i = 0; i != num; i++) {
486
487                 adj = hl[i] + cofs;
488                 tl = tlen + espt[i].pad_len;
489
490                 /* check that packet is valid */
491                 if (tun_process_check(mb[i], &ml[i], &to[i], espt[i], adj, tl,
492                                         sa->proto) == 0) {
493
494                         outh = rte_pktmbuf_mtod_offset(mb[i], uint8_t *,
495                                         mb[i]->l2_len);
496
497                         /* modify packet's layout */
498                         inh = tun_process_step2(mb[i], ml[i], hl[i], adj,
499                                         to[i], tl, sqn + k);
500
501                         /* update inner ip header */
502                         update_tun_inb_l3hdr(sa, outh, inh);
503
504                         /* update mbuf's metadata */
505                         tun_process_step3(mb[i], sa->tx_offload.msk,
506                                 sa->tx_offload.val);
507                         k++;
508                 } else
509                         dr[i - k] = i;
510         }
511
512         return k;
513 }
514
515
516 /*
517  * *process* function for tunnel packets
518  */
519 static inline uint16_t
520 trs_process(const struct rte_ipsec_sa *sa, struct rte_mbuf *mb[],
521         uint32_t sqn[], uint32_t dr[], uint16_t num, uint8_t sqh_len)
522 {
523         char *np;
524         uint32_t i, k, l2, tl;
525         uint32_t hl[num], to[num];
526         struct esp_tail espt[num];
527         struct rte_mbuf *ml[num];
528
529         /*
530          * remove icv, esp trailer and high-order
531          * 32 bits of esn from packet length
532          */
533         const uint32_t tlen = sa->icv_len + sizeof(espt[0]) + sqh_len;
534         const uint32_t cofs = sa->ctp.cipher.offset;
535
536         /*
537          * to minimize stalls due to load latency,
538          * read mbufs metadata and esp tail first.
539          */
540         for (i = 0; i != num; i++)
541                 process_step1(mb[i], tlen, &ml[i], &espt[i], &hl[i], &to[i]);
542
543         k = 0;
544         for (i = 0; i != num; i++) {
545
546                 tl = tlen + espt[i].pad_len;
547                 l2 = mb[i]->l2_len;
548
549                 /* check that packet is valid */
550                 if (trs_process_check(mb[i], &ml[i], &to[i], espt[i],
551                                 hl[i] + cofs, tl) == 0) {
552
553                         /* modify packet's layout */
554                         np = trs_process_step2(mb[i], ml[i], hl[i], cofs,
555                                 to[i], tl, sqn + k);
556                         update_trs_l3hdr(sa, np + l2, mb[i]->pkt_len,
557                                 l2, hl[i] - l2, espt[i].next_proto);
558
559                         /* update mbuf's metadata */
560                         trs_process_step3(mb[i]);
561                         k++;
562                 } else
563                         dr[i - k] = i;
564         }
565
566         return k;
567 }
568
569 /*
570  * for group of ESP inbound packets perform SQN check and update.
571  */
572 static inline uint16_t
573 esp_inb_rsn_update(struct rte_ipsec_sa *sa, const uint32_t sqn[],
574         uint32_t dr[], uint16_t num)
575 {
576         uint32_t i, k;
577         struct replay_sqn *rsn;
578
579         /* replay not enabled */
580         if (sa->replay.win_sz == 0)
581                 return num;
582
583         rsn = rsn_update_start(sa);
584
585         k = 0;
586         for (i = 0; i != num; i++) {
587                 if (esn_inb_update_sqn(rsn, sa, rte_be_to_cpu_32(sqn[i])) == 0)
588                         k++;
589                 else
590                         dr[i - k] = i;
591         }
592
593         rsn_update_finish(sa, rsn);
594         return k;
595 }
596
597 /*
598  * process group of ESP inbound packets.
599  */
600 static inline uint16_t
601 esp_inb_pkt_process(struct rte_ipsec_sa *sa, struct rte_mbuf *mb[],
602         uint16_t num, uint8_t sqh_len, esp_inb_process_t process)
603 {
604         uint32_t k, n;
605         uint32_t sqn[num];
606         uint32_t dr[num];
607
608         /* process packets, extract seq numbers */
609         k = process(sa, mb, sqn, dr, num, sqh_len);
610
611         /* handle unprocessed mbufs */
612         if (k != num && k != 0)
613                 move_bad_mbufs(mb, dr, num, num - k);
614
615         /* update SQN and replay winow */
616         n = esp_inb_rsn_update(sa, sqn, dr, k);
617
618         /* handle mbufs with wrong SQN */
619         if (n != k && n != 0)
620                 move_bad_mbufs(mb, dr, k, k - n);
621
622         if (n != num)
623                 rte_errno = EBADMSG;
624
625         return n;
626 }
627
628 /*
629  * process group of ESP inbound tunnel packets.
630  */
631 uint16_t
632 esp_inb_tun_pkt_process(const struct rte_ipsec_session *ss,
633         struct rte_mbuf *mb[], uint16_t num)
634 {
635         struct rte_ipsec_sa *sa = ss->sa;
636
637         return esp_inb_pkt_process(sa, mb, num, sa->sqh_len, tun_process);
638 }
639
640 uint16_t
641 inline_inb_tun_pkt_process(const struct rte_ipsec_session *ss,
642         struct rte_mbuf *mb[], uint16_t num)
643 {
644         return esp_inb_pkt_process(ss->sa, mb, num, 0, tun_process);
645 }
646
647 /*
648  * process group of ESP inbound transport packets.
649  */
650 uint16_t
651 esp_inb_trs_pkt_process(const struct rte_ipsec_session *ss,
652         struct rte_mbuf *mb[], uint16_t num)
653 {
654         struct rte_ipsec_sa *sa = ss->sa;
655
656         return esp_inb_pkt_process(sa, mb, num, sa->sqh_len, trs_process);
657 }
658
659 uint16_t
660 inline_inb_trs_pkt_process(const struct rte_ipsec_session *ss,
661         struct rte_mbuf *mb[], uint16_t num)
662 {
663         return esp_inb_pkt_process(ss->sa, mb, num, 0, trs_process);
664 }