af_rxrpc.c 26 KB

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