local_object.c 12 KB

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