peer_object.c 13 KB

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