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