b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved. |
| 3 | * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved. |
| 4 | * |
| 5 | * This software is available to you under a choice of one of two |
| 6 | * licenses. You may choose to be licensed under the terms of the GNU |
| 7 | * General Public License (GPL) Version 2, available from the file |
| 8 | * COPYING in the main directory of this source tree, or the |
| 9 | * OpenIB.org BSD license below: |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or |
| 12 | * without modification, are permitted provided that the following |
| 13 | * conditions are met: |
| 14 | * |
| 15 | * - Redistributions of source code must retain the above |
| 16 | * copyright notice, this list of conditions and the following |
| 17 | * disclaimer. |
| 18 | * |
| 19 | * - Redistributions in binary form must reproduce the above |
| 20 | * copyright notice, this list of conditions and the following |
| 21 | * disclaimer in the documentation and/or other materials |
| 22 | * provided with the distribution. |
| 23 | * |
| 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 25 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 26 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 27 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 28 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 29 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 30 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 31 | * SOFTWARE. |
| 32 | */ |
| 33 | |
| 34 | #ifndef _TLS_OFFLOAD_H |
| 35 | #define _TLS_OFFLOAD_H |
| 36 | |
| 37 | #include <linux/types.h> |
| 38 | #include <asm/byteorder.h> |
| 39 | #include <linux/crypto.h> |
| 40 | #include <linux/socket.h> |
| 41 | #include <linux/tcp.h> |
| 42 | #include <linux/skmsg.h> |
| 43 | #include <linux/mutex.h> |
| 44 | #include <linux/netdevice.h> |
| 45 | #include <linux/rcupdate.h> |
| 46 | #include <linux/android_kabi.h> |
| 47 | |
| 48 | #include <net/tcp.h> |
| 49 | #include <net/strparser.h> |
| 50 | #include <crypto/aead.h> |
| 51 | #include <uapi/linux/tls.h> |
| 52 | |
| 53 | |
| 54 | /* Maximum data size carried in a TLS record */ |
| 55 | #define TLS_MAX_PAYLOAD_SIZE ((size_t)1 << 14) |
| 56 | |
| 57 | #define TLS_HEADER_SIZE 5 |
| 58 | #define TLS_NONCE_OFFSET TLS_HEADER_SIZE |
| 59 | |
| 60 | #define TLS_CRYPTO_INFO_READY(info) ((info)->cipher_type) |
| 61 | |
| 62 | #define TLS_RECORD_TYPE_DATA 0x17 |
| 63 | |
| 64 | #define TLS_AAD_SPACE_SIZE 13 |
| 65 | #define TLS_DEVICE_NAME_MAX 32 |
| 66 | |
| 67 | #define MAX_IV_SIZE 16 |
| 68 | #define TLS_MAX_REC_SEQ_SIZE 8 |
| 69 | |
| 70 | /* For AES-CCM, the full 16-bytes of IV is made of '4' fields of given sizes. |
| 71 | * |
| 72 | * IV[16] = b0[1] || implicit nonce[4] || explicit nonce[8] || length[3] |
| 73 | * |
| 74 | * The field 'length' is encoded in field 'b0' as '(length width - 1)'. |
| 75 | * Hence b0 contains (3 - 1) = 2. |
| 76 | */ |
| 77 | #define TLS_AES_CCM_IV_B0_BYTE 2 |
| 78 | |
| 79 | /* |
| 80 | * This structure defines the routines for Inline TLS driver. |
| 81 | * The following routines are optional and filled with a |
| 82 | * null pointer if not defined. |
| 83 | * |
| 84 | * @name: Its the name of registered Inline tls device |
| 85 | * @dev_list: Inline tls device list |
| 86 | * int (*feature)(struct tls_device *device); |
| 87 | * Called to return Inline TLS driver capability |
| 88 | * |
| 89 | * int (*hash)(struct tls_device *device, struct sock *sk); |
| 90 | * This function sets Inline driver for listen and program |
| 91 | * device specific functioanlity as required |
| 92 | * |
| 93 | * void (*unhash)(struct tls_device *device, struct sock *sk); |
| 94 | * This function cleans listen state set by Inline TLS driver |
| 95 | * |
| 96 | * void (*release)(struct kref *kref); |
| 97 | * Release the registered device and allocated resources |
| 98 | * @kref: Number of reference to tls_device |
| 99 | */ |
| 100 | struct tls_device { |
| 101 | char name[TLS_DEVICE_NAME_MAX]; |
| 102 | struct list_head dev_list; |
| 103 | int (*feature)(struct tls_device *device); |
| 104 | int (*hash)(struct tls_device *device, struct sock *sk); |
| 105 | void (*unhash)(struct tls_device *device, struct sock *sk); |
| 106 | void (*release)(struct kref *kref); |
| 107 | struct kref kref; |
| 108 | |
| 109 | ANDROID_KABI_RESERVE(1); |
| 110 | }; |
| 111 | |
| 112 | enum { |
| 113 | TLS_BASE, |
| 114 | TLS_SW, |
| 115 | TLS_HW, |
| 116 | TLS_HW_RECORD, |
| 117 | TLS_NUM_CONFIG, |
| 118 | }; |
| 119 | |
| 120 | /* TLS records are maintained in 'struct tls_rec'. It stores the memory pages |
| 121 | * allocated or mapped for each TLS record. After encryption, the records are |
| 122 | * stores in a linked list. |
| 123 | */ |
| 124 | struct tls_rec { |
| 125 | struct list_head list; |
| 126 | int tx_ready; |
| 127 | int tx_flags; |
| 128 | |
| 129 | struct sk_msg msg_plaintext; |
| 130 | struct sk_msg msg_encrypted; |
| 131 | |
| 132 | /* AAD | msg_plaintext.sg.data | sg_tag */ |
| 133 | struct scatterlist sg_aead_in[2]; |
| 134 | /* AAD | msg_encrypted.sg.data (data contains overhead for hdr & iv & tag) */ |
| 135 | struct scatterlist sg_aead_out[2]; |
| 136 | |
| 137 | char content_type; |
| 138 | struct scatterlist sg_content_type; |
| 139 | |
| 140 | char aad_space[TLS_AAD_SPACE_SIZE]; |
| 141 | u8 iv_data[MAX_IV_SIZE]; |
| 142 | struct aead_request aead_req; |
| 143 | |
| 144 | ANDROID_KABI_RESERVE(1); |
| 145 | |
| 146 | u8 aead_req_ctx[]; |
| 147 | }; |
| 148 | |
| 149 | struct tls_msg { |
| 150 | struct strp_msg rxm; |
| 151 | u8 control; |
| 152 | }; |
| 153 | |
| 154 | struct tx_work { |
| 155 | struct delayed_work work; |
| 156 | struct sock *sk; |
| 157 | }; |
| 158 | |
| 159 | struct tls_sw_context_tx { |
| 160 | struct crypto_aead *aead_send; |
| 161 | struct crypto_wait async_wait; |
| 162 | struct tx_work tx_work; |
| 163 | struct tls_rec *open_rec; |
| 164 | struct list_head tx_list; |
| 165 | atomic_t encrypt_pending; |
| 166 | /* protect crypto_wait with encrypt_pending */ |
| 167 | spinlock_t encrypt_compl_lock; |
| 168 | int async_notify; |
| 169 | int async_capable; |
| 170 | |
| 171 | #define BIT_TX_SCHEDULED 0 |
| 172 | #define BIT_TX_CLOSING 1 |
| 173 | unsigned long tx_bitmask; |
| 174 | |
| 175 | ANDROID_KABI_RESERVE(1); |
| 176 | }; |
| 177 | |
| 178 | struct tls_sw_context_rx { |
| 179 | struct crypto_aead *aead_recv; |
| 180 | struct crypto_wait async_wait; |
| 181 | struct strparser strp; |
| 182 | struct sk_buff_head rx_list; /* list of decrypted 'data' records */ |
| 183 | void (*saved_data_ready)(struct sock *sk); |
| 184 | |
| 185 | struct sk_buff *recv_pkt; |
| 186 | u8 control; |
| 187 | int async_capable; |
| 188 | bool decrypted; |
| 189 | atomic_t decrypt_pending; |
| 190 | /* protect crypto_wait with decrypt_pending*/ |
| 191 | spinlock_t decrypt_compl_lock; |
| 192 | bool async_notify; |
| 193 | |
| 194 | ANDROID_KABI_RESERVE(1); |
| 195 | }; |
| 196 | |
| 197 | struct tls_record_info { |
| 198 | struct list_head list; |
| 199 | u32 end_seq; |
| 200 | int len; |
| 201 | int num_frags; |
| 202 | skb_frag_t frags[MAX_SKB_FRAGS]; |
| 203 | }; |
| 204 | |
| 205 | struct tls_offload_context_tx { |
| 206 | struct crypto_aead *aead_send; |
| 207 | spinlock_t lock; /* protects records list */ |
| 208 | struct list_head records_list; |
| 209 | struct tls_record_info *open_record; |
| 210 | struct tls_record_info *retransmit_hint; |
| 211 | u64 hint_record_sn; |
| 212 | u64 unacked_record_sn; |
| 213 | |
| 214 | struct scatterlist sg_tx_data[MAX_SKB_FRAGS]; |
| 215 | void (*sk_destruct)(struct sock *sk); |
| 216 | u8 driver_state[] __aligned(8); |
| 217 | /* The TLS layer reserves room for driver specific state |
| 218 | * Currently the belief is that there is not enough |
| 219 | * driver specific state to justify another layer of indirection |
| 220 | */ |
| 221 | #define TLS_DRIVER_STATE_SIZE_TX 16 |
| 222 | }; |
| 223 | |
| 224 | #define TLS_OFFLOAD_CONTEXT_SIZE_TX \ |
| 225 | (sizeof(struct tls_offload_context_tx) + TLS_DRIVER_STATE_SIZE_TX) |
| 226 | |
| 227 | enum tls_context_flags { |
| 228 | TLS_RX_SYNC_RUNNING = 0, |
| 229 | /* Unlike RX where resync is driven entirely by the core in TX only |
| 230 | * the driver knows when things went out of sync, so we need the flag |
| 231 | * to be atomic. |
| 232 | */ |
| 233 | TLS_TX_SYNC_SCHED = 1, |
| 234 | /* tls_dev_del was called for the RX side, device state was released, |
| 235 | * but tls_ctx->netdev might still be kept, because TX-side driver |
| 236 | * resources might not be released yet. Used to prevent the second |
| 237 | * tls_dev_del call in tls_device_down if it happens simultaneously. |
| 238 | */ |
| 239 | TLS_RX_DEV_CLOSED = 2, |
| 240 | }; |
| 241 | |
| 242 | struct cipher_context { |
| 243 | char *iv; |
| 244 | char *rec_seq; |
| 245 | }; |
| 246 | |
| 247 | union tls_crypto_context { |
| 248 | struct tls_crypto_info info; |
| 249 | union { |
| 250 | struct tls12_crypto_info_aes_gcm_128 aes_gcm_128; |
| 251 | struct tls12_crypto_info_aes_gcm_256 aes_gcm_256; |
| 252 | }; |
| 253 | }; |
| 254 | |
| 255 | struct tls_prot_info { |
| 256 | u16 version; |
| 257 | u16 cipher_type; |
| 258 | u16 prepend_size; |
| 259 | u16 tag_size; |
| 260 | u16 overhead_size; |
| 261 | u16 iv_size; |
| 262 | u16 salt_size; |
| 263 | u16 rec_seq_size; |
| 264 | u16 aad_size; |
| 265 | u16 tail_size; |
| 266 | }; |
| 267 | |
| 268 | struct tls_context { |
| 269 | /* read-only cache line */ |
| 270 | struct tls_prot_info prot_info; |
| 271 | |
| 272 | u8 tx_conf:3; |
| 273 | u8 rx_conf:3; |
| 274 | |
| 275 | int (*push_pending_record)(struct sock *sk, int flags); |
| 276 | void (*sk_write_space)(struct sock *sk); |
| 277 | |
| 278 | void *priv_ctx_tx; |
| 279 | void *priv_ctx_rx; |
| 280 | |
| 281 | struct net_device *netdev; |
| 282 | |
| 283 | /* rw cache line */ |
| 284 | struct cipher_context tx; |
| 285 | struct cipher_context rx; |
| 286 | |
| 287 | struct scatterlist *partially_sent_record; |
| 288 | u16 partially_sent_offset; |
| 289 | |
| 290 | bool in_tcp_sendpages; |
| 291 | bool pending_open_record_frags; |
| 292 | |
| 293 | struct mutex tx_lock; /* protects partially_sent_* fields and |
| 294 | * per-type TX fields |
| 295 | */ |
| 296 | unsigned long flags; |
| 297 | |
| 298 | /* cache cold stuff */ |
| 299 | struct proto *sk_proto; |
| 300 | |
| 301 | void (*sk_destruct)(struct sock *sk); |
| 302 | |
| 303 | union tls_crypto_context crypto_send; |
| 304 | union tls_crypto_context crypto_recv; |
| 305 | |
| 306 | struct list_head list; |
| 307 | refcount_t refcount; |
| 308 | struct rcu_head rcu; |
| 309 | }; |
| 310 | |
| 311 | enum tls_offload_ctx_dir { |
| 312 | TLS_OFFLOAD_CTX_DIR_RX, |
| 313 | TLS_OFFLOAD_CTX_DIR_TX, |
| 314 | }; |
| 315 | |
| 316 | struct tlsdev_ops { |
| 317 | int (*tls_dev_add)(struct net_device *netdev, struct sock *sk, |
| 318 | enum tls_offload_ctx_dir direction, |
| 319 | struct tls_crypto_info *crypto_info, |
| 320 | u32 start_offload_tcp_sn); |
| 321 | void (*tls_dev_del)(struct net_device *netdev, |
| 322 | struct tls_context *ctx, |
| 323 | enum tls_offload_ctx_dir direction); |
| 324 | int (*tls_dev_resync)(struct net_device *netdev, |
| 325 | struct sock *sk, u32 seq, u8 *rcd_sn, |
| 326 | enum tls_offload_ctx_dir direction); |
| 327 | |
| 328 | ANDROID_KABI_RESERVE(1); |
| 329 | ANDROID_KABI_RESERVE(2); |
| 330 | ANDROID_KABI_RESERVE(3); |
| 331 | ANDROID_KABI_RESERVE(4); |
| 332 | |
| 333 | }; |
| 334 | |
| 335 | enum tls_offload_sync_type { |
| 336 | TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ = 0, |
| 337 | TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT = 1, |
| 338 | }; |
| 339 | |
| 340 | #define TLS_DEVICE_RESYNC_NH_START_IVAL 2 |
| 341 | #define TLS_DEVICE_RESYNC_NH_MAX_IVAL 128 |
| 342 | |
| 343 | struct tls_offload_context_rx { |
| 344 | /* sw must be the first member of tls_offload_context_rx */ |
| 345 | struct tls_sw_context_rx sw; |
| 346 | enum tls_offload_sync_type resync_type; |
| 347 | /* this member is set regardless of resync_type, to avoid branches */ |
| 348 | u8 resync_nh_reset:1; |
| 349 | /* CORE_NEXT_HINT-only member, but use the hole here */ |
| 350 | u8 resync_nh_do_now:1; |
| 351 | union { |
| 352 | /* TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ */ |
| 353 | struct { |
| 354 | atomic64_t resync_req; |
| 355 | }; |
| 356 | /* TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT */ |
| 357 | struct { |
| 358 | u32 decrypted_failed; |
| 359 | u32 decrypted_tgt; |
| 360 | } resync_nh; |
| 361 | }; |
| 362 | u8 driver_state[] __aligned(8); |
| 363 | /* The TLS layer reserves room for driver specific state |
| 364 | * Currently the belief is that there is not enough |
| 365 | * driver specific state to justify another layer of indirection |
| 366 | */ |
| 367 | #define TLS_DRIVER_STATE_SIZE_RX 8 |
| 368 | }; |
| 369 | |
| 370 | #define TLS_OFFLOAD_CONTEXT_SIZE_RX \ |
| 371 | (sizeof(struct tls_offload_context_rx) + TLS_DRIVER_STATE_SIZE_RX) |
| 372 | |
| 373 | void tls_ctx_free(struct sock *sk, struct tls_context *ctx); |
| 374 | int wait_on_pending_writer(struct sock *sk, long *timeo); |
| 375 | int tls_sk_query(struct sock *sk, int optname, char __user *optval, |
| 376 | int __user *optlen); |
| 377 | int tls_sk_attach(struct sock *sk, int optname, char __user *optval, |
| 378 | unsigned int optlen); |
| 379 | void tls_err_abort(struct sock *sk, int err); |
| 380 | |
| 381 | int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx); |
| 382 | void tls_sw_strparser_arm(struct sock *sk, struct tls_context *ctx); |
| 383 | void tls_sw_strparser_done(struct tls_context *tls_ctx); |
| 384 | int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size); |
| 385 | int tls_sw_sendpage_locked(struct sock *sk, struct page *page, |
| 386 | int offset, size_t size, int flags); |
| 387 | int tls_sw_sendpage(struct sock *sk, struct page *page, |
| 388 | int offset, size_t size, int flags); |
| 389 | void tls_sw_cancel_work_tx(struct tls_context *tls_ctx); |
| 390 | void tls_sw_release_resources_tx(struct sock *sk); |
| 391 | void tls_sw_free_ctx_tx(struct tls_context *tls_ctx); |
| 392 | void tls_sw_free_resources_rx(struct sock *sk); |
| 393 | void tls_sw_release_resources_rx(struct sock *sk); |
| 394 | void tls_sw_free_ctx_rx(struct tls_context *tls_ctx); |
| 395 | int tls_sw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, |
| 396 | int nonblock, int flags, int *addr_len); |
| 397 | bool tls_sw_stream_read(const struct sock *sk); |
| 398 | ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos, |
| 399 | struct pipe_inode_info *pipe, |
| 400 | size_t len, unsigned int flags); |
| 401 | |
| 402 | int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size); |
| 403 | int tls_device_sendpage(struct sock *sk, struct page *page, |
| 404 | int offset, size_t size, int flags); |
| 405 | int tls_tx_records(struct sock *sk, int flags); |
| 406 | |
| 407 | struct tls_record_info *tls_get_record(struct tls_offload_context_tx *context, |
| 408 | u32 seq, u64 *p_record_sn); |
| 409 | |
| 410 | static inline bool tls_record_is_start_marker(struct tls_record_info *rec) |
| 411 | { |
| 412 | return rec->len == 0; |
| 413 | } |
| 414 | |
| 415 | static inline u32 tls_record_start_seq(struct tls_record_info *rec) |
| 416 | { |
| 417 | return rec->end_seq - rec->len; |
| 418 | } |
| 419 | |
| 420 | int tls_push_sg(struct sock *sk, struct tls_context *ctx, |
| 421 | struct scatterlist *sg, u16 first_offset, |
| 422 | int flags); |
| 423 | int tls_push_partial_record(struct sock *sk, struct tls_context *ctx, |
| 424 | int flags); |
| 425 | void tls_free_partial_record(struct sock *sk, struct tls_context *ctx); |
| 426 | |
| 427 | static inline struct tls_msg *tls_msg(struct sk_buff *skb) |
| 428 | { |
| 429 | return (struct tls_msg *)strp_msg(skb); |
| 430 | } |
| 431 | |
| 432 | static inline bool tls_is_partially_sent_record(struct tls_context *ctx) |
| 433 | { |
| 434 | return !!ctx->partially_sent_record; |
| 435 | } |
| 436 | |
| 437 | static inline bool tls_is_pending_open_record(struct tls_context *tls_ctx) |
| 438 | { |
| 439 | return tls_ctx->pending_open_record_frags; |
| 440 | } |
| 441 | |
| 442 | static inline bool is_tx_ready(struct tls_sw_context_tx *ctx) |
| 443 | { |
| 444 | struct tls_rec *rec; |
| 445 | |
| 446 | rec = list_first_entry(&ctx->tx_list, struct tls_rec, list); |
| 447 | if (!rec) |
| 448 | return false; |
| 449 | |
| 450 | return READ_ONCE(rec->tx_ready); |
| 451 | } |
| 452 | |
| 453 | static inline u16 tls_user_config(struct tls_context *ctx, bool tx) |
| 454 | { |
| 455 | u16 config = tx ? ctx->tx_conf : ctx->rx_conf; |
| 456 | |
| 457 | switch (config) { |
| 458 | case TLS_BASE: |
| 459 | return TLS_CONF_BASE; |
| 460 | case TLS_SW: |
| 461 | return TLS_CONF_SW; |
| 462 | case TLS_HW: |
| 463 | return TLS_CONF_HW; |
| 464 | case TLS_HW_RECORD: |
| 465 | return TLS_CONF_HW_RECORD; |
| 466 | } |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | struct sk_buff * |
| 471 | tls_validate_xmit_skb(struct sock *sk, struct net_device *dev, |
| 472 | struct sk_buff *skb); |
| 473 | |
| 474 | static inline bool tls_is_sk_tx_device_offloaded(struct sock *sk) |
| 475 | { |
| 476 | #ifdef CONFIG_SOCK_VALIDATE_XMIT |
| 477 | return sk_fullsock(sk) && |
| 478 | (smp_load_acquire(&sk->sk_validate_xmit_skb) == |
| 479 | &tls_validate_xmit_skb); |
| 480 | #else |
| 481 | return false; |
| 482 | #endif |
| 483 | } |
| 484 | |
| 485 | static inline bool tls_bigint_increment(unsigned char *seq, int len) |
| 486 | { |
| 487 | int i; |
| 488 | |
| 489 | for (i = len - 1; i >= 0; i--) { |
| 490 | ++seq[i]; |
| 491 | if (seq[i] != 0) |
| 492 | break; |
| 493 | } |
| 494 | |
| 495 | return (i == -1); |
| 496 | } |
| 497 | |
| 498 | static inline struct tls_context *tls_get_ctx(const struct sock *sk) |
| 499 | { |
| 500 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 501 | |
| 502 | /* Use RCU on icsk_ulp_data only for sock diag code, |
| 503 | * TLS data path doesn't need rcu_dereference(). |
| 504 | */ |
| 505 | return (__force void *)icsk->icsk_ulp_data; |
| 506 | } |
| 507 | |
| 508 | static inline void tls_advance_record_sn(struct sock *sk, |
| 509 | struct tls_prot_info *prot, |
| 510 | struct cipher_context *ctx) |
| 511 | { |
| 512 | if (tls_bigint_increment(ctx->rec_seq, prot->rec_seq_size)) |
| 513 | tls_err_abort(sk, -EBADMSG); |
| 514 | |
| 515 | if (prot->version != TLS_1_3_VERSION) |
| 516 | tls_bigint_increment(ctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, |
| 517 | prot->iv_size); |
| 518 | } |
| 519 | |
| 520 | static inline void tls_fill_prepend(struct tls_context *ctx, |
| 521 | char *buf, |
| 522 | size_t plaintext_len, |
| 523 | unsigned char record_type, |
| 524 | int version) |
| 525 | { |
| 526 | struct tls_prot_info *prot = &ctx->prot_info; |
| 527 | size_t pkt_len, iv_size = prot->iv_size; |
| 528 | |
| 529 | pkt_len = plaintext_len + prot->tag_size; |
| 530 | if (version != TLS_1_3_VERSION) { |
| 531 | pkt_len += iv_size; |
| 532 | |
| 533 | memcpy(buf + TLS_NONCE_OFFSET, |
| 534 | ctx->tx.iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv_size); |
| 535 | } |
| 536 | |
| 537 | /* we cover nonce explicit here as well, so buf should be of |
| 538 | * size KTLS_DTLS_HEADER_SIZE + KTLS_DTLS_NONCE_EXPLICIT_SIZE |
| 539 | */ |
| 540 | buf[0] = version == TLS_1_3_VERSION ? |
| 541 | TLS_RECORD_TYPE_DATA : record_type; |
| 542 | /* Note that VERSION must be TLS_1_2 for both TLS1.2 and TLS1.3 */ |
| 543 | buf[1] = TLS_1_2_VERSION_MINOR; |
| 544 | buf[2] = TLS_1_2_VERSION_MAJOR; |
| 545 | /* we can use IV for nonce explicit according to spec */ |
| 546 | buf[3] = pkt_len >> 8; |
| 547 | buf[4] = pkt_len & 0xFF; |
| 548 | } |
| 549 | |
| 550 | static inline void tls_make_aad(char *buf, |
| 551 | size_t size, |
| 552 | char *record_sequence, |
| 553 | int record_sequence_size, |
| 554 | unsigned char record_type, |
| 555 | int version) |
| 556 | { |
| 557 | if (version != TLS_1_3_VERSION) { |
| 558 | memcpy(buf, record_sequence, record_sequence_size); |
| 559 | buf += 8; |
| 560 | } else { |
| 561 | size += TLS_CIPHER_AES_GCM_128_TAG_SIZE; |
| 562 | } |
| 563 | |
| 564 | buf[0] = version == TLS_1_3_VERSION ? |
| 565 | TLS_RECORD_TYPE_DATA : record_type; |
| 566 | buf[1] = TLS_1_2_VERSION_MAJOR; |
| 567 | buf[2] = TLS_1_2_VERSION_MINOR; |
| 568 | buf[3] = size >> 8; |
| 569 | buf[4] = size & 0xFF; |
| 570 | } |
| 571 | |
| 572 | static inline void xor_iv_with_seq(int version, char *iv, char *seq) |
| 573 | { |
| 574 | int i; |
| 575 | |
| 576 | if (version == TLS_1_3_VERSION) { |
| 577 | for (i = 0; i < 8; i++) |
| 578 | iv[i + 4] ^= seq[i]; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | |
| 583 | static inline struct tls_sw_context_rx *tls_sw_ctx_rx( |
| 584 | const struct tls_context *tls_ctx) |
| 585 | { |
| 586 | return (struct tls_sw_context_rx *)tls_ctx->priv_ctx_rx; |
| 587 | } |
| 588 | |
| 589 | static inline struct tls_sw_context_tx *tls_sw_ctx_tx( |
| 590 | const struct tls_context *tls_ctx) |
| 591 | { |
| 592 | return (struct tls_sw_context_tx *)tls_ctx->priv_ctx_tx; |
| 593 | } |
| 594 | |
| 595 | static inline struct tls_offload_context_tx * |
| 596 | tls_offload_ctx_tx(const struct tls_context *tls_ctx) |
| 597 | { |
| 598 | return (struct tls_offload_context_tx *)tls_ctx->priv_ctx_tx; |
| 599 | } |
| 600 | |
| 601 | static inline bool tls_sw_has_ctx_tx(const struct sock *sk) |
| 602 | { |
| 603 | struct tls_context *ctx = tls_get_ctx(sk); |
| 604 | |
| 605 | if (!ctx) |
| 606 | return false; |
| 607 | return !!tls_sw_ctx_tx(ctx); |
| 608 | } |
| 609 | |
| 610 | static inline bool tls_sw_has_ctx_rx(const struct sock *sk) |
| 611 | { |
| 612 | struct tls_context *ctx = tls_get_ctx(sk); |
| 613 | |
| 614 | if (!ctx) |
| 615 | return false; |
| 616 | return !!tls_sw_ctx_rx(ctx); |
| 617 | } |
| 618 | |
| 619 | void tls_sw_write_space(struct sock *sk, struct tls_context *ctx); |
| 620 | void tls_device_write_space(struct sock *sk, struct tls_context *ctx); |
| 621 | |
| 622 | static inline struct tls_offload_context_rx * |
| 623 | tls_offload_ctx_rx(const struct tls_context *tls_ctx) |
| 624 | { |
| 625 | return (struct tls_offload_context_rx *)tls_ctx->priv_ctx_rx; |
| 626 | } |
| 627 | |
| 628 | #if IS_ENABLED(CONFIG_TLS_DEVICE) |
| 629 | static inline void *__tls_driver_ctx(struct tls_context *tls_ctx, |
| 630 | enum tls_offload_ctx_dir direction) |
| 631 | { |
| 632 | if (direction == TLS_OFFLOAD_CTX_DIR_TX) |
| 633 | return tls_offload_ctx_tx(tls_ctx)->driver_state; |
| 634 | else |
| 635 | return tls_offload_ctx_rx(tls_ctx)->driver_state; |
| 636 | } |
| 637 | |
| 638 | static inline void * |
| 639 | tls_driver_ctx(const struct sock *sk, enum tls_offload_ctx_dir direction) |
| 640 | { |
| 641 | return __tls_driver_ctx(tls_get_ctx(sk), direction); |
| 642 | } |
| 643 | #endif |
| 644 | |
| 645 | /* The TLS context is valid until sk_destruct is called */ |
| 646 | static inline void tls_offload_rx_resync_request(struct sock *sk, __be32 seq) |
| 647 | { |
| 648 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 649 | struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx); |
| 650 | |
| 651 | atomic64_set(&rx_ctx->resync_req, ((u64)ntohl(seq) << 32) | 1); |
| 652 | } |
| 653 | |
| 654 | static inline void |
| 655 | tls_offload_rx_resync_set_type(struct sock *sk, enum tls_offload_sync_type type) |
| 656 | { |
| 657 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 658 | |
| 659 | tls_offload_ctx_rx(tls_ctx)->resync_type = type; |
| 660 | } |
| 661 | |
| 662 | static inline void tls_offload_tx_resync_request(struct sock *sk) |
| 663 | { |
| 664 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 665 | |
| 666 | WARN_ON(test_and_set_bit(TLS_TX_SYNC_SCHED, &tls_ctx->flags)); |
| 667 | } |
| 668 | |
| 669 | /* Driver's seq tracking has to be disabled until resync succeeded */ |
| 670 | static inline bool tls_offload_tx_resync_pending(struct sock *sk) |
| 671 | { |
| 672 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 673 | bool ret; |
| 674 | |
| 675 | ret = test_bit(TLS_TX_SYNC_SCHED, &tls_ctx->flags); |
| 676 | smp_mb__after_atomic(); |
| 677 | return ret; |
| 678 | } |
| 679 | |
| 680 | int tls_proccess_cmsg(struct sock *sk, struct msghdr *msg, |
| 681 | unsigned char *record_type); |
| 682 | void tls_register_device(struct tls_device *device); |
| 683 | void tls_unregister_device(struct tls_device *device); |
| 684 | int decrypt_skb(struct sock *sk, struct sk_buff *skb, |
| 685 | struct scatterlist *sgout); |
| 686 | struct sk_buff *tls_encrypt_skb(struct sk_buff *skb); |
| 687 | |
| 688 | struct sk_buff *tls_validate_xmit_skb(struct sock *sk, |
| 689 | struct net_device *dev, |
| 690 | struct sk_buff *skb); |
| 691 | |
| 692 | int tls_sw_fallback_init(struct sock *sk, |
| 693 | struct tls_offload_context_tx *offload_ctx, |
| 694 | struct tls_crypto_info *crypto_info); |
| 695 | |
| 696 | #ifdef CONFIG_TLS_DEVICE |
| 697 | void tls_device_init(void); |
| 698 | void tls_device_cleanup(void); |
| 699 | int tls_set_device_offload(struct sock *sk, struct tls_context *ctx); |
| 700 | void tls_device_free_resources_tx(struct sock *sk); |
| 701 | int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx); |
| 702 | void tls_device_offload_cleanup_rx(struct sock *sk); |
| 703 | void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq); |
| 704 | int tls_device_decrypted(struct sock *sk, struct sk_buff *skb); |
| 705 | #else |
| 706 | static inline void tls_device_init(void) {} |
| 707 | static inline void tls_device_cleanup(void) {} |
| 708 | |
| 709 | static inline int |
| 710 | tls_set_device_offload(struct sock *sk, struct tls_context *ctx) |
| 711 | { |
| 712 | return -EOPNOTSUPP; |
| 713 | } |
| 714 | |
| 715 | static inline void tls_device_free_resources_tx(struct sock *sk) {} |
| 716 | |
| 717 | static inline int |
| 718 | tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx) |
| 719 | { |
| 720 | return -EOPNOTSUPP; |
| 721 | } |
| 722 | |
| 723 | static inline void tls_device_offload_cleanup_rx(struct sock *sk) {} |
| 724 | static inline void |
| 725 | tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq) {} |
| 726 | |
| 727 | static inline int tls_device_decrypted(struct sock *sk, struct sk_buff *skb) |
| 728 | { |
| 729 | return 0; |
| 730 | } |
| 731 | #endif |
| 732 | #endif /* _TLS_OFFLOAD_H */ |