peer_object.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. if (atomic_read(&peer->usage) == 0)
  116. return NULL;
  117. return peer;
  118. }
  119. }
  120. return NULL;
  121. }
  122. /*
  123. * Look up a remote transport endpoint for the specified address using RCU.
  124. */
  125. struct rxrpc_peer *rxrpc_lookup_peer_rcu(struct rxrpc_local *local,
  126. const struct sockaddr_rxrpc *srx)
  127. {
  128. struct rxrpc_peer *peer;
  129. unsigned long hash_key = rxrpc_peer_hash_key(local, srx);
  130. peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
  131. if (peer) {
  132. _net("PEER %d {%pISp}", peer->debug_id, &peer->srx.transport);
  133. _leave(" = %p {u=%d}", peer, atomic_read(&peer->usage));
  134. }
  135. return peer;
  136. }
  137. /*
  138. * assess the MTU size for the network interface through which this peer is
  139. * reached
  140. */
  141. static void rxrpc_assess_MTU_size(struct rxrpc_peer *peer)
  142. {
  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. &init_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(&init_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 = local;
  198. INIT_HLIST_HEAD(&peer->error_targets);
  199. INIT_WORK(&peer->error_distributor,
  200. &rxrpc_peer_error_distributor);
  201. peer->service_conns = RB_ROOT;
  202. seqlock_init(&peer->service_conn_lock);
  203. spin_lock_init(&peer->lock);
  204. peer->debug_id = atomic_inc_return(&rxrpc_debug_id);
  205. if (RXRPC_TX_SMSS > 2190)
  206. peer->cong_cwnd = 2;
  207. else if (RXRPC_TX_SMSS > 1095)
  208. peer->cong_cwnd = 3;
  209. else
  210. peer->cong_cwnd = 4;
  211. }
  212. _leave(" = %p", peer);
  213. return peer;
  214. }
  215. /*
  216. * Initialise peer record.
  217. */
  218. static void rxrpc_init_peer(struct rxrpc_peer *peer, unsigned long hash_key)
  219. {
  220. peer->hash_key = hash_key;
  221. rxrpc_assess_MTU_size(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_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(peer, hash_key);
  260. }
  261. _leave(" = %p", peer);
  262. return peer;
  263. }
  264. /*
  265. * Set up a new incoming peer. The address is prestored in the preallocated
  266. * peer.
  267. */
  268. struct rxrpc_peer *rxrpc_lookup_incoming_peer(struct rxrpc_local *local,
  269. struct rxrpc_peer *prealloc)
  270. {
  271. struct rxrpc_peer *peer;
  272. struct rxrpc_net *rxnet = local->rxnet;
  273. unsigned long hash_key;
  274. hash_key = rxrpc_peer_hash_key(local, &prealloc->srx);
  275. prealloc->local = local;
  276. rxrpc_init_peer(prealloc, hash_key);
  277. spin_lock(&rxnet->peer_hash_lock);
  278. /* Need to check that we aren't racing with someone else */
  279. peer = __rxrpc_lookup_peer_rcu(local, &prealloc->srx, hash_key);
  280. if (peer && !rxrpc_get_peer_maybe(peer))
  281. peer = NULL;
  282. if (!peer) {
  283. peer = prealloc;
  284. hash_add_rcu(rxnet->peer_hash, &peer->hash_link, hash_key);
  285. }
  286. spin_unlock(&rxnet->peer_hash_lock);
  287. return peer;
  288. }
  289. /*
  290. * obtain a remote transport endpoint for the specified address
  291. */
  292. struct rxrpc_peer *rxrpc_lookup_peer(struct rxrpc_local *local,
  293. struct sockaddr_rxrpc *srx, gfp_t gfp)
  294. {
  295. struct rxrpc_peer *peer, *candidate;
  296. struct rxrpc_net *rxnet = local->rxnet;
  297. unsigned long hash_key = rxrpc_peer_hash_key(local, srx);
  298. _enter("{%pISp}", &srx->transport);
  299. /* search the peer list first */
  300. rcu_read_lock();
  301. peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
  302. if (peer && !rxrpc_get_peer_maybe(peer))
  303. peer = NULL;
  304. rcu_read_unlock();
  305. if (!peer) {
  306. /* The peer is not yet present in hash - create a candidate
  307. * for a new record and then redo the search.
  308. */
  309. candidate = rxrpc_create_peer(local, srx, hash_key, gfp);
  310. if (!candidate) {
  311. _leave(" = NULL [nomem]");
  312. return NULL;
  313. }
  314. spin_lock_bh(&rxnet->peer_hash_lock);
  315. /* Need to check that we aren't racing with someone else */
  316. peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
  317. if (peer && !rxrpc_get_peer_maybe(peer))
  318. peer = NULL;
  319. if (!peer)
  320. hash_add_rcu(rxnet->peer_hash,
  321. &candidate->hash_link, hash_key);
  322. spin_unlock_bh(&rxnet->peer_hash_lock);
  323. if (peer)
  324. kfree(candidate);
  325. else
  326. peer = candidate;
  327. }
  328. _net("PEER %d {%pISp}", peer->debug_id, &peer->srx.transport);
  329. _leave(" = %p {u=%d}", peer, atomic_read(&peer->usage));
  330. return peer;
  331. }
  332. /*
  333. * Discard a ref on a remote peer record.
  334. */
  335. void __rxrpc_put_peer(struct rxrpc_peer *peer)
  336. {
  337. struct rxrpc_net *rxnet = peer->local->rxnet;
  338. ASSERT(hlist_empty(&peer->error_targets));
  339. spin_lock_bh(&rxnet->peer_hash_lock);
  340. hash_del_rcu(&peer->hash_link);
  341. spin_unlock_bh(&rxnet->peer_hash_lock);
  342. kfree_rcu(peer, rcu);
  343. }
  344. /**
  345. * rxrpc_kernel_get_peer - Get the peer address of a call
  346. * @sock: The socket on which the call is in progress.
  347. * @call: The call to query
  348. * @_srx: Where to place the result
  349. *
  350. * Get the address of the remote peer in a call.
  351. */
  352. void rxrpc_kernel_get_peer(struct socket *sock, struct rxrpc_call *call,
  353. struct sockaddr_rxrpc *_srx)
  354. {
  355. *_srx = call->peer->srx;
  356. }
  357. EXPORT_SYMBOL(rxrpc_kernel_get_peer);
  358. /**
  359. * rxrpc_kernel_get_rtt - Get a call's peer RTT
  360. * @sock: The socket on which the call is in progress.
  361. * @call: The call to query
  362. *
  363. * Get the call's peer RTT.
  364. */
  365. u64 rxrpc_kernel_get_rtt(struct socket *sock, struct rxrpc_call *call)
  366. {
  367. return call->peer->rtt;
  368. }
  369. EXPORT_SYMBOL(rxrpc_kernel_get_rtt);