11cb0ef41Sopenharmony_ci/*
21cb0ef41Sopenharmony_ci * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
31cb0ef41Sopenharmony_ci *
41cb0ef41Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
51cb0ef41Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
61cb0ef41Sopenharmony_ci * in the file LICENSE in the source distribution or at
71cb0ef41Sopenharmony_ci * https://www.openssl.org/source/license.html
81cb0ef41Sopenharmony_ci */
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#if defined(OPENSSL_SYS_LINUX)
111cb0ef41Sopenharmony_ci# ifndef OPENSSL_NO_KTLS
121cb0ef41Sopenharmony_ci#  include <linux/version.h>
131cb0ef41Sopenharmony_ci#  if LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0)
141cb0ef41Sopenharmony_ci#   define OPENSSL_NO_KTLS
151cb0ef41Sopenharmony_ci#   ifndef PEDANTIC
161cb0ef41Sopenharmony_ci#    warning "KTLS requires Kernel Headers >= 4.13.0"
171cb0ef41Sopenharmony_ci#    warning "Skipping Compilation of KTLS"
181cb0ef41Sopenharmony_ci#   endif
191cb0ef41Sopenharmony_ci#  endif
201cb0ef41Sopenharmony_ci# endif
211cb0ef41Sopenharmony_ci#endif
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci#ifndef HEADER_INTERNAL_KTLS
241cb0ef41Sopenharmony_ci# define HEADER_INTERNAL_KTLS
251cb0ef41Sopenharmony_ci# pragma once
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci# ifndef OPENSSL_NO_KTLS
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci#  if defined(__FreeBSD__)
301cb0ef41Sopenharmony_ci#   include <sys/types.h>
311cb0ef41Sopenharmony_ci#   include <sys/socket.h>
321cb0ef41Sopenharmony_ci#   include <sys/ktls.h>
331cb0ef41Sopenharmony_ci#   include <netinet/in.h>
341cb0ef41Sopenharmony_ci#   include <netinet/tcp.h>
351cb0ef41Sopenharmony_ci#   include <openssl/ssl3.h>
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci#   ifndef TCP_RXTLS_ENABLE
381cb0ef41Sopenharmony_ci#    define OPENSSL_NO_KTLS_RX
391cb0ef41Sopenharmony_ci#   endif
401cb0ef41Sopenharmony_ci#   define OPENSSL_KTLS_AES_GCM_128
411cb0ef41Sopenharmony_ci#   define OPENSSL_KTLS_AES_GCM_256
421cb0ef41Sopenharmony_ci#   define OPENSSL_KTLS_TLS13
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_citypedef struct tls_enable ktls_crypto_info_t;
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci/*
471cb0ef41Sopenharmony_ci * FreeBSD does not require any additional steps to enable KTLS before
481cb0ef41Sopenharmony_ci * setting keys.
491cb0ef41Sopenharmony_ci */
501cb0ef41Sopenharmony_cistatic ossl_inline int ktls_enable(int fd)
511cb0ef41Sopenharmony_ci{
521cb0ef41Sopenharmony_ci    return 1;
531cb0ef41Sopenharmony_ci}
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci/*
561cb0ef41Sopenharmony_ci * The TCP_TXTLS_ENABLE socket option marks the outgoing socket buffer
571cb0ef41Sopenharmony_ci * as using TLS.  If successful, then data sent using this socket will
581cb0ef41Sopenharmony_ci * be encrypted and encapsulated in TLS records using the tls_en
591cb0ef41Sopenharmony_ci * provided here.
601cb0ef41Sopenharmony_ci *
611cb0ef41Sopenharmony_ci * The TCP_RXTLS_ENABLE socket option marks the incoming socket buffer
621cb0ef41Sopenharmony_ci * as using TLS.  If successful, then data received for this socket will
631cb0ef41Sopenharmony_ci * be authenticated and decrypted using the tls_en provided here.
641cb0ef41Sopenharmony_ci */
651cb0ef41Sopenharmony_cistatic ossl_inline int ktls_start(int fd, ktls_crypto_info_t *tls_en, int is_tx)
661cb0ef41Sopenharmony_ci{
671cb0ef41Sopenharmony_ci    if (is_tx)
681cb0ef41Sopenharmony_ci        return setsockopt(fd, IPPROTO_TCP, TCP_TXTLS_ENABLE,
691cb0ef41Sopenharmony_ci                          tls_en, sizeof(*tls_en)) ? 0 : 1;
701cb0ef41Sopenharmony_ci#   ifndef OPENSSL_NO_KTLS_RX
711cb0ef41Sopenharmony_ci    return setsockopt(fd, IPPROTO_TCP, TCP_RXTLS_ENABLE, tls_en,
721cb0ef41Sopenharmony_ci                      sizeof(*tls_en)) ? 0 : 1;
731cb0ef41Sopenharmony_ci#   else
741cb0ef41Sopenharmony_ci    return 0;
751cb0ef41Sopenharmony_ci#   endif
761cb0ef41Sopenharmony_ci}
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci/*
791cb0ef41Sopenharmony_ci * Send a TLS record using the tls_en provided in ktls_start and use
801cb0ef41Sopenharmony_ci * record_type instead of the default SSL3_RT_APPLICATION_DATA.
811cb0ef41Sopenharmony_ci * When the socket is non-blocking, then this call either returns EAGAIN or
821cb0ef41Sopenharmony_ci * the entire record is pushed to TCP. It is impossible to send a partial
831cb0ef41Sopenharmony_ci * record using this control message.
841cb0ef41Sopenharmony_ci */
851cb0ef41Sopenharmony_cistatic ossl_inline int ktls_send_ctrl_message(int fd, unsigned char record_type,
861cb0ef41Sopenharmony_ci                                              const void *data, size_t length)
871cb0ef41Sopenharmony_ci{
881cb0ef41Sopenharmony_ci    struct msghdr msg = { 0 };
891cb0ef41Sopenharmony_ci    int cmsg_len = sizeof(record_type);
901cb0ef41Sopenharmony_ci    struct cmsghdr *cmsg;
911cb0ef41Sopenharmony_ci    char buf[CMSG_SPACE(cmsg_len)];
921cb0ef41Sopenharmony_ci    struct iovec msg_iov;   /* Vector of data to send/receive into */
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci    msg.msg_control = buf;
951cb0ef41Sopenharmony_ci    msg.msg_controllen = sizeof(buf);
961cb0ef41Sopenharmony_ci    cmsg = CMSG_FIRSTHDR(&msg);
971cb0ef41Sopenharmony_ci    cmsg->cmsg_level = IPPROTO_TCP;
981cb0ef41Sopenharmony_ci    cmsg->cmsg_type = TLS_SET_RECORD_TYPE;
991cb0ef41Sopenharmony_ci    cmsg->cmsg_len = CMSG_LEN(cmsg_len);
1001cb0ef41Sopenharmony_ci    *((unsigned char *)CMSG_DATA(cmsg)) = record_type;
1011cb0ef41Sopenharmony_ci    msg.msg_controllen = cmsg->cmsg_len;
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci    msg_iov.iov_base = (void *)data;
1041cb0ef41Sopenharmony_ci    msg_iov.iov_len = length;
1051cb0ef41Sopenharmony_ci    msg.msg_iov = &msg_iov;
1061cb0ef41Sopenharmony_ci    msg.msg_iovlen = 1;
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci    return sendmsg(fd, &msg, 0);
1091cb0ef41Sopenharmony_ci}
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci#   ifdef OPENSSL_NO_KTLS_RX
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_cistatic ossl_inline int ktls_read_record(int fd, void *data, size_t length)
1141cb0ef41Sopenharmony_ci{
1151cb0ef41Sopenharmony_ci    return -1;
1161cb0ef41Sopenharmony_ci}
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci#   else /* !defined(OPENSSL_NO_KTLS_RX) */
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci/*
1211cb0ef41Sopenharmony_ci * Receive a TLS record using the tls_en provided in ktls_start.  The
1221cb0ef41Sopenharmony_ci * kernel strips any explicit IV and authentication tag, but provides
1231cb0ef41Sopenharmony_ci * the TLS record header via a control message.  If there is an error
1241cb0ef41Sopenharmony_ci * with the TLS record such as an invalid header, invalid padding, or
1251cb0ef41Sopenharmony_ci * authentication failure recvmsg() will fail with an error.
1261cb0ef41Sopenharmony_ci */
1271cb0ef41Sopenharmony_cistatic ossl_inline int ktls_read_record(int fd, void *data, size_t length)
1281cb0ef41Sopenharmony_ci{
1291cb0ef41Sopenharmony_ci    struct msghdr msg = { 0 };
1301cb0ef41Sopenharmony_ci    int cmsg_len = sizeof(struct tls_get_record);
1311cb0ef41Sopenharmony_ci    struct tls_get_record *tgr;
1321cb0ef41Sopenharmony_ci    struct cmsghdr *cmsg;
1331cb0ef41Sopenharmony_ci    char buf[CMSG_SPACE(cmsg_len)];
1341cb0ef41Sopenharmony_ci    struct iovec msg_iov;   /* Vector of data to send/receive into */
1351cb0ef41Sopenharmony_ci    int ret;
1361cb0ef41Sopenharmony_ci    unsigned char *p = data;
1371cb0ef41Sopenharmony_ci    const size_t prepend_length = SSL3_RT_HEADER_LENGTH;
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci    if (length <= prepend_length) {
1401cb0ef41Sopenharmony_ci        errno = EINVAL;
1411cb0ef41Sopenharmony_ci        return -1;
1421cb0ef41Sopenharmony_ci    }
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci    msg.msg_control = buf;
1451cb0ef41Sopenharmony_ci    msg.msg_controllen = sizeof(buf);
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ci    msg_iov.iov_base = p + prepend_length;
1481cb0ef41Sopenharmony_ci    msg_iov.iov_len = length - prepend_length;
1491cb0ef41Sopenharmony_ci    msg.msg_iov = &msg_iov;
1501cb0ef41Sopenharmony_ci    msg.msg_iovlen = 1;
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ci    ret = recvmsg(fd, &msg, 0);
1531cb0ef41Sopenharmony_ci    if (ret <= 0)
1541cb0ef41Sopenharmony_ci        return ret;
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci    if ((msg.msg_flags & (MSG_EOR | MSG_CTRUNC)) != MSG_EOR) {
1571cb0ef41Sopenharmony_ci        errno = EMSGSIZE;
1581cb0ef41Sopenharmony_ci        return -1;
1591cb0ef41Sopenharmony_ci    }
1601cb0ef41Sopenharmony_ci
1611cb0ef41Sopenharmony_ci    if (msg.msg_controllen == 0) {
1621cb0ef41Sopenharmony_ci        errno = EBADMSG;
1631cb0ef41Sopenharmony_ci        return -1;
1641cb0ef41Sopenharmony_ci    }
1651cb0ef41Sopenharmony_ci
1661cb0ef41Sopenharmony_ci    cmsg = CMSG_FIRSTHDR(&msg);
1671cb0ef41Sopenharmony_ci    if (cmsg->cmsg_level != IPPROTO_TCP || cmsg->cmsg_type != TLS_GET_RECORD
1681cb0ef41Sopenharmony_ci        || cmsg->cmsg_len != CMSG_LEN(cmsg_len)) {
1691cb0ef41Sopenharmony_ci        errno = EBADMSG;
1701cb0ef41Sopenharmony_ci        return -1;
1711cb0ef41Sopenharmony_ci    }
1721cb0ef41Sopenharmony_ci
1731cb0ef41Sopenharmony_ci    tgr = (struct tls_get_record *)CMSG_DATA(cmsg);
1741cb0ef41Sopenharmony_ci    p[0] = tgr->tls_type;
1751cb0ef41Sopenharmony_ci    p[1] = tgr->tls_vmajor;
1761cb0ef41Sopenharmony_ci    p[2] = tgr->tls_vminor;
1771cb0ef41Sopenharmony_ci    *(uint16_t *)(p + 3) = htons(ret);
1781cb0ef41Sopenharmony_ci
1791cb0ef41Sopenharmony_ci    return ret + prepend_length;
1801cb0ef41Sopenharmony_ci}
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ci#   endif /* OPENSSL_NO_KTLS_RX */
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ci/*
1851cb0ef41Sopenharmony_ci * KTLS enables the sendfile system call to send data from a file over
1861cb0ef41Sopenharmony_ci * TLS.
1871cb0ef41Sopenharmony_ci */
1881cb0ef41Sopenharmony_cistatic ossl_inline ossl_ssize_t ktls_sendfile(int s, int fd, off_t off,
1891cb0ef41Sopenharmony_ci                                              size_t size, int flags)
1901cb0ef41Sopenharmony_ci{
1911cb0ef41Sopenharmony_ci    off_t sbytes = 0;
1921cb0ef41Sopenharmony_ci    int ret;
1931cb0ef41Sopenharmony_ci
1941cb0ef41Sopenharmony_ci    ret = sendfile(fd, s, off, size, NULL, &sbytes, flags);
1951cb0ef41Sopenharmony_ci    if (ret == -1 && sbytes == 0)
1961cb0ef41Sopenharmony_ci        return -1;
1971cb0ef41Sopenharmony_ci    return sbytes;
1981cb0ef41Sopenharmony_ci}
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ci#  endif                         /* __FreeBSD__ */
2011cb0ef41Sopenharmony_ci
2021cb0ef41Sopenharmony_ci#  if defined(OPENSSL_SYS_LINUX)
2031cb0ef41Sopenharmony_ci
2041cb0ef41Sopenharmony_ci#   include <linux/tls.h>
2051cb0ef41Sopenharmony_ci#   if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0)
2061cb0ef41Sopenharmony_ci#    define OPENSSL_NO_KTLS_RX
2071cb0ef41Sopenharmony_ci#    ifndef PEDANTIC
2081cb0ef41Sopenharmony_ci#     warning "KTLS requires Kernel Headers >= 4.17.0 for receiving"
2091cb0ef41Sopenharmony_ci#     warning "Skipping Compilation of KTLS receive data path"
2101cb0ef41Sopenharmony_ci#    endif
2111cb0ef41Sopenharmony_ci#   endif
2121cb0ef41Sopenharmony_ci#   define OPENSSL_KTLS_AES_GCM_128
2131cb0ef41Sopenharmony_ci#   if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 1, 0)
2141cb0ef41Sopenharmony_ci#    define OPENSSL_KTLS_AES_GCM_256
2151cb0ef41Sopenharmony_ci#    define OPENSSL_KTLS_TLS13
2161cb0ef41Sopenharmony_ci#    if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 2, 0)
2171cb0ef41Sopenharmony_ci#     define OPENSSL_KTLS_AES_CCM_128
2181cb0ef41Sopenharmony_ci#     if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 11, 0)
2191cb0ef41Sopenharmony_ci#      ifndef OPENSSL_NO_CHACHA
2201cb0ef41Sopenharmony_ci#       define OPENSSL_KTLS_CHACHA20_POLY1305
2211cb0ef41Sopenharmony_ci#      endif
2221cb0ef41Sopenharmony_ci#     endif
2231cb0ef41Sopenharmony_ci#    endif
2241cb0ef41Sopenharmony_ci#   endif
2251cb0ef41Sopenharmony_ci
2261cb0ef41Sopenharmony_ci#   include <sys/sendfile.h>
2271cb0ef41Sopenharmony_ci#   include <netinet/tcp.h>
2281cb0ef41Sopenharmony_ci#   include <linux/socket.h>
2291cb0ef41Sopenharmony_ci#   include <openssl/ssl3.h>
2301cb0ef41Sopenharmony_ci#   include <openssl/tls1.h>
2311cb0ef41Sopenharmony_ci#   include <openssl/evp.h>
2321cb0ef41Sopenharmony_ci
2331cb0ef41Sopenharmony_ci#   ifndef SOL_TLS
2341cb0ef41Sopenharmony_ci#    define SOL_TLS 282
2351cb0ef41Sopenharmony_ci#   endif
2361cb0ef41Sopenharmony_ci
2371cb0ef41Sopenharmony_ci#   ifndef TCP_ULP
2381cb0ef41Sopenharmony_ci#    define TCP_ULP 31
2391cb0ef41Sopenharmony_ci#   endif
2401cb0ef41Sopenharmony_ci
2411cb0ef41Sopenharmony_ci#   ifndef TLS_RX
2421cb0ef41Sopenharmony_ci#    define TLS_RX                  2
2431cb0ef41Sopenharmony_ci#   endif
2441cb0ef41Sopenharmony_ci
2451cb0ef41Sopenharmony_cistruct tls_crypto_info_all {
2461cb0ef41Sopenharmony_ci    union {
2471cb0ef41Sopenharmony_ci#   ifdef OPENSSL_KTLS_AES_GCM_128
2481cb0ef41Sopenharmony_ci        struct tls12_crypto_info_aes_gcm_128 gcm128;
2491cb0ef41Sopenharmony_ci#   endif
2501cb0ef41Sopenharmony_ci#   ifdef OPENSSL_KTLS_AES_GCM_256
2511cb0ef41Sopenharmony_ci        struct tls12_crypto_info_aes_gcm_256 gcm256;
2521cb0ef41Sopenharmony_ci#   endif
2531cb0ef41Sopenharmony_ci#   ifdef OPENSSL_KTLS_AES_CCM_128
2541cb0ef41Sopenharmony_ci        struct tls12_crypto_info_aes_ccm_128 ccm128;
2551cb0ef41Sopenharmony_ci#   endif
2561cb0ef41Sopenharmony_ci#   ifdef OPENSSL_KTLS_CHACHA20_POLY1305
2571cb0ef41Sopenharmony_ci        struct tls12_crypto_info_chacha20_poly1305 chacha20poly1305;
2581cb0ef41Sopenharmony_ci#   endif
2591cb0ef41Sopenharmony_ci    };
2601cb0ef41Sopenharmony_ci    size_t tls_crypto_info_len;
2611cb0ef41Sopenharmony_ci};
2621cb0ef41Sopenharmony_ci
2631cb0ef41Sopenharmony_citypedef struct tls_crypto_info_all ktls_crypto_info_t;
2641cb0ef41Sopenharmony_ci
2651cb0ef41Sopenharmony_ci/*
2661cb0ef41Sopenharmony_ci * When successful, this socket option doesn't change the behaviour of the
2671cb0ef41Sopenharmony_ci * TCP socket, except changing the TCP setsockopt handler to enable the
2681cb0ef41Sopenharmony_ci * processing of SOL_TLS socket options. All other functionality remains the
2691cb0ef41Sopenharmony_ci * same.
2701cb0ef41Sopenharmony_ci */
2711cb0ef41Sopenharmony_cistatic ossl_inline int ktls_enable(int fd)
2721cb0ef41Sopenharmony_ci{
2731cb0ef41Sopenharmony_ci    return setsockopt(fd, SOL_TCP, TCP_ULP, "tls", sizeof("tls")) ? 0 : 1;
2741cb0ef41Sopenharmony_ci}
2751cb0ef41Sopenharmony_ci
2761cb0ef41Sopenharmony_ci/*
2771cb0ef41Sopenharmony_ci * The TLS_TX socket option changes the send/sendmsg handlers of the TCP socket.
2781cb0ef41Sopenharmony_ci * If successful, then data sent using this socket will be encrypted and
2791cb0ef41Sopenharmony_ci * encapsulated in TLS records using the crypto_info provided here.
2801cb0ef41Sopenharmony_ci * The TLS_RX socket option changes the recv/recvmsg handlers of the TCP socket.
2811cb0ef41Sopenharmony_ci * If successful, then data received using this socket will be decrypted,
2821cb0ef41Sopenharmony_ci * authenticated and decapsulated using the crypto_info provided here.
2831cb0ef41Sopenharmony_ci */
2841cb0ef41Sopenharmony_cistatic ossl_inline int ktls_start(int fd, ktls_crypto_info_t *crypto_info,
2851cb0ef41Sopenharmony_ci                                  int is_tx)
2861cb0ef41Sopenharmony_ci{
2871cb0ef41Sopenharmony_ci    return setsockopt(fd, SOL_TLS, is_tx ? TLS_TX : TLS_RX,
2881cb0ef41Sopenharmony_ci                      crypto_info, crypto_info->tls_crypto_info_len) ? 0 : 1;
2891cb0ef41Sopenharmony_ci}
2901cb0ef41Sopenharmony_ci
2911cb0ef41Sopenharmony_ci/*
2921cb0ef41Sopenharmony_ci * Send a TLS record using the crypto_info provided in ktls_start and use
2931cb0ef41Sopenharmony_ci * record_type instead of the default SSL3_RT_APPLICATION_DATA.
2941cb0ef41Sopenharmony_ci * When the socket is non-blocking, then this call either returns EAGAIN or
2951cb0ef41Sopenharmony_ci * the entire record is pushed to TCP. It is impossible to send a partial
2961cb0ef41Sopenharmony_ci * record using this control message.
2971cb0ef41Sopenharmony_ci */
2981cb0ef41Sopenharmony_cistatic ossl_inline int ktls_send_ctrl_message(int fd, unsigned char record_type,
2991cb0ef41Sopenharmony_ci                                              const void *data, size_t length)
3001cb0ef41Sopenharmony_ci{
3011cb0ef41Sopenharmony_ci    struct msghdr msg;
3021cb0ef41Sopenharmony_ci    int cmsg_len = sizeof(record_type);
3031cb0ef41Sopenharmony_ci    struct cmsghdr *cmsg;
3041cb0ef41Sopenharmony_ci    union {
3051cb0ef41Sopenharmony_ci        struct cmsghdr hdr;
3061cb0ef41Sopenharmony_ci        char buf[CMSG_SPACE(sizeof(unsigned char))];
3071cb0ef41Sopenharmony_ci    } cmsgbuf;
3081cb0ef41Sopenharmony_ci    struct iovec msg_iov;       /* Vector of data to send/receive into */
3091cb0ef41Sopenharmony_ci
3101cb0ef41Sopenharmony_ci    memset(&msg, 0, sizeof(msg));
3111cb0ef41Sopenharmony_ci    msg.msg_control = cmsgbuf.buf;
3121cb0ef41Sopenharmony_ci    msg.msg_controllen = sizeof(cmsgbuf.buf);
3131cb0ef41Sopenharmony_ci    cmsg = CMSG_FIRSTHDR(&msg);
3141cb0ef41Sopenharmony_ci    cmsg->cmsg_level = SOL_TLS;
3151cb0ef41Sopenharmony_ci    cmsg->cmsg_type = TLS_SET_RECORD_TYPE;
3161cb0ef41Sopenharmony_ci    cmsg->cmsg_len = CMSG_LEN(cmsg_len);
3171cb0ef41Sopenharmony_ci    *((unsigned char *)CMSG_DATA(cmsg)) = record_type;
3181cb0ef41Sopenharmony_ci    msg.msg_controllen = cmsg->cmsg_len;
3191cb0ef41Sopenharmony_ci
3201cb0ef41Sopenharmony_ci    msg_iov.iov_base = (void *)data;
3211cb0ef41Sopenharmony_ci    msg_iov.iov_len = length;
3221cb0ef41Sopenharmony_ci    msg.msg_iov = &msg_iov;
3231cb0ef41Sopenharmony_ci    msg.msg_iovlen = 1;
3241cb0ef41Sopenharmony_ci
3251cb0ef41Sopenharmony_ci    return sendmsg(fd, &msg, 0);
3261cb0ef41Sopenharmony_ci}
3271cb0ef41Sopenharmony_ci
3281cb0ef41Sopenharmony_ci/*
3291cb0ef41Sopenharmony_ci * KTLS enables the sendfile system call to send data from a file over TLS.
3301cb0ef41Sopenharmony_ci * @flags are ignored on Linux. (placeholder for FreeBSD sendfile)
3311cb0ef41Sopenharmony_ci * */
3321cb0ef41Sopenharmony_cistatic ossl_inline ossl_ssize_t ktls_sendfile(int s, int fd, off_t off, size_t size, int flags)
3331cb0ef41Sopenharmony_ci{
3341cb0ef41Sopenharmony_ci    return sendfile(s, fd, &off, size);
3351cb0ef41Sopenharmony_ci}
3361cb0ef41Sopenharmony_ci
3371cb0ef41Sopenharmony_ci#   ifdef OPENSSL_NO_KTLS_RX
3381cb0ef41Sopenharmony_ci
3391cb0ef41Sopenharmony_ci
3401cb0ef41Sopenharmony_cistatic ossl_inline int ktls_read_record(int fd, void *data, size_t length)
3411cb0ef41Sopenharmony_ci{
3421cb0ef41Sopenharmony_ci    return -1;
3431cb0ef41Sopenharmony_ci}
3441cb0ef41Sopenharmony_ci
3451cb0ef41Sopenharmony_ci#   else /* !defined(OPENSSL_NO_KTLS_RX) */
3461cb0ef41Sopenharmony_ci
3471cb0ef41Sopenharmony_ci/*
3481cb0ef41Sopenharmony_ci * Receive a TLS record using the crypto_info provided in ktls_start.
3491cb0ef41Sopenharmony_ci * The kernel strips the TLS record header, IV and authentication tag,
3501cb0ef41Sopenharmony_ci * returning only the plaintext data or an error on failure.
3511cb0ef41Sopenharmony_ci * We add the TLS record header here to satisfy routines in rec_layer_s3.c
3521cb0ef41Sopenharmony_ci */
3531cb0ef41Sopenharmony_cistatic ossl_inline int ktls_read_record(int fd, void *data, size_t length)
3541cb0ef41Sopenharmony_ci{
3551cb0ef41Sopenharmony_ci    struct msghdr msg;
3561cb0ef41Sopenharmony_ci    struct cmsghdr *cmsg;
3571cb0ef41Sopenharmony_ci    union {
3581cb0ef41Sopenharmony_ci        struct cmsghdr hdr;
3591cb0ef41Sopenharmony_ci        char buf[CMSG_SPACE(sizeof(unsigned char))];
3601cb0ef41Sopenharmony_ci    } cmsgbuf;
3611cb0ef41Sopenharmony_ci    struct iovec msg_iov;
3621cb0ef41Sopenharmony_ci    int ret;
3631cb0ef41Sopenharmony_ci    unsigned char *p = data;
3641cb0ef41Sopenharmony_ci    const size_t prepend_length = SSL3_RT_HEADER_LENGTH;
3651cb0ef41Sopenharmony_ci
3661cb0ef41Sopenharmony_ci    if (length < prepend_length + EVP_GCM_TLS_TAG_LEN) {
3671cb0ef41Sopenharmony_ci        errno = EINVAL;
3681cb0ef41Sopenharmony_ci        return -1;
3691cb0ef41Sopenharmony_ci    }
3701cb0ef41Sopenharmony_ci
3711cb0ef41Sopenharmony_ci    memset(&msg, 0, sizeof(msg));
3721cb0ef41Sopenharmony_ci    msg.msg_control = cmsgbuf.buf;
3731cb0ef41Sopenharmony_ci    msg.msg_controllen = sizeof(cmsgbuf.buf);
3741cb0ef41Sopenharmony_ci
3751cb0ef41Sopenharmony_ci    msg_iov.iov_base = p + prepend_length;
3761cb0ef41Sopenharmony_ci    msg_iov.iov_len = length - prepend_length - EVP_GCM_TLS_TAG_LEN;
3771cb0ef41Sopenharmony_ci    msg.msg_iov = &msg_iov;
3781cb0ef41Sopenharmony_ci    msg.msg_iovlen = 1;
3791cb0ef41Sopenharmony_ci
3801cb0ef41Sopenharmony_ci    ret = recvmsg(fd, &msg, 0);
3811cb0ef41Sopenharmony_ci    if (ret < 0)
3821cb0ef41Sopenharmony_ci        return ret;
3831cb0ef41Sopenharmony_ci
3841cb0ef41Sopenharmony_ci    if (msg.msg_controllen > 0) {
3851cb0ef41Sopenharmony_ci        cmsg = CMSG_FIRSTHDR(&msg);
3861cb0ef41Sopenharmony_ci        if (cmsg->cmsg_type == TLS_GET_RECORD_TYPE) {
3871cb0ef41Sopenharmony_ci            p[0] = *((unsigned char *)CMSG_DATA(cmsg));
3881cb0ef41Sopenharmony_ci            p[1] = TLS1_2_VERSION_MAJOR;
3891cb0ef41Sopenharmony_ci            p[2] = TLS1_2_VERSION_MINOR;
3901cb0ef41Sopenharmony_ci            /* returned length is limited to msg_iov.iov_len above */
3911cb0ef41Sopenharmony_ci            p[3] = (ret >> 8) & 0xff;
3921cb0ef41Sopenharmony_ci            p[4] = ret & 0xff;
3931cb0ef41Sopenharmony_ci            ret += prepend_length;
3941cb0ef41Sopenharmony_ci        }
3951cb0ef41Sopenharmony_ci    }
3961cb0ef41Sopenharmony_ci
3971cb0ef41Sopenharmony_ci    return ret;
3981cb0ef41Sopenharmony_ci}
3991cb0ef41Sopenharmony_ci
4001cb0ef41Sopenharmony_ci#   endif /* OPENSSL_NO_KTLS_RX */
4011cb0ef41Sopenharmony_ci
4021cb0ef41Sopenharmony_ci#  endif /* OPENSSL_SYS_LINUX */
4031cb0ef41Sopenharmony_ci# endif /* OPENSSL_NO_KTLS */
4041cb0ef41Sopenharmony_ci#endif /* HEADER_INTERNAL_KTLS */
405