tls.h 13 KB

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