local_object.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. default:
  55. BUG();
  56. }
  57. }
  58. /*
  59. * Allocate a new local endpoint.
  60. */
  61. static struct rxrpc_local *rxrpc_alloc_local(const struct sockaddr_rxrpc *srx)
  62. {
  63. struct rxrpc_local *local;
  64. local = kzalloc(sizeof(struct rxrpc_local), GFP_KERNEL);
  65. if (local) {
  66. atomic_set(&local->usage, 1);
  67. INIT_LIST_HEAD(&local->link);
  68. INIT_WORK(&local->processor, rxrpc_local_processor);
  69. INIT_LIST_HEAD(&local->services);
  70. init_rwsem(&local->defrag_sem);
  71. skb_queue_head_init(&local->accept_queue);
  72. skb_queue_head_init(&local->reject_queue);
  73. skb_queue_head_init(&local->event_queue);
  74. local->client_conns = RB_ROOT;
  75. spin_lock_init(&local->client_conns_lock);
  76. spin_lock_init(&local->lock);
  77. rwlock_init(&local->services_lock);
  78. local->debug_id = atomic_inc_return(&rxrpc_debug_id);
  79. memcpy(&local->srx, srx, sizeof(*srx));
  80. }
  81. _leave(" = %p", local);
  82. return local;
  83. }
  84. /*
  85. * create the local socket
  86. * - must be called with rxrpc_local_mutex locked
  87. */
  88. static int rxrpc_open_socket(struct rxrpc_local *local)
  89. {
  90. struct sock *sock;
  91. int ret, opt;
  92. _enter("%p{%d}", local, local->srx.transport_type);
  93. /* create a socket to represent the local endpoint */
  94. ret = sock_create_kern(&init_net, PF_INET, local->srx.transport_type,
  95. IPPROTO_UDP, &local->socket);
  96. if (ret < 0) {
  97. _leave(" = %d [socket]", ret);
  98. return ret;
  99. }
  100. /* if a local address was supplied then bind it */
  101. if (local->srx.transport_len > sizeof(sa_family_t)) {
  102. _debug("bind");
  103. ret = kernel_bind(local->socket,
  104. (struct sockaddr *)&local->srx.transport,
  105. local->srx.transport_len);
  106. if (ret < 0) {
  107. _debug("bind failed %d", ret);
  108. goto error;
  109. }
  110. }
  111. /* we want to receive ICMP errors */
  112. opt = 1;
  113. ret = kernel_setsockopt(local->socket, SOL_IP, IP_RECVERR,
  114. (char *) &opt, sizeof(opt));
  115. if (ret < 0) {
  116. _debug("setsockopt failed");
  117. goto error;
  118. }
  119. /* we want to set the don't fragment bit */
  120. opt = IP_PMTUDISC_DO;
  121. ret = kernel_setsockopt(local->socket, SOL_IP, IP_MTU_DISCOVER,
  122. (char *) &opt, sizeof(opt));
  123. if (ret < 0) {
  124. _debug("setsockopt failed");
  125. goto error;
  126. }
  127. /* set the socket up */
  128. sock = local->socket->sk;
  129. sock->sk_user_data = local;
  130. sock->sk_data_ready = rxrpc_data_ready;
  131. sock->sk_error_report = rxrpc_error_report;
  132. _leave(" = 0");
  133. return 0;
  134. error:
  135. kernel_sock_shutdown(local->socket, SHUT_RDWR);
  136. local->socket->sk->sk_user_data = NULL;
  137. sock_release(local->socket);
  138. local->socket = NULL;
  139. _leave(" = %d", ret);
  140. return ret;
  141. }
  142. /*
  143. * Look up or create a new local endpoint using the specified local address.
  144. */
  145. struct rxrpc_local *rxrpc_lookup_local(const struct sockaddr_rxrpc *srx)
  146. {
  147. struct rxrpc_local *local;
  148. struct list_head *cursor;
  149. const char *age;
  150. long diff;
  151. int ret;
  152. if (srx->transport.family == AF_INET) {
  153. _enter("{%d,%u,%pI4+%hu}",
  154. srx->transport_type,
  155. srx->transport.family,
  156. &srx->transport.sin.sin_addr,
  157. ntohs(srx->transport.sin.sin_port));
  158. } else {
  159. _enter("{%d,%u}",
  160. srx->transport_type,
  161. srx->transport.family);
  162. return ERR_PTR(-EAFNOSUPPORT);
  163. }
  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 {%d,%u,%pI4+%hu}",
  206. age,
  207. local->debug_id,
  208. local->srx.transport_type,
  209. local->srx.transport.family,
  210. &local->srx.transport.sin.sin_addr,
  211. ntohs(local->srx.transport.sin.sin_port));
  212. _leave(" = %p", local);
  213. return local;
  214. nomem:
  215. ret = -ENOMEM;
  216. sock_error:
  217. mutex_unlock(&rxrpc_local_mutex);
  218. kfree(local);
  219. _leave(" = %d", ret);
  220. return ERR_PTR(ret);
  221. addr_in_use:
  222. mutex_unlock(&rxrpc_local_mutex);
  223. _leave(" = -EADDRINUSE");
  224. return ERR_PTR(-EADDRINUSE);
  225. }
  226. /*
  227. * A local endpoint reached its end of life.
  228. */
  229. void __rxrpc_put_local(struct rxrpc_local *local)
  230. {
  231. _enter("%d", local->debug_id);
  232. rxrpc_queue_work(&local->processor);
  233. }
  234. /*
  235. * Destroy a local endpoint's socket and then hand the record to RCU to dispose
  236. * of.
  237. *
  238. * Closing the socket cannot be done from bottom half context or RCU callback
  239. * context because it might sleep.
  240. */
  241. static void rxrpc_local_destroyer(struct rxrpc_local *local)
  242. {
  243. struct socket *socket = local->socket;
  244. _enter("%d", local->debug_id);
  245. /* We can get a race between an incoming call packet queueing the
  246. * processor again and the work processor starting the destruction
  247. * process which will shut down the UDP socket.
  248. */
  249. if (local->dead) {
  250. _leave(" [already dead]");
  251. return;
  252. }
  253. local->dead = true;
  254. mutex_lock(&rxrpc_local_mutex);
  255. list_del_init(&local->link);
  256. mutex_unlock(&rxrpc_local_mutex);
  257. ASSERT(RB_EMPTY_ROOT(&local->client_conns));
  258. ASSERT(list_empty(&local->services));
  259. if (socket) {
  260. local->socket = NULL;
  261. kernel_sock_shutdown(socket, SHUT_RDWR);
  262. socket->sk->sk_user_data = NULL;
  263. sock_release(socket);
  264. }
  265. /* At this point, there should be no more packets coming in to the
  266. * local endpoint.
  267. */
  268. rxrpc_purge_queue(&local->accept_queue);
  269. rxrpc_purge_queue(&local->reject_queue);
  270. rxrpc_purge_queue(&local->event_queue);
  271. _debug("rcu local %d", local->debug_id);
  272. call_rcu(&local->rcu, rxrpc_local_rcu);
  273. }
  274. /*
  275. * Process events on an endpoint
  276. */
  277. static void rxrpc_local_processor(struct work_struct *work)
  278. {
  279. struct rxrpc_local *local =
  280. container_of(work, struct rxrpc_local, processor);
  281. bool again;
  282. _enter("%d", local->debug_id);
  283. do {
  284. again = false;
  285. if (atomic_read(&local->usage) == 0)
  286. return rxrpc_local_destroyer(local);
  287. if (!skb_queue_empty(&local->accept_queue)) {
  288. rxrpc_accept_incoming_calls(local);
  289. again = true;
  290. }
  291. if (!skb_queue_empty(&local->reject_queue)) {
  292. rxrpc_reject_packets(local);
  293. again = true;
  294. }
  295. if (!skb_queue_empty(&local->event_queue)) {
  296. rxrpc_process_local_events(local);
  297. again = true;
  298. }
  299. } while (again);
  300. }
  301. /*
  302. * Destroy a local endpoint after the RCU grace period expires.
  303. */
  304. static void rxrpc_local_rcu(struct rcu_head *rcu)
  305. {
  306. struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
  307. _enter("%d", local->debug_id);
  308. ASSERT(!work_pending(&local->processor));
  309. _net("DESTROY LOCAL %d", local->debug_id);
  310. kfree(local);
  311. _leave("");
  312. }
  313. /*
  314. * Verify the local endpoint list is empty by this point.
  315. */
  316. void __exit rxrpc_destroy_all_locals(void)
  317. {
  318. struct rxrpc_local *local;
  319. _enter("");
  320. flush_workqueue(rxrpc_workqueue);
  321. if (!list_empty(&rxrpc_local_endpoints)) {
  322. mutex_lock(&rxrpc_local_mutex);
  323. list_for_each_entry(local, &rxrpc_local_endpoints, link) {
  324. pr_err("AF_RXRPC: Leaked local %p {%d}\n",
  325. local, atomic_read(&local->usage));
  326. }
  327. mutex_unlock(&rxrpc_local_mutex);
  328. BUG();
  329. }
  330. rcu_barrier();
  331. }