ar-local.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* AF_RXRPC local endpoint management
  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/skbuff.h>
  14. #include <linux/slab.h>
  15. #include <net/sock.h>
  16. #include <net/af_rxrpc.h>
  17. #include "ar-internal.h"
  18. static LIST_HEAD(rxrpc_locals);
  19. DEFINE_RWLOCK(rxrpc_local_lock);
  20. static DECLARE_RWSEM(rxrpc_local_sem);
  21. static DECLARE_WAIT_QUEUE_HEAD(rxrpc_local_wq);
  22. static void rxrpc_destroy_local(struct work_struct *work);
  23. /*
  24. * allocate a new local
  25. */
  26. static
  27. struct rxrpc_local *rxrpc_alloc_local(struct sockaddr_rxrpc *srx)
  28. {
  29. struct rxrpc_local *local;
  30. local = kzalloc(sizeof(struct rxrpc_local), GFP_KERNEL);
  31. if (local) {
  32. INIT_WORK(&local->destroyer, &rxrpc_destroy_local);
  33. INIT_WORK(&local->acceptor, &rxrpc_accept_incoming_calls);
  34. INIT_WORK(&local->rejecter, &rxrpc_reject_packets);
  35. INIT_LIST_HEAD(&local->services);
  36. INIT_LIST_HEAD(&local->link);
  37. init_rwsem(&local->defrag_sem);
  38. skb_queue_head_init(&local->accept_queue);
  39. skb_queue_head_init(&local->reject_queue);
  40. spin_lock_init(&local->lock);
  41. rwlock_init(&local->services_lock);
  42. atomic_set(&local->usage, 1);
  43. local->debug_id = atomic_inc_return(&rxrpc_debug_id);
  44. memcpy(&local->srx, srx, sizeof(*srx));
  45. }
  46. _leave(" = %p", local);
  47. return local;
  48. }
  49. /*
  50. * create the local socket
  51. * - must be called with rxrpc_local_sem writelocked
  52. */
  53. static int rxrpc_create_local(struct rxrpc_local *local)
  54. {
  55. struct sock *sock;
  56. int ret, opt;
  57. _enter("%p{%d}", local, local->srx.transport_type);
  58. /* create a socket to represent the local endpoint */
  59. ret = sock_create_kern(PF_INET, local->srx.transport_type, IPPROTO_UDP,
  60. &local->socket);
  61. if (ret < 0) {
  62. _leave(" = %d [socket]", ret);
  63. return ret;
  64. }
  65. /* if a local address was supplied then bind it */
  66. if (local->srx.transport_len > sizeof(sa_family_t)) {
  67. _debug("bind");
  68. ret = kernel_bind(local->socket,
  69. (struct sockaddr *) &local->srx.transport,
  70. local->srx.transport_len);
  71. if (ret < 0) {
  72. _debug("bind failed");
  73. goto error;
  74. }
  75. }
  76. /* we want to receive ICMP errors */
  77. opt = 1;
  78. ret = kernel_setsockopt(local->socket, SOL_IP, IP_RECVERR,
  79. (char *) &opt, sizeof(opt));
  80. if (ret < 0) {
  81. _debug("setsockopt failed");
  82. goto error;
  83. }
  84. /* we want to set the don't fragment bit */
  85. opt = IP_PMTUDISC_DO;
  86. ret = kernel_setsockopt(local->socket, SOL_IP, IP_MTU_DISCOVER,
  87. (char *) &opt, sizeof(opt));
  88. if (ret < 0) {
  89. _debug("setsockopt failed");
  90. goto error;
  91. }
  92. write_lock_bh(&rxrpc_local_lock);
  93. list_add(&local->link, &rxrpc_locals);
  94. write_unlock_bh(&rxrpc_local_lock);
  95. /* set the socket up */
  96. sock = local->socket->sk;
  97. sock->sk_user_data = local;
  98. sock->sk_data_ready = rxrpc_data_ready;
  99. sock->sk_error_report = rxrpc_UDP_error_report;
  100. _leave(" = 0");
  101. return 0;
  102. error:
  103. kernel_sock_shutdown(local->socket, SHUT_RDWR);
  104. local->socket->sk->sk_user_data = NULL;
  105. sock_release(local->socket);
  106. local->socket = NULL;
  107. _leave(" = %d", ret);
  108. return ret;
  109. }
  110. /*
  111. * create a new local endpoint using the specified UDP address
  112. */
  113. struct rxrpc_local *rxrpc_lookup_local(struct sockaddr_rxrpc *srx)
  114. {
  115. struct rxrpc_local *local;
  116. int ret;
  117. _enter("{%d,%u,%pI4+%hu}",
  118. srx->transport_type,
  119. srx->transport.family,
  120. &srx->transport.sin.sin_addr,
  121. ntohs(srx->transport.sin.sin_port));
  122. down_write(&rxrpc_local_sem);
  123. /* see if we have a suitable local local endpoint already */
  124. read_lock_bh(&rxrpc_local_lock);
  125. list_for_each_entry(local, &rxrpc_locals, link) {
  126. _debug("CMP {%d,%u,%pI4+%hu}",
  127. local->srx.transport_type,
  128. local->srx.transport.family,
  129. &local->srx.transport.sin.sin_addr,
  130. ntohs(local->srx.transport.sin.sin_port));
  131. if (local->srx.transport_type != srx->transport_type ||
  132. local->srx.transport.family != srx->transport.family)
  133. continue;
  134. switch (srx->transport.family) {
  135. case AF_INET:
  136. if (local->srx.transport.sin.sin_port !=
  137. srx->transport.sin.sin_port)
  138. continue;
  139. if (memcmp(&local->srx.transport.sin.sin_addr,
  140. &srx->transport.sin.sin_addr,
  141. sizeof(struct in_addr)) != 0)
  142. continue;
  143. goto found_local;
  144. default:
  145. BUG();
  146. }
  147. }
  148. read_unlock_bh(&rxrpc_local_lock);
  149. /* we didn't find one, so we need to create one */
  150. local = rxrpc_alloc_local(srx);
  151. if (!local) {
  152. up_write(&rxrpc_local_sem);
  153. return ERR_PTR(-ENOMEM);
  154. }
  155. ret = rxrpc_create_local(local);
  156. if (ret < 0) {
  157. up_write(&rxrpc_local_sem);
  158. kfree(local);
  159. _leave(" = %d", ret);
  160. return ERR_PTR(ret);
  161. }
  162. up_write(&rxrpc_local_sem);
  163. _net("LOCAL new %d {%d,%u,%pI4+%hu}",
  164. local->debug_id,
  165. local->srx.transport_type,
  166. local->srx.transport.family,
  167. &local->srx.transport.sin.sin_addr,
  168. ntohs(local->srx.transport.sin.sin_port));
  169. _leave(" = %p [new]", local);
  170. return local;
  171. found_local:
  172. rxrpc_get_local(local);
  173. read_unlock_bh(&rxrpc_local_lock);
  174. up_write(&rxrpc_local_sem);
  175. _net("LOCAL old %d {%d,%u,%pI4+%hu}",
  176. local->debug_id,
  177. local->srx.transport_type,
  178. local->srx.transport.family,
  179. &local->srx.transport.sin.sin_addr,
  180. ntohs(local->srx.transport.sin.sin_port));
  181. _leave(" = %p [reuse]", local);
  182. return local;
  183. }
  184. /*
  185. * release a local endpoint
  186. */
  187. void rxrpc_put_local(struct rxrpc_local *local)
  188. {
  189. _enter("%p{u=%d}", local, atomic_read(&local->usage));
  190. ASSERTCMP(atomic_read(&local->usage), >, 0);
  191. /* to prevent a race, the decrement and the dequeue must be effectively
  192. * atomic */
  193. write_lock_bh(&rxrpc_local_lock);
  194. if (unlikely(atomic_dec_and_test(&local->usage))) {
  195. _debug("destroy local");
  196. rxrpc_queue_work(&local->destroyer);
  197. }
  198. write_unlock_bh(&rxrpc_local_lock);
  199. _leave("");
  200. }
  201. /*
  202. * destroy a local endpoint
  203. */
  204. static void rxrpc_destroy_local(struct work_struct *work)
  205. {
  206. struct rxrpc_local *local =
  207. container_of(work, struct rxrpc_local, destroyer);
  208. _enter("%p{%d}", local, atomic_read(&local->usage));
  209. down_write(&rxrpc_local_sem);
  210. write_lock_bh(&rxrpc_local_lock);
  211. if (atomic_read(&local->usage) > 0) {
  212. write_unlock_bh(&rxrpc_local_lock);
  213. up_read(&rxrpc_local_sem);
  214. _leave(" [resurrected]");
  215. return;
  216. }
  217. list_del(&local->link);
  218. local->socket->sk->sk_user_data = NULL;
  219. write_unlock_bh(&rxrpc_local_lock);
  220. downgrade_write(&rxrpc_local_sem);
  221. ASSERT(list_empty(&local->services));
  222. ASSERT(!work_pending(&local->acceptor));
  223. ASSERT(!work_pending(&local->rejecter));
  224. /* finish cleaning up the local descriptor */
  225. rxrpc_purge_queue(&local->accept_queue);
  226. rxrpc_purge_queue(&local->reject_queue);
  227. kernel_sock_shutdown(local->socket, SHUT_RDWR);
  228. sock_release(local->socket);
  229. up_read(&rxrpc_local_sem);
  230. _net("DESTROY LOCAL %d", local->debug_id);
  231. kfree(local);
  232. if (list_empty(&rxrpc_locals))
  233. wake_up_all(&rxrpc_local_wq);
  234. _leave("");
  235. }
  236. /*
  237. * preemptively destroy all local local endpoint rather than waiting for
  238. * them to be destroyed
  239. */
  240. void __exit rxrpc_destroy_all_locals(void)
  241. {
  242. DECLARE_WAITQUEUE(myself,current);
  243. _enter("");
  244. /* we simply have to wait for them to go away */
  245. if (!list_empty(&rxrpc_locals)) {
  246. set_current_state(TASK_UNINTERRUPTIBLE);
  247. add_wait_queue(&rxrpc_local_wq, &myself);
  248. while (!list_empty(&rxrpc_locals)) {
  249. schedule();
  250. set_current_state(TASK_UNINTERRUPTIBLE);
  251. }
  252. remove_wait_queue(&rxrpc_local_wq, &myself);
  253. set_current_state(TASK_RUNNING);
  254. }
  255. _leave("");
  256. }