local_object.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Local endpoint object management
  3. *
  4. * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/module.h>
  9. #include <linux/net.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/slab.h>
  12. #include <linux/udp.h>
  13. #include <linux/ip.h>
  14. #include <linux/hashtable.h>
  15. #include <net/sock.h>
  16. #include <net/udp.h>
  17. #include <net/udp_tunnel.h>
  18. #include <net/af_rxrpc.h>
  19. #include "ar-internal.h"
  20. static void rxrpc_local_processor(struct work_struct *);
  21. static void rxrpc_local_rcu(struct rcu_head *);
  22. /*
  23. * Compare a local to an address. Return -ve, 0 or +ve to indicate less than,
  24. * same or greater than.
  25. *
  26. * We explicitly don't compare the RxRPC service ID as we want to reject
  27. * conflicting uses by differing services. Further, we don't want to share
  28. * addresses with different options (IPv6), so we don't compare those bits
  29. * either.
  30. */
  31. static long rxrpc_local_cmp_key(const struct rxrpc_local *local,
  32. const struct sockaddr_rxrpc *srx)
  33. {
  34. long diff;
  35. diff = ((local->srx.transport_type - srx->transport_type) ?:
  36. (local->srx.transport_len - srx->transport_len) ?:
  37. (local->srx.transport.family - srx->transport.family));
  38. if (diff != 0)
  39. return diff;
  40. switch (srx->transport.family) {
  41. case AF_INET:
  42. /* If the choice of UDP port is left up to the transport, then
  43. * the endpoint record doesn't match.
  44. */
  45. return ((u16 __force)local->srx.transport.sin.sin_port -
  46. (u16 __force)srx->transport.sin.sin_port) ?:
  47. memcmp(&local->srx.transport.sin.sin_addr,
  48. &srx->transport.sin.sin_addr,
  49. sizeof(struct in_addr));
  50. #ifdef CONFIG_AF_RXRPC_IPV6
  51. case AF_INET6:
  52. /* If the choice of UDP6 port is left up to the transport, then
  53. * the endpoint record doesn't match.
  54. */
  55. return ((u16 __force)local->srx.transport.sin6.sin6_port -
  56. (u16 __force)srx->transport.sin6.sin6_port) ?:
  57. memcmp(&local->srx.transport.sin6.sin6_addr,
  58. &srx->transport.sin6.sin6_addr,
  59. sizeof(struct in6_addr));
  60. #endif
  61. default:
  62. BUG();
  63. }
  64. }
  65. /*
  66. * Allocate a new local endpoint.
  67. */
  68. static struct rxrpc_local *rxrpc_alloc_local(struct rxrpc_net *rxnet,
  69. const struct sockaddr_rxrpc *srx)
  70. {
  71. struct rxrpc_local *local;
  72. local = kzalloc(sizeof(struct rxrpc_local), GFP_KERNEL);
  73. if (local) {
  74. refcount_set(&local->ref, 1);
  75. atomic_set(&local->active_users, 1);
  76. local->rxnet = rxnet;
  77. INIT_HLIST_NODE(&local->link);
  78. INIT_WORK(&local->processor, rxrpc_local_processor);
  79. init_rwsem(&local->defrag_sem);
  80. skb_queue_head_init(&local->reject_queue);
  81. skb_queue_head_init(&local->event_queue);
  82. local->client_bundles = RB_ROOT;
  83. spin_lock_init(&local->client_bundles_lock);
  84. spin_lock_init(&local->lock);
  85. rwlock_init(&local->services_lock);
  86. local->debug_id = atomic_inc_return(&rxrpc_debug_id);
  87. memcpy(&local->srx, srx, sizeof(*srx));
  88. local->srx.srx_service = 0;
  89. trace_rxrpc_local(local->debug_id, rxrpc_local_new, 1, NULL);
  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 udp_tunnel_sock_cfg tuncfg = {NULL};
  101. struct sockaddr_rxrpc *srx = &local->srx;
  102. struct udp_port_cfg udp_conf = {0};
  103. struct sock *usk;
  104. int ret;
  105. _enter("%p{%d,%d}",
  106. local, srx->transport_type, srx->transport.family);
  107. udp_conf.family = srx->transport.family;
  108. udp_conf.use_udp_checksums = true;
  109. if (udp_conf.family == AF_INET) {
  110. udp_conf.local_ip = srx->transport.sin.sin_addr;
  111. udp_conf.local_udp_port = srx->transport.sin.sin_port;
  112. #if IS_ENABLED(CONFIG_AF_RXRPC_IPV6)
  113. } else {
  114. udp_conf.local_ip6 = srx->transport.sin6.sin6_addr;
  115. udp_conf.local_udp_port = srx->transport.sin6.sin6_port;
  116. udp_conf.use_udp6_tx_checksums = true;
  117. udp_conf.use_udp6_rx_checksums = true;
  118. #endif
  119. }
  120. ret = udp_sock_create(net, &udp_conf, &local->socket);
  121. if (ret < 0) {
  122. _leave(" = %d [socket]", ret);
  123. return ret;
  124. }
  125. tuncfg.encap_type = UDP_ENCAP_RXRPC;
  126. tuncfg.encap_rcv = rxrpc_input_packet;
  127. tuncfg.encap_err_rcv = rxrpc_encap_err_rcv;
  128. tuncfg.sk_user_data = local;
  129. setup_udp_tunnel_sock(net, local->socket, &tuncfg);
  130. /* set the socket up */
  131. usk = local->socket->sk;
  132. usk->sk_error_report = rxrpc_error_report;
  133. switch (srx->transport.family) {
  134. case AF_INET6:
  135. /* we want to receive ICMPv6 errors */
  136. ip6_sock_set_recverr(usk);
  137. /* Fall through and set IPv4 options too otherwise we don't get
  138. * errors from IPv4 packets sent through the IPv6 socket.
  139. */
  140. fallthrough;
  141. case AF_INET:
  142. /* we want to receive ICMP errors */
  143. ip_sock_set_recverr(usk);
  144. /* we want to set the don't fragment bit */
  145. ip_sock_set_mtu_discover(usk, IP_PMTUDISC_DO);
  146. /* We want receive timestamps. */
  147. sock_enable_timestamps(usk);
  148. break;
  149. default:
  150. BUG();
  151. }
  152. _leave(" = 0");
  153. return 0;
  154. }
  155. /*
  156. * Look up or create a new local endpoint using the specified local address.
  157. */
  158. struct rxrpc_local *rxrpc_lookup_local(struct net *net,
  159. const struct sockaddr_rxrpc *srx)
  160. {
  161. struct rxrpc_local *local;
  162. struct rxrpc_net *rxnet = rxrpc_net(net);
  163. struct hlist_node *cursor;
  164. const char *age;
  165. long diff;
  166. int ret;
  167. _enter("{%d,%d,%pISp}",
  168. srx->transport_type, srx->transport.family, &srx->transport);
  169. mutex_lock(&rxnet->local_mutex);
  170. hlist_for_each(cursor, &rxnet->local_endpoints) {
  171. local = hlist_entry(cursor, struct rxrpc_local, link);
  172. diff = rxrpc_local_cmp_key(local, srx);
  173. if (diff != 0)
  174. continue;
  175. /* Services aren't allowed to share transport sockets, so
  176. * reject that here. It is possible that the object is dying -
  177. * but it may also still have the local transport address that
  178. * we want bound.
  179. */
  180. if (srx->srx_service) {
  181. local = NULL;
  182. goto addr_in_use;
  183. }
  184. /* Found a match. We want to replace a dying object.
  185. * Attempting to bind the transport socket may still fail if
  186. * we're attempting to use a local address that the dying
  187. * object is still using.
  188. */
  189. if (!rxrpc_use_local(local))
  190. break;
  191. age = "old";
  192. goto found;
  193. }
  194. local = rxrpc_alloc_local(rxnet, srx);
  195. if (!local)
  196. goto nomem;
  197. ret = rxrpc_open_socket(local, net);
  198. if (ret < 0)
  199. goto sock_error;
  200. if (cursor) {
  201. hlist_replace_rcu(cursor, &local->link);
  202. cursor->pprev = NULL;
  203. } else {
  204. hlist_add_head_rcu(&local->link, &rxnet->local_endpoints);
  205. }
  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. if (local)
  218. call_rcu(&local->rcu, rxrpc_local_rcu);
  219. _leave(" = %d", ret);
  220. return ERR_PTR(ret);
  221. addr_in_use:
  222. mutex_unlock(&rxnet->local_mutex);
  223. _leave(" = -EADDRINUSE");
  224. return ERR_PTR(-EADDRINUSE);
  225. }
  226. /*
  227. * Get a ref on a local endpoint.
  228. */
  229. struct rxrpc_local *rxrpc_get_local(struct rxrpc_local *local)
  230. {
  231. const void *here = __builtin_return_address(0);
  232. int r;
  233. __refcount_inc(&local->ref, &r);
  234. trace_rxrpc_local(local->debug_id, rxrpc_local_got, r + 1, here);
  235. return local;
  236. }
  237. /*
  238. * Get a ref on a local endpoint unless its usage has already reached 0.
  239. */
  240. struct rxrpc_local *rxrpc_get_local_maybe(struct rxrpc_local *local)
  241. {
  242. const void *here = __builtin_return_address(0);
  243. int r;
  244. if (local) {
  245. if (__refcount_inc_not_zero(&local->ref, &r))
  246. trace_rxrpc_local(local->debug_id, rxrpc_local_got,
  247. r + 1, here);
  248. else
  249. local = NULL;
  250. }
  251. return local;
  252. }
  253. /*
  254. * Queue a local endpoint and pass the caller's reference to the work item.
  255. */
  256. void rxrpc_queue_local(struct rxrpc_local *local)
  257. {
  258. const void *here = __builtin_return_address(0);
  259. unsigned int debug_id = local->debug_id;
  260. int r = refcount_read(&local->ref);
  261. if (rxrpc_queue_work(&local->processor))
  262. trace_rxrpc_local(debug_id, rxrpc_local_queued, r + 1, here);
  263. else
  264. rxrpc_put_local(local);
  265. }
  266. /*
  267. * Drop a ref on a local endpoint.
  268. */
  269. void rxrpc_put_local(struct rxrpc_local *local)
  270. {
  271. const void *here = __builtin_return_address(0);
  272. unsigned int debug_id;
  273. bool dead;
  274. int r;
  275. if (local) {
  276. debug_id = local->debug_id;
  277. dead = __refcount_dec_and_test(&local->ref, &r);
  278. trace_rxrpc_local(debug_id, rxrpc_local_put, r, here);
  279. if (dead)
  280. call_rcu(&local->rcu, rxrpc_local_rcu);
  281. }
  282. }
  283. /*
  284. * Start using a local endpoint.
  285. */
  286. struct rxrpc_local *rxrpc_use_local(struct rxrpc_local *local)
  287. {
  288. local = rxrpc_get_local_maybe(local);
  289. if (!local)
  290. return NULL;
  291. if (!__rxrpc_use_local(local)) {
  292. rxrpc_put_local(local);
  293. return NULL;
  294. }
  295. return local;
  296. }
  297. /*
  298. * Cease using a local endpoint. Once the number of active users reaches 0, we
  299. * start the closure of the transport in the work processor.
  300. */
  301. void rxrpc_unuse_local(struct rxrpc_local *local)
  302. {
  303. if (local) {
  304. if (__rxrpc_unuse_local(local)) {
  305. rxrpc_get_local(local);
  306. rxrpc_queue_local(local);
  307. }
  308. }
  309. }
  310. /*
  311. * Destroy a local endpoint's socket and then hand the record to RCU to dispose
  312. * of.
  313. *
  314. * Closing the socket cannot be done from bottom half context or RCU callback
  315. * context because it might sleep.
  316. */
  317. static void rxrpc_local_destroyer(struct rxrpc_local *local)
  318. {
  319. struct socket *socket = local->socket;
  320. struct rxrpc_net *rxnet = local->rxnet;
  321. _enter("%d", local->debug_id);
  322. local->dead = true;
  323. mutex_lock(&rxnet->local_mutex);
  324. hlist_del_init_rcu(&local->link);
  325. mutex_unlock(&rxnet->local_mutex);
  326. rxrpc_clean_up_local_conns(local);
  327. rxrpc_service_connection_reaper(&rxnet->service_conn_reaper);
  328. ASSERT(!local->service);
  329. if (socket) {
  330. local->socket = NULL;
  331. kernel_sock_shutdown(socket, SHUT_RDWR);
  332. socket->sk->sk_user_data = NULL;
  333. sock_release(socket);
  334. }
  335. /* At this point, there should be no more packets coming in to the
  336. * local endpoint.
  337. */
  338. rxrpc_purge_queue(&local->reject_queue);
  339. rxrpc_purge_queue(&local->event_queue);
  340. }
  341. /*
  342. * Process events on an endpoint. The work item carries a ref which
  343. * we must release.
  344. */
  345. static void rxrpc_local_processor(struct work_struct *work)
  346. {
  347. struct rxrpc_local *local =
  348. container_of(work, struct rxrpc_local, processor);
  349. bool again;
  350. if (local->dead)
  351. return;
  352. trace_rxrpc_local(local->debug_id, rxrpc_local_processing,
  353. refcount_read(&local->ref), NULL);
  354. do {
  355. again = false;
  356. if (!__rxrpc_use_local(local)) {
  357. rxrpc_local_destroyer(local);
  358. break;
  359. }
  360. if (!skb_queue_empty(&local->reject_queue)) {
  361. rxrpc_reject_packets(local);
  362. again = true;
  363. }
  364. if (!skb_queue_empty(&local->event_queue)) {
  365. rxrpc_process_local_events(local);
  366. again = true;
  367. }
  368. __rxrpc_unuse_local(local);
  369. } while (again);
  370. rxrpc_put_local(local);
  371. }
  372. /*
  373. * Destroy a local endpoint after the RCU grace period expires.
  374. */
  375. static void rxrpc_local_rcu(struct rcu_head *rcu)
  376. {
  377. struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
  378. _enter("%d", local->debug_id);
  379. ASSERT(!work_pending(&local->processor));
  380. _net("DESTROY LOCAL %d", local->debug_id);
  381. kfree(local);
  382. _leave("");
  383. }
  384. /*
  385. * Verify the local endpoint list is empty by this point.
  386. */
  387. void rxrpc_destroy_all_locals(struct rxrpc_net *rxnet)
  388. {
  389. struct rxrpc_local *local;
  390. _enter("");
  391. flush_workqueue(rxrpc_workqueue);
  392. if (!hlist_empty(&rxnet->local_endpoints)) {
  393. mutex_lock(&rxnet->local_mutex);
  394. hlist_for_each_entry(local, &rxnet->local_endpoints, link) {
  395. pr_err("AF_RXRPC: Leaked local %p {%d}\n",
  396. local, refcount_read(&local->ref));
  397. }
  398. mutex_unlock(&rxnet->local_mutex);
  399. BUG();
  400. }
  401. }