af_rxrpc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. /* local epoch for detecting local-end reset */
  36. u32 rxrpc_epoch;
  37. /* current debugging ID */
  38. atomic_t rxrpc_debug_id;
  39. /* count of skbs currently in use */
  40. atomic_t rxrpc_n_tx_skbs, rxrpc_n_rx_skbs;
  41. struct workqueue_struct *rxrpc_workqueue;
  42. static void rxrpc_sock_destructor(struct sock *);
  43. /*
  44. * see if an RxRPC socket is currently writable
  45. */
  46. static inline int rxrpc_writable(struct sock *sk)
  47. {
  48. return atomic_read(&sk->sk_wmem_alloc) < (size_t) sk->sk_sndbuf;
  49. }
  50. /*
  51. * wait for write bufferage to become available
  52. */
  53. static void rxrpc_write_space(struct sock *sk)
  54. {
  55. _enter("%p", sk);
  56. rcu_read_lock();
  57. if (rxrpc_writable(sk)) {
  58. struct socket_wq *wq = rcu_dereference(sk->sk_wq);
  59. if (skwq_has_sleeper(wq))
  60. wake_up_interruptible(&wq->wait);
  61. sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
  62. }
  63. rcu_read_unlock();
  64. }
  65. /*
  66. * validate an RxRPC address
  67. */
  68. static int rxrpc_validate_address(struct rxrpc_sock *rx,
  69. struct sockaddr_rxrpc *srx,
  70. int len)
  71. {
  72. unsigned int tail;
  73. if (len < sizeof(struct sockaddr_rxrpc))
  74. return -EINVAL;
  75. if (srx->srx_family != AF_RXRPC)
  76. return -EAFNOSUPPORT;
  77. if (srx->transport_type != SOCK_DGRAM)
  78. return -ESOCKTNOSUPPORT;
  79. len -= offsetof(struct sockaddr_rxrpc, transport);
  80. if (srx->transport_len < sizeof(sa_family_t) ||
  81. srx->transport_len > len)
  82. return -EINVAL;
  83. if (srx->transport.family != rx->family)
  84. return -EAFNOSUPPORT;
  85. switch (srx->transport.family) {
  86. case AF_INET:
  87. if (srx->transport_len < sizeof(struct sockaddr_in))
  88. return -EINVAL;
  89. tail = offsetof(struct sockaddr_rxrpc, transport.sin.__pad);
  90. break;
  91. #ifdef CONFIG_AF_RXRPC_IPV6
  92. case AF_INET6:
  93. if (srx->transport_len < sizeof(struct sockaddr_in6))
  94. return -EINVAL;
  95. tail = offsetof(struct sockaddr_rxrpc, transport) +
  96. sizeof(struct sockaddr_in6);
  97. break;
  98. #endif
  99. default:
  100. return -EAFNOSUPPORT;
  101. }
  102. if (tail < len)
  103. memset((void *)srx + tail, 0, len - tail);
  104. _debug("INET: %pISp", &srx->transport);
  105. return 0;
  106. }
  107. /*
  108. * bind a local address to an RxRPC socket
  109. */
  110. static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
  111. {
  112. struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)saddr;
  113. struct sock *sk = sock->sk;
  114. struct rxrpc_local *local;
  115. struct rxrpc_sock *rx = rxrpc_sk(sk);
  116. u16 service_id = srx->srx_service;
  117. int ret;
  118. _enter("%p,%p,%d", rx, saddr, len);
  119. ret = rxrpc_validate_address(rx, srx, len);
  120. if (ret < 0)
  121. goto error;
  122. lock_sock(&rx->sk);
  123. if (rx->sk.sk_state != RXRPC_UNBOUND) {
  124. ret = -EINVAL;
  125. goto error_unlock;
  126. }
  127. memcpy(&rx->srx, srx, sizeof(rx->srx));
  128. local = rxrpc_lookup_local(&rx->srx);
  129. if (IS_ERR(local)) {
  130. ret = PTR_ERR(local);
  131. goto error_unlock;
  132. }
  133. if (service_id) {
  134. write_lock(&local->services_lock);
  135. if (rcu_access_pointer(local->service))
  136. goto service_in_use;
  137. rx->local = local;
  138. rcu_assign_pointer(local->service, rx);
  139. write_unlock(&local->services_lock);
  140. rx->sk.sk_state = RXRPC_SERVER_BOUND;
  141. } else {
  142. rx->local = local;
  143. rx->sk.sk_state = RXRPC_CLIENT_BOUND;
  144. }
  145. release_sock(&rx->sk);
  146. _leave(" = 0");
  147. return 0;
  148. service_in_use:
  149. write_unlock(&local->services_lock);
  150. rxrpc_put_local(local);
  151. ret = -EADDRINUSE;
  152. error_unlock:
  153. release_sock(&rx->sk);
  154. error:
  155. _leave(" = %d", ret);
  156. return ret;
  157. }
  158. /*
  159. * set the number of pending calls permitted on a listening socket
  160. */
  161. static int rxrpc_listen(struct socket *sock, int backlog)
  162. {
  163. struct sock *sk = sock->sk;
  164. struct rxrpc_sock *rx = rxrpc_sk(sk);
  165. unsigned int max, old;
  166. int ret;
  167. _enter("%p,%d", rx, backlog);
  168. lock_sock(&rx->sk);
  169. switch (rx->sk.sk_state) {
  170. case RXRPC_UNBOUND:
  171. ret = -EADDRNOTAVAIL;
  172. break;
  173. case RXRPC_SERVER_BOUND:
  174. ASSERT(rx->local != NULL);
  175. max = READ_ONCE(rxrpc_max_backlog);
  176. ret = -EINVAL;
  177. if (backlog == INT_MAX)
  178. backlog = max;
  179. else if (backlog < 0 || backlog > max)
  180. break;
  181. old = sk->sk_max_ack_backlog;
  182. sk->sk_max_ack_backlog = backlog;
  183. ret = rxrpc_service_prealloc(rx, GFP_KERNEL);
  184. if (ret == 0)
  185. rx->sk.sk_state = RXRPC_SERVER_LISTENING;
  186. else
  187. sk->sk_max_ack_backlog = old;
  188. break;
  189. default:
  190. ret = -EBUSY;
  191. break;
  192. }
  193. release_sock(&rx->sk);
  194. _leave(" = %d", ret);
  195. return ret;
  196. }
  197. /**
  198. * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
  199. * @sock: The socket on which to make the call
  200. * @srx: The address of the peer to contact
  201. * @key: The security context to use (defaults to socket setting)
  202. * @user_call_ID: The ID to use
  203. * @gfp: The allocation constraints
  204. * @notify_rx: Where to send notifications instead of socket queue
  205. *
  206. * Allow a kernel service to begin a call on the nominated socket. This just
  207. * sets up all the internal tracking structures and allocates connection and
  208. * call IDs as appropriate. The call to be used is returned.
  209. *
  210. * The default socket destination address and security may be overridden by
  211. * supplying @srx and @key.
  212. */
  213. struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock,
  214. struct sockaddr_rxrpc *srx,
  215. struct key *key,
  216. unsigned long user_call_ID,
  217. gfp_t gfp,
  218. rxrpc_notify_rx_t notify_rx)
  219. {
  220. struct rxrpc_conn_parameters cp;
  221. struct rxrpc_call *call;
  222. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  223. int ret;
  224. _enter(",,%x,%lx", key_serial(key), user_call_ID);
  225. ret = rxrpc_validate_address(rx, srx, sizeof(*srx));
  226. if (ret < 0)
  227. return ERR_PTR(ret);
  228. lock_sock(&rx->sk);
  229. if (!key)
  230. key = rx->key;
  231. if (key && !key->payload.data[0])
  232. key = NULL; /* a no-security key */
  233. memset(&cp, 0, sizeof(cp));
  234. cp.local = rx->local;
  235. cp.key = key;
  236. cp.security_level = 0;
  237. cp.exclusive = false;
  238. cp.service_id = srx->srx_service;
  239. call = rxrpc_new_client_call(rx, &cp, srx, user_call_ID, gfp);
  240. if (!IS_ERR(call))
  241. call->notify_rx = notify_rx;
  242. release_sock(&rx->sk);
  243. _leave(" = %p", call);
  244. return call;
  245. }
  246. EXPORT_SYMBOL(rxrpc_kernel_begin_call);
  247. /**
  248. * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
  249. * @sock: The socket the call is on
  250. * @call: The call to end
  251. *
  252. * Allow a kernel service to end a call it was using. The call must be
  253. * complete before this is called (the call should be aborted if necessary).
  254. */
  255. void rxrpc_kernel_end_call(struct socket *sock, struct rxrpc_call *call)
  256. {
  257. _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
  258. rxrpc_release_call(rxrpc_sk(sock->sk), call);
  259. rxrpc_put_call(call, rxrpc_call_put_kernel);
  260. }
  261. EXPORT_SYMBOL(rxrpc_kernel_end_call);
  262. /**
  263. * rxrpc_kernel_new_call_notification - Get notifications of new calls
  264. * @sock: The socket to intercept received messages on
  265. * @notify_new_call: Function to be called when new calls appear
  266. * @discard_new_call: Function to discard preallocated calls
  267. *
  268. * Allow a kernel service to be given notifications about new calls.
  269. */
  270. void rxrpc_kernel_new_call_notification(
  271. struct socket *sock,
  272. rxrpc_notify_new_call_t notify_new_call,
  273. rxrpc_discard_new_call_t discard_new_call)
  274. {
  275. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  276. rx->notify_new_call = notify_new_call;
  277. rx->discard_new_call = discard_new_call;
  278. }
  279. EXPORT_SYMBOL(rxrpc_kernel_new_call_notification);
  280. /*
  281. * connect an RxRPC socket
  282. * - this just targets it at a specific destination; no actual connection
  283. * negotiation takes place
  284. */
  285. static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
  286. int addr_len, int flags)
  287. {
  288. struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)addr;
  289. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  290. int ret;
  291. _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
  292. ret = rxrpc_validate_address(rx, srx, addr_len);
  293. if (ret < 0) {
  294. _leave(" = %d [bad addr]", ret);
  295. return ret;
  296. }
  297. lock_sock(&rx->sk);
  298. ret = -EISCONN;
  299. if (test_bit(RXRPC_SOCK_CONNECTED, &rx->flags))
  300. goto error;
  301. switch (rx->sk.sk_state) {
  302. case RXRPC_UNBOUND:
  303. rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
  304. case RXRPC_CLIENT_UNBOUND:
  305. case RXRPC_CLIENT_BOUND:
  306. break;
  307. default:
  308. ret = -EBUSY;
  309. goto error;
  310. }
  311. rx->connect_srx = *srx;
  312. set_bit(RXRPC_SOCK_CONNECTED, &rx->flags);
  313. ret = 0;
  314. error:
  315. release_sock(&rx->sk);
  316. return ret;
  317. }
  318. /*
  319. * send a message through an RxRPC socket
  320. * - in a client this does a number of things:
  321. * - finds/sets up a connection for the security specified (if any)
  322. * - initiates a call (ID in control data)
  323. * - ends the request phase of a call (if MSG_MORE is not set)
  324. * - sends a call data packet
  325. * - may send an abort (abort code in control data)
  326. */
  327. static int rxrpc_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
  328. {
  329. struct rxrpc_local *local;
  330. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  331. int ret;
  332. _enter(",{%d},,%zu", rx->sk.sk_state, len);
  333. if (m->msg_flags & MSG_OOB)
  334. return -EOPNOTSUPP;
  335. if (m->msg_name) {
  336. ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen);
  337. if (ret < 0) {
  338. _leave(" = %d [bad addr]", ret);
  339. return ret;
  340. }
  341. }
  342. lock_sock(&rx->sk);
  343. switch (rx->sk.sk_state) {
  344. case RXRPC_UNBOUND:
  345. rx->srx.srx_family = AF_RXRPC;
  346. rx->srx.srx_service = 0;
  347. rx->srx.transport_type = SOCK_DGRAM;
  348. rx->srx.transport.family = rx->family;
  349. switch (rx->family) {
  350. case AF_INET:
  351. rx->srx.transport_len = sizeof(struct sockaddr_in);
  352. break;
  353. #ifdef CONFIG_AF_RXRPC_IPV6
  354. case AF_INET6:
  355. rx->srx.transport_len = sizeof(struct sockaddr_in6);
  356. break;
  357. #endif
  358. default:
  359. ret = -EAFNOSUPPORT;
  360. goto error_unlock;
  361. }
  362. local = rxrpc_lookup_local(&rx->srx);
  363. if (IS_ERR(local)) {
  364. ret = PTR_ERR(local);
  365. goto error_unlock;
  366. }
  367. rx->local = local;
  368. rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
  369. /* Fall through */
  370. case RXRPC_CLIENT_UNBOUND:
  371. case RXRPC_CLIENT_BOUND:
  372. if (!m->msg_name &&
  373. test_bit(RXRPC_SOCK_CONNECTED, &rx->flags)) {
  374. m->msg_name = &rx->connect_srx;
  375. m->msg_namelen = sizeof(rx->connect_srx);
  376. }
  377. case RXRPC_SERVER_BOUND:
  378. case RXRPC_SERVER_LISTENING:
  379. ret = rxrpc_do_sendmsg(rx, m, len);
  380. break;
  381. default:
  382. ret = -EINVAL;
  383. break;
  384. }
  385. error_unlock:
  386. release_sock(&rx->sk);
  387. _leave(" = %d", ret);
  388. return ret;
  389. }
  390. /*
  391. * set RxRPC socket options
  392. */
  393. static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
  394. char __user *optval, unsigned int optlen)
  395. {
  396. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  397. unsigned int min_sec_level;
  398. int ret;
  399. _enter(",%d,%d,,%d", level, optname, optlen);
  400. lock_sock(&rx->sk);
  401. ret = -EOPNOTSUPP;
  402. if (level == SOL_RXRPC) {
  403. switch (optname) {
  404. case RXRPC_EXCLUSIVE_CONNECTION:
  405. ret = -EINVAL;
  406. if (optlen != 0)
  407. goto error;
  408. ret = -EISCONN;
  409. if (rx->sk.sk_state != RXRPC_UNBOUND)
  410. goto error;
  411. rx->exclusive = true;
  412. goto success;
  413. case RXRPC_SECURITY_KEY:
  414. ret = -EINVAL;
  415. if (rx->key)
  416. goto error;
  417. ret = -EISCONN;
  418. if (rx->sk.sk_state != RXRPC_UNBOUND)
  419. goto error;
  420. ret = rxrpc_request_key(rx, optval, optlen);
  421. goto error;
  422. case RXRPC_SECURITY_KEYRING:
  423. ret = -EINVAL;
  424. if (rx->key)
  425. goto error;
  426. ret = -EISCONN;
  427. if (rx->sk.sk_state != RXRPC_UNBOUND)
  428. goto error;
  429. ret = rxrpc_server_keyring(rx, optval, optlen);
  430. goto error;
  431. case RXRPC_MIN_SECURITY_LEVEL:
  432. ret = -EINVAL;
  433. if (optlen != sizeof(unsigned int))
  434. goto error;
  435. ret = -EISCONN;
  436. if (rx->sk.sk_state != RXRPC_UNBOUND)
  437. goto error;
  438. ret = get_user(min_sec_level,
  439. (unsigned int __user *) optval);
  440. if (ret < 0)
  441. goto error;
  442. ret = -EINVAL;
  443. if (min_sec_level > RXRPC_SECURITY_MAX)
  444. goto error;
  445. rx->min_sec_level = min_sec_level;
  446. goto success;
  447. default:
  448. break;
  449. }
  450. }
  451. success:
  452. ret = 0;
  453. error:
  454. release_sock(&rx->sk);
  455. return ret;
  456. }
  457. /*
  458. * permit an RxRPC socket to be polled
  459. */
  460. static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
  461. poll_table *wait)
  462. {
  463. struct sock *sk = sock->sk;
  464. struct rxrpc_sock *rx = rxrpc_sk(sk);
  465. unsigned int mask;
  466. sock_poll_wait(file, sk_sleep(sk), wait);
  467. mask = 0;
  468. /* the socket is readable if there are any messages waiting on the Rx
  469. * queue */
  470. if (!list_empty(&rx->recvmsg_q))
  471. mask |= POLLIN | POLLRDNORM;
  472. /* the socket is writable if there is space to add new data to the
  473. * socket; there is no guarantee that any particular call in progress
  474. * on the socket may have space in the Tx ACK window */
  475. if (rxrpc_writable(sk))
  476. mask |= POLLOUT | POLLWRNORM;
  477. return mask;
  478. }
  479. /*
  480. * create an RxRPC socket
  481. */
  482. static int rxrpc_create(struct net *net, struct socket *sock, int protocol,
  483. int kern)
  484. {
  485. struct rxrpc_sock *rx;
  486. struct sock *sk;
  487. _enter("%p,%d", sock, protocol);
  488. if (!net_eq(net, &init_net))
  489. return -EAFNOSUPPORT;
  490. /* we support transport protocol UDP/UDP6 only */
  491. if (protocol != PF_INET &&
  492. IS_ENABLED(CONFIG_AF_RXRPC_IPV6) && protocol != PF_INET6)
  493. return -EPROTONOSUPPORT;
  494. if (sock->type != SOCK_DGRAM)
  495. return -ESOCKTNOSUPPORT;
  496. sock->ops = &rxrpc_rpc_ops;
  497. sock->state = SS_UNCONNECTED;
  498. sk = sk_alloc(net, PF_RXRPC, GFP_KERNEL, &rxrpc_proto, kern);
  499. if (!sk)
  500. return -ENOMEM;
  501. sock_init_data(sock, sk);
  502. sock_set_flag(sk, SOCK_RCU_FREE);
  503. sk->sk_state = RXRPC_UNBOUND;
  504. sk->sk_write_space = rxrpc_write_space;
  505. sk->sk_max_ack_backlog = 0;
  506. sk->sk_destruct = rxrpc_sock_destructor;
  507. rx = rxrpc_sk(sk);
  508. rx->family = protocol;
  509. rx->calls = RB_ROOT;
  510. spin_lock_init(&rx->incoming_lock);
  511. INIT_LIST_HEAD(&rx->sock_calls);
  512. INIT_LIST_HEAD(&rx->to_be_accepted);
  513. INIT_LIST_HEAD(&rx->recvmsg_q);
  514. rwlock_init(&rx->recvmsg_lock);
  515. rwlock_init(&rx->call_lock);
  516. memset(&rx->srx, 0, sizeof(rx->srx));
  517. _leave(" = 0 [%p]", rx);
  518. return 0;
  519. }
  520. /*
  521. * Kill all the calls on a socket and shut it down.
  522. */
  523. static int rxrpc_shutdown(struct socket *sock, int flags)
  524. {
  525. struct sock *sk = sock->sk;
  526. struct rxrpc_sock *rx = rxrpc_sk(sk);
  527. int ret = 0;
  528. _enter("%p,%d", sk, flags);
  529. if (flags != SHUT_RDWR)
  530. return -EOPNOTSUPP;
  531. if (sk->sk_state == RXRPC_CLOSE)
  532. return -ESHUTDOWN;
  533. lock_sock(sk);
  534. spin_lock_bh(&sk->sk_receive_queue.lock);
  535. if (sk->sk_state < RXRPC_CLOSE) {
  536. sk->sk_state = RXRPC_CLOSE;
  537. sk->sk_shutdown = SHUTDOWN_MASK;
  538. } else {
  539. ret = -ESHUTDOWN;
  540. }
  541. spin_unlock_bh(&sk->sk_receive_queue.lock);
  542. rxrpc_discard_prealloc(rx);
  543. release_sock(sk);
  544. return ret;
  545. }
  546. /*
  547. * RxRPC socket destructor
  548. */
  549. static void rxrpc_sock_destructor(struct sock *sk)
  550. {
  551. _enter("%p", sk);
  552. rxrpc_purge_queue(&sk->sk_receive_queue);
  553. WARN_ON(atomic_read(&sk->sk_wmem_alloc));
  554. WARN_ON(!sk_unhashed(sk));
  555. WARN_ON(sk->sk_socket);
  556. if (!sock_flag(sk, SOCK_DEAD)) {
  557. printk("Attempt to release alive rxrpc socket: %p\n", sk);
  558. return;
  559. }
  560. }
  561. /*
  562. * release an RxRPC socket
  563. */
  564. static int rxrpc_release_sock(struct sock *sk)
  565. {
  566. struct rxrpc_sock *rx = rxrpc_sk(sk);
  567. _enter("%p{%d,%d}", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));
  568. /* declare the socket closed for business */
  569. sock_orphan(sk);
  570. sk->sk_shutdown = SHUTDOWN_MASK;
  571. spin_lock_bh(&sk->sk_receive_queue.lock);
  572. sk->sk_state = RXRPC_CLOSE;
  573. spin_unlock_bh(&sk->sk_receive_queue.lock);
  574. if (rx->local && rcu_access_pointer(rx->local->service) == rx) {
  575. write_lock(&rx->local->services_lock);
  576. rcu_assign_pointer(rx->local->service, NULL);
  577. write_unlock(&rx->local->services_lock);
  578. }
  579. /* try to flush out this socket */
  580. rxrpc_discard_prealloc(rx);
  581. rxrpc_release_calls_on_socket(rx);
  582. flush_workqueue(rxrpc_workqueue);
  583. rxrpc_purge_queue(&sk->sk_receive_queue);
  584. rxrpc_put_local(rx->local);
  585. rx->local = NULL;
  586. key_put(rx->key);
  587. rx->key = NULL;
  588. key_put(rx->securities);
  589. rx->securities = NULL;
  590. sock_put(sk);
  591. _leave(" = 0");
  592. return 0;
  593. }
  594. /*
  595. * release an RxRPC BSD socket on close() or equivalent
  596. */
  597. static int rxrpc_release(struct socket *sock)
  598. {
  599. struct sock *sk = sock->sk;
  600. _enter("%p{%p}", sock, sk);
  601. if (!sk)
  602. return 0;
  603. sock->sk = NULL;
  604. return rxrpc_release_sock(sk);
  605. }
  606. /*
  607. * RxRPC network protocol
  608. */
  609. static const struct proto_ops rxrpc_rpc_ops = {
  610. .family = PF_RXRPC,
  611. .owner = THIS_MODULE,
  612. .release = rxrpc_release,
  613. .bind = rxrpc_bind,
  614. .connect = rxrpc_connect,
  615. .socketpair = sock_no_socketpair,
  616. .accept = sock_no_accept,
  617. .getname = sock_no_getname,
  618. .poll = rxrpc_poll,
  619. .ioctl = sock_no_ioctl,
  620. .listen = rxrpc_listen,
  621. .shutdown = rxrpc_shutdown,
  622. .setsockopt = rxrpc_setsockopt,
  623. .getsockopt = sock_no_getsockopt,
  624. .sendmsg = rxrpc_sendmsg,
  625. .recvmsg = rxrpc_recvmsg,
  626. .mmap = sock_no_mmap,
  627. .sendpage = sock_no_sendpage,
  628. };
  629. static struct proto rxrpc_proto = {
  630. .name = "RXRPC",
  631. .owner = THIS_MODULE,
  632. .obj_size = sizeof(struct rxrpc_sock),
  633. .max_header = sizeof(struct rxrpc_wire_header),
  634. };
  635. static const struct net_proto_family rxrpc_family_ops = {
  636. .family = PF_RXRPC,
  637. .create = rxrpc_create,
  638. .owner = THIS_MODULE,
  639. };
  640. /*
  641. * initialise and register the RxRPC protocol
  642. */
  643. static int __init af_rxrpc_init(void)
  644. {
  645. int ret = -1;
  646. BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > FIELD_SIZEOF(struct sk_buff, cb));
  647. get_random_bytes(&rxrpc_epoch, sizeof(rxrpc_epoch));
  648. rxrpc_epoch |= RXRPC_RANDOM_EPOCH;
  649. get_random_bytes(&rxrpc_client_conn_ids.cur,
  650. sizeof(rxrpc_client_conn_ids.cur));
  651. rxrpc_client_conn_ids.cur &= 0x3fffffff;
  652. if (rxrpc_client_conn_ids.cur == 0)
  653. rxrpc_client_conn_ids.cur = 1;
  654. ret = -ENOMEM;
  655. rxrpc_call_jar = kmem_cache_create(
  656. "rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
  657. SLAB_HWCACHE_ALIGN, NULL);
  658. if (!rxrpc_call_jar) {
  659. pr_notice("Failed to allocate call jar\n");
  660. goto error_call_jar;
  661. }
  662. rxrpc_workqueue = alloc_workqueue("krxrpcd", 0, 1);
  663. if (!rxrpc_workqueue) {
  664. pr_notice("Failed to allocate work queue\n");
  665. goto error_work_queue;
  666. }
  667. ret = rxrpc_init_security();
  668. if (ret < 0) {
  669. pr_crit("Cannot initialise security\n");
  670. goto error_security;
  671. }
  672. ret = proto_register(&rxrpc_proto, 1);
  673. if (ret < 0) {
  674. pr_crit("Cannot register protocol\n");
  675. goto error_proto;
  676. }
  677. ret = sock_register(&rxrpc_family_ops);
  678. if (ret < 0) {
  679. pr_crit("Cannot register socket family\n");
  680. goto error_sock;
  681. }
  682. ret = register_key_type(&key_type_rxrpc);
  683. if (ret < 0) {
  684. pr_crit("Cannot register client key type\n");
  685. goto error_key_type;
  686. }
  687. ret = register_key_type(&key_type_rxrpc_s);
  688. if (ret < 0) {
  689. pr_crit("Cannot register server key type\n");
  690. goto error_key_type_s;
  691. }
  692. ret = rxrpc_sysctl_init();
  693. if (ret < 0) {
  694. pr_crit("Cannot register sysctls\n");
  695. goto error_sysctls;
  696. }
  697. #ifdef CONFIG_PROC_FS
  698. proc_create("rxrpc_calls", 0, init_net.proc_net, &rxrpc_call_seq_fops);
  699. proc_create("rxrpc_conns", 0, init_net.proc_net,
  700. &rxrpc_connection_seq_fops);
  701. #endif
  702. return 0;
  703. error_sysctls:
  704. unregister_key_type(&key_type_rxrpc_s);
  705. error_key_type_s:
  706. unregister_key_type(&key_type_rxrpc);
  707. error_key_type:
  708. sock_unregister(PF_RXRPC);
  709. error_sock:
  710. proto_unregister(&rxrpc_proto);
  711. error_proto:
  712. rxrpc_exit_security();
  713. error_security:
  714. destroy_workqueue(rxrpc_workqueue);
  715. error_work_queue:
  716. kmem_cache_destroy(rxrpc_call_jar);
  717. error_call_jar:
  718. return ret;
  719. }
  720. /*
  721. * unregister the RxRPC protocol
  722. */
  723. static void __exit af_rxrpc_exit(void)
  724. {
  725. _enter("");
  726. rxrpc_sysctl_exit();
  727. unregister_key_type(&key_type_rxrpc_s);
  728. unregister_key_type(&key_type_rxrpc);
  729. sock_unregister(PF_RXRPC);
  730. proto_unregister(&rxrpc_proto);
  731. rxrpc_destroy_all_calls();
  732. rxrpc_destroy_all_connections();
  733. ASSERTCMP(atomic_read(&rxrpc_n_tx_skbs), ==, 0);
  734. ASSERTCMP(atomic_read(&rxrpc_n_rx_skbs), ==, 0);
  735. rxrpc_destroy_all_locals();
  736. remove_proc_entry("rxrpc_conns", init_net.proc_net);
  737. remove_proc_entry("rxrpc_calls", init_net.proc_net);
  738. destroy_workqueue(rxrpc_workqueue);
  739. rxrpc_exit_security();
  740. kmem_cache_destroy(rxrpc_call_jar);
  741. _leave("");
  742. }
  743. module_init(af_rxrpc_init);
  744. module_exit(af_rxrpc_exit);