af_rxrpc.c 21 KB

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