local_object.c 11 KB

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