tls_main.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  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. #include <linux/module.h>
  34. #include <net/tcp.h>
  35. #include <net/inet_common.h>
  36. #include <linux/highmem.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/sched/signal.h>
  39. #include <linux/inetdevice.h>
  40. #include <linux/inet_diag.h>
  41. #include <net/snmp.h>
  42. #include <net/tls.h>
  43. #include <net/tls_toe.h>
  44. #include "tls.h"
  45. MODULE_AUTHOR("Mellanox Technologies");
  46. MODULE_DESCRIPTION("Transport Layer Security Support");
  47. MODULE_LICENSE("Dual BSD/GPL");
  48. MODULE_ALIAS_TCP_ULP("tls");
  49. enum {
  50. TLSV4,
  51. TLSV6,
  52. TLS_NUM_PROTS,
  53. };
  54. #define CHECK_CIPHER_DESC(cipher,ci) \
  55. static_assert(cipher ## _IV_SIZE <= TLS_MAX_IV_SIZE); \
  56. static_assert(cipher ## _SALT_SIZE <= TLS_MAX_SALT_SIZE); \
  57. static_assert(cipher ## _REC_SEQ_SIZE <= TLS_MAX_REC_SEQ_SIZE); \
  58. static_assert(cipher ## _TAG_SIZE == TLS_TAG_SIZE); \
  59. static_assert(sizeof_field(struct ci, iv) == cipher ## _IV_SIZE); \
  60. static_assert(sizeof_field(struct ci, key) == cipher ## _KEY_SIZE); \
  61. static_assert(sizeof_field(struct ci, salt) == cipher ## _SALT_SIZE); \
  62. static_assert(sizeof_field(struct ci, rec_seq) == cipher ## _REC_SEQ_SIZE);
  63. #define __CIPHER_DESC(ci) \
  64. .iv_offset = offsetof(struct ci, iv), \
  65. .key_offset = offsetof(struct ci, key), \
  66. .salt_offset = offsetof(struct ci, salt), \
  67. .rec_seq_offset = offsetof(struct ci, rec_seq), \
  68. .crypto_info = sizeof(struct ci)
  69. #define CIPHER_DESC(cipher,ci,algname,_offloadable) [cipher - TLS_CIPHER_MIN] = { \
  70. .nonce = cipher ## _IV_SIZE, \
  71. .iv = cipher ## _IV_SIZE, \
  72. .key = cipher ## _KEY_SIZE, \
  73. .salt = cipher ## _SALT_SIZE, \
  74. .tag = cipher ## _TAG_SIZE, \
  75. .rec_seq = cipher ## _REC_SEQ_SIZE, \
  76. .cipher_name = algname, \
  77. .offloadable = _offloadable, \
  78. __CIPHER_DESC(ci), \
  79. }
  80. #define CIPHER_DESC_NONCE0(cipher,ci,algname,_offloadable) [cipher - TLS_CIPHER_MIN] = { \
  81. .nonce = 0, \
  82. .iv = cipher ## _IV_SIZE, \
  83. .key = cipher ## _KEY_SIZE, \
  84. .salt = cipher ## _SALT_SIZE, \
  85. .tag = cipher ## _TAG_SIZE, \
  86. .rec_seq = cipher ## _REC_SEQ_SIZE, \
  87. .cipher_name = algname, \
  88. .offloadable = _offloadable, \
  89. __CIPHER_DESC(ci), \
  90. }
  91. const struct tls_cipher_desc tls_cipher_desc[TLS_CIPHER_MAX + 1 - TLS_CIPHER_MIN] = {
  92. CIPHER_DESC(TLS_CIPHER_AES_GCM_128, tls12_crypto_info_aes_gcm_128, "gcm(aes)", true),
  93. CIPHER_DESC(TLS_CIPHER_AES_GCM_256, tls12_crypto_info_aes_gcm_256, "gcm(aes)", true),
  94. CIPHER_DESC(TLS_CIPHER_AES_CCM_128, tls12_crypto_info_aes_ccm_128, "ccm(aes)", false),
  95. CIPHER_DESC_NONCE0(TLS_CIPHER_CHACHA20_POLY1305, tls12_crypto_info_chacha20_poly1305, "rfc7539(chacha20,poly1305)", false),
  96. CIPHER_DESC(TLS_CIPHER_SM4_GCM, tls12_crypto_info_sm4_gcm, "gcm(sm4)", false),
  97. CIPHER_DESC(TLS_CIPHER_SM4_CCM, tls12_crypto_info_sm4_ccm, "ccm(sm4)", false),
  98. CIPHER_DESC(TLS_CIPHER_ARIA_GCM_128, tls12_crypto_info_aria_gcm_128, "gcm(aria)", false),
  99. CIPHER_DESC(TLS_CIPHER_ARIA_GCM_256, tls12_crypto_info_aria_gcm_256, "gcm(aria)", false),
  100. };
  101. CHECK_CIPHER_DESC(TLS_CIPHER_AES_GCM_128, tls12_crypto_info_aes_gcm_128);
  102. CHECK_CIPHER_DESC(TLS_CIPHER_AES_GCM_256, tls12_crypto_info_aes_gcm_256);
  103. CHECK_CIPHER_DESC(TLS_CIPHER_AES_CCM_128, tls12_crypto_info_aes_ccm_128);
  104. CHECK_CIPHER_DESC(TLS_CIPHER_CHACHA20_POLY1305, tls12_crypto_info_chacha20_poly1305);
  105. CHECK_CIPHER_DESC(TLS_CIPHER_SM4_GCM, tls12_crypto_info_sm4_gcm);
  106. CHECK_CIPHER_DESC(TLS_CIPHER_SM4_CCM, tls12_crypto_info_sm4_ccm);
  107. CHECK_CIPHER_DESC(TLS_CIPHER_ARIA_GCM_128, tls12_crypto_info_aria_gcm_128);
  108. CHECK_CIPHER_DESC(TLS_CIPHER_ARIA_GCM_256, tls12_crypto_info_aria_gcm_256);
  109. static const struct proto *saved_tcpv6_prot;
  110. static DEFINE_MUTEX(tcpv6_prot_mutex);
  111. static const struct proto *saved_tcpv4_prot;
  112. static DEFINE_MUTEX(tcpv4_prot_mutex);
  113. static struct proto tls_prots[TLS_NUM_PROTS][TLS_NUM_CONFIG][TLS_NUM_CONFIG];
  114. static struct proto_ops tls_proto_ops[TLS_NUM_PROTS][TLS_NUM_CONFIG][TLS_NUM_CONFIG];
  115. static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
  116. const struct proto *base);
  117. void update_sk_prot(struct sock *sk, struct tls_context *ctx)
  118. {
  119. int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
  120. WRITE_ONCE(sk->sk_prot,
  121. &tls_prots[ip_ver][ctx->tx_conf][ctx->rx_conf]);
  122. WRITE_ONCE(sk->sk_socket->ops,
  123. &tls_proto_ops[ip_ver][ctx->tx_conf][ctx->rx_conf]);
  124. }
  125. int wait_on_pending_writer(struct sock *sk, long *timeo)
  126. {
  127. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  128. int ret, rc = 0;
  129. add_wait_queue(sk_sleep(sk), &wait);
  130. while (1) {
  131. if (!*timeo) {
  132. rc = -EAGAIN;
  133. break;
  134. }
  135. if (signal_pending(current)) {
  136. rc = sock_intr_errno(*timeo);
  137. break;
  138. }
  139. ret = sk_wait_event(sk, timeo,
  140. !READ_ONCE(sk->sk_write_pending), &wait);
  141. if (ret) {
  142. if (ret < 0)
  143. rc = ret;
  144. break;
  145. }
  146. }
  147. remove_wait_queue(sk_sleep(sk), &wait);
  148. return rc;
  149. }
  150. int tls_push_sg(struct sock *sk,
  151. struct tls_context *ctx,
  152. struct scatterlist *sg,
  153. u16 first_offset,
  154. int flags)
  155. {
  156. struct bio_vec bvec;
  157. struct msghdr msg = {
  158. .msg_flags = MSG_SPLICE_PAGES | flags,
  159. };
  160. int ret = 0;
  161. struct page *p;
  162. size_t size;
  163. int offset = first_offset;
  164. size = sg->length - offset;
  165. offset += sg->offset;
  166. ctx->splicing_pages = true;
  167. while (1) {
  168. /* is sending application-limited? */
  169. tcp_rate_check_app_limited(sk);
  170. p = sg_page(sg);
  171. retry:
  172. bvec_set_page(&bvec, p, size, offset);
  173. iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, size);
  174. ret = tcp_sendmsg_locked(sk, &msg, size);
  175. if (ret != size) {
  176. if (ret > 0) {
  177. offset += ret;
  178. size -= ret;
  179. goto retry;
  180. }
  181. offset -= sg->offset;
  182. ctx->partially_sent_offset = offset;
  183. ctx->partially_sent_record = (void *)sg;
  184. ctx->splicing_pages = false;
  185. return ret;
  186. }
  187. put_page(p);
  188. sk_mem_uncharge(sk, sg->length);
  189. sg = sg_next(sg);
  190. if (!sg)
  191. break;
  192. offset = sg->offset;
  193. size = sg->length;
  194. }
  195. ctx->splicing_pages = false;
  196. return 0;
  197. }
  198. static int tls_handle_open_record(struct sock *sk, int flags)
  199. {
  200. struct tls_context *ctx = tls_get_ctx(sk);
  201. if (tls_is_pending_open_record(ctx))
  202. return ctx->push_pending_record(sk, flags);
  203. return 0;
  204. }
  205. int tls_process_cmsg(struct sock *sk, struct msghdr *msg,
  206. unsigned char *record_type)
  207. {
  208. struct cmsghdr *cmsg;
  209. int rc = -EINVAL;
  210. for_each_cmsghdr(cmsg, msg) {
  211. if (!CMSG_OK(msg, cmsg))
  212. return -EINVAL;
  213. if (cmsg->cmsg_level != SOL_TLS)
  214. continue;
  215. switch (cmsg->cmsg_type) {
  216. case TLS_SET_RECORD_TYPE:
  217. if (cmsg->cmsg_len < CMSG_LEN(sizeof(*record_type)))
  218. return -EINVAL;
  219. if (msg->msg_flags & MSG_MORE)
  220. return -EINVAL;
  221. *record_type = *(unsigned char *)CMSG_DATA(cmsg);
  222. rc = tls_handle_open_record(sk, msg->msg_flags);
  223. break;
  224. default:
  225. return -EINVAL;
  226. }
  227. }
  228. return rc;
  229. }
  230. int tls_push_partial_record(struct sock *sk, struct tls_context *ctx,
  231. int flags)
  232. {
  233. struct scatterlist *sg;
  234. u16 offset;
  235. sg = ctx->partially_sent_record;
  236. offset = ctx->partially_sent_offset;
  237. ctx->partially_sent_record = NULL;
  238. return tls_push_sg(sk, ctx, sg, offset, flags);
  239. }
  240. void tls_free_partial_record(struct sock *sk, struct tls_context *ctx)
  241. {
  242. struct scatterlist *sg;
  243. for (sg = ctx->partially_sent_record; sg; sg = sg_next(sg)) {
  244. put_page(sg_page(sg));
  245. sk_mem_uncharge(sk, sg->length);
  246. }
  247. ctx->partially_sent_record = NULL;
  248. }
  249. static void tls_write_space(struct sock *sk)
  250. {
  251. struct tls_context *ctx = tls_get_ctx(sk);
  252. /* If splicing_pages call lower protocol write space handler
  253. * to ensure we wake up any waiting operations there. For example
  254. * if splicing pages where to call sk_wait_event.
  255. */
  256. if (ctx->splicing_pages) {
  257. ctx->sk_write_space(sk);
  258. return;
  259. }
  260. #ifdef CONFIG_TLS_DEVICE
  261. if (ctx->tx_conf == TLS_HW)
  262. tls_device_write_space(sk, ctx);
  263. else
  264. #endif
  265. tls_sw_write_space(sk, ctx);
  266. ctx->sk_write_space(sk);
  267. }
  268. /**
  269. * tls_ctx_free() - free TLS ULP context
  270. * @sk: socket to with @ctx is attached
  271. * @ctx: TLS context structure
  272. *
  273. * Free TLS context. If @sk is %NULL caller guarantees that the socket
  274. * to which @ctx was attached has no outstanding references.
  275. */
  276. void tls_ctx_free(struct sock *sk, struct tls_context *ctx)
  277. {
  278. if (!ctx)
  279. return;
  280. memzero_explicit(&ctx->crypto_send, sizeof(ctx->crypto_send));
  281. memzero_explicit(&ctx->crypto_recv, sizeof(ctx->crypto_recv));
  282. mutex_destroy(&ctx->tx_lock);
  283. if (sk)
  284. kfree_rcu(ctx, rcu);
  285. else
  286. kfree(ctx);
  287. }
  288. static void tls_sk_proto_cleanup(struct sock *sk,
  289. struct tls_context *ctx, long timeo)
  290. {
  291. if (unlikely(sk->sk_write_pending) &&
  292. !wait_on_pending_writer(sk, &timeo))
  293. tls_handle_open_record(sk, 0);
  294. /* We need these for tls_sw_fallback handling of other packets */
  295. if (ctx->tx_conf == TLS_SW) {
  296. tls_sw_release_resources_tx(sk);
  297. TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXSW);
  298. } else if (ctx->tx_conf == TLS_HW) {
  299. tls_device_free_resources_tx(sk);
  300. TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXDEVICE);
  301. }
  302. if (ctx->rx_conf == TLS_SW) {
  303. tls_sw_release_resources_rx(sk);
  304. TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXSW);
  305. } else if (ctx->rx_conf == TLS_HW) {
  306. tls_device_offload_cleanup_rx(sk);
  307. TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXDEVICE);
  308. }
  309. }
  310. static void tls_sk_proto_close(struct sock *sk, long timeout)
  311. {
  312. struct inet_connection_sock *icsk = inet_csk(sk);
  313. struct tls_context *ctx = tls_get_ctx(sk);
  314. long timeo = sock_sndtimeo(sk, 0);
  315. bool free_ctx;
  316. if (ctx->tx_conf == TLS_SW)
  317. tls_sw_cancel_work_tx(ctx);
  318. lock_sock(sk);
  319. free_ctx = ctx->tx_conf != TLS_HW && ctx->rx_conf != TLS_HW;
  320. if (ctx->tx_conf != TLS_BASE || ctx->rx_conf != TLS_BASE)
  321. tls_sk_proto_cleanup(sk, ctx, timeo);
  322. write_lock_bh(&sk->sk_callback_lock);
  323. if (free_ctx)
  324. rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
  325. WRITE_ONCE(sk->sk_prot, ctx->sk_proto);
  326. if (sk->sk_write_space == tls_write_space)
  327. sk->sk_write_space = ctx->sk_write_space;
  328. write_unlock_bh(&sk->sk_callback_lock);
  329. release_sock(sk);
  330. if (ctx->tx_conf == TLS_SW)
  331. tls_sw_free_ctx_tx(ctx);
  332. if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW)
  333. tls_sw_strparser_done(ctx);
  334. if (ctx->rx_conf == TLS_SW)
  335. tls_sw_free_ctx_rx(ctx);
  336. ctx->sk_proto->close(sk, timeout);
  337. if (free_ctx)
  338. tls_ctx_free(sk, ctx);
  339. }
  340. static __poll_t tls_sk_poll(struct file *file, struct socket *sock,
  341. struct poll_table_struct *wait)
  342. {
  343. struct tls_sw_context_rx *ctx;
  344. struct tls_context *tls_ctx;
  345. struct sock *sk = sock->sk;
  346. struct sk_psock *psock;
  347. __poll_t mask = 0;
  348. u8 shutdown;
  349. int state;
  350. mask = tcp_poll(file, sock, wait);
  351. state = inet_sk_state_load(sk);
  352. shutdown = READ_ONCE(sk->sk_shutdown);
  353. if (unlikely(state != TCP_ESTABLISHED || shutdown & RCV_SHUTDOWN))
  354. return mask;
  355. tls_ctx = tls_get_ctx(sk);
  356. ctx = tls_sw_ctx_rx(tls_ctx);
  357. psock = sk_psock_get(sk);
  358. if ((skb_queue_empty_lockless(&ctx->rx_list) &&
  359. !tls_strp_msg_ready(ctx) &&
  360. sk_psock_queue_empty(psock)) ||
  361. READ_ONCE(ctx->key_update_pending))
  362. mask &= ~(EPOLLIN | EPOLLRDNORM);
  363. if (psock)
  364. sk_psock_put(sk, psock);
  365. return mask;
  366. }
  367. static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
  368. int __user *optlen, int tx)
  369. {
  370. int rc = 0;
  371. const struct tls_cipher_desc *cipher_desc;
  372. struct tls_context *ctx = tls_get_ctx(sk);
  373. struct tls_crypto_info *crypto_info;
  374. struct cipher_context *cctx;
  375. int len;
  376. if (get_user(len, optlen))
  377. return -EFAULT;
  378. if (!optval || (len < sizeof(*crypto_info))) {
  379. rc = -EINVAL;
  380. goto out;
  381. }
  382. if (!ctx) {
  383. rc = -EBUSY;
  384. goto out;
  385. }
  386. /* get user crypto info */
  387. if (tx) {
  388. crypto_info = &ctx->crypto_send.info;
  389. cctx = &ctx->tx;
  390. } else {
  391. crypto_info = &ctx->crypto_recv.info;
  392. cctx = &ctx->rx;
  393. }
  394. if (!TLS_CRYPTO_INFO_READY(crypto_info)) {
  395. rc = -EBUSY;
  396. goto out;
  397. }
  398. if (len == sizeof(*crypto_info)) {
  399. if (copy_to_user(optval, crypto_info, sizeof(*crypto_info)))
  400. rc = -EFAULT;
  401. goto out;
  402. }
  403. cipher_desc = get_cipher_desc(crypto_info->cipher_type);
  404. if (!cipher_desc || len != cipher_desc->crypto_info) {
  405. rc = -EINVAL;
  406. goto out;
  407. }
  408. memcpy(crypto_info_iv(crypto_info, cipher_desc),
  409. cctx->iv + cipher_desc->salt, cipher_desc->iv);
  410. memcpy(crypto_info_rec_seq(crypto_info, cipher_desc),
  411. cctx->rec_seq, cipher_desc->rec_seq);
  412. if (copy_to_user(optval, crypto_info, cipher_desc->crypto_info))
  413. rc = -EFAULT;
  414. out:
  415. return rc;
  416. }
  417. static int do_tls_getsockopt_tx_zc(struct sock *sk, char __user *optval,
  418. int __user *optlen)
  419. {
  420. struct tls_context *ctx = tls_get_ctx(sk);
  421. unsigned int value;
  422. int len;
  423. if (get_user(len, optlen))
  424. return -EFAULT;
  425. if (len != sizeof(value))
  426. return -EINVAL;
  427. value = ctx->zerocopy_sendfile;
  428. if (copy_to_user(optval, &value, sizeof(value)))
  429. return -EFAULT;
  430. return 0;
  431. }
  432. static int do_tls_getsockopt_no_pad(struct sock *sk, char __user *optval,
  433. int __user *optlen)
  434. {
  435. struct tls_context *ctx = tls_get_ctx(sk);
  436. int value, len;
  437. if (ctx->prot_info.version != TLS_1_3_VERSION)
  438. return -EINVAL;
  439. if (get_user(len, optlen))
  440. return -EFAULT;
  441. if (len < sizeof(value))
  442. return -EINVAL;
  443. value = -EINVAL;
  444. if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW)
  445. value = ctx->rx_no_pad;
  446. if (value < 0)
  447. return value;
  448. if (put_user(sizeof(value), optlen))
  449. return -EFAULT;
  450. if (copy_to_user(optval, &value, sizeof(value)))
  451. return -EFAULT;
  452. return 0;
  453. }
  454. static int do_tls_getsockopt_tx_payload_len(struct sock *sk, char __user *optval,
  455. int __user *optlen)
  456. {
  457. struct tls_context *ctx = tls_get_ctx(sk);
  458. u16 payload_len = ctx->tx_max_payload_len;
  459. int len;
  460. if (get_user(len, optlen))
  461. return -EFAULT;
  462. if (len < sizeof(payload_len))
  463. return -EINVAL;
  464. if (put_user(sizeof(payload_len), optlen))
  465. return -EFAULT;
  466. if (copy_to_user(optval, &payload_len, sizeof(payload_len)))
  467. return -EFAULT;
  468. return 0;
  469. }
  470. static int do_tls_getsockopt(struct sock *sk, int optname,
  471. char __user *optval, int __user *optlen)
  472. {
  473. int rc = 0;
  474. lock_sock(sk);
  475. switch (optname) {
  476. case TLS_TX:
  477. case TLS_RX:
  478. rc = do_tls_getsockopt_conf(sk, optval, optlen,
  479. optname == TLS_TX);
  480. break;
  481. case TLS_TX_ZEROCOPY_RO:
  482. rc = do_tls_getsockopt_tx_zc(sk, optval, optlen);
  483. break;
  484. case TLS_RX_EXPECT_NO_PAD:
  485. rc = do_tls_getsockopt_no_pad(sk, optval, optlen);
  486. break;
  487. case TLS_TX_MAX_PAYLOAD_LEN:
  488. rc = do_tls_getsockopt_tx_payload_len(sk, optval, optlen);
  489. break;
  490. default:
  491. rc = -ENOPROTOOPT;
  492. break;
  493. }
  494. release_sock(sk);
  495. return rc;
  496. }
  497. static int tls_getsockopt(struct sock *sk, int level, int optname,
  498. char __user *optval, int __user *optlen)
  499. {
  500. struct tls_context *ctx = tls_get_ctx(sk);
  501. if (level != SOL_TLS)
  502. return ctx->sk_proto->getsockopt(sk, level,
  503. optname, optval, optlen);
  504. return do_tls_getsockopt(sk, optname, optval, optlen);
  505. }
  506. static int validate_crypto_info(const struct tls_crypto_info *crypto_info,
  507. const struct tls_crypto_info *alt_crypto_info)
  508. {
  509. if (crypto_info->version != TLS_1_2_VERSION &&
  510. crypto_info->version != TLS_1_3_VERSION)
  511. return -EINVAL;
  512. switch (crypto_info->cipher_type) {
  513. case TLS_CIPHER_ARIA_GCM_128:
  514. case TLS_CIPHER_ARIA_GCM_256:
  515. if (crypto_info->version != TLS_1_2_VERSION)
  516. return -EINVAL;
  517. break;
  518. }
  519. /* Ensure that TLS version and ciphers are same in both directions */
  520. if (TLS_CRYPTO_INFO_READY(alt_crypto_info)) {
  521. if (alt_crypto_info->version != crypto_info->version ||
  522. alt_crypto_info->cipher_type != crypto_info->cipher_type)
  523. return -EINVAL;
  524. }
  525. return 0;
  526. }
  527. static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval,
  528. unsigned int optlen, int tx)
  529. {
  530. struct tls_crypto_info *crypto_info, *alt_crypto_info;
  531. struct tls_crypto_info *old_crypto_info = NULL;
  532. struct tls_context *ctx = tls_get_ctx(sk);
  533. const struct tls_cipher_desc *cipher_desc;
  534. union tls_crypto_context *crypto_ctx;
  535. union tls_crypto_context tmp = {};
  536. bool update = false;
  537. int rc = 0;
  538. int conf;
  539. if (sockptr_is_null(optval) || (optlen < sizeof(*crypto_info)))
  540. return -EINVAL;
  541. if (tx) {
  542. crypto_ctx = &ctx->crypto_send;
  543. alt_crypto_info = &ctx->crypto_recv.info;
  544. } else {
  545. crypto_ctx = &ctx->crypto_recv;
  546. alt_crypto_info = &ctx->crypto_send.info;
  547. }
  548. crypto_info = &crypto_ctx->info;
  549. if (TLS_CRYPTO_INFO_READY(crypto_info)) {
  550. /* Currently we only support setting crypto info more
  551. * than one time for TLS 1.3
  552. */
  553. if (crypto_info->version != TLS_1_3_VERSION) {
  554. TLS_INC_STATS(sock_net(sk), tx ? LINUX_MIB_TLSTXREKEYERROR
  555. : LINUX_MIB_TLSRXREKEYERROR);
  556. return -EBUSY;
  557. }
  558. update = true;
  559. old_crypto_info = crypto_info;
  560. crypto_info = &tmp.info;
  561. crypto_ctx = &tmp;
  562. }
  563. rc = copy_from_sockptr(crypto_info, optval, sizeof(*crypto_info));
  564. if (rc) {
  565. rc = -EFAULT;
  566. goto err_crypto_info;
  567. }
  568. if (update) {
  569. /* Ensure that TLS version and ciphers are not modified */
  570. if (crypto_info->version != old_crypto_info->version ||
  571. crypto_info->cipher_type != old_crypto_info->cipher_type)
  572. rc = -EINVAL;
  573. } else {
  574. rc = validate_crypto_info(crypto_info, alt_crypto_info);
  575. }
  576. if (rc)
  577. goto err_crypto_info;
  578. cipher_desc = get_cipher_desc(crypto_info->cipher_type);
  579. if (!cipher_desc) {
  580. rc = -EINVAL;
  581. goto err_crypto_info;
  582. }
  583. if (optlen != cipher_desc->crypto_info) {
  584. rc = -EINVAL;
  585. goto err_crypto_info;
  586. }
  587. rc = copy_from_sockptr_offset(crypto_info + 1, optval,
  588. sizeof(*crypto_info),
  589. optlen - sizeof(*crypto_info));
  590. if (rc) {
  591. rc = -EFAULT;
  592. goto err_crypto_info;
  593. }
  594. if (tx) {
  595. rc = tls_set_device_offload(sk);
  596. conf = TLS_HW;
  597. if (!rc) {
  598. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSTXDEVICE);
  599. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXDEVICE);
  600. } else {
  601. rc = tls_set_sw_offload(sk, 1,
  602. update ? crypto_info : NULL);
  603. if (rc)
  604. goto err_crypto_info;
  605. if (update) {
  606. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSTXREKEYOK);
  607. } else {
  608. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSTXSW);
  609. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXSW);
  610. }
  611. conf = TLS_SW;
  612. }
  613. } else {
  614. rc = tls_set_device_offload_rx(sk, ctx);
  615. conf = TLS_HW;
  616. if (!rc) {
  617. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXDEVICE);
  618. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXDEVICE);
  619. } else {
  620. rc = tls_set_sw_offload(sk, 0,
  621. update ? crypto_info : NULL);
  622. if (rc)
  623. goto err_crypto_info;
  624. if (update) {
  625. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXREKEYOK);
  626. } else {
  627. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXSW);
  628. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXSW);
  629. }
  630. conf = TLS_SW;
  631. }
  632. if (!update)
  633. tls_sw_strparser_arm(sk, ctx);
  634. }
  635. if (tx)
  636. ctx->tx_conf = conf;
  637. else
  638. ctx->rx_conf = conf;
  639. update_sk_prot(sk, ctx);
  640. if (update)
  641. return 0;
  642. if (tx) {
  643. ctx->sk_write_space = sk->sk_write_space;
  644. sk->sk_write_space = tls_write_space;
  645. } else {
  646. struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(ctx);
  647. tls_strp_check_rcv(&rx_ctx->strp);
  648. }
  649. return 0;
  650. err_crypto_info:
  651. if (update) {
  652. TLS_INC_STATS(sock_net(sk), tx ? LINUX_MIB_TLSTXREKEYERROR
  653. : LINUX_MIB_TLSRXREKEYERROR);
  654. }
  655. memzero_explicit(crypto_ctx, sizeof(*crypto_ctx));
  656. return rc;
  657. }
  658. static int do_tls_setsockopt_tx_zc(struct sock *sk, sockptr_t optval,
  659. unsigned int optlen)
  660. {
  661. struct tls_context *ctx = tls_get_ctx(sk);
  662. unsigned int value;
  663. if (sockptr_is_null(optval) || optlen != sizeof(value))
  664. return -EINVAL;
  665. if (copy_from_sockptr(&value, optval, sizeof(value)))
  666. return -EFAULT;
  667. if (value > 1)
  668. return -EINVAL;
  669. ctx->zerocopy_sendfile = value;
  670. return 0;
  671. }
  672. static int do_tls_setsockopt_no_pad(struct sock *sk, sockptr_t optval,
  673. unsigned int optlen)
  674. {
  675. struct tls_context *ctx = tls_get_ctx(sk);
  676. u32 val;
  677. int rc;
  678. if (ctx->prot_info.version != TLS_1_3_VERSION ||
  679. sockptr_is_null(optval) || optlen < sizeof(val))
  680. return -EINVAL;
  681. rc = copy_from_sockptr(&val, optval, sizeof(val));
  682. if (rc)
  683. return -EFAULT;
  684. if (val > 1)
  685. return -EINVAL;
  686. rc = check_zeroed_sockptr(optval, sizeof(val), optlen - sizeof(val));
  687. if (rc < 1)
  688. return rc == 0 ? -EINVAL : rc;
  689. lock_sock(sk);
  690. rc = -EINVAL;
  691. if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW) {
  692. ctx->rx_no_pad = val;
  693. tls_update_rx_zc_capable(ctx);
  694. rc = 0;
  695. }
  696. release_sock(sk);
  697. return rc;
  698. }
  699. static int do_tls_setsockopt_tx_payload_len(struct sock *sk, sockptr_t optval,
  700. unsigned int optlen)
  701. {
  702. struct tls_context *ctx = tls_get_ctx(sk);
  703. struct tls_sw_context_tx *sw_ctx = tls_sw_ctx_tx(ctx);
  704. u16 value;
  705. bool tls_13 = ctx->prot_info.version == TLS_1_3_VERSION;
  706. if (sw_ctx && sw_ctx->open_rec)
  707. return -EBUSY;
  708. if (sockptr_is_null(optval) || optlen != sizeof(value))
  709. return -EINVAL;
  710. if (copy_from_sockptr(&value, optval, sizeof(value)))
  711. return -EFAULT;
  712. if (value < TLS_MIN_RECORD_SIZE_LIM - (tls_13 ? 1 : 0) ||
  713. value > TLS_MAX_PAYLOAD_SIZE)
  714. return -EINVAL;
  715. ctx->tx_max_payload_len = value;
  716. return 0;
  717. }
  718. static int do_tls_setsockopt(struct sock *sk, int optname, sockptr_t optval,
  719. unsigned int optlen)
  720. {
  721. int rc = 0;
  722. switch (optname) {
  723. case TLS_TX:
  724. case TLS_RX:
  725. lock_sock(sk);
  726. rc = do_tls_setsockopt_conf(sk, optval, optlen,
  727. optname == TLS_TX);
  728. release_sock(sk);
  729. break;
  730. case TLS_TX_ZEROCOPY_RO:
  731. lock_sock(sk);
  732. rc = do_tls_setsockopt_tx_zc(sk, optval, optlen);
  733. release_sock(sk);
  734. break;
  735. case TLS_RX_EXPECT_NO_PAD:
  736. rc = do_tls_setsockopt_no_pad(sk, optval, optlen);
  737. break;
  738. case TLS_TX_MAX_PAYLOAD_LEN:
  739. lock_sock(sk);
  740. rc = do_tls_setsockopt_tx_payload_len(sk, optval, optlen);
  741. release_sock(sk);
  742. break;
  743. default:
  744. rc = -ENOPROTOOPT;
  745. break;
  746. }
  747. return rc;
  748. }
  749. static int tls_setsockopt(struct sock *sk, int level, int optname,
  750. sockptr_t optval, unsigned int optlen)
  751. {
  752. struct tls_context *ctx = tls_get_ctx(sk);
  753. if (level != SOL_TLS)
  754. return ctx->sk_proto->setsockopt(sk, level, optname, optval,
  755. optlen);
  756. return do_tls_setsockopt(sk, optname, optval, optlen);
  757. }
  758. static int tls_disconnect(struct sock *sk, int flags)
  759. {
  760. return -EOPNOTSUPP;
  761. }
  762. struct tls_context *tls_ctx_create(struct sock *sk)
  763. {
  764. struct inet_connection_sock *icsk = inet_csk(sk);
  765. struct tls_context *ctx;
  766. ctx = kzalloc_obj(*ctx, GFP_ATOMIC);
  767. if (!ctx)
  768. return NULL;
  769. mutex_init(&ctx->tx_lock);
  770. ctx->sk_proto = READ_ONCE(sk->sk_prot);
  771. ctx->sk = sk;
  772. /* Release semantic of rcu_assign_pointer() ensures that
  773. * ctx->sk_proto is visible before changing sk->sk_prot in
  774. * update_sk_prot(), and prevents reading uninitialized value in
  775. * tls_{getsockopt, setsockopt}. Note that we do not need a
  776. * read barrier in tls_{getsockopt,setsockopt} as there is an
  777. * address dependency between sk->sk_proto->{getsockopt,setsockopt}
  778. * and ctx->sk_proto.
  779. */
  780. rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
  781. return ctx;
  782. }
  783. static void build_proto_ops(struct proto_ops ops[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
  784. const struct proto_ops *base)
  785. {
  786. ops[TLS_BASE][TLS_BASE] = *base;
  787. ops[TLS_SW ][TLS_BASE] = ops[TLS_BASE][TLS_BASE];
  788. ops[TLS_SW ][TLS_BASE].splice_eof = tls_sw_splice_eof;
  789. ops[TLS_BASE][TLS_SW ] = ops[TLS_BASE][TLS_BASE];
  790. ops[TLS_BASE][TLS_SW ].splice_read = tls_sw_splice_read;
  791. ops[TLS_BASE][TLS_SW ].poll = tls_sk_poll;
  792. ops[TLS_BASE][TLS_SW ].read_sock = tls_sw_read_sock;
  793. ops[TLS_SW ][TLS_SW ] = ops[TLS_SW ][TLS_BASE];
  794. ops[TLS_SW ][TLS_SW ].splice_read = tls_sw_splice_read;
  795. ops[TLS_SW ][TLS_SW ].poll = tls_sk_poll;
  796. ops[TLS_SW ][TLS_SW ].read_sock = tls_sw_read_sock;
  797. #ifdef CONFIG_TLS_DEVICE
  798. ops[TLS_HW ][TLS_BASE] = ops[TLS_BASE][TLS_BASE];
  799. ops[TLS_HW ][TLS_SW ] = ops[TLS_BASE][TLS_SW ];
  800. ops[TLS_BASE][TLS_HW ] = ops[TLS_BASE][TLS_SW ];
  801. ops[TLS_SW ][TLS_HW ] = ops[TLS_SW ][TLS_SW ];
  802. ops[TLS_HW ][TLS_HW ] = ops[TLS_HW ][TLS_SW ];
  803. #endif
  804. #ifdef CONFIG_TLS_TOE
  805. ops[TLS_HW_RECORD][TLS_HW_RECORD] = *base;
  806. #endif
  807. }
  808. static void tls_build_proto(struct sock *sk)
  809. {
  810. int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
  811. struct proto *prot = READ_ONCE(sk->sk_prot);
  812. /* Build IPv6 TLS whenever the address of tcpv6 _prot changes */
  813. if (ip_ver == TLSV6 &&
  814. unlikely(prot != smp_load_acquire(&saved_tcpv6_prot))) {
  815. mutex_lock(&tcpv6_prot_mutex);
  816. if (likely(prot != saved_tcpv6_prot)) {
  817. build_protos(tls_prots[TLSV6], prot);
  818. build_proto_ops(tls_proto_ops[TLSV6],
  819. sk->sk_socket->ops);
  820. smp_store_release(&saved_tcpv6_prot, prot);
  821. }
  822. mutex_unlock(&tcpv6_prot_mutex);
  823. }
  824. if (ip_ver == TLSV4 &&
  825. unlikely(prot != smp_load_acquire(&saved_tcpv4_prot))) {
  826. mutex_lock(&tcpv4_prot_mutex);
  827. if (likely(prot != saved_tcpv4_prot)) {
  828. build_protos(tls_prots[TLSV4], prot);
  829. build_proto_ops(tls_proto_ops[TLSV4],
  830. sk->sk_socket->ops);
  831. smp_store_release(&saved_tcpv4_prot, prot);
  832. }
  833. mutex_unlock(&tcpv4_prot_mutex);
  834. }
  835. }
  836. static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
  837. const struct proto *base)
  838. {
  839. prot[TLS_BASE][TLS_BASE] = *base;
  840. prot[TLS_BASE][TLS_BASE].setsockopt = tls_setsockopt;
  841. prot[TLS_BASE][TLS_BASE].getsockopt = tls_getsockopt;
  842. prot[TLS_BASE][TLS_BASE].disconnect = tls_disconnect;
  843. prot[TLS_BASE][TLS_BASE].close = tls_sk_proto_close;
  844. prot[TLS_SW][TLS_BASE] = prot[TLS_BASE][TLS_BASE];
  845. prot[TLS_SW][TLS_BASE].sendmsg = tls_sw_sendmsg;
  846. prot[TLS_SW][TLS_BASE].splice_eof = tls_sw_splice_eof;
  847. prot[TLS_BASE][TLS_SW] = prot[TLS_BASE][TLS_BASE];
  848. prot[TLS_BASE][TLS_SW].recvmsg = tls_sw_recvmsg;
  849. prot[TLS_BASE][TLS_SW].sock_is_readable = tls_sw_sock_is_readable;
  850. prot[TLS_BASE][TLS_SW].close = tls_sk_proto_close;
  851. prot[TLS_SW][TLS_SW] = prot[TLS_SW][TLS_BASE];
  852. prot[TLS_SW][TLS_SW].recvmsg = tls_sw_recvmsg;
  853. prot[TLS_SW][TLS_SW].sock_is_readable = tls_sw_sock_is_readable;
  854. prot[TLS_SW][TLS_SW].close = tls_sk_proto_close;
  855. #ifdef CONFIG_TLS_DEVICE
  856. prot[TLS_HW][TLS_BASE] = prot[TLS_BASE][TLS_BASE];
  857. prot[TLS_HW][TLS_BASE].sendmsg = tls_device_sendmsg;
  858. prot[TLS_HW][TLS_BASE].splice_eof = tls_device_splice_eof;
  859. prot[TLS_HW][TLS_SW] = prot[TLS_BASE][TLS_SW];
  860. prot[TLS_HW][TLS_SW].sendmsg = tls_device_sendmsg;
  861. prot[TLS_HW][TLS_SW].splice_eof = tls_device_splice_eof;
  862. prot[TLS_BASE][TLS_HW] = prot[TLS_BASE][TLS_SW];
  863. prot[TLS_SW][TLS_HW] = prot[TLS_SW][TLS_SW];
  864. prot[TLS_HW][TLS_HW] = prot[TLS_HW][TLS_SW];
  865. #endif
  866. #ifdef CONFIG_TLS_TOE
  867. prot[TLS_HW_RECORD][TLS_HW_RECORD] = *base;
  868. prot[TLS_HW_RECORD][TLS_HW_RECORD].hash = tls_toe_hash;
  869. prot[TLS_HW_RECORD][TLS_HW_RECORD].unhash = tls_toe_unhash;
  870. #endif
  871. }
  872. static int tls_init(struct sock *sk)
  873. {
  874. struct tls_context *ctx;
  875. int rc = 0;
  876. tls_build_proto(sk);
  877. #ifdef CONFIG_TLS_TOE
  878. if (tls_toe_bypass(sk))
  879. return 0;
  880. #endif
  881. /* The TLS ulp is currently supported only for TCP sockets
  882. * in ESTABLISHED state.
  883. * Supporting sockets in LISTEN state will require us
  884. * to modify the accept implementation to clone rather then
  885. * share the ulp context.
  886. */
  887. if (sk->sk_state != TCP_ESTABLISHED)
  888. return -ENOTCONN;
  889. /* allocate tls context */
  890. write_lock_bh(&sk->sk_callback_lock);
  891. ctx = tls_ctx_create(sk);
  892. if (!ctx) {
  893. rc = -ENOMEM;
  894. goto out;
  895. }
  896. ctx->tx_conf = TLS_BASE;
  897. ctx->rx_conf = TLS_BASE;
  898. ctx->tx_max_payload_len = TLS_MAX_PAYLOAD_SIZE;
  899. update_sk_prot(sk, ctx);
  900. out:
  901. write_unlock_bh(&sk->sk_callback_lock);
  902. return rc;
  903. }
  904. static void tls_update(struct sock *sk, struct proto *p,
  905. void (*write_space)(struct sock *sk))
  906. {
  907. struct tls_context *ctx;
  908. WARN_ON_ONCE(sk->sk_prot == p);
  909. ctx = tls_get_ctx(sk);
  910. if (likely(ctx)) {
  911. ctx->sk_write_space = write_space;
  912. ctx->sk_proto = p;
  913. } else {
  914. /* Pairs with lockless read in sk_clone_lock(). */
  915. WRITE_ONCE(sk->sk_prot, p);
  916. sk->sk_write_space = write_space;
  917. }
  918. }
  919. static u16 tls_user_config(struct tls_context *ctx, bool tx)
  920. {
  921. u16 config = tx ? ctx->tx_conf : ctx->rx_conf;
  922. switch (config) {
  923. case TLS_BASE:
  924. return TLS_CONF_BASE;
  925. case TLS_SW:
  926. return TLS_CONF_SW;
  927. case TLS_HW:
  928. return TLS_CONF_HW;
  929. case TLS_HW_RECORD:
  930. return TLS_CONF_HW_RECORD;
  931. }
  932. return 0;
  933. }
  934. static int tls_get_info(struct sock *sk, struct sk_buff *skb, bool net_admin)
  935. {
  936. u16 version, cipher_type;
  937. struct tls_context *ctx;
  938. struct nlattr *start;
  939. int err;
  940. start = nla_nest_start_noflag(skb, INET_ULP_INFO_TLS);
  941. if (!start)
  942. return -EMSGSIZE;
  943. rcu_read_lock();
  944. ctx = rcu_dereference(inet_csk(sk)->icsk_ulp_data);
  945. if (!ctx) {
  946. err = 0;
  947. goto nla_failure;
  948. }
  949. version = ctx->prot_info.version;
  950. if (version) {
  951. err = nla_put_u16(skb, TLS_INFO_VERSION, version);
  952. if (err)
  953. goto nla_failure;
  954. }
  955. cipher_type = ctx->prot_info.cipher_type;
  956. if (cipher_type) {
  957. err = nla_put_u16(skb, TLS_INFO_CIPHER, cipher_type);
  958. if (err)
  959. goto nla_failure;
  960. }
  961. err = nla_put_u16(skb, TLS_INFO_TXCONF, tls_user_config(ctx, true));
  962. if (err)
  963. goto nla_failure;
  964. err = nla_put_u16(skb, TLS_INFO_RXCONF, tls_user_config(ctx, false));
  965. if (err)
  966. goto nla_failure;
  967. if (ctx->tx_conf == TLS_HW && ctx->zerocopy_sendfile) {
  968. err = nla_put_flag(skb, TLS_INFO_ZC_RO_TX);
  969. if (err)
  970. goto nla_failure;
  971. }
  972. if (ctx->rx_no_pad) {
  973. err = nla_put_flag(skb, TLS_INFO_RX_NO_PAD);
  974. if (err)
  975. goto nla_failure;
  976. }
  977. err = nla_put_u16(skb, TLS_INFO_TX_MAX_PAYLOAD_LEN,
  978. ctx->tx_max_payload_len);
  979. if (err)
  980. goto nla_failure;
  981. rcu_read_unlock();
  982. nla_nest_end(skb, start);
  983. return 0;
  984. nla_failure:
  985. rcu_read_unlock();
  986. nla_nest_cancel(skb, start);
  987. return err;
  988. }
  989. static size_t tls_get_info_size(const struct sock *sk, bool net_admin)
  990. {
  991. size_t size = 0;
  992. size += nla_total_size(0) + /* INET_ULP_INFO_TLS */
  993. nla_total_size(sizeof(u16)) + /* TLS_INFO_VERSION */
  994. nla_total_size(sizeof(u16)) + /* TLS_INFO_CIPHER */
  995. nla_total_size(sizeof(u16)) + /* TLS_INFO_RXCONF */
  996. nla_total_size(sizeof(u16)) + /* TLS_INFO_TXCONF */
  997. nla_total_size(0) + /* TLS_INFO_ZC_RO_TX */
  998. nla_total_size(0) + /* TLS_INFO_RX_NO_PAD */
  999. nla_total_size(sizeof(u16)) + /* TLS_INFO_TX_MAX_PAYLOAD_LEN */
  1000. 0;
  1001. return size;
  1002. }
  1003. static int __net_init tls_init_net(struct net *net)
  1004. {
  1005. int err;
  1006. net->mib.tls_statistics = alloc_percpu(struct linux_tls_mib);
  1007. if (!net->mib.tls_statistics)
  1008. return -ENOMEM;
  1009. err = tls_proc_init(net);
  1010. if (err)
  1011. goto err_free_stats;
  1012. return 0;
  1013. err_free_stats:
  1014. free_percpu(net->mib.tls_statistics);
  1015. return err;
  1016. }
  1017. static void __net_exit tls_exit_net(struct net *net)
  1018. {
  1019. tls_proc_fini(net);
  1020. free_percpu(net->mib.tls_statistics);
  1021. }
  1022. static struct pernet_operations tls_proc_ops = {
  1023. .init = tls_init_net,
  1024. .exit = tls_exit_net,
  1025. };
  1026. static struct tcp_ulp_ops tcp_tls_ulp_ops __read_mostly = {
  1027. .name = "tls",
  1028. .owner = THIS_MODULE,
  1029. .init = tls_init,
  1030. .update = tls_update,
  1031. .get_info = tls_get_info,
  1032. .get_info_size = tls_get_info_size,
  1033. };
  1034. static int __init tls_register(void)
  1035. {
  1036. int err;
  1037. err = register_pernet_subsys(&tls_proc_ops);
  1038. if (err)
  1039. return err;
  1040. err = tls_strp_dev_init();
  1041. if (err)
  1042. goto err_pernet;
  1043. err = tls_device_init();
  1044. if (err)
  1045. goto err_strp;
  1046. tcp_register_ulp(&tcp_tls_ulp_ops);
  1047. return 0;
  1048. err_strp:
  1049. tls_strp_dev_exit();
  1050. err_pernet:
  1051. unregister_pernet_subsys(&tls_proc_ops);
  1052. return err;
  1053. }
  1054. static void __exit tls_unregister(void)
  1055. {
  1056. tcp_unregister_ulp(&tcp_tls_ulp_ops);
  1057. tls_strp_dev_exit();
  1058. tls_device_cleanup();
  1059. unregister_pernet_subsys(&tls_proc_ops);
  1060. }
  1061. module_init(tls_register);
  1062. module_exit(tls_unregister);