local_object.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. atomic_set(&local->active_users, 1);
  79. local->rxnet = rxnet;
  80. INIT_LIST_HEAD(&local->link);
  81. INIT_WORK(&local->processor, rxrpc_local_processor);
  82. init_rwsem(&local->defrag_sem);
  83. skb_queue_head_init(&local->reject_queue);
  84. skb_queue_head_init(&local->event_queue);
  85. local->client_conns = RB_ROOT;
  86. spin_lock_init(&local->client_conns_lock);
  87. spin_lock_init(&local->lock);
  88. rwlock_init(&local->services_lock);
  89. local->debug_id = atomic_inc_return(&rxrpc_debug_id);
  90. memcpy(&local->srx, srx, sizeof(*srx));
  91. local->srx.srx_service = 0;
  92. trace_rxrpc_local(local->debug_id, rxrpc_local_new, 1, NULL);
  93. }
  94. _leave(" = %p", local);
  95. return local;
  96. }
  97. /*
  98. * create the local socket
  99. * - must be called with rxrpc_local_mutex locked
  100. */
  101. static int rxrpc_open_socket(struct rxrpc_local *local, struct net *net)
  102. {
  103. struct sock *usk;
  104. int ret, opt;
  105. _enter("%p{%d,%d}",
  106. local, local->srx.transport_type, local->srx.transport.family);
  107. /* create a socket to represent the local endpoint */
  108. ret = sock_create_kern(net, local->srx.transport.family,
  109. local->srx.transport_type, 0, &local->socket);
  110. if (ret < 0) {
  111. _leave(" = %d [socket]", ret);
  112. return ret;
  113. }
  114. /* set the socket up */
  115. usk = local->socket->sk;
  116. inet_sk(usk)->mc_loop = 0;
  117. /* Enable CHECKSUM_UNNECESSARY to CHECKSUM_COMPLETE conversion */
  118. inet_inc_convert_csum(usk);
  119. rcu_assign_sk_user_data(usk, local);
  120. udp_sk(usk)->encap_type = UDP_ENCAP_RXRPC;
  121. udp_sk(usk)->encap_rcv = rxrpc_input_packet;
  122. udp_sk(usk)->encap_destroy = NULL;
  123. udp_sk(usk)->gro_receive = NULL;
  124. udp_sk(usk)->gro_complete = NULL;
  125. udp_encap_enable();
  126. #if IS_ENABLED(CONFIG_AF_RXRPC_IPV6)
  127. if (local->srx.transport.family == AF_INET6)
  128. udpv6_encap_enable();
  129. #endif
  130. usk->sk_error_report = rxrpc_error_report;
  131. /* if a local address was supplied then bind it */
  132. if (local->srx.transport_len > sizeof(sa_family_t)) {
  133. _debug("bind");
  134. ret = kernel_bind(local->socket,
  135. (struct sockaddr *)&local->srx.transport,
  136. local->srx.transport_len);
  137. if (ret < 0) {
  138. _debug("bind failed %d", ret);
  139. goto error;
  140. }
  141. }
  142. switch (local->srx.transport.family) {
  143. case AF_INET6:
  144. /* we want to receive ICMPv6 errors */
  145. opt = 1;
  146. ret = kernel_setsockopt(local->socket, SOL_IPV6, IPV6_RECVERR,
  147. (char *) &opt, sizeof(opt));
  148. if (ret < 0) {
  149. _debug("setsockopt failed");
  150. goto error;
  151. }
  152. /* we want to set the don't fragment bit */
  153. opt = IPV6_PMTUDISC_DO;
  154. ret = kernel_setsockopt(local->socket, SOL_IPV6, IPV6_MTU_DISCOVER,
  155. (char *) &opt, sizeof(opt));
  156. if (ret < 0) {
  157. _debug("setsockopt failed");
  158. goto error;
  159. }
  160. /* Fall through and set IPv4 options too otherwise we don't get
  161. * errors from IPv4 packets sent through the IPv6 socket.
  162. */
  163. case AF_INET:
  164. /* we want to receive ICMP errors */
  165. opt = 1;
  166. ret = kernel_setsockopt(local->socket, SOL_IP, IP_RECVERR,
  167. (char *) &opt, sizeof(opt));
  168. if (ret < 0) {
  169. _debug("setsockopt failed");
  170. goto error;
  171. }
  172. /* we want to set the don't fragment bit */
  173. opt = IP_PMTUDISC_DO;
  174. ret = kernel_setsockopt(local->socket, SOL_IP, IP_MTU_DISCOVER,
  175. (char *) &opt, sizeof(opt));
  176. if (ret < 0) {
  177. _debug("setsockopt failed");
  178. goto error;
  179. }
  180. /* We want receive timestamps. */
  181. opt = 1;
  182. ret = kernel_setsockopt(local->socket, SOL_SOCKET, SO_TIMESTAMPNS,
  183. (char *)&opt, sizeof(opt));
  184. if (ret < 0) {
  185. _debug("setsockopt failed");
  186. goto error;
  187. }
  188. break;
  189. default:
  190. BUG();
  191. }
  192. _leave(" = 0");
  193. return 0;
  194. error:
  195. kernel_sock_shutdown(local->socket, SHUT_RDWR);
  196. local->socket->sk->sk_user_data = NULL;
  197. sock_release(local->socket);
  198. local->socket = NULL;
  199. _leave(" = %d", ret);
  200. return ret;
  201. }
  202. /*
  203. * Look up or create a new local endpoint using the specified local address.
  204. */
  205. struct rxrpc_local *rxrpc_lookup_local(struct net *net,
  206. const struct sockaddr_rxrpc *srx)
  207. {
  208. struct rxrpc_local *local;
  209. struct rxrpc_net *rxnet = rxrpc_net(net);
  210. struct list_head *cursor;
  211. const char *age;
  212. long diff;
  213. int ret;
  214. _enter("{%d,%d,%pISp}",
  215. srx->transport_type, srx->transport.family, &srx->transport);
  216. mutex_lock(&rxnet->local_mutex);
  217. for (cursor = rxnet->local_endpoints.next;
  218. cursor != &rxnet->local_endpoints;
  219. cursor = cursor->next) {
  220. local = list_entry(cursor, struct rxrpc_local, link);
  221. diff = rxrpc_local_cmp_key(local, srx);
  222. if (diff < 0)
  223. continue;
  224. if (diff > 0)
  225. break;
  226. /* Services aren't allowed to share transport sockets, so
  227. * reject that here. It is possible that the object is dying -
  228. * but it may also still have the local transport address that
  229. * we want bound.
  230. */
  231. if (srx->srx_service) {
  232. local = NULL;
  233. goto addr_in_use;
  234. }
  235. /* Found a match. We replace a dying object. Attempting to
  236. * bind the transport socket may still fail if we're attempting
  237. * to use a local address that the dying object is still using.
  238. */
  239. if (!rxrpc_use_local(local))
  240. break;
  241. age = "old";
  242. goto found;
  243. }
  244. local = rxrpc_alloc_local(rxnet, srx);
  245. if (!local)
  246. goto nomem;
  247. ret = rxrpc_open_socket(local, net);
  248. if (ret < 0)
  249. goto sock_error;
  250. if (cursor != &rxnet->local_endpoints)
  251. list_replace_init(cursor, &local->link);
  252. else
  253. list_add_tail(&local->link, cursor);
  254. age = "new";
  255. found:
  256. mutex_unlock(&rxnet->local_mutex);
  257. _net("LOCAL %s %d {%pISp}",
  258. age, local->debug_id, &local->srx.transport);
  259. _leave(" = %p", local);
  260. return local;
  261. nomem:
  262. ret = -ENOMEM;
  263. sock_error:
  264. mutex_unlock(&rxnet->local_mutex);
  265. if (local)
  266. call_rcu(&local->rcu, rxrpc_local_rcu);
  267. _leave(" = %d", ret);
  268. return ERR_PTR(ret);
  269. addr_in_use:
  270. mutex_unlock(&rxnet->local_mutex);
  271. _leave(" = -EADDRINUSE");
  272. return ERR_PTR(-EADDRINUSE);
  273. }
  274. /*
  275. * Get a ref on a local endpoint.
  276. */
  277. struct rxrpc_local *rxrpc_get_local(struct rxrpc_local *local)
  278. {
  279. const void *here = __builtin_return_address(0);
  280. int n;
  281. n = atomic_inc_return(&local->usage);
  282. trace_rxrpc_local(local->debug_id, rxrpc_local_got, n, here);
  283. return local;
  284. }
  285. /*
  286. * Get a ref on a local endpoint unless its usage has already reached 0.
  287. */
  288. struct rxrpc_local *rxrpc_get_local_maybe(struct rxrpc_local *local)
  289. {
  290. const void *here = __builtin_return_address(0);
  291. if (local) {
  292. int n = atomic_fetch_add_unless(&local->usage, 1, 0);
  293. if (n > 0)
  294. trace_rxrpc_local(local->debug_id, rxrpc_local_got,
  295. n + 1, here);
  296. else
  297. local = NULL;
  298. }
  299. return local;
  300. }
  301. /*
  302. * Queue a local endpoint and pass the caller's reference to the work item.
  303. */
  304. void rxrpc_queue_local(struct rxrpc_local *local)
  305. {
  306. const void *here = __builtin_return_address(0);
  307. unsigned int debug_id = local->debug_id;
  308. int n = atomic_read(&local->usage);
  309. if (rxrpc_queue_work(&local->processor))
  310. trace_rxrpc_local(debug_id, rxrpc_local_queued, n, here);
  311. else
  312. rxrpc_put_local(local);
  313. }
  314. /*
  315. * Drop a ref on a local endpoint.
  316. */
  317. void rxrpc_put_local(struct rxrpc_local *local)
  318. {
  319. const void *here = __builtin_return_address(0);
  320. unsigned int debug_id;
  321. int n;
  322. if (local) {
  323. debug_id = local->debug_id;
  324. n = atomic_dec_return(&local->usage);
  325. trace_rxrpc_local(debug_id, rxrpc_local_put, n, here);
  326. if (n == 0)
  327. call_rcu(&local->rcu, rxrpc_local_rcu);
  328. }
  329. }
  330. /*
  331. * Start using a local endpoint.
  332. */
  333. struct rxrpc_local *rxrpc_use_local(struct rxrpc_local *local)
  334. {
  335. local = rxrpc_get_local_maybe(local);
  336. if (!local)
  337. return NULL;
  338. if (!__rxrpc_use_local(local)) {
  339. rxrpc_put_local(local);
  340. return NULL;
  341. }
  342. return local;
  343. }
  344. /*
  345. * Cease using a local endpoint. Once the number of active users reaches 0, we
  346. * start the closure of the transport in the work processor.
  347. */
  348. void rxrpc_unuse_local(struct rxrpc_local *local)
  349. {
  350. if (local) {
  351. if (__rxrpc_unuse_local(local)) {
  352. rxrpc_get_local(local);
  353. rxrpc_queue_local(local);
  354. }
  355. }
  356. }
  357. /*
  358. * Destroy a local endpoint's socket and then hand the record to RCU to dispose
  359. * of.
  360. *
  361. * Closing the socket cannot be done from bottom half context or RCU callback
  362. * context because it might sleep.
  363. */
  364. static void rxrpc_local_destroyer(struct rxrpc_local *local)
  365. {
  366. struct socket *socket = local->socket;
  367. struct rxrpc_net *rxnet = local->rxnet;
  368. _enter("%d", local->debug_id);
  369. local->dead = true;
  370. mutex_lock(&rxnet->local_mutex);
  371. list_del_init(&local->link);
  372. mutex_unlock(&rxnet->local_mutex);
  373. rxrpc_clean_up_local_conns(local);
  374. rxrpc_service_connection_reaper(&rxnet->service_conn_reaper);
  375. ASSERT(!local->service);
  376. if (socket) {
  377. local->socket = NULL;
  378. kernel_sock_shutdown(socket, SHUT_RDWR);
  379. socket->sk->sk_user_data = NULL;
  380. sock_release(socket);
  381. }
  382. /* At this point, there should be no more packets coming in to the
  383. * local endpoint.
  384. */
  385. rxrpc_purge_queue(&local->reject_queue);
  386. rxrpc_purge_queue(&local->event_queue);
  387. }
  388. /*
  389. * Process events on an endpoint. The work item carries a ref which
  390. * we must release.
  391. */
  392. static void rxrpc_local_processor(struct work_struct *work)
  393. {
  394. struct rxrpc_local *local =
  395. container_of(work, struct rxrpc_local, processor);
  396. bool again;
  397. trace_rxrpc_local(local->debug_id, rxrpc_local_processing,
  398. atomic_read(&local->usage), NULL);
  399. do {
  400. again = false;
  401. if (!__rxrpc_use_local(local)) {
  402. rxrpc_local_destroyer(local);
  403. break;
  404. }
  405. if (!skb_queue_empty(&local->reject_queue)) {
  406. rxrpc_reject_packets(local);
  407. again = true;
  408. }
  409. if (!skb_queue_empty(&local->event_queue)) {
  410. rxrpc_process_local_events(local);
  411. again = true;
  412. }
  413. __rxrpc_unuse_local(local);
  414. } while (again);
  415. rxrpc_put_local(local);
  416. }
  417. /*
  418. * Destroy a local endpoint after the RCU grace period expires.
  419. */
  420. static void rxrpc_local_rcu(struct rcu_head *rcu)
  421. {
  422. struct rxrpc_local *local = container_of(rcu, struct rxrpc_local, rcu);
  423. _enter("%d", local->debug_id);
  424. ASSERT(!work_pending(&local->processor));
  425. _net("DESTROY LOCAL %d", local->debug_id);
  426. kfree(local);
  427. _leave("");
  428. }
  429. /*
  430. * Verify the local endpoint list is empty by this point.
  431. */
  432. void rxrpc_destroy_all_locals(struct rxrpc_net *rxnet)
  433. {
  434. struct rxrpc_local *local;
  435. _enter("");
  436. flush_workqueue(rxrpc_workqueue);
  437. if (!list_empty(&rxnet->local_endpoints)) {
  438. mutex_lock(&rxnet->local_mutex);
  439. list_for_each_entry(local, &rxnet->local_endpoints, link) {
  440. pr_err("AF_RXRPC: Leaked local %p {%d}\n",
  441. local, atomic_read(&local->usage));
  442. }
  443. mutex_unlock(&rxnet->local_mutex);
  444. BUG();
  445. }
  446. }