peer_object.c 12 KB

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