local_object.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* Local endpoint object management
  2. *
  3. * Copyright (C) 2016 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 Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/module.h>
  13. #include <linux/net.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/slab.h>
  16. #include <linux/udp.h>
  17. #include <linux/ip.h>
  18. #include <linux/hashtable.h>
  19. #include <net/sock.h>
  20. #include <net/af_rxrpc.h>
  21. #include "ar-internal.h"
  22. static void rxrpc_local_processor(struct work_struct *);
  23. static void rxrpc_local_rcu(struct rcu_head *);
  24. static DEFINE_MUTEX(rxrpc_local_mutex);
  25. static LIST_HEAD(rxrpc_local_endpoints);
  26. /*
  27. * Compare a local to an address. Return -ve, 0 or +ve to indicate less than,
  28. * same or greater than.
  29. *
  30. * We explicitly don't compare the RxRPC service ID as we want to reject
  31. * conflicting uses by differing services. Further, we don't want to share
  32. * addresses with different options (IPv6), so we don't compare those bits
  33. * either.
  34. */
  35. static long rxrpc_local_cmp_key(const struct rxrpc_local *local,
  36. const struct sockaddr_rxrpc *srx)
  37. {
  38. long diff;
  39. diff = ((local->srx.transport_type - srx->transport_type) ?:
  40. (local->srx.transport_len - srx->transport_len) ?:
  41. (local->srx.transport.family - srx->transport.family));
  42. if (diff != 0)
  43. return diff;
  44. switch (srx->transport.family) {
  45. case AF_INET:
  46. /* If the choice of UDP port is left up to the transport, then
  47. * the endpoint record doesn't match.
  48. */
  49. return ((u16 __force)local->srx.transport.sin.sin_port -
  50. (u16 __force)srx->transport.sin.sin_port) ?:
  51. memcmp(&local->srx.transport.sin.sin_addr,
  52. &srx->transport.sin.sin_addr,
  53. sizeof(struct in_addr));
  54. #ifdef CONFIG_AF_RXRPC_IPV6
  55. case AF_INET6:
  56. /* If the choice of UDP6 port is left up to the transport, then
  57. * the endpoint record doesn't match.
  58. */
  59. return ((u16 __force)local->srx.transport.sin6.sin6_port -
  60. (u16 __force)srx->transport.sin6.sin6_port) ?:
  61. memcmp(&local->srx.transport.sin6.sin6_addr,
  62. &srx->transport.sin6.sin6_addr,
  63. sizeof(struct in6_addr));
  64. #endif
  65. default:
  66. BUG();
  67. }
  68. }
  69. /*
  70. * Allocate a new local endpoint.
  71. */
  72. static struct rxrpc_local *rxrpc_alloc_local(const struct sockaddr_rxrpc *srx)
  73. {
  74. struct rxrpc_local *local;
  75. local = kzalloc(sizeof(struct rxrpc_local), GFP_KERNEL);
  76. if (local) {
  77. atomic_set(&local->usage, 1);
  78. INIT_LIST_HEAD(&local->link);
  79. INIT_WORK(&local->processor, rxrpc_local_processor);
  80. init_rwsem(&local->defrag_sem);
  81. skb_queue_head_init(&local->reject_queue);
  82. skb_queue_head_init(&local->event_queue);
  83. local->client_conns = RB_ROOT;
  84. spin_lock_init(&local->client_conns_lock);
  85. spin_lock_init(&local->lock);
  86. rwlock_init(&local->services_lock);
  87. local->debug_id = atomic_inc_return(&rxrpc_debug_id);
  88. memcpy(&local->srx, srx, sizeof(*srx));
  89. }
  90. _leave(" = %p", local);
  91. return local;
  92. }
  93. /*
  94. * create the local socket
  95. * - must be called with rxrpc_local_mutex locked
  96. */
  97. static int rxrpc_open_socket(struct rxrpc_local *local)
  98. {
  99. struct sock *sock;
  100. int ret, opt;
  101. _enter("%p{%d,%d}",
  102. local, local->srx.transport_type, local->srx.transport.family);
  103. /* create a socket to represent the local endpoint */
  104. ret = sock_create_kern(&init_net, local->srx.transport.family,
  105. local->srx.transport_type, 0, &local->socket);
  106. if (ret < 0) {
  107. _leave(" = %d [socket]", ret);
  108. return ret;
  109. }
  110. /* if a local address was supplied then bind it */
  111. if (local->srx.transport_len > sizeof(sa_family_t)) {
  112. _debug("bind");
  113. ret = kernel_bind(local->socket,
  114. (struct sockaddr *)&local->srx.transport,
  115. local->srx.transport_len);
  116. if (ret < 0) {
  117. _debug("bind failed %d", ret);
  118. goto error;
  119. }
  120. }
  121. /* we want to receive ICMP errors */
  122. opt = 1;
  123. ret = kernel_setsockopt(local->socket, SOL_IP, IP_RECVERR,
  124. (char *) &opt, sizeof(opt));
  125. if (ret < 0) {
  126. _debug("setsockopt failed");
  127. goto error;
  128. }
  129. /* we want to set the don't fragment bit */
  130. opt = IP_PMTUDISC_DO;
  131. ret = kernel_setsockopt(local->socket, SOL_IP, IP_MTU_DISCOVER,
  132. (char *) &opt, sizeof(opt));
  133. if (ret < 0) {
  134. _debug("setsockopt failed");
  135. goto error;
  136. }
  137. /* set the socket up */
  138. sock = local->socket->sk;
  139. sock->sk_user_data = local;
  140. sock->sk_data_ready = rxrpc_data_ready;
  141. sock->sk_error_report = rxrpc_error_report;
  142. _leave(" = 0");
  143. return 0;
  144. error:
  145. kernel_sock_shutdown(local->socket, SHUT_RDWR);
  146. local->socket->sk->sk_user_data = NULL;
  147. sock_release(local->socket);
  148. local->socket = NULL;
  149. _leave(" = %d", ret);
  150. return ret;
  151. }
  152. /*
  153. * Look up or create a new local endpoint using the specified local address.
  154. */
  155. struct rxrpc_local *rxrpc_lookup_local(const struct sockaddr_rxrpc *srx)
  156. {
  157. struct rxrpc_local *local;
  158. struct list_head *cursor;
  159. const char *age;
  160. long diff;
  161. int ret;
  162. _enter("{%d,%d,%pISp}",
  163. srx->transport_type, srx->transport.family, &srx->transport);
  164. mutex_lock(&rxrpc_local_mutex);
  165. for (cursor = rxrpc_local_endpoints.next;
  166. cursor != &rxrpc_local_endpoints;
  167. cursor = cursor->next) {
  168. local = list_entry(cursor, struct rxrpc_local, link);
  169. diff = rxrpc_local_cmp_key(local, srx);
  170. if (diff < 0)
  171. continue;
  172. if (diff > 0)
  173. break;
  174. /* Services aren't allowed to share transport sockets, so
  175. * reject that here. It is possible that the object is dying -
  176. * but it may also still have the local transport address that
  177. * we want bound.
  178. */
  179. if (srx->srx_service) {
  180. local = NULL;
  181. goto addr_in_use;
  182. }
  183. /* Found a match. We replace a dying object. Attempting to
  184. * bind the transport socket may still fail if we're attempting
  185. * to use a local address that the dying object is still using.
  186. */
  187. if (!rxrpc_get_local_maybe(local)) {
  188. cursor = cursor->next;
  189. list_del_init(&local->link);
  190. break;
  191. }
  192. age = "old";
  193. goto found;
  194. }
  195. local = rxrpc_alloc_local(srx);
  196. if (!local)
  197. goto nomem;
  198. ret = rxrpc_open_socket(local);
  199. if (ret < 0)
  200. goto sock_error;
  201. list_add_tail(&local->link, cursor);
  202. age = "new";
  203. found:
  204. mutex_unlock(&rxrpc_local_mutex);
  205. _net("LOCAL %s %d {%pISp}",
  206. age, local->debug_id, &local->srx.transport);
  207. _leave(" = %p", local);
  208. return local;
  209. nomem:
  210. ret = -ENOMEM;
  211. sock_error:
  212. mutex_unlock(&rxrpc_local_mutex);
  213. kfree(local);
  214. _leave(" = %d", ret);
  215. return ERR_PTR(ret);
  216. addr_in_use:
  217. mutex_unlock(&rxrpc_local_mutex);
  218. _leave(" = -EADDRINUSE");
  219. return ERR_PTR(-EADDRINUSE);
  220. }
  221. /*
  222. * A local endpoint reached its end of life.
  223. */
  224. void __rxrpc_put_local(struct rxrpc_local *local)
  225. {
  226. _enter("%d", local->debug_id);
  227. rxrpc_queue_work(&local->processor);
  228. }
  229. /*
  230. * Destroy a local endpoint's socket and then hand the record to RCU to dispose
  231. * of.
  232. *
  233. * Closing the socket cannot be done from bottom half context or RCU callback
  234. * context because it might sleep.
  235. */
  236. static void rxrpc_local_destroyer(struct rxrpc_local *local)
  237. {
  238. struct socket *socket = local->socket;
  239. _enter("%d", local->debug_id);
  240. /* We can get a race between an incoming call packet queueing the
  241. * processor again and the work processor starting the destruction
  242. * process which will shut down the UDP socket.
  243. */
  244. if (local->dead) {
  245. _leave(" [already dead]");
  246. return;
  247. }
  248. local->dead = true;
  249. mutex_lock(&rxrpc_local_mutex);
  250. list_del_init(&local->link);
  251. mutex_unlock(&rxrpc_local_mutex);
  252. ASSERT(RB_EMPTY_ROOT(&local->client_conns));
  253. ASSERT(!local->service);
  254. if (socket) {
  255. local->socket = NULL;
  256. kernel_sock_shutdown(socket, SHUT_RDWR);
  257. socket->sk->sk_user_data = NULL;
  258. sock_release(socket);
  259. }
  260. /* At this point, there should be no more packets coming in to the
  261. * local endpoint.
  262. */
  263. rxrpc_purge_queue(&local->reject_queue);
  264. rxrpc_purge_queue(&local->event_queue);
  265. _debug("rcu local %d", local->debug_id);
  266. call_rcu(&local->rcu, rxrpc_local_rcu);
  267. }
  268. /*
  269. * Process events on an endpoint
  270. */
  271. static void rxrpc_local_processor(struct work_struct *work)
  272. {
  273. struct rxrpc_local *local =
  274. container_of(work, struct rxrpc_local, processor);
  275. bool again;
  276. _enter("%d", local->debug_id);
  277. do {
  278. again = false;
  279. if (atomic_read(&local->usage) == 0)
  280. return rxrpc_local_destroyer(local);
  281. if (!skb_queue_empty(&local->reject_queue)) {
  282. rxrpc_reject_packets(local);
  283. again = true;
  284. }
  285. if (!skb_queue_empty(&local->event_queue)) {
  286. rxrpc_process_local_events(local);
  287. again = true;
  288. }
  289. } while (again);
  290. }
  291. /*
  292. * Destroy a local endpoint after the RCU grace period expires.
  293. */
  294. static void rxrpc_local_rcu(struct rcu_head *rcu)
  295. {
  296. struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
  297. _enter("%d", local->debug_id);
  298. ASSERT(!work_pending(&local->processor));
  299. _net("DESTROY LOCAL %d", local->debug_id);
  300. kfree(local);
  301. _leave("");
  302. }
  303. /*
  304. * Verify the local endpoint list is empty by this point.
  305. */
  306. void __exit rxrpc_destroy_all_locals(void)
  307. {
  308. struct rxrpc_local *local;
  309. _enter("");
  310. flush_workqueue(rxrpc_workqueue);
  311. if (!list_empty(&rxrpc_local_endpoints)) {
  312. mutex_lock(&rxrpc_local_mutex);
  313. list_for_each_entry(local, &rxrpc_local_endpoints, link) {
  314. pr_err("AF_RXRPC: Leaked local %p {%d}\n",
  315. local, atomic_read(&local->usage));
  316. }
  317. mutex_unlock(&rxrpc_local_mutex);
  318. BUG();
  319. }
  320. rcu_barrier();
  321. }