| 1 |
/* |
| 2 |
* Copyright (c) 2016 Tom Herbert <[email protected]> |
| 3 |
* Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved. |
| 4 |
* Copyright (c) 2016-2017, Dave Watson <[email protected]>. All rights reserved. |
| 5 |
* |
| 6 |
* This software is available to you under a choice of one of two |
| 7 |
* licenses. You may choose to be licensed under the terms of the GNU |
| 8 |
* General Public License (GPL) Version 2, available from the file |
| 9 |
* COPYING in the main directory of this source tree, or the |
| 10 |
* OpenIB.org BSD license below: |
| 11 |
* |
| 12 |
* Redistribution and use in source and binary forms, with or |
| 13 |
* without modification, are permitted provided that the following |
| 14 |
* conditions are met: |
| 15 |
* |
| 16 |
* - Redistributions of source code must retain the above |
| 17 |
* copyright notice, this list of conditions and the following |
| 18 |
* disclaimer. |
| 19 |
* |
| 20 |
* - Redistributions in binary form must reproduce the above |
| 21 |
* copyright notice, this list of conditions and the following |
| 22 |
* disclaimer in the documentation and/or other materials |
| 23 |
* provided with the distribution. |
| 24 |
* |
| 25 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 26 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 27 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 28 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 29 |
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 30 |
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 31 |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 32 |
* SOFTWARE. |
| 33 |
*/ |
| 34 |
|
| 35 |
#ifndef _TLS_INT_H |
| 36 |
#define _TLS_INT_H |
| 37 |
|
| 38 |
#include <asm/byteorder.h> |
| 39 |
#include <linux/types.h> |
| 40 |
#include <linux/skmsg.h> |
| 41 |
#include <net/tls.h> |
| 42 |
#include <net/tls_prot.h> |
| 43 |
|
| 44 |
#define TLS_PAGE_ORDER (min_t(unsigned int, PAGE_ALLOC_COSTLY_ORDER, \ |
| 45 |
TLS_MAX_PAYLOAD_SIZE >> PAGE_SHIFT)) |
| 46 |
|
| 47 |
#define __TLS_INC_STATS(net, field) \ |
| 48 |
__SNMP_INC_STATS((net)->mib.tls_statistics, field) |
| 49 |
#define TLS_INC_STATS(net, field) \ |
| 50 |
SNMP_INC_STATS((net)->mib.tls_statistics, field) |
| 51 |
#define TLS_DEC_STATS(net, field) \ |
| 52 |
SNMP_DEC_STATS((net)->mib.tls_statistics, field) |
| 53 |
|
| 54 |
struct tls_cipher_desc { |
| 55 |
unsigned int nonce; |
| 56 |
unsigned int iv; |
| 57 |
unsigned int key; |
| 58 |
unsigned int salt; |
| 59 |
unsigned int tag; |
| 60 |
unsigned int rec_seq; |
| 61 |
unsigned int iv_offset; |
| 62 |
unsigned int key_offset; |
| 63 |
unsigned int salt_offset; |
| 64 |
unsigned int rec_seq_offset; |
| 65 |
char *cipher_name; |
| 66 |
bool offloadable; |
| 67 |
size_t crypto_info; |
| 68 |
}; |
| 69 |
|
| 70 |
#define TLS_CIPHER_MIN TLS_CIPHER_AES_GCM_128 |
| 71 |
#define TLS_CIPHER_MAX TLS_CIPHER_ARIA_GCM_256 |
| 72 |
extern const struct tls_cipher_desc tls_cipher_desc[TLS_CIPHER_MAX + 1 - TLS_CIPHER_MIN]; |
| 73 |
|
| 74 |
static inline const struct tls_cipher_desc *get_cipher_desc(u16 cipher_type) |
| 75 |
{ |
| 76 |
if (cipher_type < TLS_CIPHER_MIN || cipher_type > TLS_CIPHER_MAX) |
| 77 |
return NULL; |
| 78 |
|
| 79 |
return &tls_cipher_desc[cipher_type - TLS_CIPHER_MIN]; |
| 80 |
} |
| 81 |
|
| 82 |
static inline char *crypto_info_iv(struct tls_crypto_info *crypto_info, |
| 83 |
const struct tls_cipher_desc *cipher_desc) |
| 84 |
{ |
| 85 |
return (char *)crypto_info + cipher_desc->iv_offset; |
| 86 |
} |
| 87 |
|
| 88 |
static inline char *crypto_info_key(struct tls_crypto_info *crypto_info, |
| 89 |
const struct tls_cipher_desc *cipher_desc) |
| 90 |
{ |
| 91 |
return (char *)crypto_info + cipher_desc->key_offset; |
| 92 |
} |
| 93 |
|
| 94 |
static inline char *crypto_info_salt(struct tls_crypto_info *crypto_info, |
| 95 |
const struct tls_cipher_desc *cipher_desc) |
| 96 |
{ |
| 97 |
return (char *)crypto_info + cipher_desc->salt_offset; |
| 98 |
} |
| 99 |
|
| 100 |
static inline char *crypto_info_rec_seq(struct tls_crypto_info *crypto_info, |
| 101 |
const struct tls_cipher_desc *cipher_desc) |
| 102 |
{ |
| 103 |
return (char *)crypto_info + cipher_desc->rec_seq_offset; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
/* TLS records are maintained in 'struct tls_rec'. It stores the memory pages |
| 108 |
* allocated or mapped for each TLS record. After encryption, the records are |
| 109 |
* stores in a linked list. |
| 110 |
*/ |
| 111 |
struct tls_rec { |
| 112 |
struct list_head list; |
| 113 |
int tx_ready; |
| 114 |
int tx_flags; |
| 115 |
|
| 116 |
struct sk_msg msg_plaintext; |
| 117 |
struct sk_msg msg_encrypted; |
| 118 |
|
| 119 |
/* AAD | msg_plaintext.sg.data | sg_tag */ |
| 120 |
struct scatterlist sg_aead_in[2]; |
| 121 |
/* AAD | msg_encrypted.sg.data (data contains overhead for hdr & iv & tag) */ |
| 122 |
struct scatterlist sg_aead_out[2]; |
| 123 |
|
| 124 |
char content_type; |
| 125 |
struct scatterlist sg_content_type; |
| 126 |
|
| 127 |
struct sock *sk; |
| 128 |
|
| 129 |
char aad_space[TLS_AAD_SPACE_SIZE]; |
| 130 |
u8 iv_data[TLS_MAX_IV_SIZE]; |
| 131 |
|
| 132 |
/* Must be last --ends in a flexible-array member. */ |
| 133 |
struct aead_request aead_req; |
| 134 |
}; |
| 135 |
|
| 136 |
int __net_init tls_proc_init(struct net *net); |
| 137 |
void __net_exit tls_proc_fini(struct net *net); |
| 138 |
|
| 139 |
struct tls_context *tls_ctx_create(struct sock *sk); |
| 140 |
void tls_ctx_free(struct sock *sk, struct tls_context *ctx); |
| 141 |
void update_sk_prot(struct sock *sk, struct tls_context *ctx); |
| 142 |
|
| 143 |
int wait_on_pending_writer(struct sock *sk, long *timeo); |
| 144 |
void tls_err_abort(struct sock *sk, int err); |
| 145 |
void tls_strp_abort_strp(struct tls_strparser *strp, int err); |
| 146 |
|
| 147 |
int init_prot_info(struct tls_prot_info *prot, |
| 148 |
const struct tls_crypto_info *crypto_info, |
| 149 |
const struct tls_cipher_desc *cipher_desc); |
| 150 |
int tls_set_sw_offload(struct sock *sk, int tx, |
| 151 |
struct tls_crypto_info *new_crypto_info); |
| 152 |
void tls_update_rx_zc_capable(struct tls_context *tls_ctx); |
| 153 |
void tls_sw_strparser_arm(struct sock *sk, struct tls_context *ctx); |
| 154 |
void tls_sw_strparser_done(struct tls_context *tls_ctx); |
| 155 |
int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size); |
| 156 |
void tls_sw_splice_eof(struct socket *sock); |
| 157 |
void tls_sw_cancel_work_tx(struct tls_context *tls_ctx); |
| 158 |
void tls_sw_release_resources_tx(struct sock *sk); |
| 159 |
void tls_sw_free_ctx_tx(struct tls_context *tls_ctx); |
| 160 |
void tls_sw_free_resources_rx(struct sock *sk); |
| 161 |
void tls_sw_release_resources_rx(struct sock *sk); |
| 162 |
void tls_sw_free_ctx_rx(struct tls_context *tls_ctx); |
| 163 |
int tls_sw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, |
| 164 |
int flags, int *addr_len); |
| 165 |
bool tls_sw_sock_is_readable(struct sock *sk); |
| 166 |
ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos, |
| 167 |
struct pipe_inode_info *pipe, |
| 168 |
size_t len, unsigned int flags); |
| 169 |
int tls_sw_read_sock(struct sock *sk, read_descriptor_t *desc, |
| 170 |
sk_read_actor_t read_actor); |
| 171 |
|
| 172 |
int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size); |
| 173 |
void tls_device_splice_eof(struct socket *sock); |
| 174 |
int tls_tx_records(struct sock *sk, int flags); |
| 175 |
|
| 176 |
void tls_sw_write_space(struct sock *sk, struct tls_context *ctx); |
| 177 |
void tls_device_write_space(struct sock *sk, struct tls_context *ctx); |
| 178 |
|
| 179 |
int tls_process_cmsg(struct sock *sk, struct msghdr *msg, |
| 180 |
unsigned char *record_type); |
| 181 |
int decrypt_skb(struct sock *sk, struct scatterlist *sgout); |
| 182 |
|
| 183 |
int tls_sw_fallback_init(struct sock *sk, |
| 184 |
struct tls_offload_context_tx *offload_ctx, |
| 185 |
struct tls_crypto_info *crypto_info); |
| 186 |
|
| 187 |
int tls_strp_dev_init(void); |
| 188 |
void tls_strp_dev_exit(void); |
| 189 |
|
| 190 |
void tls_strp_done(struct tls_strparser *strp); |
| 191 |
void tls_strp_stop(struct tls_strparser *strp); |
| 192 |
int tls_strp_init(struct tls_strparser *strp, struct sock *sk); |
| 193 |
void tls_strp_data_ready(struct tls_strparser *strp); |
| 194 |
|
| 195 |
void tls_strp_check_rcv(struct tls_strparser *strp); |
| 196 |
void tls_strp_msg_done(struct tls_strparser *strp); |
| 197 |
|
| 198 |
int tls_rx_msg_size(struct tls_strparser *strp, struct sk_buff *skb); |
| 199 |
void tls_rx_msg_ready(struct tls_strparser *strp); |
| 200 |
|
| 201 |
bool tls_strp_msg_load(struct tls_strparser *strp, bool force_refresh); |
| 202 |
int tls_strp_msg_cow(struct tls_sw_context_rx *ctx); |
| 203 |
struct sk_buff *tls_strp_msg_detach(struct tls_sw_context_rx *ctx); |
| 204 |
int tls_strp_msg_hold(struct tls_strparser *strp, struct sk_buff_head *dst); |
| 205 |
|
| 206 |
static inline struct tls_msg *tls_msg(struct sk_buff *skb) |
| 207 |
{ |
| 208 |
struct sk_skb_cb *scb = (struct sk_skb_cb *)skb->cb; |
| 209 |
|
| 210 |
return &scb->tls; |
| 211 |
} |
| 212 |
|
| 213 |
static inline struct sk_buff *tls_strp_msg(struct tls_sw_context_rx *ctx) |
| 214 |
{ |
| 215 |
DEBUG_NET_WARN_ON_ONCE(!ctx->strp.msg_ready || !ctx->strp.anchor->len); |
| 216 |
return ctx->strp.anchor; |
| 217 |
} |
| 218 |
|
| 219 |
static inline bool tls_strp_msg_ready(struct tls_sw_context_rx *ctx) |
| 220 |
{ |
| 221 |
return READ_ONCE(ctx->strp.msg_ready); |
| 222 |
} |
| 223 |
|
| 224 |
static inline bool tls_strp_msg_mixed_decrypted(struct tls_sw_context_rx *ctx) |
| 225 |
{ |
| 226 |
return ctx->strp.mixed_decrypted; |
| 227 |
} |
| 228 |
|
| 229 |
#ifdef CONFIG_TLS_DEVICE |
| 230 |
int tls_device_init(void); |
| 231 |
void tls_device_cleanup(void); |
| 232 |
int tls_set_device_offload(struct sock *sk); |
| 233 |
void tls_device_free_resources_tx(struct sock *sk); |
| 234 |
int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx); |
| 235 |
void tls_device_offload_cleanup_rx(struct sock *sk); |
| 236 |
void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq); |
| 237 |
int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx); |
| 238 |
#else |
| 239 |
static inline int tls_device_init(void) { return 0; } |
| 240 |
static inline void tls_device_cleanup(void) {} |
| 241 |
|
| 242 |
static inline int |
| 243 |
tls_set_device_offload(struct sock *sk) |
| 244 |
{ |
| 245 |
return -EOPNOTSUPP; |
| 246 |
} |
| 247 |
|
| 248 |
static inline void tls_device_free_resources_tx(struct sock *sk) {} |
| 249 |
|
| 250 |
static inline int |
| 251 |
tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx) |
| 252 |
{ |
| 253 |
return -EOPNOTSUPP; |
| 254 |
} |
| 255 |
|
| 256 |
static inline void tls_device_offload_cleanup_rx(struct sock *sk) {} |
| 257 |
static inline void |
| 258 |
tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq) {} |
| 259 |
|
| 260 |
static inline int |
| 261 |
tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx) |
| 262 |
{ |
| 263 |
return 0; |
| 264 |
} |
| 265 |
#endif |
| 266 |
|
| 267 |
int tls_push_sg(struct sock *sk, struct tls_context *ctx, |
| 268 |
struct scatterlist *sg, u16 first_offset, |
| 269 |
int flags); |
| 270 |
int tls_push_partial_record(struct sock *sk, struct tls_context *ctx, |
| 271 |
int flags); |
| 272 |
void tls_free_partial_record(struct sock *sk, struct tls_context *ctx); |
| 273 |
|
| 274 |
static inline bool tls_is_partially_sent_record(struct tls_context *ctx) |
| 275 |
{ |
| 276 |
return !!ctx->partially_sent_record; |
| 277 |
} |
| 278 |
|
| 279 |
static inline bool tls_is_pending_open_record(struct tls_context *tls_ctx) |
| 280 |
{ |
| 281 |
return tls_ctx->pending_open_record_frags; |
| 282 |
} |
| 283 |
|
| 284 |
static inline bool tls_bigint_increment(unsigned char *seq, int len) |
| 285 |
{ |
| 286 |
int i; |
| 287 |
|
| 288 |
for (i = len - 1; i >= 0; i--) { |
| 289 |
++seq[i]; |
| 290 |
if (seq[i] != 0) |
| 291 |
break; |
| 292 |
} |
| 293 |
|
| 294 |
return (i == -1); |
| 295 |
} |
| 296 |
|
| 297 |
static inline void tls_bigint_subtract(unsigned char *seq, int n) |
| 298 |
{ |
| 299 |
u64 rcd_sn; |
| 300 |
__be64 *p; |
| 301 |
|
| 302 |
BUILD_BUG_ON(TLS_MAX_REC_SEQ_SIZE != 8); |
| 303 |
|
| 304 |
p = (__be64 *)seq; |
| 305 |
rcd_sn = be64_to_cpu(*p); |
| 306 |
*p = cpu_to_be64(rcd_sn - n); |
| 307 |
} |
| 308 |
|
| 309 |
static inline void |
| 310 |
tls_advance_record_sn(struct sock *sk, struct tls_prot_info *prot, |
| 311 |
struct cipher_context *ctx) |
| 312 |
{ |
| 313 |
if (tls_bigint_increment(ctx->rec_seq, prot->rec_seq_size)) |
| 314 |
tls_err_abort(sk, -EBADMSG); |
| 315 |
|
| 316 |
if (prot->version != TLS_1_3_VERSION && |
| 317 |
prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305) |
| 318 |
tls_bigint_increment(ctx->iv + prot->salt_size, |
| 319 |
prot->iv_size); |
| 320 |
} |
| 321 |
|
| 322 |
static inline void |
| 323 |
tls_xor_iv_with_seq(struct tls_prot_info *prot, char *iv, char *seq) |
| 324 |
{ |
| 325 |
int i; |
| 326 |
|
| 327 |
if (prot->version == TLS_1_3_VERSION || |
| 328 |
prot->cipher_type == TLS_CIPHER_CHACHA20_POLY1305) { |
| 329 |
for (i = 0; i < 8; i++) |
| 330 |
iv[i + 4] ^= seq[i]; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
static inline void |
| 335 |
tls_fill_prepend(struct tls_context *ctx, char *buf, size_t plaintext_len, |
| 336 |
unsigned char record_type) |
| 337 |
{ |
| 338 |
struct tls_prot_info *prot = &ctx->prot_info; |
| 339 |
size_t pkt_len, iv_size = prot->iv_size; |
| 340 |
|
| 341 |
pkt_len = plaintext_len + prot->tag_size; |
| 342 |
if (prot->version != TLS_1_3_VERSION && |
| 343 |
prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305) { |
| 344 |
pkt_len += iv_size; |
| 345 |
|
| 346 |
memcpy(buf + TLS_NONCE_OFFSET, |
| 347 |
ctx->tx.iv + prot->salt_size, iv_size); |
| 348 |
} |
| 349 |
|
| 350 |
/* we cover nonce explicit here as well, so buf should be of |
| 351 |
* size KTLS_DTLS_HEADER_SIZE + KTLS_DTLS_NONCE_EXPLICIT_SIZE |
| 352 |
*/ |
| 353 |
buf[0] = prot->version == TLS_1_3_VERSION ? |
| 354 |
TLS_RECORD_TYPE_DATA : record_type; |
| 355 |
/* Note that VERSION must be TLS_1_2 for both TLS1.2 and TLS1.3 */ |
| 356 |
buf[1] = TLS_1_2_VERSION_MINOR; |
| 357 |
buf[2] = TLS_1_2_VERSION_MAJOR; |
| 358 |
/* we can use IV for nonce explicit according to spec */ |
| 359 |
buf[3] = pkt_len >> 8; |
| 360 |
buf[4] = pkt_len & 0xFF; |
| 361 |
} |
| 362 |
|
| 363 |
static inline |
| 364 |
void tls_make_aad(char *buf, size_t size, char *record_sequence, |
| 365 |
unsigned char record_type, struct tls_prot_info *prot) |
| 366 |
{ |
| 367 |
if (prot->version != TLS_1_3_VERSION) { |
| 368 |
memcpy(buf, record_sequence, prot->rec_seq_size); |
| 369 |
buf += 8; |
| 370 |
} else { |
| 371 |
size += prot->tag_size; |
| 372 |
} |
| 373 |
|
| 374 |
buf[0] = prot->version == TLS_1_3_VERSION ? |
| 375 |
TLS_RECORD_TYPE_DATA : record_type; |
| 376 |
buf[1] = TLS_1_2_VERSION_MAJOR; |
| 377 |
buf[2] = TLS_1_2_VERSION_MINOR; |
| 378 |
buf[3] = size >> 8; |
| 379 |
buf[4] = size & 0xFF; |
| 380 |
} |
| 381 |
|
| 382 |
#endif |