af_rxrpc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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/net.h>
  13. #include <linux/slab.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/poll.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/key-type.h>
  18. #include <net/net_namespace.h>
  19. #include <net/sock.h>
  20. #include <net/af_rxrpc.h>
  21. #include "ar-internal.h"
  22. MODULE_DESCRIPTION("RxRPC network protocol");
  23. MODULE_AUTHOR("Red Hat, Inc.");
  24. MODULE_LICENSE("GPL");
  25. MODULE_ALIAS_NETPROTO(PF_RXRPC);
  26. unsigned rxrpc_debug; // = RXRPC_DEBUG_KPROTO;
  27. module_param_named(debug, rxrpc_debug, uint, S_IWUSR | S_IRUGO);
  28. MODULE_PARM_DESC(debug, "RxRPC debugging mask");
  29. static int sysctl_rxrpc_max_qlen __read_mostly = 10;
  30. static struct proto rxrpc_proto;
  31. static const struct proto_ops rxrpc_rpc_ops;
  32. /* local epoch for detecting local-end reset */
  33. __be32 rxrpc_epoch;
  34. /* current debugging ID */
  35. atomic_t rxrpc_debug_id;
  36. /* count of skbs currently in use */
  37. atomic_t rxrpc_n_skbs;
  38. struct workqueue_struct *rxrpc_workqueue;
  39. static void rxrpc_sock_destructor(struct sock *);
  40. /*
  41. * see if an RxRPC socket is currently writable
  42. */
  43. static inline int rxrpc_writable(struct sock *sk)
  44. {
  45. return atomic_read(&sk->sk_wmem_alloc) < (size_t) sk->sk_sndbuf;
  46. }
  47. /*
  48. * wait for write bufferage to become available
  49. */
  50. static void rxrpc_write_space(struct sock *sk)
  51. {
  52. _enter("%p", sk);
  53. rcu_read_lock();
  54. if (rxrpc_writable(sk)) {
  55. struct socket_wq *wq = rcu_dereference(sk->sk_wq);
  56. if (wq_has_sleeper(wq))
  57. wake_up_interruptible(&wq->wait);
  58. sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
  59. }
  60. rcu_read_unlock();
  61. }
  62. /*
  63. * validate an RxRPC address
  64. */
  65. static int rxrpc_validate_address(struct rxrpc_sock *rx,
  66. struct sockaddr_rxrpc *srx,
  67. int len)
  68. {
  69. if (len < sizeof(struct sockaddr_rxrpc))
  70. return -EINVAL;
  71. if (srx->srx_family != AF_RXRPC)
  72. return -EAFNOSUPPORT;
  73. if (srx->transport_type != SOCK_DGRAM)
  74. return -ESOCKTNOSUPPORT;
  75. len -= offsetof(struct sockaddr_rxrpc, transport);
  76. if (srx->transport_len < sizeof(sa_family_t) ||
  77. srx->transport_len > len)
  78. return -EINVAL;
  79. if (srx->transport.family != rx->proto)
  80. return -EAFNOSUPPORT;
  81. switch (srx->transport.family) {
  82. case AF_INET:
  83. _debug("INET: %x @ %pI4",
  84. ntohs(srx->transport.sin.sin_port),
  85. &srx->transport.sin.sin_addr);
  86. if (srx->transport_len > 8)
  87. memset((void *)&srx->transport + 8, 0,
  88. srx->transport_len - 8);
  89. break;
  90. case AF_INET6:
  91. default:
  92. return -EAFNOSUPPORT;
  93. }
  94. return 0;
  95. }
  96. /*
  97. * bind a local address to an RxRPC socket
  98. */
  99. static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
  100. {
  101. struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) saddr;
  102. struct sock *sk = sock->sk;
  103. struct rxrpc_local *local;
  104. struct rxrpc_sock *rx = rxrpc_sk(sk), *prx;
  105. __be16 service_id;
  106. int ret;
  107. _enter("%p,%p,%d", rx, saddr, len);
  108. ret = rxrpc_validate_address(rx, srx, len);
  109. if (ret < 0)
  110. goto error;
  111. lock_sock(&rx->sk);
  112. if (rx->sk.sk_state != RXRPC_UNCONNECTED) {
  113. ret = -EINVAL;
  114. goto error_unlock;
  115. }
  116. memcpy(&rx->srx, srx, sizeof(rx->srx));
  117. /* find a local transport endpoint if we don't have one already */
  118. local = rxrpc_lookup_local(&rx->srx);
  119. if (IS_ERR(local)) {
  120. ret = PTR_ERR(local);
  121. goto error_unlock;
  122. }
  123. rx->local = local;
  124. if (srx->srx_service) {
  125. service_id = htons(srx->srx_service);
  126. write_lock_bh(&local->services_lock);
  127. list_for_each_entry(prx, &local->services, listen_link) {
  128. if (prx->service_id == service_id)
  129. goto service_in_use;
  130. }
  131. rx->service_id = service_id;
  132. list_add_tail(&rx->listen_link, &local->services);
  133. write_unlock_bh(&local->services_lock);
  134. rx->sk.sk_state = RXRPC_SERVER_BOUND;
  135. } else {
  136. rx->sk.sk_state = RXRPC_CLIENT_BOUND;
  137. }
  138. release_sock(&rx->sk);
  139. _leave(" = 0");
  140. return 0;
  141. service_in_use:
  142. ret = -EADDRINUSE;
  143. write_unlock_bh(&local->services_lock);
  144. error_unlock:
  145. release_sock(&rx->sk);
  146. error:
  147. _leave(" = %d", ret);
  148. return ret;
  149. }
  150. /*
  151. * set the number of pending calls permitted on a listening socket
  152. */
  153. static int rxrpc_listen(struct socket *sock, int backlog)
  154. {
  155. struct sock *sk = sock->sk;
  156. struct rxrpc_sock *rx = rxrpc_sk(sk);
  157. int ret;
  158. _enter("%p,%d", rx, backlog);
  159. lock_sock(&rx->sk);
  160. switch (rx->sk.sk_state) {
  161. case RXRPC_UNCONNECTED:
  162. ret = -EADDRNOTAVAIL;
  163. break;
  164. case RXRPC_CLIENT_BOUND:
  165. case RXRPC_CLIENT_CONNECTED:
  166. default:
  167. ret = -EBUSY;
  168. break;
  169. case RXRPC_SERVER_BOUND:
  170. ASSERT(rx->local != NULL);
  171. sk->sk_max_ack_backlog = backlog;
  172. rx->sk.sk_state = RXRPC_SERVER_LISTENING;
  173. ret = 0;
  174. break;
  175. }
  176. release_sock(&rx->sk);
  177. _leave(" = %d", ret);
  178. return ret;
  179. }
  180. /*
  181. * find a transport by address
  182. */
  183. static struct rxrpc_transport *rxrpc_name_to_transport(struct socket *sock,
  184. struct sockaddr *addr,
  185. int addr_len, int flags,
  186. gfp_t gfp)
  187. {
  188. struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) addr;
  189. struct rxrpc_transport *trans;
  190. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  191. struct rxrpc_peer *peer;
  192. _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
  193. ASSERT(rx->local != NULL);
  194. ASSERT(rx->sk.sk_state > RXRPC_UNCONNECTED);
  195. if (rx->srx.transport_type != srx->transport_type)
  196. return ERR_PTR(-ESOCKTNOSUPPORT);
  197. if (rx->srx.transport.family != srx->transport.family)
  198. return ERR_PTR(-EAFNOSUPPORT);
  199. /* find a remote transport endpoint from the local one */
  200. peer = rxrpc_get_peer(srx, gfp);
  201. if (IS_ERR(peer))
  202. return ERR_CAST(peer);
  203. /* find a transport */
  204. trans = rxrpc_get_transport(rx->local, peer, gfp);
  205. rxrpc_put_peer(peer);
  206. _leave(" = %p", trans);
  207. return trans;
  208. }
  209. /**
  210. * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
  211. * @sock: The socket on which to make the call
  212. * @srx: The address of the peer to contact (defaults to socket setting)
  213. * @key: The security context to use (defaults to socket setting)
  214. * @user_call_ID: The ID to use
  215. *
  216. * Allow a kernel service to begin a call on the nominated socket. This just
  217. * sets up all the internal tracking structures and allocates connection and
  218. * call IDs as appropriate. The call to be used is returned.
  219. *
  220. * The default socket destination address and security may be overridden by
  221. * supplying @srx and @key.
  222. */
  223. struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock,
  224. struct sockaddr_rxrpc *srx,
  225. struct key *key,
  226. unsigned long user_call_ID,
  227. gfp_t gfp)
  228. {
  229. struct rxrpc_conn_bundle *bundle;
  230. struct rxrpc_transport *trans;
  231. struct rxrpc_call *call;
  232. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  233. __be16 service_id;
  234. _enter(",,%x,%lx", key_serial(key), user_call_ID);
  235. lock_sock(&rx->sk);
  236. if (srx) {
  237. trans = rxrpc_name_to_transport(sock, (struct sockaddr *) srx,
  238. sizeof(*srx), 0, gfp);
  239. if (IS_ERR(trans)) {
  240. call = ERR_CAST(trans);
  241. trans = NULL;
  242. goto out_notrans;
  243. }
  244. } else {
  245. trans = rx->trans;
  246. if (!trans) {
  247. call = ERR_PTR(-ENOTCONN);
  248. goto out_notrans;
  249. }
  250. atomic_inc(&trans->usage);
  251. }
  252. service_id = rx->service_id;
  253. if (srx)
  254. service_id = htons(srx->srx_service);
  255. if (!key)
  256. key = rx->key;
  257. if (key && !key->payload.data)
  258. key = NULL; /* a no-security key */
  259. bundle = rxrpc_get_bundle(rx, trans, key, service_id, gfp);
  260. if (IS_ERR(bundle)) {
  261. call = ERR_CAST(bundle);
  262. goto out;
  263. }
  264. call = rxrpc_get_client_call(rx, trans, bundle, user_call_ID, true,
  265. gfp);
  266. rxrpc_put_bundle(trans, bundle);
  267. out:
  268. rxrpc_put_transport(trans);
  269. out_notrans:
  270. release_sock(&rx->sk);
  271. _leave(" = %p", call);
  272. return call;
  273. }
  274. EXPORT_SYMBOL(rxrpc_kernel_begin_call);
  275. /**
  276. * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
  277. * @call: The call to end
  278. *
  279. * Allow a kernel service to end a call it was using. The call must be
  280. * complete before this is called (the call should be aborted if necessary).
  281. */
  282. void rxrpc_kernel_end_call(struct rxrpc_call *call)
  283. {
  284. _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
  285. rxrpc_remove_user_ID(call->socket, call);
  286. rxrpc_put_call(call);
  287. }
  288. EXPORT_SYMBOL(rxrpc_kernel_end_call);
  289. /**
  290. * rxrpc_kernel_intercept_rx_messages - Intercept received RxRPC messages
  291. * @sock: The socket to intercept received messages on
  292. * @interceptor: The function to pass the messages to
  293. *
  294. * Allow a kernel service to intercept messages heading for the Rx queue on an
  295. * RxRPC socket. They get passed to the specified function instead.
  296. * @interceptor should free the socket buffers it is given. @interceptor is
  297. * called with the socket receive queue spinlock held and softirqs disabled -
  298. * this ensures that the messages will be delivered in the right order.
  299. */
  300. void rxrpc_kernel_intercept_rx_messages(struct socket *sock,
  301. rxrpc_interceptor_t interceptor)
  302. {
  303. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  304. _enter("");
  305. rx->interceptor = interceptor;
  306. }
  307. EXPORT_SYMBOL(rxrpc_kernel_intercept_rx_messages);
  308. /*
  309. * connect an RxRPC socket
  310. * - this just targets it at a specific destination; no actual connection
  311. * negotiation takes place
  312. */
  313. static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
  314. int addr_len, int flags)
  315. {
  316. struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) addr;
  317. struct sock *sk = sock->sk;
  318. struct rxrpc_transport *trans;
  319. struct rxrpc_local *local;
  320. struct rxrpc_sock *rx = rxrpc_sk(sk);
  321. int ret;
  322. _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
  323. ret = rxrpc_validate_address(rx, srx, addr_len);
  324. if (ret < 0) {
  325. _leave(" = %d [bad addr]", ret);
  326. return ret;
  327. }
  328. lock_sock(&rx->sk);
  329. switch (rx->sk.sk_state) {
  330. case RXRPC_UNCONNECTED:
  331. /* find a local transport endpoint if we don't have one already */
  332. ASSERTCMP(rx->local, ==, NULL);
  333. rx->srx.srx_family = AF_RXRPC;
  334. rx->srx.srx_service = 0;
  335. rx->srx.transport_type = srx->transport_type;
  336. rx->srx.transport_len = sizeof(sa_family_t);
  337. rx->srx.transport.family = srx->transport.family;
  338. local = rxrpc_lookup_local(&rx->srx);
  339. if (IS_ERR(local)) {
  340. release_sock(&rx->sk);
  341. return PTR_ERR(local);
  342. }
  343. rx->local = local;
  344. rx->sk.sk_state = RXRPC_CLIENT_BOUND;
  345. case RXRPC_CLIENT_BOUND:
  346. break;
  347. case RXRPC_CLIENT_CONNECTED:
  348. release_sock(&rx->sk);
  349. return -EISCONN;
  350. default:
  351. release_sock(&rx->sk);
  352. return -EBUSY; /* server sockets can't connect as well */
  353. }
  354. trans = rxrpc_name_to_transport(sock, addr, addr_len, flags,
  355. GFP_KERNEL);
  356. if (IS_ERR(trans)) {
  357. release_sock(&rx->sk);
  358. _leave(" = %ld", PTR_ERR(trans));
  359. return PTR_ERR(trans);
  360. }
  361. rx->trans = trans;
  362. rx->service_id = htons(srx->srx_service);
  363. rx->sk.sk_state = RXRPC_CLIENT_CONNECTED;
  364. release_sock(&rx->sk);
  365. return 0;
  366. }
  367. /*
  368. * send a message through an RxRPC socket
  369. * - in a client this does a number of things:
  370. * - finds/sets up a connection for the security specified (if any)
  371. * - initiates a call (ID in control data)
  372. * - ends the request phase of a call (if MSG_MORE is not set)
  373. * - sends a call data packet
  374. * - may send an abort (abort code in control data)
  375. */
  376. static int rxrpc_sendmsg(struct kiocb *iocb, struct socket *sock,
  377. 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(iocb, 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(iocb, 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 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))
  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 __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);
  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. struct sk_buff *dummy_skb;
  673. int ret = -1;
  674. BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > sizeof(dummy_skb->cb));
  675. rxrpc_epoch = htonl(get_seconds());
  676. ret = -ENOMEM;
  677. rxrpc_call_jar = kmem_cache_create(
  678. "rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
  679. SLAB_HWCACHE_ALIGN, NULL);
  680. if (!rxrpc_call_jar) {
  681. printk(KERN_NOTICE "RxRPC: Failed to allocate call jar\n");
  682. goto error_call_jar;
  683. }
  684. rxrpc_workqueue = alloc_workqueue("krxrpcd", 0, 1);
  685. if (!rxrpc_workqueue) {
  686. printk(KERN_NOTICE "RxRPC: Failed to allocate work queue\n");
  687. goto error_work_queue;
  688. }
  689. ret = proto_register(&rxrpc_proto, 1);
  690. if (ret < 0) {
  691. printk(KERN_CRIT "RxRPC: Cannot register protocol\n");
  692. goto error_proto;
  693. }
  694. ret = sock_register(&rxrpc_family_ops);
  695. if (ret < 0) {
  696. printk(KERN_CRIT "RxRPC: Cannot register socket family\n");
  697. goto error_sock;
  698. }
  699. ret = register_key_type(&key_type_rxrpc);
  700. if (ret < 0) {
  701. printk(KERN_CRIT "RxRPC: Cannot register client key type\n");
  702. goto error_key_type;
  703. }
  704. ret = register_key_type(&key_type_rxrpc_s);
  705. if (ret < 0) {
  706. printk(KERN_CRIT "RxRPC: Cannot register server key type\n");
  707. goto error_key_type_s;
  708. }
  709. #ifdef CONFIG_PROC_FS
  710. proc_net_fops_create(&init_net, "rxrpc_calls", 0, &rxrpc_call_seq_fops);
  711. proc_net_fops_create(&init_net, "rxrpc_conns", 0, &rxrpc_connection_seq_fops);
  712. #endif
  713. return 0;
  714. error_key_type_s:
  715. unregister_key_type(&key_type_rxrpc);
  716. error_key_type:
  717. sock_unregister(PF_RXRPC);
  718. error_sock:
  719. proto_unregister(&rxrpc_proto);
  720. error_proto:
  721. destroy_workqueue(rxrpc_workqueue);
  722. error_work_queue:
  723. kmem_cache_destroy(rxrpc_call_jar);
  724. error_call_jar:
  725. return ret;
  726. }
  727. /*
  728. * unregister the RxRPC protocol
  729. */
  730. static void __exit af_rxrpc_exit(void)
  731. {
  732. _enter("");
  733. unregister_key_type(&key_type_rxrpc_s);
  734. unregister_key_type(&key_type_rxrpc);
  735. sock_unregister(PF_RXRPC);
  736. proto_unregister(&rxrpc_proto);
  737. rxrpc_destroy_all_calls();
  738. rxrpc_destroy_all_connections();
  739. rxrpc_destroy_all_transports();
  740. rxrpc_destroy_all_peers();
  741. rxrpc_destroy_all_locals();
  742. ASSERTCMP(atomic_read(&rxrpc_n_skbs), ==, 0);
  743. _debug("flush scheduled work");
  744. flush_workqueue(rxrpc_workqueue);
  745. proc_net_remove(&init_net, "rxrpc_conns");
  746. proc_net_remove(&init_net, "rxrpc_calls");
  747. destroy_workqueue(rxrpc_workqueue);
  748. kmem_cache_destroy(rxrpc_call_jar);
  749. _leave("");
  750. }
  751. module_init(af_rxrpc_init);
  752. module_exit(af_rxrpc_exit);