local_object.c 12 KB

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