local_object.c 12 KB

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