local_object.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. trace_rxrpc_local(local, rxrpc_local_new, 1, NULL);
  91. }
  92. _leave(" = %p", local);
  93. return local;
  94. }
  95. /*
  96. * create the local socket
  97. * - must be called with rxrpc_local_mutex locked
  98. */
  99. static int rxrpc_open_socket(struct rxrpc_local *local, struct net *net)
  100. {
  101. struct sock *sock;
  102. int ret, opt;
  103. _enter("%p{%d,%d}",
  104. local, local->srx.transport_type, local->srx.transport.family);
  105. /* create a socket to represent the local endpoint */
  106. ret = sock_create_kern(net, local->srx.transport.family,
  107. local->srx.transport_type, 0, &local->socket);
  108. if (ret < 0) {
  109. _leave(" = %d [socket]", ret);
  110. return ret;
  111. }
  112. /* if a local address was supplied then bind it */
  113. if (local->srx.transport_len > sizeof(sa_family_t)) {
  114. _debug("bind");
  115. ret = kernel_bind(local->socket,
  116. (struct sockaddr *)&local->srx.transport,
  117. local->srx.transport_len);
  118. if (ret < 0) {
  119. _debug("bind failed %d", ret);
  120. goto error;
  121. }
  122. }
  123. /* we want to receive ICMP errors */
  124. opt = 1;
  125. ret = kernel_setsockopt(local->socket, SOL_IP, IP_RECVERR,
  126. (char *) &opt, sizeof(opt));
  127. if (ret < 0) {
  128. _debug("setsockopt failed");
  129. goto error;
  130. }
  131. /* we want to set the don't fragment bit */
  132. opt = IP_PMTUDISC_DO;
  133. ret = kernel_setsockopt(local->socket, SOL_IP, IP_MTU_DISCOVER,
  134. (char *) &opt, sizeof(opt));
  135. if (ret < 0) {
  136. _debug("setsockopt failed");
  137. goto error;
  138. }
  139. /* set the socket up */
  140. sock = local->socket->sk;
  141. sock->sk_user_data = local;
  142. sock->sk_data_ready = rxrpc_data_ready;
  143. sock->sk_error_report = rxrpc_error_report;
  144. _leave(" = 0");
  145. return 0;
  146. error:
  147. kernel_sock_shutdown(local->socket, SHUT_RDWR);
  148. local->socket->sk->sk_user_data = NULL;
  149. sock_release(local->socket);
  150. local->socket = NULL;
  151. _leave(" = %d", ret);
  152. return ret;
  153. }
  154. /*
  155. * Look up or create a new local endpoint using the specified local address.
  156. */
  157. struct rxrpc_local *rxrpc_lookup_local(struct net *net,
  158. const struct sockaddr_rxrpc *srx)
  159. {
  160. struct rxrpc_local *local;
  161. struct rxrpc_net *rxnet = rxrpc_net(net);
  162. struct list_head *cursor;
  163. const char *age;
  164. long diff;
  165. int ret;
  166. _enter("{%d,%d,%pISp}",
  167. srx->transport_type, srx->transport.family, &srx->transport);
  168. mutex_lock(&rxnet->local_mutex);
  169. for (cursor = rxnet->local_endpoints.next;
  170. cursor != &rxnet->local_endpoints;
  171. cursor = cursor->next) {
  172. local = list_entry(cursor, struct rxrpc_local, link);
  173. diff = rxrpc_local_cmp_key(local, srx);
  174. if (diff < 0)
  175. continue;
  176. if (diff > 0)
  177. break;
  178. /* Services aren't allowed to share transport sockets, so
  179. * reject that here. It is possible that the object is dying -
  180. * but it may also still have the local transport address that
  181. * we want bound.
  182. */
  183. if (srx->srx_service) {
  184. local = NULL;
  185. goto addr_in_use;
  186. }
  187. /* Found a match. We replace a dying object. Attempting to
  188. * bind the transport socket may still fail if we're attempting
  189. * to use a local address that the dying object is still using.
  190. */
  191. if (!rxrpc_get_local_maybe(local)) {
  192. cursor = cursor->next;
  193. list_del_init(&local->link);
  194. break;
  195. }
  196. age = "old";
  197. goto found;
  198. }
  199. local = rxrpc_alloc_local(rxnet, srx);
  200. if (!local)
  201. goto nomem;
  202. ret = rxrpc_open_socket(local, net);
  203. if (ret < 0)
  204. goto sock_error;
  205. list_add_tail(&local->link, cursor);
  206. age = "new";
  207. found:
  208. mutex_unlock(&rxnet->local_mutex);
  209. _net("LOCAL %s %d {%pISp}",
  210. age, local->debug_id, &local->srx.transport);
  211. _leave(" = %p", local);
  212. return local;
  213. nomem:
  214. ret = -ENOMEM;
  215. sock_error:
  216. mutex_unlock(&rxnet->local_mutex);
  217. kfree(local);
  218. _leave(" = %d", ret);
  219. return ERR_PTR(ret);
  220. addr_in_use:
  221. mutex_unlock(&rxnet->local_mutex);
  222. _leave(" = -EADDRINUSE");
  223. return ERR_PTR(-EADDRINUSE);
  224. }
  225. /*
  226. * Get a ref on a local endpoint.
  227. */
  228. struct rxrpc_local *rxrpc_get_local(struct rxrpc_local *local)
  229. {
  230. const void *here = __builtin_return_address(0);
  231. int n;
  232. n = atomic_inc_return(&local->usage);
  233. trace_rxrpc_local(local, rxrpc_local_got, n, here);
  234. return local;
  235. }
  236. /*
  237. * Get a ref on a local endpoint unless its usage has already reached 0.
  238. */
  239. struct rxrpc_local *rxrpc_get_local_maybe(struct rxrpc_local *local)
  240. {
  241. const void *here = __builtin_return_address(0);
  242. if (local) {
  243. int n = __atomic_add_unless(&local->usage, 1, 0);
  244. if (n > 0)
  245. trace_rxrpc_local(local, rxrpc_local_got, n + 1, here);
  246. else
  247. local = NULL;
  248. }
  249. return local;
  250. }
  251. /*
  252. * Queue a local endpoint.
  253. */
  254. void rxrpc_queue_local(struct rxrpc_local *local)
  255. {
  256. const void *here = __builtin_return_address(0);
  257. if (rxrpc_queue_work(&local->processor))
  258. trace_rxrpc_local(local, rxrpc_local_queued,
  259. atomic_read(&local->usage), here);
  260. }
  261. /*
  262. * A local endpoint reached its end of life.
  263. */
  264. static void __rxrpc_put_local(struct rxrpc_local *local)
  265. {
  266. _enter("%d", local->debug_id);
  267. rxrpc_queue_work(&local->processor);
  268. }
  269. /*
  270. * Drop a ref on a local endpoint.
  271. */
  272. void rxrpc_put_local(struct rxrpc_local *local)
  273. {
  274. const void *here = __builtin_return_address(0);
  275. int n;
  276. if (local) {
  277. n = atomic_dec_return(&local->usage);
  278. trace_rxrpc_local(local, rxrpc_local_put, n, here);
  279. if (n == 0)
  280. __rxrpc_put_local(local);
  281. }
  282. }
  283. /*
  284. * Destroy a local endpoint's socket and then hand the record to RCU to dispose
  285. * of.
  286. *
  287. * Closing the socket cannot be done from bottom half context or RCU callback
  288. * context because it might sleep.
  289. */
  290. static void rxrpc_local_destroyer(struct rxrpc_local *local)
  291. {
  292. struct socket *socket = local->socket;
  293. struct rxrpc_net *rxnet = local->rxnet;
  294. _enter("%d", local->debug_id);
  295. /* We can get a race between an incoming call packet queueing the
  296. * processor again and the work processor starting the destruction
  297. * process which will shut down the UDP socket.
  298. */
  299. if (local->dead) {
  300. _leave(" [already dead]");
  301. return;
  302. }
  303. local->dead = true;
  304. mutex_lock(&rxnet->local_mutex);
  305. list_del_init(&local->link);
  306. mutex_unlock(&rxnet->local_mutex);
  307. ASSERT(RB_EMPTY_ROOT(&local->client_conns));
  308. ASSERT(!local->service);
  309. if (socket) {
  310. local->socket = NULL;
  311. kernel_sock_shutdown(socket, SHUT_RDWR);
  312. socket->sk->sk_user_data = NULL;
  313. sock_release(socket);
  314. }
  315. /* At this point, there should be no more packets coming in to the
  316. * local endpoint.
  317. */
  318. rxrpc_purge_queue(&local->reject_queue);
  319. rxrpc_purge_queue(&local->event_queue);
  320. _debug("rcu local %d", local->debug_id);
  321. call_rcu(&local->rcu, rxrpc_local_rcu);
  322. }
  323. /*
  324. * Process events on an endpoint
  325. */
  326. static void rxrpc_local_processor(struct work_struct *work)
  327. {
  328. struct rxrpc_local *local =
  329. container_of(work, struct rxrpc_local, processor);
  330. bool again;
  331. trace_rxrpc_local(local, rxrpc_local_processing,
  332. atomic_read(&local->usage), NULL);
  333. do {
  334. again = false;
  335. if (atomic_read(&local->usage) == 0)
  336. return rxrpc_local_destroyer(local);
  337. if (!skb_queue_empty(&local->reject_queue)) {
  338. rxrpc_reject_packets(local);
  339. again = true;
  340. }
  341. if (!skb_queue_empty(&local->event_queue)) {
  342. rxrpc_process_local_events(local);
  343. again = true;
  344. }
  345. } while (again);
  346. }
  347. /*
  348. * Destroy a local endpoint after the RCU grace period expires.
  349. */
  350. static void rxrpc_local_rcu(struct rcu_head *rcu)
  351. {
  352. struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
  353. _enter("%d", local->debug_id);
  354. ASSERT(!work_pending(&local->processor));
  355. _net("DESTROY LOCAL %d", local->debug_id);
  356. kfree(local);
  357. _leave("");
  358. }
  359. /*
  360. * Verify the local endpoint list is empty by this point.
  361. */
  362. void rxrpc_destroy_all_locals(struct rxrpc_net *rxnet)
  363. {
  364. struct rxrpc_local *local;
  365. _enter("");
  366. flush_workqueue(rxrpc_workqueue);
  367. if (!list_empty(&rxnet->local_endpoints)) {
  368. mutex_lock(&rxnet->local_mutex);
  369. list_for_each_entry(local, &rxnet->local_endpoints, link) {
  370. pr_err("AF_RXRPC: Leaked local %p {%d}\n",
  371. local, atomic_read(&local->usage));
  372. }
  373. mutex_unlock(&rxnet->local_mutex);
  374. BUG();
  375. }
  376. }