af_rxrpc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. /* AF_RXRPC implementation
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/net.h>
  15. #include <linux/slab.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/random.h>
  18. #include <linux/poll.h>
  19. #include <linux/proc_fs.h>
  20. #include <linux/key-type.h>
  21. #include <net/net_namespace.h>
  22. #include <net/sock.h>
  23. #include <net/af_rxrpc.h>
  24. #define CREATE_TRACE_POINTS
  25. #include "ar-internal.h"
  26. MODULE_DESCRIPTION("RxRPC network protocol");
  27. MODULE_AUTHOR("Red Hat, Inc.");
  28. MODULE_LICENSE("GPL");
  29. MODULE_ALIAS_NETPROTO(PF_RXRPC);
  30. unsigned int rxrpc_debug; // = RXRPC_DEBUG_KPROTO;
  31. module_param_named(debug, rxrpc_debug, uint, S_IWUSR | S_IRUGO);
  32. MODULE_PARM_DESC(debug, "RxRPC debugging mask");
  33. static struct proto rxrpc_proto;
  34. static const struct proto_ops rxrpc_rpc_ops;
  35. /* current debugging ID */
  36. atomic_t rxrpc_debug_id;
  37. /* count of skbs currently in use */
  38. atomic_t rxrpc_n_tx_skbs, rxrpc_n_rx_skbs;
  39. struct workqueue_struct *rxrpc_workqueue;
  40. static void rxrpc_sock_destructor(struct sock *);
  41. /*
  42. * see if an RxRPC socket is currently writable
  43. */
  44. static inline int rxrpc_writable(struct sock *sk)
  45. {
  46. return refcount_read(&sk->sk_wmem_alloc) < (size_t) sk->sk_sndbuf;
  47. }
  48. /*
  49. * wait for write bufferage to become available
  50. */
  51. static void rxrpc_write_space(struct sock *sk)
  52. {
  53. _enter("%p", sk);
  54. rcu_read_lock();
  55. if (rxrpc_writable(sk)) {
  56. struct socket_wq *wq = rcu_dereference(sk->sk_wq);
  57. if (skwq_has_sleeper(wq))
  58. wake_up_interruptible(&wq->wait);
  59. sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
  60. }
  61. rcu_read_unlock();
  62. }
  63. /*
  64. * validate an RxRPC address
  65. */
  66. static int rxrpc_validate_address(struct rxrpc_sock *rx,
  67. struct sockaddr_rxrpc *srx,
  68. int len)
  69. {
  70. unsigned int tail;
  71. if (len < sizeof(struct sockaddr_rxrpc))
  72. return -EINVAL;
  73. if (srx->srx_family != AF_RXRPC)
  74. return -EAFNOSUPPORT;
  75. if (srx->transport_type != SOCK_DGRAM)
  76. return -ESOCKTNOSUPPORT;
  77. len -= offsetof(struct sockaddr_rxrpc, transport);
  78. if (srx->transport_len < sizeof(sa_family_t) ||
  79. srx->transport_len > len)
  80. return -EINVAL;
  81. if (srx->transport.family != rx->family)
  82. return -EAFNOSUPPORT;
  83. switch (srx->transport.family) {
  84. case AF_INET:
  85. if (srx->transport_len < sizeof(struct sockaddr_in))
  86. return -EINVAL;
  87. tail = offsetof(struct sockaddr_rxrpc, transport.sin.__pad);
  88. break;
  89. #ifdef CONFIG_AF_RXRPC_IPV6
  90. case AF_INET6:
  91. if (srx->transport_len < sizeof(struct sockaddr_in6))
  92. return -EINVAL;
  93. tail = offsetof(struct sockaddr_rxrpc, transport) +
  94. sizeof(struct sockaddr_in6);
  95. break;
  96. #endif
  97. default:
  98. return -EAFNOSUPPORT;
  99. }
  100. if (tail < len)
  101. memset((void *)srx + tail, 0, len - tail);
  102. _debug("INET: %pISp", &srx->transport);
  103. return 0;
  104. }
  105. /*
  106. * bind a local address to an RxRPC socket
  107. */
  108. static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
  109. {
  110. struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)saddr;
  111. struct rxrpc_local *local;
  112. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  113. u16 service_id = srx->srx_service;
  114. int ret;
  115. _enter("%p,%p,%d", rx, saddr, len);
  116. ret = rxrpc_validate_address(rx, srx, len);
  117. if (ret < 0)
  118. goto error;
  119. lock_sock(&rx->sk);
  120. switch (rx->sk.sk_state) {
  121. case RXRPC_UNBOUND:
  122. rx->srx = *srx;
  123. local = rxrpc_lookup_local(sock_net(&rx->sk), &rx->srx);
  124. if (IS_ERR(local)) {
  125. ret = PTR_ERR(local);
  126. goto error_unlock;
  127. }
  128. if (service_id) {
  129. write_lock(&local->services_lock);
  130. if (rcu_access_pointer(local->service))
  131. goto service_in_use;
  132. rx->local = local;
  133. rcu_assign_pointer(local->service, rx);
  134. write_unlock(&local->services_lock);
  135. rx->sk.sk_state = RXRPC_SERVER_BOUND;
  136. } else {
  137. rx->local = local;
  138. rx->sk.sk_state = RXRPC_CLIENT_BOUND;
  139. }
  140. break;
  141. case RXRPC_SERVER_BOUND:
  142. ret = -EINVAL;
  143. if (service_id == 0)
  144. goto error_unlock;
  145. ret = -EADDRINUSE;
  146. if (service_id == rx->srx.srx_service)
  147. goto error_unlock;
  148. ret = -EINVAL;
  149. srx->srx_service = rx->srx.srx_service;
  150. if (memcmp(srx, &rx->srx, sizeof(*srx)) != 0)
  151. goto error_unlock;
  152. rx->second_service = service_id;
  153. rx->sk.sk_state = RXRPC_SERVER_BOUND2;
  154. break;
  155. default:
  156. ret = -EINVAL;
  157. goto error_unlock;
  158. }
  159. release_sock(&rx->sk);
  160. _leave(" = 0");
  161. return 0;
  162. service_in_use:
  163. write_unlock(&local->services_lock);
  164. rxrpc_put_local(local);
  165. ret = -EADDRINUSE;
  166. error_unlock:
  167. release_sock(&rx->sk);
  168. error:
  169. _leave(" = %d", ret);
  170. return ret;
  171. }
  172. /*
  173. * set the number of pending calls permitted on a listening socket
  174. */
  175. static int rxrpc_listen(struct socket *sock, int backlog)
  176. {
  177. struct sock *sk = sock->sk;
  178. struct rxrpc_sock *rx = rxrpc_sk(sk);
  179. unsigned int max, old;
  180. int ret;
  181. _enter("%p,%d", rx, backlog);
  182. lock_sock(&rx->sk);
  183. switch (rx->sk.sk_state) {
  184. case RXRPC_UNBOUND:
  185. ret = -EADDRNOTAVAIL;
  186. break;
  187. case RXRPC_SERVER_BOUND:
  188. case RXRPC_SERVER_BOUND2:
  189. ASSERT(rx->local != NULL);
  190. max = READ_ONCE(rxrpc_max_backlog);
  191. ret = -EINVAL;
  192. if (backlog == INT_MAX)
  193. backlog = max;
  194. else if (backlog < 0 || backlog > max)
  195. break;
  196. old = sk->sk_max_ack_backlog;
  197. sk->sk_max_ack_backlog = backlog;
  198. ret = rxrpc_service_prealloc(rx, GFP_KERNEL);
  199. if (ret == 0)
  200. rx->sk.sk_state = RXRPC_SERVER_LISTENING;
  201. else
  202. sk->sk_max_ack_backlog = old;
  203. break;
  204. case RXRPC_SERVER_LISTENING:
  205. if (backlog == 0) {
  206. rx->sk.sk_state = RXRPC_SERVER_LISTEN_DISABLED;
  207. sk->sk_max_ack_backlog = 0;
  208. rxrpc_discard_prealloc(rx);
  209. ret = 0;
  210. break;
  211. }
  212. default:
  213. ret = -EBUSY;
  214. break;
  215. }
  216. release_sock(&rx->sk);
  217. _leave(" = %d", ret);
  218. return ret;
  219. }
  220. /**
  221. * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
  222. * @sock: The socket on which to make the call
  223. * @srx: The address of the peer to contact
  224. * @key: The security context to use (defaults to socket setting)
  225. * @user_call_ID: The ID to use
  226. * @tx_total_len: Total length of data to transmit during the call (or -1)
  227. * @gfp: The allocation constraints
  228. * @notify_rx: Where to send notifications instead of socket queue
  229. *
  230. * Allow a kernel service to begin a call on the nominated socket. This just
  231. * sets up all the internal tracking structures and allocates connection and
  232. * call IDs as appropriate. The call to be used is returned.
  233. *
  234. * The default socket destination address and security may be overridden by
  235. * supplying @srx and @key.
  236. */
  237. struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock,
  238. struct sockaddr_rxrpc *srx,
  239. struct key *key,
  240. unsigned long user_call_ID,
  241. s64 tx_total_len,
  242. gfp_t gfp,
  243. rxrpc_notify_rx_t notify_rx)
  244. {
  245. struct rxrpc_conn_parameters cp;
  246. struct rxrpc_call *call;
  247. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  248. int ret;
  249. _enter(",,%x,%lx", key_serial(key), user_call_ID);
  250. ret = rxrpc_validate_address(rx, srx, sizeof(*srx));
  251. if (ret < 0)
  252. return ERR_PTR(ret);
  253. lock_sock(&rx->sk);
  254. if (!key)
  255. key = rx->key;
  256. if (key && !key->payload.data[0])
  257. key = NULL; /* a no-security key */
  258. memset(&cp, 0, sizeof(cp));
  259. cp.local = rx->local;
  260. cp.key = key;
  261. cp.security_level = 0;
  262. cp.exclusive = false;
  263. cp.service_id = srx->srx_service;
  264. call = rxrpc_new_client_call(rx, &cp, srx, user_call_ID, tx_total_len,
  265. gfp);
  266. /* The socket has been unlocked. */
  267. if (!IS_ERR(call)) {
  268. call->notify_rx = notify_rx;
  269. mutex_unlock(&call->user_mutex);
  270. }
  271. _leave(" = %p", call);
  272. return call;
  273. }
  274. EXPORT_SYMBOL(rxrpc_kernel_begin_call);
  275. /**
  276. * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
  277. * @sock: The socket the call is on
  278. * @call: The call to end
  279. *
  280. * Allow a kernel service to end a call it was using. The call must be
  281. * complete before this is called (the call should be aborted if necessary).
  282. */
  283. void rxrpc_kernel_end_call(struct socket *sock, struct rxrpc_call *call)
  284. {
  285. _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
  286. mutex_lock(&call->user_mutex);
  287. rxrpc_release_call(rxrpc_sk(sock->sk), call);
  288. mutex_unlock(&call->user_mutex);
  289. rxrpc_put_call(call, rxrpc_call_put_kernel);
  290. }
  291. EXPORT_SYMBOL(rxrpc_kernel_end_call);
  292. /**
  293. * rxrpc_kernel_check_call - Check a call's state
  294. * @sock: The socket the call is on
  295. * @call: The call to check
  296. * @_compl: Where to store the completion state
  297. * @_abort_code: Where to store any abort code
  298. *
  299. * Allow a kernel service to query the state of a call and find out the manner
  300. * of its termination if it has completed. Returns -EINPROGRESS if the call is
  301. * still going, 0 if the call finished successfully, -ECONNABORTED if the call
  302. * was aborted and an appropriate error if the call failed in some other way.
  303. */
  304. int rxrpc_kernel_check_call(struct socket *sock, struct rxrpc_call *call,
  305. enum rxrpc_call_completion *_compl, u32 *_abort_code)
  306. {
  307. if (call->state != RXRPC_CALL_COMPLETE)
  308. return -EINPROGRESS;
  309. smp_rmb();
  310. *_compl = call->completion;
  311. *_abort_code = call->abort_code;
  312. return call->error;
  313. }
  314. EXPORT_SYMBOL(rxrpc_kernel_check_call);
  315. /**
  316. * rxrpc_kernel_retry_call - Allow a kernel service to retry a call
  317. * @sock: The socket the call is on
  318. * @call: The call to retry
  319. * @srx: The address of the peer to contact
  320. * @key: The security context to use (defaults to socket setting)
  321. *
  322. * Allow a kernel service to try resending a client call that failed due to a
  323. * network error to a new address. The Tx queue is maintained intact, thereby
  324. * relieving the need to re-encrypt any request data that has already been
  325. * buffered.
  326. */
  327. int rxrpc_kernel_retry_call(struct socket *sock, struct rxrpc_call *call,
  328. struct sockaddr_rxrpc *srx, struct key *key)
  329. {
  330. struct rxrpc_conn_parameters cp;
  331. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  332. int ret;
  333. _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
  334. if (!key)
  335. key = rx->key;
  336. if (key && !key->payload.data[0])
  337. key = NULL; /* a no-security key */
  338. memset(&cp, 0, sizeof(cp));
  339. cp.local = rx->local;
  340. cp.key = key;
  341. cp.security_level = 0;
  342. cp.exclusive = false;
  343. cp.service_id = srx->srx_service;
  344. mutex_lock(&call->user_mutex);
  345. ret = rxrpc_prepare_call_for_retry(rx, call);
  346. if (ret == 0)
  347. ret = rxrpc_retry_client_call(rx, call, &cp, srx, GFP_KERNEL);
  348. mutex_unlock(&call->user_mutex);
  349. _leave(" = %d", ret);
  350. return ret;
  351. }
  352. EXPORT_SYMBOL(rxrpc_kernel_retry_call);
  353. /**
  354. * rxrpc_kernel_new_call_notification - Get notifications of new calls
  355. * @sock: The socket to intercept received messages on
  356. * @notify_new_call: Function to be called when new calls appear
  357. * @discard_new_call: Function to discard preallocated calls
  358. *
  359. * Allow a kernel service to be given notifications about new calls.
  360. */
  361. void rxrpc_kernel_new_call_notification(
  362. struct socket *sock,
  363. rxrpc_notify_new_call_t notify_new_call,
  364. rxrpc_discard_new_call_t discard_new_call)
  365. {
  366. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  367. rx->notify_new_call = notify_new_call;
  368. rx->discard_new_call = discard_new_call;
  369. }
  370. EXPORT_SYMBOL(rxrpc_kernel_new_call_notification);
  371. /*
  372. * connect an RxRPC socket
  373. * - this just targets it at a specific destination; no actual connection
  374. * negotiation takes place
  375. */
  376. static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
  377. int addr_len, int flags)
  378. {
  379. struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)addr;
  380. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  381. int ret;
  382. _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
  383. ret = rxrpc_validate_address(rx, srx, addr_len);
  384. if (ret < 0) {
  385. _leave(" = %d [bad addr]", ret);
  386. return ret;
  387. }
  388. lock_sock(&rx->sk);
  389. ret = -EISCONN;
  390. if (test_bit(RXRPC_SOCK_CONNECTED, &rx->flags))
  391. goto error;
  392. switch (rx->sk.sk_state) {
  393. case RXRPC_UNBOUND:
  394. rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
  395. case RXRPC_CLIENT_UNBOUND:
  396. case RXRPC_CLIENT_BOUND:
  397. break;
  398. default:
  399. ret = -EBUSY;
  400. goto error;
  401. }
  402. rx->connect_srx = *srx;
  403. set_bit(RXRPC_SOCK_CONNECTED, &rx->flags);
  404. ret = 0;
  405. error:
  406. release_sock(&rx->sk);
  407. return ret;
  408. }
  409. /*
  410. * send a message through an RxRPC socket
  411. * - in a client this does a number of things:
  412. * - finds/sets up a connection for the security specified (if any)
  413. * - initiates a call (ID in control data)
  414. * - ends the request phase of a call (if MSG_MORE is not set)
  415. * - sends a call data packet
  416. * - may send an abort (abort code in control data)
  417. */
  418. static int rxrpc_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
  419. {
  420. struct rxrpc_local *local;
  421. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  422. int ret;
  423. _enter(",{%d},,%zu", rx->sk.sk_state, len);
  424. if (m->msg_flags & MSG_OOB)
  425. return -EOPNOTSUPP;
  426. if (m->msg_name) {
  427. ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen);
  428. if (ret < 0) {
  429. _leave(" = %d [bad addr]", ret);
  430. return ret;
  431. }
  432. }
  433. lock_sock(&rx->sk);
  434. switch (rx->sk.sk_state) {
  435. case RXRPC_UNBOUND:
  436. rx->srx.srx_family = AF_RXRPC;
  437. rx->srx.srx_service = 0;
  438. rx->srx.transport_type = SOCK_DGRAM;
  439. rx->srx.transport.family = rx->family;
  440. switch (rx->family) {
  441. case AF_INET:
  442. rx->srx.transport_len = sizeof(struct sockaddr_in);
  443. break;
  444. #ifdef CONFIG_AF_RXRPC_IPV6
  445. case AF_INET6:
  446. rx->srx.transport_len = sizeof(struct sockaddr_in6);
  447. break;
  448. #endif
  449. default:
  450. ret = -EAFNOSUPPORT;
  451. goto error_unlock;
  452. }
  453. local = rxrpc_lookup_local(sock_net(sock->sk), &rx->srx);
  454. if (IS_ERR(local)) {
  455. ret = PTR_ERR(local);
  456. goto error_unlock;
  457. }
  458. rx->local = local;
  459. rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
  460. /* Fall through */
  461. case RXRPC_CLIENT_UNBOUND:
  462. case RXRPC_CLIENT_BOUND:
  463. if (!m->msg_name &&
  464. test_bit(RXRPC_SOCK_CONNECTED, &rx->flags)) {
  465. m->msg_name = &rx->connect_srx;
  466. m->msg_namelen = sizeof(rx->connect_srx);
  467. }
  468. case RXRPC_SERVER_BOUND:
  469. case RXRPC_SERVER_LISTENING:
  470. ret = rxrpc_do_sendmsg(rx, m, len);
  471. /* The socket has been unlocked */
  472. goto out;
  473. default:
  474. ret = -EINVAL;
  475. goto error_unlock;
  476. }
  477. error_unlock:
  478. release_sock(&rx->sk);
  479. out:
  480. _leave(" = %d", ret);
  481. return ret;
  482. }
  483. /*
  484. * set RxRPC socket options
  485. */
  486. static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
  487. char __user *optval, unsigned int optlen)
  488. {
  489. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  490. unsigned int min_sec_level;
  491. u16 service_upgrade[2];
  492. int ret;
  493. _enter(",%d,%d,,%d", level, optname, optlen);
  494. lock_sock(&rx->sk);
  495. ret = -EOPNOTSUPP;
  496. if (level == SOL_RXRPC) {
  497. switch (optname) {
  498. case RXRPC_EXCLUSIVE_CONNECTION:
  499. ret = -EINVAL;
  500. if (optlen != 0)
  501. goto error;
  502. ret = -EISCONN;
  503. if (rx->sk.sk_state != RXRPC_UNBOUND)
  504. goto error;
  505. rx->exclusive = true;
  506. goto success;
  507. case RXRPC_SECURITY_KEY:
  508. ret = -EINVAL;
  509. if (rx->key)
  510. goto error;
  511. ret = -EISCONN;
  512. if (rx->sk.sk_state != RXRPC_UNBOUND)
  513. goto error;
  514. ret = rxrpc_request_key(rx, optval, optlen);
  515. goto error;
  516. case RXRPC_SECURITY_KEYRING:
  517. ret = -EINVAL;
  518. if (rx->key)
  519. goto error;
  520. ret = -EISCONN;
  521. if (rx->sk.sk_state != RXRPC_UNBOUND)
  522. goto error;
  523. ret = rxrpc_server_keyring(rx, optval, optlen);
  524. goto error;
  525. case RXRPC_MIN_SECURITY_LEVEL:
  526. ret = -EINVAL;
  527. if (optlen != sizeof(unsigned int))
  528. goto error;
  529. ret = -EISCONN;
  530. if (rx->sk.sk_state != RXRPC_UNBOUND)
  531. goto error;
  532. ret = get_user(min_sec_level,
  533. (unsigned int __user *) optval);
  534. if (ret < 0)
  535. goto error;
  536. ret = -EINVAL;
  537. if (min_sec_level > RXRPC_SECURITY_MAX)
  538. goto error;
  539. rx->min_sec_level = min_sec_level;
  540. goto success;
  541. case RXRPC_UPGRADEABLE_SERVICE:
  542. ret = -EINVAL;
  543. if (optlen != sizeof(service_upgrade) ||
  544. rx->service_upgrade.from != 0)
  545. goto error;
  546. ret = -EISCONN;
  547. if (rx->sk.sk_state != RXRPC_SERVER_BOUND2)
  548. goto error;
  549. ret = -EFAULT;
  550. if (copy_from_user(service_upgrade, optval,
  551. sizeof(service_upgrade)) != 0)
  552. goto error;
  553. ret = -EINVAL;
  554. if ((service_upgrade[0] != rx->srx.srx_service ||
  555. service_upgrade[1] != rx->second_service) &&
  556. (service_upgrade[0] != rx->second_service ||
  557. service_upgrade[1] != rx->srx.srx_service))
  558. goto error;
  559. rx->service_upgrade.from = service_upgrade[0];
  560. rx->service_upgrade.to = service_upgrade[1];
  561. goto success;
  562. default:
  563. break;
  564. }
  565. }
  566. success:
  567. ret = 0;
  568. error:
  569. release_sock(&rx->sk);
  570. return ret;
  571. }
  572. /*
  573. * Get socket options.
  574. */
  575. static int rxrpc_getsockopt(struct socket *sock, int level, int optname,
  576. char __user *optval, int __user *_optlen)
  577. {
  578. int optlen;
  579. if (level != SOL_RXRPC)
  580. return -EOPNOTSUPP;
  581. if (get_user(optlen, _optlen))
  582. return -EFAULT;
  583. switch (optname) {
  584. case RXRPC_SUPPORTED_CMSG:
  585. if (optlen < sizeof(int))
  586. return -ETOOSMALL;
  587. if (put_user(RXRPC__SUPPORTED - 1, (int __user *)optval) ||
  588. put_user(sizeof(int), _optlen))
  589. return -EFAULT;
  590. return 0;
  591. default:
  592. return -EOPNOTSUPP;
  593. }
  594. }
  595. /*
  596. * permit an RxRPC socket to be polled
  597. */
  598. static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
  599. poll_table *wait)
  600. {
  601. struct sock *sk = sock->sk;
  602. struct rxrpc_sock *rx = rxrpc_sk(sk);
  603. unsigned int mask;
  604. sock_poll_wait(file, sk_sleep(sk), wait);
  605. mask = 0;
  606. /* the socket is readable if there are any messages waiting on the Rx
  607. * queue */
  608. if (!list_empty(&rx->recvmsg_q))
  609. mask |= POLLIN | POLLRDNORM;
  610. /* the socket is writable if there is space to add new data to the
  611. * socket; there is no guarantee that any particular call in progress
  612. * on the socket may have space in the Tx ACK window */
  613. if (rxrpc_writable(sk))
  614. mask |= POLLOUT | POLLWRNORM;
  615. return mask;
  616. }
  617. /*
  618. * create an RxRPC socket
  619. */
  620. static int rxrpc_create(struct net *net, struct socket *sock, int protocol,
  621. int kern)
  622. {
  623. struct rxrpc_sock *rx;
  624. struct sock *sk;
  625. _enter("%p,%d", sock, protocol);
  626. /* we support transport protocol UDP/UDP6 only */
  627. if (protocol != PF_INET &&
  628. IS_ENABLED(CONFIG_AF_RXRPC_IPV6) && protocol != PF_INET6)
  629. return -EPROTONOSUPPORT;
  630. if (sock->type != SOCK_DGRAM)
  631. return -ESOCKTNOSUPPORT;
  632. sock->ops = &rxrpc_rpc_ops;
  633. sock->state = SS_UNCONNECTED;
  634. sk = sk_alloc(net, PF_RXRPC, GFP_KERNEL, &rxrpc_proto, kern);
  635. if (!sk)
  636. return -ENOMEM;
  637. sock_init_data(sock, sk);
  638. sock_set_flag(sk, SOCK_RCU_FREE);
  639. sk->sk_state = RXRPC_UNBOUND;
  640. sk->sk_write_space = rxrpc_write_space;
  641. sk->sk_max_ack_backlog = 0;
  642. sk->sk_destruct = rxrpc_sock_destructor;
  643. rx = rxrpc_sk(sk);
  644. rx->family = protocol;
  645. rx->calls = RB_ROOT;
  646. spin_lock_init(&rx->incoming_lock);
  647. INIT_LIST_HEAD(&rx->sock_calls);
  648. INIT_LIST_HEAD(&rx->to_be_accepted);
  649. INIT_LIST_HEAD(&rx->recvmsg_q);
  650. rwlock_init(&rx->recvmsg_lock);
  651. rwlock_init(&rx->call_lock);
  652. memset(&rx->srx, 0, sizeof(rx->srx));
  653. _leave(" = 0 [%p]", rx);
  654. return 0;
  655. }
  656. /*
  657. * Kill all the calls on a socket and shut it down.
  658. */
  659. static int rxrpc_shutdown(struct socket *sock, int flags)
  660. {
  661. struct sock *sk = sock->sk;
  662. struct rxrpc_sock *rx = rxrpc_sk(sk);
  663. int ret = 0;
  664. _enter("%p,%d", sk, flags);
  665. if (flags != SHUT_RDWR)
  666. return -EOPNOTSUPP;
  667. if (sk->sk_state == RXRPC_CLOSE)
  668. return -ESHUTDOWN;
  669. lock_sock(sk);
  670. spin_lock_bh(&sk->sk_receive_queue.lock);
  671. if (sk->sk_state < RXRPC_CLOSE) {
  672. sk->sk_state = RXRPC_CLOSE;
  673. sk->sk_shutdown = SHUTDOWN_MASK;
  674. } else {
  675. ret = -ESHUTDOWN;
  676. }
  677. spin_unlock_bh(&sk->sk_receive_queue.lock);
  678. rxrpc_discard_prealloc(rx);
  679. release_sock(sk);
  680. return ret;
  681. }
  682. /*
  683. * RxRPC socket destructor
  684. */
  685. static void rxrpc_sock_destructor(struct sock *sk)
  686. {
  687. _enter("%p", sk);
  688. rxrpc_purge_queue(&sk->sk_receive_queue);
  689. WARN_ON(refcount_read(&sk->sk_wmem_alloc));
  690. WARN_ON(!sk_unhashed(sk));
  691. WARN_ON(sk->sk_socket);
  692. if (!sock_flag(sk, SOCK_DEAD)) {
  693. printk("Attempt to release alive rxrpc socket: %p\n", sk);
  694. return;
  695. }
  696. }
  697. /*
  698. * release an RxRPC socket
  699. */
  700. static int rxrpc_release_sock(struct sock *sk)
  701. {
  702. struct rxrpc_sock *rx = rxrpc_sk(sk);
  703. _enter("%p{%d,%d}", sk, sk->sk_state, refcount_read(&sk->sk_refcnt));
  704. /* declare the socket closed for business */
  705. sock_orphan(sk);
  706. sk->sk_shutdown = SHUTDOWN_MASK;
  707. spin_lock_bh(&sk->sk_receive_queue.lock);
  708. sk->sk_state = RXRPC_CLOSE;
  709. spin_unlock_bh(&sk->sk_receive_queue.lock);
  710. if (rx->local && rcu_access_pointer(rx->local->service) == rx) {
  711. write_lock(&rx->local->services_lock);
  712. rcu_assign_pointer(rx->local->service, NULL);
  713. write_unlock(&rx->local->services_lock);
  714. }
  715. /* try to flush out this socket */
  716. rxrpc_discard_prealloc(rx);
  717. rxrpc_release_calls_on_socket(rx);
  718. flush_workqueue(rxrpc_workqueue);
  719. rxrpc_purge_queue(&sk->sk_receive_queue);
  720. rxrpc_put_local(rx->local);
  721. rx->local = NULL;
  722. key_put(rx->key);
  723. rx->key = NULL;
  724. key_put(rx->securities);
  725. rx->securities = NULL;
  726. sock_put(sk);
  727. _leave(" = 0");
  728. return 0;
  729. }
  730. /*
  731. * release an RxRPC BSD socket on close() or equivalent
  732. */
  733. static int rxrpc_release(struct socket *sock)
  734. {
  735. struct sock *sk = sock->sk;
  736. _enter("%p{%p}", sock, sk);
  737. if (!sk)
  738. return 0;
  739. sock->sk = NULL;
  740. return rxrpc_release_sock(sk);
  741. }
  742. /*
  743. * RxRPC network protocol
  744. */
  745. static const struct proto_ops rxrpc_rpc_ops = {
  746. .family = PF_RXRPC,
  747. .owner = THIS_MODULE,
  748. .release = rxrpc_release,
  749. .bind = rxrpc_bind,
  750. .connect = rxrpc_connect,
  751. .socketpair = sock_no_socketpair,
  752. .accept = sock_no_accept,
  753. .getname = sock_no_getname,
  754. .poll = rxrpc_poll,
  755. .ioctl = sock_no_ioctl,
  756. .listen = rxrpc_listen,
  757. .shutdown = rxrpc_shutdown,
  758. .setsockopt = rxrpc_setsockopt,
  759. .getsockopt = rxrpc_getsockopt,
  760. .sendmsg = rxrpc_sendmsg,
  761. .recvmsg = rxrpc_recvmsg,
  762. .mmap = sock_no_mmap,
  763. .sendpage = sock_no_sendpage,
  764. };
  765. static struct proto rxrpc_proto = {
  766. .name = "RXRPC",
  767. .owner = THIS_MODULE,
  768. .obj_size = sizeof(struct rxrpc_sock),
  769. .max_header = sizeof(struct rxrpc_wire_header),
  770. };
  771. static const struct net_proto_family rxrpc_family_ops = {
  772. .family = PF_RXRPC,
  773. .create = rxrpc_create,
  774. .owner = THIS_MODULE,
  775. };
  776. /*
  777. * initialise and register the RxRPC protocol
  778. */
  779. static int __init af_rxrpc_init(void)
  780. {
  781. int ret = -1;
  782. unsigned int tmp;
  783. BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > FIELD_SIZEOF(struct sk_buff, cb));
  784. get_random_bytes(&tmp, sizeof(tmp));
  785. tmp &= 0x3fffffff;
  786. if (tmp == 0)
  787. tmp = 1;
  788. idr_set_cursor(&rxrpc_client_conn_ids, tmp);
  789. ret = -ENOMEM;
  790. rxrpc_call_jar = kmem_cache_create(
  791. "rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
  792. SLAB_HWCACHE_ALIGN, NULL);
  793. if (!rxrpc_call_jar) {
  794. pr_notice("Failed to allocate call jar\n");
  795. goto error_call_jar;
  796. }
  797. rxrpc_workqueue = alloc_workqueue("krxrpcd", 0, 1);
  798. if (!rxrpc_workqueue) {
  799. pr_notice("Failed to allocate work queue\n");
  800. goto error_work_queue;
  801. }
  802. ret = rxrpc_init_security();
  803. if (ret < 0) {
  804. pr_crit("Cannot initialise security\n");
  805. goto error_security;
  806. }
  807. ret = register_pernet_subsys(&rxrpc_net_ops);
  808. if (ret)
  809. goto error_pernet;
  810. ret = proto_register(&rxrpc_proto, 1);
  811. if (ret < 0) {
  812. pr_crit("Cannot register protocol\n");
  813. goto error_proto;
  814. }
  815. ret = sock_register(&rxrpc_family_ops);
  816. if (ret < 0) {
  817. pr_crit("Cannot register socket family\n");
  818. goto error_sock;
  819. }
  820. ret = register_key_type(&key_type_rxrpc);
  821. if (ret < 0) {
  822. pr_crit("Cannot register client key type\n");
  823. goto error_key_type;
  824. }
  825. ret = register_key_type(&key_type_rxrpc_s);
  826. if (ret < 0) {
  827. pr_crit("Cannot register server key type\n");
  828. goto error_key_type_s;
  829. }
  830. ret = rxrpc_sysctl_init();
  831. if (ret < 0) {
  832. pr_crit("Cannot register sysctls\n");
  833. goto error_sysctls;
  834. }
  835. return 0;
  836. error_sysctls:
  837. unregister_key_type(&key_type_rxrpc_s);
  838. error_key_type_s:
  839. unregister_key_type(&key_type_rxrpc);
  840. error_key_type:
  841. sock_unregister(PF_RXRPC);
  842. error_sock:
  843. proto_unregister(&rxrpc_proto);
  844. error_proto:
  845. unregister_pernet_subsys(&rxrpc_net_ops);
  846. error_pernet:
  847. rxrpc_exit_security();
  848. error_security:
  849. destroy_workqueue(rxrpc_workqueue);
  850. error_work_queue:
  851. kmem_cache_destroy(rxrpc_call_jar);
  852. error_call_jar:
  853. return ret;
  854. }
  855. /*
  856. * unregister the RxRPC protocol
  857. */
  858. static void __exit af_rxrpc_exit(void)
  859. {
  860. _enter("");
  861. rxrpc_sysctl_exit();
  862. unregister_key_type(&key_type_rxrpc_s);
  863. unregister_key_type(&key_type_rxrpc);
  864. sock_unregister(PF_RXRPC);
  865. proto_unregister(&rxrpc_proto);
  866. unregister_pernet_subsys(&rxrpc_net_ops);
  867. ASSERTCMP(atomic_read(&rxrpc_n_tx_skbs), ==, 0);
  868. ASSERTCMP(atomic_read(&rxrpc_n_rx_skbs), ==, 0);
  869. /* Make sure the local and peer records pinned by any dying connections
  870. * are released.
  871. */
  872. rcu_barrier();
  873. rxrpc_destroy_client_conn_ids();
  874. destroy_workqueue(rxrpc_workqueue);
  875. rxrpc_exit_security();
  876. kmem_cache_destroy(rxrpc_call_jar);
  877. _leave("");
  878. }
  879. module_init(af_rxrpc_init);
  880. module_exit(af_rxrpc_exit);