ipsec: reorder packet process for ESP inbound
[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
21 /*
22  * setup crypto op and crypto sym op for ESP inbound packet.
23  */
24 static inline void
25 inb_cop_prepare(struct rte_crypto_op *cop,
26         const struct rte_ipsec_sa *sa, struct rte_mbuf *mb,
27         const union sym_op_data *icv, uint32_t pofs, uint32_t plen)
28 {
29         struct rte_crypto_sym_op *sop;
30         struct aead_gcm_iv *gcm;
31         struct aesctr_cnt_blk *ctr;
32         uint64_t *ivc, *ivp;
33         uint32_t algo;
34
35         algo = sa->algo_type;
36
37         /* fill sym op fields */
38         sop = cop->sym;
39
40         switch (algo) {
41         case ALGO_TYPE_AES_GCM:
42                 sop->aead.data.offset = pofs + sa->ctp.cipher.offset;
43                 sop->aead.data.length = plen - sa->ctp.cipher.length;
44                 sop->aead.digest.data = icv->va;
45                 sop->aead.digest.phys_addr = icv->pa;
46                 sop->aead.aad.data = icv->va + sa->icv_len;
47                 sop->aead.aad.phys_addr = icv->pa + sa->icv_len;
48
49                 /* fill AAD IV (located inside crypto op) */
50                 gcm = rte_crypto_op_ctod_offset(cop, struct aead_gcm_iv *,
51                         sa->iv_ofs);
52                 ivp = rte_pktmbuf_mtod_offset(mb, uint64_t *,
53                         pofs + sizeof(struct esp_hdr));
54                 aead_gcm_iv_fill(gcm, ivp[0], sa->salt);
55                 break;
56         case ALGO_TYPE_AES_CBC:
57         case ALGO_TYPE_3DES_CBC:
58                 sop->cipher.data.offset = pofs + sa->ctp.cipher.offset;
59                 sop->cipher.data.length = plen - sa->ctp.cipher.length;
60                 sop->auth.data.offset = pofs + sa->ctp.auth.offset;
61                 sop->auth.data.length = plen - sa->ctp.auth.length;
62                 sop->auth.digest.data = icv->va;
63                 sop->auth.digest.phys_addr = icv->pa;
64
65                 /* copy iv from the input packet to the cop */
66                 ivc = rte_crypto_op_ctod_offset(cop, uint64_t *, sa->iv_ofs);
67                 ivp = rte_pktmbuf_mtod_offset(mb, uint64_t *,
68                         pofs + sizeof(struct esp_hdr));
69                 copy_iv(ivc, ivp, sa->iv_len);
70                 break;
71         case ALGO_TYPE_AES_CTR:
72                 sop->cipher.data.offset = pofs + sa->ctp.cipher.offset;
73                 sop->cipher.data.length = plen - sa->ctp.cipher.length;
74                 sop->auth.data.offset = pofs + sa->ctp.auth.offset;
75                 sop->auth.data.length = plen - sa->ctp.auth.length;
76                 sop->auth.digest.data = icv->va;
77                 sop->auth.digest.phys_addr = icv->pa;
78
79                 /* copy iv from the input packet to the cop */
80                 ctr = rte_crypto_op_ctod_offset(cop, struct aesctr_cnt_blk *,
81                         sa->iv_ofs);
82                 ivp = rte_pktmbuf_mtod_offset(mb, uint64_t *,
83                         pofs + sizeof(struct esp_hdr));
84                 aes_ctr_cnt_blk_fill(ctr, ivp[0], sa->salt);
85                 break;
86         case ALGO_TYPE_NULL:
87                 sop->cipher.data.offset = pofs + sa->ctp.cipher.offset;
88                 sop->cipher.data.length = plen - sa->ctp.cipher.length;
89                 sop->auth.data.offset = pofs + sa->ctp.auth.offset;
90                 sop->auth.data.length = plen - sa->ctp.auth.length;
91                 sop->auth.digest.data = icv->va;
92                 sop->auth.digest.phys_addr = icv->pa;
93                 break;
94         }
95 }
96
97 /*
98  * for pure cryptodev (lookaside none) depending on SA settings,
99  * we might have to write some extra data to the packet.
100  */
101 static inline void
102 inb_pkt_xprepare(const struct rte_ipsec_sa *sa, rte_be64_t sqc,
103         const union sym_op_data *icv)
104 {
105         struct aead_gcm_aad *aad;
106
107         /* insert SQN.hi between ESP trailer and ICV */
108         if (sa->sqh_len != 0)
109                 insert_sqh(sqn_hi32(sqc), icv->va, sa->icv_len);
110
111         /*
112          * fill AAD fields, if any (aad fields are placed after icv),
113          * right now we support only one AEAD algorithm: AES-GCM.
114          */
115         if (sa->aad_len != 0) {
116                 aad = (struct aead_gcm_aad *)(icv->va + sa->icv_len);
117                 aead_gcm_aad_fill(aad, sa->spi, sqc, IS_ESN(sa));
118         }
119 }
120
121 /*
122  * setup/update packet data and metadata for ESP inbound tunnel case.
123  */
124 static inline int32_t
125 inb_pkt_prepare(const struct rte_ipsec_sa *sa, const struct replay_sqn *rsn,
126         struct rte_mbuf *mb, uint32_t hlen, union sym_op_data *icv)
127 {
128         int32_t rc;
129         uint64_t sqn;
130         uint32_t clen, icv_ofs, plen;
131         struct rte_mbuf *ml;
132         struct esp_hdr *esph;
133
134         esph = rte_pktmbuf_mtod_offset(mb, struct esp_hdr *, hlen);
135
136         /*
137          * retrieve and reconstruct SQN, then check it, then
138          * convert it back into network byte order.
139          */
140         sqn = rte_be_to_cpu_32(esph->seq);
141         if (IS_ESN(sa))
142                 sqn = reconstruct_esn(rsn->sqn, sqn, sa->replay.win_sz);
143
144         rc = esn_inb_check_sqn(rsn, sa, sqn);
145         if (rc != 0)
146                 return rc;
147
148         sqn = rte_cpu_to_be_64(sqn);
149
150         /* start packet manipulation */
151         plen = mb->pkt_len;
152         plen = plen - hlen;
153
154         ml = rte_pktmbuf_lastseg(mb);
155         icv_ofs = ml->data_len - sa->icv_len + sa->sqh_len;
156
157         /* check that packet has a valid length */
158         clen = plen - sa->ctp.cipher.length;
159         if ((int32_t)clen < 0 || (clen & (sa->pad_align - 1)) != 0)
160                 return -EBADMSG;
161
162         /* we have to allocate space for AAD somewhere,
163          * right now - just use free trailing space at the last segment.
164          * Would probably be more convenient to reserve space for AAD
165          * inside rte_crypto_op itself
166          * (again for IV space is already reserved inside cop).
167          */
168         if (sa->aad_len + sa->sqh_len > rte_pktmbuf_tailroom(ml))
169                 return -ENOSPC;
170
171         icv->va = rte_pktmbuf_mtod_offset(ml, void *, icv_ofs);
172         icv->pa = rte_pktmbuf_iova_offset(ml, icv_ofs);
173
174         inb_pkt_xprepare(sa, sqn, icv);
175         return plen;
176 }
177
178 /*
179  * setup/update packets and crypto ops for ESP inbound case.
180  */
181 uint16_t
182 esp_inb_pkt_prepare(const struct rte_ipsec_session *ss, struct rte_mbuf *mb[],
183         struct rte_crypto_op *cop[], uint16_t num)
184 {
185         int32_t rc;
186         uint32_t i, k, hl;
187         struct rte_ipsec_sa *sa;
188         struct rte_cryptodev_sym_session *cs;
189         struct replay_sqn *rsn;
190         union sym_op_data icv;
191         uint32_t dr[num];
192
193         sa = ss->sa;
194         cs = ss->crypto.ses;
195         rsn = rsn_acquire(sa);
196
197         k = 0;
198         for (i = 0; i != num; i++) {
199
200                 hl = mb[i]->l2_len + mb[i]->l3_len;
201                 rc = inb_pkt_prepare(sa, rsn, mb[i], hl, &icv);
202                 if (rc >= 0) {
203                         lksd_none_cop_prepare(cop[k], cs, mb[i]);
204                         inb_cop_prepare(cop[k], sa, mb[i], &icv, hl, rc);
205                         k++;
206                 } else
207                         dr[i - k] = i;
208         }
209
210         rsn_release(sa, rsn);
211
212         /* copy not prepared mbufs beyond good ones */
213         if (k != num && k != 0) {
214                 move_bad_mbufs(mb, dr, num, num - k);
215                 rte_errno = EBADMSG;
216         }
217
218         return k;
219 }
220
221 /*
222  * Start with processing inbound packet.
223  * This is common part for both tunnel and transport mode.
224  * Extract information that will be needed later from mbuf metadata and
225  * actual packet data:
226  * - mbuf for packet's last segment
227  * - length of the L2/L3 headers
228  * - esp tail structure
229  */
230 static inline void
231 process_step1(struct rte_mbuf *mb, uint32_t tlen, struct rte_mbuf **ml,
232         struct esp_tail *espt, uint32_t *hlen)
233 {
234         const struct esp_tail *pt;
235
236         ml[0] = rte_pktmbuf_lastseg(mb);
237         hlen[0] = mb->l2_len + mb->l3_len;
238         pt = rte_pktmbuf_mtod_offset(ml[0], const struct esp_tail *,
239                 ml[0]->data_len - tlen);
240         espt[0] = pt[0];
241 }
242
243 /*
244  * packet checks for transport mode:
245  * - no reported IPsec related failures in ol_flags
246  * - tail length is valid
247  * - padding bytes are valid
248  */
249 static inline int32_t
250 trs_process_check(const struct rte_mbuf *mb, const struct rte_mbuf *ml,
251         struct esp_tail espt, uint32_t hlen, uint32_t tlen)
252 {
253         const uint8_t *pd;
254         int32_t ofs;
255
256         ofs = ml->data_len - tlen;
257         pd = rte_pktmbuf_mtod_offset(ml, const uint8_t *, ofs);
258
259         return ((mb->ol_flags & PKT_RX_SEC_OFFLOAD_FAILED) != 0 ||
260                 ofs < 0 || tlen + hlen > mb->pkt_len ||
261                 (espt.pad_len != 0 && memcmp(pd, esp_pad_bytes, espt.pad_len)));
262 }
263
264 /*
265  * packet checks for tunnel mode:
266  * - same as for trasnport mode
267  * - esp tail next proto contains expected for that SA value
268  */
269 static inline int32_t
270 tun_process_check(const struct rte_mbuf *mb, struct rte_mbuf *ml,
271         struct esp_tail espt, uint32_t hlen, const uint32_t tlen, uint8_t proto)
272 {
273         return (trs_process_check(mb, ml, espt, hlen, tlen) ||
274                 espt.next_proto != proto);
275 }
276
277 /*
278  * step two for tunnel mode:
279  * - read SQN value (for future use)
280  * - cut of ICV, ESP tail and padding bytes
281  * - cut of ESP header and IV, also if needed - L2/L3 headers
282  *   (controlled by *adj* value)
283  */
284 static inline void *
285 tun_process_step2(struct rte_mbuf *mb, struct rte_mbuf *ml, uint32_t hlen,
286         uint32_t adj, uint32_t tlen, uint32_t *sqn)
287 {
288         const struct esp_hdr *ph;
289
290         /* read SQN value */
291         ph = rte_pktmbuf_mtod_offset(mb, const struct esp_hdr *, hlen);
292         sqn[0] = ph->seq;
293
294         /* cut of ICV, ESP tail and padding bytes */
295         ml->data_len -= tlen;
296         mb->pkt_len -= tlen;
297
298         /* cut of L2/L3 headers, ESP header and IV */
299         return rte_pktmbuf_adj(mb, adj);
300 }
301
302 /*
303  * step two for transport mode:
304  * - read SQN value (for future use)
305  * - cut of ICV, ESP tail and padding bytes
306  * - cut of ESP header and IV
307  * - move L2/L3 header to fill the gap after ESP header removal
308  */
309 static inline void *
310 trs_process_step2(struct rte_mbuf *mb, struct rte_mbuf *ml, uint32_t hlen,
311         uint32_t adj, uint32_t tlen, uint32_t *sqn)
312 {
313         char *np, *op;
314
315         /* get start of the packet before modifications */
316         op = rte_pktmbuf_mtod(mb, char *);
317
318         /* cut off ESP header and IV */
319         np = tun_process_step2(mb, ml, hlen, adj, tlen, sqn);
320
321         /* move header bytes to fill the gap after ESP header removal */
322         remove_esph(np, op, hlen);
323         return np;
324 }
325
326 /*
327  * step three for transport mode:
328  * update mbuf metadata:
329  * - packet_type
330  * - ol_flags
331  */
332 static inline void
333 trs_process_step3(struct rte_mbuf *mb)
334 {
335         /* reset mbuf packet type */
336         mb->packet_type &= (RTE_PTYPE_L2_MASK | RTE_PTYPE_L3_MASK);
337
338         /* clear the PKT_RX_SEC_OFFLOAD flag if set */
339         mb->ol_flags &= ~PKT_RX_SEC_OFFLOAD;
340 }
341
342 /*
343  * step three for tunnel mode:
344  * update mbuf metadata:
345  * - packet_type
346  * - ol_flags
347  * - tx_offload
348  */
349 static inline void
350 tun_process_step3(struct rte_mbuf *mb, uint64_t txof_msk, uint64_t txof_val)
351 {
352         /* reset mbuf metatdata: L2/L3 len, packet type */
353         mb->packet_type = RTE_PTYPE_UNKNOWN;
354         mb->tx_offload = (mb->tx_offload & txof_msk) | txof_val;
355
356         /* clear the PKT_RX_SEC_OFFLOAD flag if set */
357         mb->ol_flags &= ~PKT_RX_SEC_OFFLOAD;
358 }
359
360
361 /*
362  * *process* function for tunnel packets
363  */
364 static inline uint16_t
365 tun_process(const struct rte_ipsec_sa *sa, struct rte_mbuf *mb[],
366         uint32_t sqn[], uint32_t dr[], uint16_t num)
367 {
368         uint32_t adj, i, k, tl;
369         uint32_t hl[num];
370         struct esp_tail espt[num];
371         struct rte_mbuf *ml[num];
372
373         const uint32_t tlen = sa->icv_len + sizeof(espt[0]);
374         const uint32_t cofs = sa->ctp.cipher.offset;
375
376         /*
377          * to minimize stalls due to load latency,
378          * read mbufs metadata and esp tail first.
379          */
380         for (i = 0; i != num; i++)
381                 process_step1(mb[i], tlen, &ml[i], &espt[i], &hl[i]);
382
383         k = 0;
384         for (i = 0; i != num; i++) {
385
386                 adj = hl[i] + cofs;
387                 tl = tlen + espt[i].pad_len;
388
389                 /* check that packet is valid */
390                 if (tun_process_check(mb[i], ml[i], espt[i], adj, tl,
391                                         sa->proto) == 0) {
392
393                         /* modify packet's layout */
394                         tun_process_step2(mb[i], ml[i], hl[i], adj,
395                                 tl, sqn + k);
396                         /* update mbuf's metadata */
397                         tun_process_step3(mb[i], sa->tx_offload.msk,
398                                 sa->tx_offload.val);
399                         k++;
400                 } else
401                         dr[i - k] = i;
402         }
403
404         return k;
405 }
406
407
408 /*
409  * *process* function for tunnel packets
410  */
411 static inline uint16_t
412 trs_process(const struct rte_ipsec_sa *sa, struct rte_mbuf *mb[],
413         uint32_t sqn[], uint32_t dr[], uint16_t num)
414 {
415         char *np;
416         uint32_t i, k, l2, tl;
417         uint32_t hl[num];
418         struct esp_tail espt[num];
419         struct rte_mbuf *ml[num];
420
421         const uint32_t tlen = sa->icv_len + sizeof(espt[0]);
422         const uint32_t cofs = sa->ctp.cipher.offset;
423
424         /*
425          * to minimize stalls due to load latency,
426          * read mbufs metadata and esp tail first.
427          */
428         for (i = 0; i != num; i++)
429                 process_step1(mb[i], tlen, &ml[i], &espt[i], &hl[i]);
430
431         k = 0;
432         for (i = 0; i != num; i++) {
433
434                 tl = tlen + espt[i].pad_len;
435                 l2 = mb[i]->l2_len;
436
437                 /* check that packet is valid */
438                 if (trs_process_check(mb[i], ml[i], espt[i], hl[i] + cofs,
439                                 tl) == 0) {
440
441                         /* modify packet's layout */
442                         np = trs_process_step2(mb[i], ml[i], hl[i], cofs, tl,
443                                 sqn + k);
444                         update_trs_l3hdr(sa, np + l2, mb[i]->pkt_len,
445                                 l2, hl[i] - l2, espt[i].next_proto);
446
447                         /* update mbuf's metadata */
448                         trs_process_step3(mb[i]);
449                         k++;
450                 } else
451                         dr[i - k] = i;
452         }
453
454         return k;
455 }
456
457 /*
458  * for group of ESP inbound packets perform SQN check and update.
459  */
460 static inline uint16_t
461 esp_inb_rsn_update(struct rte_ipsec_sa *sa, const uint32_t sqn[],
462         uint32_t dr[], uint16_t num)
463 {
464         uint32_t i, k;
465         struct replay_sqn *rsn;
466
467         /* replay not enabled */
468         if (sa->replay.win_sz == 0)
469                 return num;
470
471         rsn = rsn_update_start(sa);
472
473         k = 0;
474         for (i = 0; i != num; i++) {
475                 if (esn_inb_update_sqn(rsn, sa, rte_be_to_cpu_32(sqn[i])) == 0)
476                         k++;
477                 else
478                         dr[i - k] = i;
479         }
480
481         rsn_update_finish(sa, rsn);
482         return k;
483 }
484
485 /*
486  * process group of ESP inbound packets.
487  */
488 static inline uint16_t
489 esp_inb_pkt_process(const struct rte_ipsec_session *ss,
490         struct rte_mbuf *mb[], uint16_t num, esp_inb_process_t process)
491 {
492         uint32_t k, n;
493         struct rte_ipsec_sa *sa;
494         uint32_t sqn[num];
495         uint32_t dr[num];
496
497         sa = ss->sa;
498
499         /* process packets, extract seq numbers */
500         k = process(sa, mb, sqn, dr, num);
501
502         /* handle unprocessed mbufs */
503         if (k != num && k != 0)
504                 move_bad_mbufs(mb, dr, num, num - k);
505
506         /* update SQN and replay winow */
507         n = esp_inb_rsn_update(sa, sqn, dr, k);
508
509         /* handle mbufs with wrong SQN */
510         if (n != k && n != 0)
511                 move_bad_mbufs(mb, dr, k, k - n);
512
513         if (n != num)
514                 rte_errno = EBADMSG;
515
516         return n;
517 }
518
519 /*
520  * process group of ESP inbound tunnel packets.
521  */
522 uint16_t
523 esp_inb_tun_pkt_process(const struct rte_ipsec_session *ss,
524         struct rte_mbuf *mb[], uint16_t num)
525 {
526         return esp_inb_pkt_process(ss, mb, num, tun_process);
527 }
528
529 /*
530  * process group of ESP inbound transport packets.
531  */
532 uint16_t
533 esp_inb_trs_pkt_process(const struct rte_ipsec_session *ss,
534         struct rte_mbuf *mb[], uint16_t num)
535 {
536         return esp_inb_pkt_process(ss, mb, num, trs_process);
537 }