158790314SJakub Kicinski /* 284c61fe1SJakub Kicinski * Copyright (c) 2016 Tom Herbert <tom@herbertland.com> 358790314SJakub Kicinski * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved. 458790314SJakub Kicinski * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved. 558790314SJakub Kicinski * 658790314SJakub Kicinski * This software is available to you under a choice of one of two 758790314SJakub Kicinski * licenses. You may choose to be licensed under the terms of the GNU 858790314SJakub Kicinski * General Public License (GPL) Version 2, available from the file 958790314SJakub Kicinski * COPYING in the main directory of this source tree, or the 1058790314SJakub Kicinski * OpenIB.org BSD license below: 1158790314SJakub Kicinski * 1258790314SJakub Kicinski * Redistribution and use in source and binary forms, with or 1358790314SJakub Kicinski * without modification, are permitted provided that the following 1458790314SJakub Kicinski * conditions are met: 1558790314SJakub Kicinski * 1658790314SJakub Kicinski * - Redistributions of source code must retain the above 1758790314SJakub Kicinski * copyright notice, this list of conditions and the following 1858790314SJakub Kicinski * disclaimer. 1958790314SJakub Kicinski * 2058790314SJakub Kicinski * - Redistributions in binary form must reproduce the above 2158790314SJakub Kicinski * copyright notice, this list of conditions and the following 2258790314SJakub Kicinski * disclaimer in the documentation and/or other materials 2358790314SJakub Kicinski * provided with the distribution. 2458790314SJakub Kicinski * 2558790314SJakub Kicinski * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 2658790314SJakub Kicinski * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 2758790314SJakub Kicinski * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 2858790314SJakub Kicinski * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 2958790314SJakub Kicinski * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 3058790314SJakub Kicinski * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 3158790314SJakub Kicinski * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 3258790314SJakub Kicinski * SOFTWARE. 3358790314SJakub Kicinski */ 3458790314SJakub Kicinski 3558790314SJakub Kicinski #ifndef _TLS_INT_H 3658790314SJakub Kicinski #define _TLS_INT_H 3758790314SJakub Kicinski 3858790314SJakub Kicinski #include <asm/byteorder.h> 3958790314SJakub Kicinski #include <linux/types.h> 4058790314SJakub Kicinski #include <linux/skmsg.h> 4158790314SJakub Kicinski #include <net/tls.h> 4258790314SJakub Kicinski 43fd31f399SJakub Kicinski #define TLS_PAGE_ORDER (min_t(unsigned int, PAGE_ALLOC_COSTLY_ORDER, \ 44fd31f399SJakub Kicinski TLS_MAX_PAYLOAD_SIZE >> PAGE_SHIFT)) 45fd31f399SJakub Kicinski 4658790314SJakub Kicinski #define __TLS_INC_STATS(net, field) \ 4758790314SJakub Kicinski __SNMP_INC_STATS((net)->mib.tls_statistics, field) 4858790314SJakub Kicinski #define TLS_INC_STATS(net, field) \ 4958790314SJakub Kicinski SNMP_INC_STATS((net)->mib.tls_statistics, field) 5058790314SJakub Kicinski #define TLS_DEC_STATS(net, field) \ 5158790314SJakub Kicinski SNMP_DEC_STATS((net)->mib.tls_statistics, field) 5258790314SJakub Kicinski 5358790314SJakub Kicinski /* TLS records are maintained in 'struct tls_rec'. It stores the memory pages 5458790314SJakub Kicinski * allocated or mapped for each TLS record. After encryption, the records are 5558790314SJakub Kicinski * stores in a linked list. 5658790314SJakub Kicinski */ 5758790314SJakub Kicinski struct tls_rec { 5858790314SJakub Kicinski struct list_head list; 5958790314SJakub Kicinski int tx_ready; 6058790314SJakub Kicinski int tx_flags; 6158790314SJakub Kicinski 6258790314SJakub Kicinski struct sk_msg msg_plaintext; 6358790314SJakub Kicinski struct sk_msg msg_encrypted; 6458790314SJakub Kicinski 6558790314SJakub Kicinski /* AAD | msg_plaintext.sg.data | sg_tag */ 6658790314SJakub Kicinski struct scatterlist sg_aead_in[2]; 6758790314SJakub Kicinski /* AAD | msg_encrypted.sg.data (data contains overhead for hdr & iv & tag) */ 6858790314SJakub Kicinski struct scatterlist sg_aead_out[2]; 6958790314SJakub Kicinski 7058790314SJakub Kicinski char content_type; 7158790314SJakub Kicinski struct scatterlist sg_content_type; 7258790314SJakub Kicinski 73*8d338c76SHerbert Xu struct sock *sk; 74*8d338c76SHerbert Xu 7558790314SJakub Kicinski char aad_space[TLS_AAD_SPACE_SIZE]; 7658790314SJakub Kicinski u8 iv_data[MAX_IV_SIZE]; 7758790314SJakub Kicinski struct aead_request aead_req; 7858790314SJakub Kicinski u8 aead_req_ctx[]; 7958790314SJakub Kicinski }; 8058790314SJakub Kicinski 8158790314SJakub Kicinski int __net_init tls_proc_init(struct net *net); 8258790314SJakub Kicinski void __net_exit tls_proc_fini(struct net *net); 8358790314SJakub Kicinski 8458790314SJakub Kicinski struct tls_context *tls_ctx_create(struct sock *sk); 8558790314SJakub Kicinski void tls_ctx_free(struct sock *sk, struct tls_context *ctx); 8658790314SJakub Kicinski void update_sk_prot(struct sock *sk, struct tls_context *ctx); 8758790314SJakub Kicinski 8858790314SJakub Kicinski int wait_on_pending_writer(struct sock *sk, long *timeo); 8958790314SJakub Kicinski int tls_sk_query(struct sock *sk, int optname, char __user *optval, 9058790314SJakub Kicinski int __user *optlen); 9158790314SJakub Kicinski int tls_sk_attach(struct sock *sk, int optname, char __user *optval, 9258790314SJakub Kicinski unsigned int optlen); 9358790314SJakub Kicinski void tls_err_abort(struct sock *sk, int err); 9458790314SJakub Kicinski 9558790314SJakub Kicinski int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx); 9658790314SJakub Kicinski void tls_update_rx_zc_capable(struct tls_context *tls_ctx); 9758790314SJakub Kicinski void tls_sw_strparser_arm(struct sock *sk, struct tls_context *ctx); 9858790314SJakub Kicinski void tls_sw_strparser_done(struct tls_context *tls_ctx); 9958790314SJakub Kicinski int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size); 10058790314SJakub Kicinski int tls_sw_sendpage_locked(struct sock *sk, struct page *page, 10158790314SJakub Kicinski int offset, size_t size, int flags); 10258790314SJakub Kicinski int tls_sw_sendpage(struct sock *sk, struct page *page, 10358790314SJakub Kicinski int offset, size_t size, int flags); 10458790314SJakub Kicinski void tls_sw_cancel_work_tx(struct tls_context *tls_ctx); 10558790314SJakub Kicinski void tls_sw_release_resources_tx(struct sock *sk); 10658790314SJakub Kicinski void tls_sw_free_ctx_tx(struct tls_context *tls_ctx); 10758790314SJakub Kicinski void tls_sw_free_resources_rx(struct sock *sk); 10858790314SJakub Kicinski void tls_sw_release_resources_rx(struct sock *sk); 10958790314SJakub Kicinski void tls_sw_free_ctx_rx(struct tls_context *tls_ctx); 11058790314SJakub Kicinski int tls_sw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 11158790314SJakub Kicinski int flags, int *addr_len); 11258790314SJakub Kicinski bool tls_sw_sock_is_readable(struct sock *sk); 11358790314SJakub Kicinski ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos, 11458790314SJakub Kicinski struct pipe_inode_info *pipe, 11558790314SJakub Kicinski size_t len, unsigned int flags); 11658790314SJakub Kicinski 11758790314SJakub Kicinski int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size); 11858790314SJakub Kicinski int tls_device_sendpage(struct sock *sk, struct page *page, 11958790314SJakub Kicinski int offset, size_t size, int flags); 12058790314SJakub Kicinski int tls_tx_records(struct sock *sk, int flags); 12158790314SJakub Kicinski 12258790314SJakub Kicinski void tls_sw_write_space(struct sock *sk, struct tls_context *ctx); 12358790314SJakub Kicinski void tls_device_write_space(struct sock *sk, struct tls_context *ctx); 12458790314SJakub Kicinski 12558790314SJakub Kicinski int tls_process_cmsg(struct sock *sk, struct msghdr *msg, 12658790314SJakub Kicinski unsigned char *record_type); 127541cc48bSJakub Kicinski int decrypt_skb(struct sock *sk, struct scatterlist *sgout); 12858790314SJakub Kicinski 12958790314SJakub Kicinski int tls_sw_fallback_init(struct sock *sk, 13058790314SJakub Kicinski struct tls_offload_context_tx *offload_ctx, 13158790314SJakub Kicinski struct tls_crypto_info *crypto_info); 13258790314SJakub Kicinski 13384c61fe1SJakub Kicinski int tls_strp_dev_init(void); 13484c61fe1SJakub Kicinski void tls_strp_dev_exit(void); 13584c61fe1SJakub Kicinski 13684c61fe1SJakub Kicinski void tls_strp_done(struct tls_strparser *strp); 13784c61fe1SJakub Kicinski void tls_strp_stop(struct tls_strparser *strp); 13884c61fe1SJakub Kicinski int tls_strp_init(struct tls_strparser *strp, struct sock *sk); 13984c61fe1SJakub Kicinski void tls_strp_data_ready(struct tls_strparser *strp); 14084c61fe1SJakub Kicinski 14184c61fe1SJakub Kicinski void tls_strp_check_rcv(struct tls_strparser *strp); 14284c61fe1SJakub Kicinski void tls_strp_msg_done(struct tls_strparser *strp); 14384c61fe1SJakub Kicinski 14484c61fe1SJakub Kicinski int tls_rx_msg_size(struct tls_strparser *strp, struct sk_buff *skb); 14584c61fe1SJakub Kicinski void tls_rx_msg_ready(struct tls_strparser *strp); 14684c61fe1SJakub Kicinski 14784c61fe1SJakub Kicinski void tls_strp_msg_load(struct tls_strparser *strp, bool force_refresh); 1488b3c59a7SJakub Kicinski int tls_strp_msg_cow(struct tls_sw_context_rx *ctx); 149d4e5db64SJakub Kicinski struct sk_buff *tls_strp_msg_detach(struct tls_sw_context_rx *ctx); 15084c61fe1SJakub Kicinski int tls_strp_msg_hold(struct tls_strparser *strp, struct sk_buff_head *dst); 151c618db2aSJakub Kicinski 15258790314SJakub Kicinski static inline struct tls_msg *tls_msg(struct sk_buff *skb) 15358790314SJakub Kicinski { 15458790314SJakub Kicinski struct sk_skb_cb *scb = (struct sk_skb_cb *)skb->cb; 15558790314SJakub Kicinski 15658790314SJakub Kicinski return &scb->tls; 15758790314SJakub Kicinski } 15858790314SJakub Kicinski 159541cc48bSJakub Kicinski static inline struct sk_buff *tls_strp_msg(struct tls_sw_context_rx *ctx) 160541cc48bSJakub Kicinski { 16184c61fe1SJakub Kicinski DEBUG_NET_WARN_ON_ONCE(!ctx->strp.msg_ready || !ctx->strp.anchor->len); 16284c61fe1SJakub Kicinski return ctx->strp.anchor; 163541cc48bSJakub Kicinski } 164541cc48bSJakub Kicinski 165b92a13d4SJakub Kicinski static inline bool tls_strp_msg_ready(struct tls_sw_context_rx *ctx) 166b92a13d4SJakub Kicinski { 16784c61fe1SJakub Kicinski return ctx->strp.msg_ready; 168b92a13d4SJakub Kicinski } 169b92a13d4SJakub Kicinski 17058790314SJakub Kicinski #ifdef CONFIG_TLS_DEVICE 171816cd168SJakub Kicinski int tls_device_init(void); 17258790314SJakub Kicinski void tls_device_cleanup(void); 17358790314SJakub Kicinski int tls_set_device_offload(struct sock *sk, struct tls_context *ctx); 17458790314SJakub Kicinski void tls_device_free_resources_tx(struct sock *sk); 17558790314SJakub Kicinski int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx); 17658790314SJakub Kicinski void tls_device_offload_cleanup_rx(struct sock *sk); 17758790314SJakub Kicinski void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq); 178541cc48bSJakub Kicinski int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx); 17958790314SJakub Kicinski #else 180816cd168SJakub Kicinski static inline int tls_device_init(void) { return 0; } 18158790314SJakub Kicinski static inline void tls_device_cleanup(void) {} 18258790314SJakub Kicinski 18358790314SJakub Kicinski static inline int 18458790314SJakub Kicinski tls_set_device_offload(struct sock *sk, struct tls_context *ctx) 18558790314SJakub Kicinski { 18658790314SJakub Kicinski return -EOPNOTSUPP; 18758790314SJakub Kicinski } 18858790314SJakub Kicinski 18958790314SJakub Kicinski static inline void tls_device_free_resources_tx(struct sock *sk) {} 19058790314SJakub Kicinski 19158790314SJakub Kicinski static inline int 19258790314SJakub Kicinski tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx) 19358790314SJakub Kicinski { 19458790314SJakub Kicinski return -EOPNOTSUPP; 19558790314SJakub Kicinski } 19658790314SJakub Kicinski 19758790314SJakub Kicinski static inline void tls_device_offload_cleanup_rx(struct sock *sk) {} 19858790314SJakub Kicinski static inline void 19958790314SJakub Kicinski tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq) {} 20058790314SJakub Kicinski 20158790314SJakub Kicinski static inline int 202541cc48bSJakub Kicinski tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx) 20358790314SJakub Kicinski { 20458790314SJakub Kicinski return 0; 20558790314SJakub Kicinski } 20658790314SJakub Kicinski #endif 20758790314SJakub Kicinski 20858790314SJakub Kicinski int tls_push_sg(struct sock *sk, struct tls_context *ctx, 20958790314SJakub Kicinski struct scatterlist *sg, u16 first_offset, 21058790314SJakub Kicinski int flags); 21158790314SJakub Kicinski int tls_push_partial_record(struct sock *sk, struct tls_context *ctx, 21258790314SJakub Kicinski int flags); 21358790314SJakub Kicinski void tls_free_partial_record(struct sock *sk, struct tls_context *ctx); 21458790314SJakub Kicinski 21558790314SJakub Kicinski static inline bool tls_is_partially_sent_record(struct tls_context *ctx) 21658790314SJakub Kicinski { 21758790314SJakub Kicinski return !!ctx->partially_sent_record; 21858790314SJakub Kicinski } 21958790314SJakub Kicinski 22058790314SJakub Kicinski static inline bool tls_is_pending_open_record(struct tls_context *tls_ctx) 22158790314SJakub Kicinski { 22258790314SJakub Kicinski return tls_ctx->pending_open_record_frags; 22358790314SJakub Kicinski } 22458790314SJakub Kicinski 22558790314SJakub Kicinski static inline bool tls_bigint_increment(unsigned char *seq, int len) 22658790314SJakub Kicinski { 22758790314SJakub Kicinski int i; 22858790314SJakub Kicinski 22958790314SJakub Kicinski for (i = len - 1; i >= 0; i--) { 23058790314SJakub Kicinski ++seq[i]; 23158790314SJakub Kicinski if (seq[i] != 0) 23258790314SJakub Kicinski break; 23358790314SJakub Kicinski } 23458790314SJakub Kicinski 23558790314SJakub Kicinski return (i == -1); 23658790314SJakub Kicinski } 23758790314SJakub Kicinski 23858790314SJakub Kicinski static inline void tls_bigint_subtract(unsigned char *seq, int n) 23958790314SJakub Kicinski { 24058790314SJakub Kicinski u64 rcd_sn; 24158790314SJakub Kicinski __be64 *p; 24258790314SJakub Kicinski 24358790314SJakub Kicinski BUILD_BUG_ON(TLS_MAX_REC_SEQ_SIZE != 8); 24458790314SJakub Kicinski 24558790314SJakub Kicinski p = (__be64 *)seq; 24658790314SJakub Kicinski rcd_sn = be64_to_cpu(*p); 24758790314SJakub Kicinski *p = cpu_to_be64(rcd_sn - n); 24858790314SJakub Kicinski } 24958790314SJakub Kicinski 25058790314SJakub Kicinski static inline void 25158790314SJakub Kicinski tls_advance_record_sn(struct sock *sk, struct tls_prot_info *prot, 25258790314SJakub Kicinski struct cipher_context *ctx) 25358790314SJakub Kicinski { 25458790314SJakub Kicinski if (tls_bigint_increment(ctx->rec_seq, prot->rec_seq_size)) 25558790314SJakub Kicinski tls_err_abort(sk, -EBADMSG); 25658790314SJakub Kicinski 25758790314SJakub Kicinski if (prot->version != TLS_1_3_VERSION && 25858790314SJakub Kicinski prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305) 25958790314SJakub Kicinski tls_bigint_increment(ctx->iv + prot->salt_size, 26058790314SJakub Kicinski prot->iv_size); 26158790314SJakub Kicinski } 26258790314SJakub Kicinski 26358790314SJakub Kicinski static inline void 26458790314SJakub Kicinski tls_xor_iv_with_seq(struct tls_prot_info *prot, char *iv, char *seq) 26558790314SJakub Kicinski { 26658790314SJakub Kicinski int i; 26758790314SJakub Kicinski 26858790314SJakub Kicinski if (prot->version == TLS_1_3_VERSION || 26958790314SJakub Kicinski prot->cipher_type == TLS_CIPHER_CHACHA20_POLY1305) { 27058790314SJakub Kicinski for (i = 0; i < 8; i++) 27158790314SJakub Kicinski iv[i + 4] ^= seq[i]; 27258790314SJakub Kicinski } 27358790314SJakub Kicinski } 27458790314SJakub Kicinski 27558790314SJakub Kicinski static inline void 27658790314SJakub Kicinski tls_fill_prepend(struct tls_context *ctx, char *buf, size_t plaintext_len, 27758790314SJakub Kicinski unsigned char record_type) 27858790314SJakub Kicinski { 27958790314SJakub Kicinski struct tls_prot_info *prot = &ctx->prot_info; 28058790314SJakub Kicinski size_t pkt_len, iv_size = prot->iv_size; 28158790314SJakub Kicinski 28258790314SJakub Kicinski pkt_len = plaintext_len + prot->tag_size; 28358790314SJakub Kicinski if (prot->version != TLS_1_3_VERSION && 28458790314SJakub Kicinski prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305) { 28558790314SJakub Kicinski pkt_len += iv_size; 28658790314SJakub Kicinski 28758790314SJakub Kicinski memcpy(buf + TLS_NONCE_OFFSET, 28858790314SJakub Kicinski ctx->tx.iv + prot->salt_size, iv_size); 28958790314SJakub Kicinski } 29058790314SJakub Kicinski 29158790314SJakub Kicinski /* we cover nonce explicit here as well, so buf should be of 29258790314SJakub Kicinski * size KTLS_DTLS_HEADER_SIZE + KTLS_DTLS_NONCE_EXPLICIT_SIZE 29358790314SJakub Kicinski */ 29458790314SJakub Kicinski buf[0] = prot->version == TLS_1_3_VERSION ? 29558790314SJakub Kicinski TLS_RECORD_TYPE_DATA : record_type; 29658790314SJakub Kicinski /* Note that VERSION must be TLS_1_2 for both TLS1.2 and TLS1.3 */ 29758790314SJakub Kicinski buf[1] = TLS_1_2_VERSION_MINOR; 29858790314SJakub Kicinski buf[2] = TLS_1_2_VERSION_MAJOR; 29958790314SJakub Kicinski /* we can use IV for nonce explicit according to spec */ 30058790314SJakub Kicinski buf[3] = pkt_len >> 8; 30158790314SJakub Kicinski buf[4] = pkt_len & 0xFF; 30258790314SJakub Kicinski } 30358790314SJakub Kicinski 30458790314SJakub Kicinski static inline 30558790314SJakub Kicinski void tls_make_aad(char *buf, size_t size, char *record_sequence, 30658790314SJakub Kicinski unsigned char record_type, struct tls_prot_info *prot) 30758790314SJakub Kicinski { 30858790314SJakub Kicinski if (prot->version != TLS_1_3_VERSION) { 30958790314SJakub Kicinski memcpy(buf, record_sequence, prot->rec_seq_size); 31058790314SJakub Kicinski buf += 8; 31158790314SJakub Kicinski } else { 31258790314SJakub Kicinski size += prot->tag_size; 31358790314SJakub Kicinski } 31458790314SJakub Kicinski 31558790314SJakub Kicinski buf[0] = prot->version == TLS_1_3_VERSION ? 31658790314SJakub Kicinski TLS_RECORD_TYPE_DATA : record_type; 31758790314SJakub Kicinski buf[1] = TLS_1_2_VERSION_MAJOR; 31858790314SJakub Kicinski buf[2] = TLS_1_2_VERSION_MINOR; 31958790314SJakub Kicinski buf[3] = size >> 8; 32058790314SJakub Kicinski buf[4] = size & 0xFF; 32158790314SJakub Kicinski } 32258790314SJakub Kicinski 32358790314SJakub Kicinski #endif 324