tls.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
  3. * Copyright (c) 2016-2017, Dave Watson <[email protected]>. 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. #ifndef _TLS_OFFLOAD_H
  34. #define _TLS_OFFLOAD_H
  35. #include <linux/types.h>
  36. #include <asm/byteorder.h>
  37. #include <linux/crypto.h>
  38. #include <linux/socket.h>
  39. #include <linux/tcp.h>
  40. #include <linux/mutex.h>
  41. #include <linux/netdevice.h>
  42. #include <linux/rcupdate.h>
  43. #include <linux/android_kabi.h>
  44. #include <net/net_namespace.h>
  45. #include <net/tcp.h>
  46. #include <net/strparser.h>
  47. #include <crypto/aead.h>
  48. #include <uapi/linux/tls.h>
  49. struct tls_rec;
  50. struct tls_cipher_size_desc {
  51. unsigned int iv;
  52. unsigned int key;
  53. unsigned int salt;
  54. unsigned int tag;
  55. unsigned int rec_seq;
  56. };
  57. extern const struct tls_cipher_size_desc tls_cipher_size_desc[];
  58. /* Maximum data size carried in a TLS record */
  59. #define TLS_MAX_PAYLOAD_SIZE ((size_t)1 << 14)
  60. #define TLS_HEADER_SIZE 5
  61. #define TLS_NONCE_OFFSET TLS_HEADER_SIZE
  62. #define TLS_CRYPTO_INFO_READY(info) ((info)->cipher_type)
  63. #define TLS_RECORD_TYPE_DATA 0x17
  64. #define TLS_AAD_SPACE_SIZE 13
  65. #define MAX_IV_SIZE 16
  66. #define TLS_TAG_SIZE 16
  67. #define TLS_MAX_REC_SEQ_SIZE 8
  68. #define TLS_MAX_AAD_SIZE TLS_AAD_SPACE_SIZE
  69. /* For CCM mode, the full 16-bytes of IV is made of '4' fields of given sizes.
  70. *
  71. * IV[16] = b0[1] || implicit nonce[4] || explicit nonce[8] || length[3]
  72. *
  73. * The field 'length' is encoded in field 'b0' as '(length width - 1)'.
  74. * Hence b0 contains (3 - 1) = 2.
  75. */
  76. #define TLS_AES_CCM_IV_B0_BYTE 2
  77. #define TLS_SM4_CCM_IV_B0_BYTE 2
  78. enum {
  79. TLS_BASE,
  80. TLS_SW,
  81. TLS_HW,
  82. TLS_HW_RECORD,
  83. TLS_NUM_CONFIG,
  84. };
  85. struct tx_work {
  86. struct delayed_work work;
  87. struct sock *sk;
  88. };
  89. struct tls_sw_context_tx {
  90. struct crypto_aead *aead_send;
  91. struct crypto_wait async_wait;
  92. struct tx_work tx_work;
  93. struct tls_rec *open_rec;
  94. struct list_head tx_list;
  95. atomic_t encrypt_pending;
  96. /* protect crypto_wait with encrypt_pending */
  97. spinlock_t encrypt_compl_lock;
  98. int async_notify;
  99. u8 async_capable:1;
  100. #define BIT_TX_SCHEDULED 0
  101. #define BIT_TX_CLOSING 1
  102. unsigned long tx_bitmask;
  103. ANDROID_KABI_RESERVE(1);
  104. };
  105. struct tls_strparser {
  106. struct sock *sk;
  107. u32 mark : 8;
  108. u32 stopped : 1;
  109. u32 copy_mode : 1;
  110. u32 mixed_decrypted : 1;
  111. u32 msg_ready : 1;
  112. struct strp_msg stm;
  113. struct sk_buff *anchor;
  114. struct work_struct work;
  115. };
  116. struct tls_sw_context_rx {
  117. struct crypto_aead *aead_recv;
  118. struct crypto_wait async_wait;
  119. struct sk_buff_head rx_list; /* list of decrypted 'data' records */
  120. void (*saved_data_ready)(struct sock *sk);
  121. u8 reader_present;
  122. u8 async_capable:1;
  123. u8 zc_capable:1;
  124. u8 reader_contended:1;
  125. struct tls_strparser strp;
  126. atomic_t decrypt_pending;
  127. /* protect crypto_wait with decrypt_pending*/
  128. spinlock_t decrypt_compl_lock;
  129. struct sk_buff_head async_hold;
  130. struct wait_queue_head wq;
  131. ANDROID_KABI_RESERVE(1);
  132. };
  133. struct tls_record_info {
  134. struct list_head list;
  135. u32 end_seq;
  136. int len;
  137. int num_frags;
  138. skb_frag_t frags[MAX_SKB_FRAGS];
  139. };
  140. struct tls_offload_context_tx {
  141. struct crypto_aead *aead_send;
  142. spinlock_t lock; /* protects records list */
  143. struct list_head records_list;
  144. struct tls_record_info *open_record;
  145. struct tls_record_info *retransmit_hint;
  146. u64 hint_record_sn;
  147. u64 unacked_record_sn;
  148. struct scatterlist sg_tx_data[MAX_SKB_FRAGS];
  149. void (*sk_destruct)(struct sock *sk);
  150. struct work_struct destruct_work;
  151. struct tls_context *ctx;
  152. u8 driver_state[] __aligned(8);
  153. /* The TLS layer reserves room for driver specific state
  154. * Currently the belief is that there is not enough
  155. * driver specific state to justify another layer of indirection
  156. */
  157. #define TLS_DRIVER_STATE_SIZE_TX 16
  158. };
  159. #define TLS_OFFLOAD_CONTEXT_SIZE_TX \
  160. (sizeof(struct tls_offload_context_tx) + TLS_DRIVER_STATE_SIZE_TX)
  161. enum tls_context_flags {
  162. /* tls_device_down was called after the netdev went down, device state
  163. * was released, and kTLS works in software, even though rx_conf is
  164. * still TLS_HW (needed for transition).
  165. */
  166. TLS_RX_DEV_DEGRADED = 0,
  167. /* Unlike RX where resync is driven entirely by the core in TX only
  168. * the driver knows when things went out of sync, so we need the flag
  169. * to be atomic.
  170. */
  171. TLS_TX_SYNC_SCHED = 1,
  172. /* tls_dev_del was called for the RX side, device state was released,
  173. * but tls_ctx->netdev might still be kept, because TX-side driver
  174. * resources might not be released yet. Used to prevent the second
  175. * tls_dev_del call in tls_device_down if it happens simultaneously.
  176. */
  177. TLS_RX_DEV_CLOSED = 2,
  178. };
  179. struct cipher_context {
  180. char *iv;
  181. char *rec_seq;
  182. };
  183. union tls_crypto_context {
  184. struct tls_crypto_info info;
  185. union {
  186. struct tls12_crypto_info_aes_gcm_128 aes_gcm_128;
  187. struct tls12_crypto_info_aes_gcm_256 aes_gcm_256;
  188. struct tls12_crypto_info_chacha20_poly1305 chacha20_poly1305;
  189. struct tls12_crypto_info_sm4_gcm sm4_gcm;
  190. struct tls12_crypto_info_sm4_ccm sm4_ccm;
  191. };
  192. };
  193. struct tls_prot_info {
  194. u16 version;
  195. u16 cipher_type;
  196. u16 prepend_size;
  197. u16 tag_size;
  198. u16 overhead_size;
  199. u16 iv_size;
  200. u16 salt_size;
  201. u16 rec_seq_size;
  202. u16 aad_size;
  203. u16 tail_size;
  204. };
  205. struct tls_context {
  206. /* read-only cache line */
  207. struct tls_prot_info prot_info;
  208. u8 tx_conf:3;
  209. u8 rx_conf:3;
  210. u8 zerocopy_sendfile:1;
  211. u8 rx_no_pad:1;
  212. int (*push_pending_record)(struct sock *sk, int flags);
  213. void (*sk_write_space)(struct sock *sk);
  214. void *priv_ctx_tx;
  215. void *priv_ctx_rx;
  216. struct net_device __rcu *netdev;
  217. /* rw cache line */
  218. struct cipher_context tx;
  219. struct cipher_context rx;
  220. struct scatterlist *partially_sent_record;
  221. u16 partially_sent_offset;
  222. bool in_tcp_sendpages;
  223. bool pending_open_record_frags;
  224. struct mutex tx_lock; /* protects partially_sent_* fields and
  225. * per-type TX fields
  226. */
  227. unsigned long flags;
  228. /* cache cold stuff */
  229. struct proto *sk_proto;
  230. struct sock *sk;
  231. void (*sk_destruct)(struct sock *sk);
  232. union tls_crypto_context crypto_send;
  233. union tls_crypto_context crypto_recv;
  234. struct list_head list;
  235. refcount_t refcount;
  236. struct rcu_head rcu;
  237. };
  238. enum tls_offload_ctx_dir {
  239. TLS_OFFLOAD_CTX_DIR_RX,
  240. TLS_OFFLOAD_CTX_DIR_TX,
  241. };
  242. struct tlsdev_ops {
  243. int (*tls_dev_add)(struct net_device *netdev, struct sock *sk,
  244. enum tls_offload_ctx_dir direction,
  245. struct tls_crypto_info *crypto_info,
  246. u32 start_offload_tcp_sn);
  247. void (*tls_dev_del)(struct net_device *netdev,
  248. struct tls_context *ctx,
  249. enum tls_offload_ctx_dir direction);
  250. int (*tls_dev_resync)(struct net_device *netdev,
  251. struct sock *sk, u32 seq, u8 *rcd_sn,
  252. enum tls_offload_ctx_dir direction);
  253. ANDROID_KABI_RESERVE(1);
  254. ANDROID_KABI_RESERVE(2);
  255. ANDROID_KABI_RESERVE(3);
  256. ANDROID_KABI_RESERVE(4);
  257. };
  258. enum tls_offload_sync_type {
  259. TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ = 0,
  260. TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT = 1,
  261. TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ_ASYNC = 2,
  262. };
  263. #define TLS_DEVICE_RESYNC_NH_START_IVAL 2
  264. #define TLS_DEVICE_RESYNC_NH_MAX_IVAL 128
  265. #define TLS_DEVICE_RESYNC_ASYNC_LOGMAX 13
  266. struct tls_offload_resync_async {
  267. atomic64_t req;
  268. u16 loglen;
  269. u16 rcd_delta;
  270. u32 log[TLS_DEVICE_RESYNC_ASYNC_LOGMAX];
  271. };
  272. struct tls_offload_context_rx {
  273. /* sw must be the first member of tls_offload_context_rx */
  274. struct tls_sw_context_rx sw;
  275. enum tls_offload_sync_type resync_type;
  276. /* this member is set regardless of resync_type, to avoid branches */
  277. u8 resync_nh_reset:1;
  278. /* CORE_NEXT_HINT-only member, but use the hole here */
  279. u8 resync_nh_do_now:1;
  280. union {
  281. /* TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ */
  282. struct {
  283. atomic64_t resync_req;
  284. };
  285. /* TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT */
  286. struct {
  287. u32 decrypted_failed;
  288. u32 decrypted_tgt;
  289. } resync_nh;
  290. /* TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ_ASYNC */
  291. struct {
  292. struct tls_offload_resync_async *resync_async;
  293. };
  294. };
  295. u8 driver_state[] __aligned(8);
  296. /* The TLS layer reserves room for driver specific state
  297. * Currently the belief is that there is not enough
  298. * driver specific state to justify another layer of indirection
  299. */
  300. #define TLS_DRIVER_STATE_SIZE_RX 8
  301. };
  302. #define TLS_OFFLOAD_CONTEXT_SIZE_RX \
  303. (sizeof(struct tls_offload_context_rx) + TLS_DRIVER_STATE_SIZE_RX)
  304. struct tls_record_info *tls_get_record(struct tls_offload_context_tx *context,
  305. u32 seq, u64 *p_record_sn);
  306. static inline bool tls_record_is_start_marker(struct tls_record_info *rec)
  307. {
  308. return rec->len == 0;
  309. }
  310. static inline u32 tls_record_start_seq(struct tls_record_info *rec)
  311. {
  312. return rec->end_seq - rec->len;
  313. }
  314. struct sk_buff *
  315. tls_validate_xmit_skb(struct sock *sk, struct net_device *dev,
  316. struct sk_buff *skb);
  317. struct sk_buff *
  318. tls_validate_xmit_skb_sw(struct sock *sk, struct net_device *dev,
  319. struct sk_buff *skb);
  320. static inline bool tls_is_sk_tx_device_offloaded(struct sock *sk)
  321. {
  322. #ifdef CONFIG_SOCK_VALIDATE_XMIT
  323. return sk_fullsock(sk) &&
  324. (smp_load_acquire(&sk->sk_validate_xmit_skb) ==
  325. &tls_validate_xmit_skb);
  326. #else
  327. return false;
  328. #endif
  329. }
  330. static inline struct tls_context *tls_get_ctx(const struct sock *sk)
  331. {
  332. struct inet_connection_sock *icsk = inet_csk(sk);
  333. /* Use RCU on icsk_ulp_data only for sock diag code,
  334. * TLS data path doesn't need rcu_dereference().
  335. */
  336. return (__force void *)icsk->icsk_ulp_data;
  337. }
  338. static inline struct tls_sw_context_rx *tls_sw_ctx_rx(
  339. const struct tls_context *tls_ctx)
  340. {
  341. return (struct tls_sw_context_rx *)tls_ctx->priv_ctx_rx;
  342. }
  343. static inline struct tls_sw_context_tx *tls_sw_ctx_tx(
  344. const struct tls_context *tls_ctx)
  345. {
  346. return (struct tls_sw_context_tx *)tls_ctx->priv_ctx_tx;
  347. }
  348. static inline struct tls_offload_context_tx *
  349. tls_offload_ctx_tx(const struct tls_context *tls_ctx)
  350. {
  351. return (struct tls_offload_context_tx *)tls_ctx->priv_ctx_tx;
  352. }
  353. static inline bool tls_sw_has_ctx_tx(const struct sock *sk)
  354. {
  355. struct tls_context *ctx = tls_get_ctx(sk);
  356. if (!ctx)
  357. return false;
  358. return !!tls_sw_ctx_tx(ctx);
  359. }
  360. static inline bool tls_sw_has_ctx_rx(const struct sock *sk)
  361. {
  362. struct tls_context *ctx = tls_get_ctx(sk);
  363. if (!ctx)
  364. return false;
  365. return !!tls_sw_ctx_rx(ctx);
  366. }
  367. static inline struct tls_offload_context_rx *
  368. tls_offload_ctx_rx(const struct tls_context *tls_ctx)
  369. {
  370. return (struct tls_offload_context_rx *)tls_ctx->priv_ctx_rx;
  371. }
  372. static inline void *__tls_driver_ctx(struct tls_context *tls_ctx,
  373. enum tls_offload_ctx_dir direction)
  374. {
  375. if (direction == TLS_OFFLOAD_CTX_DIR_TX)
  376. return tls_offload_ctx_tx(tls_ctx)->driver_state;
  377. else
  378. return tls_offload_ctx_rx(tls_ctx)->driver_state;
  379. }
  380. static inline void *
  381. tls_driver_ctx(const struct sock *sk, enum tls_offload_ctx_dir direction)
  382. {
  383. return __tls_driver_ctx(tls_get_ctx(sk), direction);
  384. }
  385. #define RESYNC_REQ BIT(0)
  386. #define RESYNC_REQ_ASYNC BIT(1)
  387. /* The TLS context is valid until sk_destruct is called */
  388. static inline void tls_offload_rx_resync_request(struct sock *sk, __be32 seq)
  389. {
  390. struct tls_context *tls_ctx = tls_get_ctx(sk);
  391. struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx);
  392. atomic64_set(&rx_ctx->resync_req, ((u64)ntohl(seq) << 32) | RESYNC_REQ);
  393. }
  394. /* Log all TLS record header TCP sequences in [seq, seq+len] */
  395. static inline void
  396. tls_offload_rx_resync_async_request_start(struct sock *sk, __be32 seq, u16 len)
  397. {
  398. struct tls_context *tls_ctx = tls_get_ctx(sk);
  399. struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx);
  400. atomic64_set(&rx_ctx->resync_async->req, ((u64)ntohl(seq) << 32) |
  401. ((u64)len << 16) | RESYNC_REQ | RESYNC_REQ_ASYNC);
  402. rx_ctx->resync_async->loglen = 0;
  403. rx_ctx->resync_async->rcd_delta = 0;
  404. }
  405. static inline void
  406. tls_offload_rx_resync_async_request_end(struct sock *sk, __be32 seq)
  407. {
  408. struct tls_context *tls_ctx = tls_get_ctx(sk);
  409. struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx);
  410. atomic64_set(&rx_ctx->resync_async->req,
  411. ((u64)ntohl(seq) << 32) | RESYNC_REQ);
  412. }
  413. static inline void
  414. tls_offload_rx_resync_set_type(struct sock *sk, enum tls_offload_sync_type type)
  415. {
  416. struct tls_context *tls_ctx = tls_get_ctx(sk);
  417. tls_offload_ctx_rx(tls_ctx)->resync_type = type;
  418. }
  419. /* Driver's seq tracking has to be disabled until resync succeeded */
  420. static inline bool tls_offload_tx_resync_pending(struct sock *sk)
  421. {
  422. struct tls_context *tls_ctx = tls_get_ctx(sk);
  423. bool ret;
  424. ret = test_bit(TLS_TX_SYNC_SCHED, &tls_ctx->flags);
  425. smp_mb__after_atomic();
  426. return ret;
  427. }
  428. struct sk_buff *tls_encrypt_skb(struct sk_buff *skb);
  429. #ifdef CONFIG_TLS_DEVICE
  430. void tls_device_sk_destruct(struct sock *sk);
  431. void tls_offload_tx_resync_request(struct sock *sk, u32 got_seq, u32 exp_seq);
  432. static inline bool tls_is_sk_rx_device_offloaded(struct sock *sk)
  433. {
  434. if (!sk_fullsock(sk) ||
  435. smp_load_acquire(&sk->sk_destruct) != tls_device_sk_destruct)
  436. return false;
  437. return tls_get_ctx(sk)->rx_conf == TLS_HW;
  438. }
  439. #endif
  440. #endif /* _TLS_OFFLOAD_H */