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