tls_main.c 30 KB

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