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