peer_object.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* RxRPC remote transport endpoint record management
  3. *
  4. * Copyright (C) 2007, 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/udp.h>
  12. #include <linux/in.h>
  13. #include <linux/in6.h>
  14. #include <linux/slab.h>
  15. #include <linux/hashtable.h>
  16. #include <net/sock.h>
  17. #include <net/af_rxrpc.h>
  18. #include <net/ip.h>
  19. #include <net/route.h>
  20. #include <net/ip6_route.h>
  21. #include "ar-internal.h"
  22. static const struct sockaddr_rxrpc rxrpc_null_addr;
  23. /*
  24. * Hash a peer key.
  25. */
  26. static unsigned long rxrpc_peer_hash_key(struct rxrpc_local *local,
  27. const struct sockaddr_rxrpc *srx)
  28. {
  29. const u16 *p;
  30. unsigned int i, size;
  31. unsigned long hash_key;
  32. _enter("");
  33. hash_key = (unsigned long)local / __alignof__(*local);
  34. hash_key += srx->transport_type;
  35. hash_key += srx->transport_len;
  36. hash_key += srx->transport.family;
  37. switch (srx->transport.family) {
  38. case AF_INET:
  39. hash_key += (u16 __force)srx->transport.sin.sin_port;
  40. size = sizeof(srx->transport.sin.sin_addr);
  41. p = (u16 *)&srx->transport.sin.sin_addr;
  42. break;
  43. #ifdef CONFIG_AF_RXRPC_IPV6
  44. case AF_INET6:
  45. hash_key += (u16 __force)srx->transport.sin.sin_port;
  46. size = sizeof(srx->transport.sin6.sin6_addr);
  47. p = (u16 *)&srx->transport.sin6.sin6_addr;
  48. break;
  49. #endif
  50. default:
  51. WARN(1, "AF_RXRPC: Unsupported transport address family\n");
  52. return 0;
  53. }
  54. /* Step through the peer address in 16-bit portions for speed */
  55. for (i = 0; i < size; i += sizeof(*p), p++)
  56. hash_key += *p;
  57. _leave(" 0x%lx", hash_key);
  58. return hash_key;
  59. }
  60. /*
  61. * Compare a peer to a key. Return -ve, 0 or +ve to indicate less than, same
  62. * or greater than.
  63. *
  64. * Unfortunately, the primitives in linux/hashtable.h don't allow for sorted
  65. * buckets and mid-bucket insertion, so we don't make full use of this
  66. * information at this point.
  67. */
  68. static long rxrpc_peer_cmp_key(const struct rxrpc_peer *peer,
  69. struct rxrpc_local *local,
  70. const struct sockaddr_rxrpc *srx,
  71. unsigned long hash_key)
  72. {
  73. long diff;
  74. diff = ((peer->hash_key - hash_key) ?:
  75. ((unsigned long)peer->local - (unsigned long)local) ?:
  76. (peer->srx.transport_type - srx->transport_type) ?:
  77. (peer->srx.transport_len - srx->transport_len) ?:
  78. (peer->srx.transport.family - srx->transport.family));
  79. if (diff != 0)
  80. return diff;
  81. switch (srx->transport.family) {
  82. case AF_INET:
  83. return ((u16 __force)peer->srx.transport.sin.sin_port -
  84. (u16 __force)srx->transport.sin.sin_port) ?:
  85. memcmp(&peer->srx.transport.sin.sin_addr,
  86. &srx->transport.sin.sin_addr,
  87. sizeof(struct in_addr));
  88. #ifdef CONFIG_AF_RXRPC_IPV6
  89. case AF_INET6:
  90. return ((u16 __force)peer->srx.transport.sin6.sin6_port -
  91. (u16 __force)srx->transport.sin6.sin6_port) ?:
  92. memcmp(&peer->srx.transport.sin6.sin6_addr,
  93. &srx->transport.sin6.sin6_addr,
  94. sizeof(struct in6_addr));
  95. #endif
  96. default:
  97. BUG();
  98. }
  99. }
  100. /*
  101. * Look up a remote transport endpoint for the specified address using RCU.
  102. */
  103. static struct rxrpc_peer *__rxrpc_lookup_peer_rcu(
  104. struct rxrpc_local *local,
  105. const struct sockaddr_rxrpc *srx,
  106. unsigned long hash_key)
  107. {
  108. struct rxrpc_peer *peer;
  109. struct rxrpc_net *rxnet = local->rxnet;
  110. hash_for_each_possible_rcu(rxnet->peer_hash, peer, hash_link, hash_key) {
  111. if (rxrpc_peer_cmp_key(peer, local, srx, hash_key) == 0 &&
  112. refcount_read(&peer->ref) > 0)
  113. return peer;
  114. }
  115. return NULL;
  116. }
  117. /*
  118. * Look up a remote transport endpoint for the specified address using RCU.
  119. */
  120. struct rxrpc_peer *rxrpc_lookup_peer_rcu(struct rxrpc_local *local,
  121. const struct sockaddr_rxrpc *srx)
  122. {
  123. struct rxrpc_peer *peer;
  124. unsigned long hash_key = rxrpc_peer_hash_key(local, srx);
  125. peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
  126. if (peer)
  127. _leave(" = %p {u=%d}", peer, refcount_read(&peer->ref));
  128. return peer;
  129. }
  130. /*
  131. * assess the MTU size for the network interface through which this peer is
  132. * reached
  133. */
  134. static void rxrpc_assess_MTU_size(struct rxrpc_local *local,
  135. struct rxrpc_peer *peer)
  136. {
  137. struct net *net = local->net;
  138. struct dst_entry *dst;
  139. struct rtable *rt;
  140. struct flowi fl;
  141. struct flowi4 *fl4 = &fl.u.ip4;
  142. #ifdef CONFIG_AF_RXRPC_IPV6
  143. struct flowi6 *fl6 = &fl.u.ip6;
  144. #endif
  145. peer->if_mtu = 1500;
  146. memset(&fl, 0, sizeof(fl));
  147. switch (peer->srx.transport.family) {
  148. case AF_INET:
  149. rt = ip_route_output_ports(
  150. net, fl4, NULL,
  151. peer->srx.transport.sin.sin_addr.s_addr, 0,
  152. htons(7000), htons(7001), IPPROTO_UDP, 0, 0);
  153. if (IS_ERR(rt)) {
  154. _leave(" [route err %ld]", PTR_ERR(rt));
  155. return;
  156. }
  157. dst = &rt->dst;
  158. break;
  159. #ifdef CONFIG_AF_RXRPC_IPV6
  160. case AF_INET6:
  161. fl6->flowi6_iif = LOOPBACK_IFINDEX;
  162. fl6->flowi6_scope = RT_SCOPE_UNIVERSE;
  163. fl6->flowi6_proto = IPPROTO_UDP;
  164. memcpy(&fl6->daddr, &peer->srx.transport.sin6.sin6_addr,
  165. sizeof(struct in6_addr));
  166. fl6->fl6_dport = htons(7001);
  167. fl6->fl6_sport = htons(7000);
  168. dst = ip6_route_output(net, NULL, fl6);
  169. if (dst->error) {
  170. _leave(" [route err %d]", dst->error);
  171. return;
  172. }
  173. break;
  174. #endif
  175. default:
  176. BUG();
  177. }
  178. peer->if_mtu = dst_mtu(dst);
  179. dst_release(dst);
  180. _leave(" [if_mtu %u]", peer->if_mtu);
  181. }
  182. /*
  183. * Allocate a peer.
  184. */
  185. struct rxrpc_peer *rxrpc_alloc_peer(struct rxrpc_local *local, gfp_t gfp,
  186. enum rxrpc_peer_trace why)
  187. {
  188. struct rxrpc_peer *peer;
  189. _enter("");
  190. peer = kzalloc(sizeof(struct rxrpc_peer), gfp);
  191. if (peer) {
  192. refcount_set(&peer->ref, 1);
  193. peer->local = rxrpc_get_local(local, rxrpc_local_get_peer);
  194. INIT_HLIST_HEAD(&peer->error_targets);
  195. peer->service_conns = RB_ROOT;
  196. seqlock_init(&peer->service_conn_lock);
  197. spin_lock_init(&peer->lock);
  198. spin_lock_init(&peer->rtt_input_lock);
  199. peer->debug_id = atomic_inc_return(&rxrpc_debug_id);
  200. rxrpc_peer_init_rtt(peer);
  201. peer->cong_ssthresh = RXRPC_TX_MAX_WINDOW;
  202. trace_rxrpc_peer(peer->debug_id, 1, why);
  203. }
  204. _leave(" = %p", peer);
  205. return peer;
  206. }
  207. /*
  208. * Initialise peer record.
  209. */
  210. static void rxrpc_init_peer(struct rxrpc_local *local, struct rxrpc_peer *peer,
  211. unsigned long hash_key)
  212. {
  213. peer->hash_key = hash_key;
  214. rxrpc_assess_MTU_size(local, peer);
  215. peer->mtu = peer->if_mtu;
  216. peer->rtt_last_req = ktime_get_real();
  217. switch (peer->srx.transport.family) {
  218. case AF_INET:
  219. peer->hdrsize = sizeof(struct iphdr);
  220. break;
  221. #ifdef CONFIG_AF_RXRPC_IPV6
  222. case AF_INET6:
  223. peer->hdrsize = sizeof(struct ipv6hdr);
  224. break;
  225. #endif
  226. default:
  227. BUG();
  228. }
  229. switch (peer->srx.transport_type) {
  230. case SOCK_DGRAM:
  231. peer->hdrsize += sizeof(struct udphdr);
  232. break;
  233. default:
  234. BUG();
  235. }
  236. peer->hdrsize += sizeof(struct rxrpc_wire_header);
  237. peer->maxdata = peer->mtu - peer->hdrsize;
  238. }
  239. /*
  240. * Set up a new peer.
  241. */
  242. static struct rxrpc_peer *rxrpc_create_peer(struct rxrpc_local *local,
  243. struct sockaddr_rxrpc *srx,
  244. unsigned long hash_key,
  245. gfp_t gfp)
  246. {
  247. struct rxrpc_peer *peer;
  248. _enter("");
  249. peer = rxrpc_alloc_peer(local, gfp, rxrpc_peer_new_client);
  250. if (peer) {
  251. memcpy(&peer->srx, srx, sizeof(*srx));
  252. rxrpc_init_peer(local, peer, hash_key);
  253. }
  254. _leave(" = %p", peer);
  255. return peer;
  256. }
  257. static void rxrpc_free_peer(struct rxrpc_peer *peer)
  258. {
  259. trace_rxrpc_peer(peer->debug_id, 0, rxrpc_peer_free);
  260. rxrpc_put_local(peer->local, rxrpc_local_put_peer);
  261. kfree_rcu(peer, rcu);
  262. }
  263. /*
  264. * Set up a new incoming peer. There shouldn't be any other matching peers
  265. * since we've already done a search in the list from the non-reentrant context
  266. * (the data_ready handler) that is the only place we can add new peers.
  267. */
  268. void rxrpc_new_incoming_peer(struct rxrpc_local *local, struct rxrpc_peer *peer)
  269. {
  270. struct rxrpc_net *rxnet = local->rxnet;
  271. unsigned long hash_key;
  272. hash_key = rxrpc_peer_hash_key(local, &peer->srx);
  273. rxrpc_init_peer(local, peer, hash_key);
  274. spin_lock_bh(&rxnet->peer_hash_lock);
  275. hash_add_rcu(rxnet->peer_hash, &peer->hash_link, hash_key);
  276. list_add_tail(&peer->keepalive_link, &rxnet->peer_keepalive_new);
  277. spin_unlock_bh(&rxnet->peer_hash_lock);
  278. }
  279. /*
  280. * obtain a remote transport endpoint for the specified address
  281. */
  282. struct rxrpc_peer *rxrpc_lookup_peer(struct rxrpc_local *local,
  283. struct sockaddr_rxrpc *srx, gfp_t gfp)
  284. {
  285. struct rxrpc_peer *peer, *candidate;
  286. struct rxrpc_net *rxnet = local->rxnet;
  287. unsigned long hash_key = rxrpc_peer_hash_key(local, srx);
  288. _enter("{%pISp}", &srx->transport);
  289. /* search the peer list first */
  290. rcu_read_lock();
  291. peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
  292. if (peer && !rxrpc_get_peer_maybe(peer, rxrpc_peer_get_lookup_client))
  293. peer = NULL;
  294. rcu_read_unlock();
  295. if (!peer) {
  296. /* The peer is not yet present in hash - create a candidate
  297. * for a new record and then redo the search.
  298. */
  299. candidate = rxrpc_create_peer(local, srx, hash_key, gfp);
  300. if (!candidate) {
  301. _leave(" = NULL [nomem]");
  302. return NULL;
  303. }
  304. spin_lock_bh(&rxnet->peer_hash_lock);
  305. /* Need to check that we aren't racing with someone else */
  306. peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
  307. if (peer && !rxrpc_get_peer_maybe(peer, rxrpc_peer_get_lookup_client))
  308. peer = NULL;
  309. if (!peer) {
  310. hash_add_rcu(rxnet->peer_hash,
  311. &candidate->hash_link, hash_key);
  312. list_add_tail(&candidate->keepalive_link,
  313. &rxnet->peer_keepalive_new);
  314. }
  315. spin_unlock_bh(&rxnet->peer_hash_lock);
  316. if (peer)
  317. rxrpc_free_peer(candidate);
  318. else
  319. peer = candidate;
  320. }
  321. _leave(" = %p {u=%d}", peer, refcount_read(&peer->ref));
  322. return peer;
  323. }
  324. /*
  325. * Get a ref on a peer record.
  326. */
  327. struct rxrpc_peer *rxrpc_get_peer(struct rxrpc_peer *peer, enum rxrpc_peer_trace why)
  328. {
  329. int r;
  330. __refcount_inc(&peer->ref, &r);
  331. trace_rxrpc_peer(peer->debug_id, r + 1, why);
  332. return peer;
  333. }
  334. /*
  335. * Get a ref on a peer record unless its usage has already reached 0.
  336. */
  337. struct rxrpc_peer *rxrpc_get_peer_maybe(struct rxrpc_peer *peer,
  338. enum rxrpc_peer_trace why)
  339. {
  340. int r;
  341. if (peer) {
  342. if (__refcount_inc_not_zero(&peer->ref, &r))
  343. trace_rxrpc_peer(peer->debug_id, r + 1, why);
  344. else
  345. peer = NULL;
  346. }
  347. return peer;
  348. }
  349. /*
  350. * Discard a peer record.
  351. */
  352. static void __rxrpc_put_peer(struct rxrpc_peer *peer)
  353. {
  354. struct rxrpc_net *rxnet = peer->local->rxnet;
  355. ASSERT(hlist_empty(&peer->error_targets));
  356. spin_lock_bh(&rxnet->peer_hash_lock);
  357. hash_del_rcu(&peer->hash_link);
  358. list_del_init(&peer->keepalive_link);
  359. spin_unlock_bh(&rxnet->peer_hash_lock);
  360. rxrpc_free_peer(peer);
  361. }
  362. /*
  363. * Drop a ref on a peer record.
  364. */
  365. void rxrpc_put_peer(struct rxrpc_peer *peer, enum rxrpc_peer_trace why)
  366. {
  367. unsigned int debug_id;
  368. bool dead;
  369. int r;
  370. if (peer) {
  371. debug_id = peer->debug_id;
  372. dead = __refcount_dec_and_test(&peer->ref, &r);
  373. trace_rxrpc_peer(debug_id, r - 1, why);
  374. if (dead)
  375. __rxrpc_put_peer(peer);
  376. }
  377. }
  378. /*
  379. * Make sure all peer records have been discarded.
  380. */
  381. void rxrpc_destroy_all_peers(struct rxrpc_net *rxnet)
  382. {
  383. struct rxrpc_peer *peer;
  384. int i;
  385. for (i = 0; i < HASH_SIZE(rxnet->peer_hash); i++) {
  386. if (hlist_empty(&rxnet->peer_hash[i]))
  387. continue;
  388. hlist_for_each_entry(peer, &rxnet->peer_hash[i], hash_link) {
  389. pr_err("Leaked peer %u {%u} %pISp\n",
  390. peer->debug_id,
  391. refcount_read(&peer->ref),
  392. &peer->srx.transport);
  393. }
  394. }
  395. }
  396. /**
  397. * rxrpc_kernel_get_call_peer - Get the peer address of a call
  398. * @sock: The socket on which the call is in progress.
  399. * @call: The call to query
  400. *
  401. * Get a record for the remote peer in a call.
  402. */
  403. struct rxrpc_peer *rxrpc_kernel_get_call_peer(struct socket *sock, struct rxrpc_call *call)
  404. {
  405. return call->peer;
  406. }
  407. EXPORT_SYMBOL(rxrpc_kernel_get_call_peer);
  408. /**
  409. * rxrpc_kernel_get_srtt - Get a call's peer smoothed RTT
  410. * @peer: The peer to query
  411. *
  412. * Get the call's peer smoothed RTT in uS or UINT_MAX if we have no samples.
  413. */
  414. unsigned int rxrpc_kernel_get_srtt(const struct rxrpc_peer *peer)
  415. {
  416. return peer->rtt_count > 0 ? peer->srtt_us >> 3 : UINT_MAX;
  417. }
  418. EXPORT_SYMBOL(rxrpc_kernel_get_srtt);
  419. /**
  420. * rxrpc_kernel_remote_srx - Get the address of a peer
  421. * @peer: The peer to query
  422. *
  423. * Get a pointer to the address from a peer record. The caller is responsible
  424. * for making sure that the address is not deallocated.
  425. */
  426. const struct sockaddr_rxrpc *rxrpc_kernel_remote_srx(const struct rxrpc_peer *peer)
  427. {
  428. return peer ? &peer->srx : &rxrpc_null_addr;
  429. }
  430. EXPORT_SYMBOL(rxrpc_kernel_remote_srx);
  431. /**
  432. * rxrpc_kernel_remote_addr - Get the peer transport address of a call
  433. * @peer: The peer to query
  434. *
  435. * Get a pointer to the transport address from a peer record. The caller is
  436. * responsible for making sure that the address is not deallocated.
  437. */
  438. const struct sockaddr *rxrpc_kernel_remote_addr(const struct rxrpc_peer *peer)
  439. {
  440. return (const struct sockaddr *)
  441. (peer ? &peer->srx.transport : &rxrpc_null_addr.transport);
  442. }
  443. EXPORT_SYMBOL(rxrpc_kernel_remote_addr);