peer_object.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. static DEFINE_HASHTABLE(rxrpc_peer_hash, 10);
  27. static DEFINE_SPINLOCK(rxrpc_peer_hash_lock);
  28. /*
  29. * Hash a peer key.
  30. */
  31. static unsigned long rxrpc_peer_hash_key(struct rxrpc_local *local,
  32. const struct sockaddr_rxrpc *srx)
  33. {
  34. const u16 *p;
  35. unsigned int i, size;
  36. unsigned long hash_key;
  37. _enter("");
  38. hash_key = (unsigned long)local / __alignof__(*local);
  39. hash_key += srx->transport_type;
  40. hash_key += srx->transport_len;
  41. hash_key += srx->transport.family;
  42. switch (srx->transport.family) {
  43. case AF_INET:
  44. hash_key += (u16 __force)srx->transport.sin.sin_port;
  45. size = sizeof(srx->transport.sin.sin_addr);
  46. p = (u16 *)&srx->transport.sin.sin_addr;
  47. break;
  48. #ifdef CONFIG_AF_RXRPC_IPV6
  49. case AF_INET6:
  50. hash_key += (u16 __force)srx->transport.sin.sin_port;
  51. size = sizeof(srx->transport.sin6.sin6_addr);
  52. p = (u16 *)&srx->transport.sin6.sin6_addr;
  53. break;
  54. #endif
  55. default:
  56. WARN(1, "AF_RXRPC: Unsupported transport address family\n");
  57. return 0;
  58. }
  59. /* Step through the peer address in 16-bit portions for speed */
  60. for (i = 0; i < size; i += sizeof(*p), p++)
  61. hash_key += *p;
  62. _leave(" 0x%lx", hash_key);
  63. return hash_key;
  64. }
  65. /*
  66. * Compare a peer to a key. Return -ve, 0 or +ve to indicate less than, same
  67. * or greater than.
  68. *
  69. * Unfortunately, the primitives in linux/hashtable.h don't allow for sorted
  70. * buckets and mid-bucket insertion, so we don't make full use of this
  71. * information at this point.
  72. */
  73. static long rxrpc_peer_cmp_key(const struct rxrpc_peer *peer,
  74. struct rxrpc_local *local,
  75. const struct sockaddr_rxrpc *srx,
  76. unsigned long hash_key)
  77. {
  78. long diff;
  79. diff = ((peer->hash_key - hash_key) ?:
  80. ((unsigned long)peer->local - (unsigned long)local) ?:
  81. (peer->srx.transport_type - srx->transport_type) ?:
  82. (peer->srx.transport_len - srx->transport_len) ?:
  83. (peer->srx.transport.family - srx->transport.family));
  84. if (diff != 0)
  85. return diff;
  86. switch (srx->transport.family) {
  87. case AF_INET:
  88. return ((u16 __force)peer->srx.transport.sin.sin_port -
  89. (u16 __force)srx->transport.sin.sin_port) ?:
  90. memcmp(&peer->srx.transport.sin.sin_addr,
  91. &srx->transport.sin.sin_addr,
  92. sizeof(struct in_addr));
  93. #ifdef CONFIG_AF_RXRPC_IPV6
  94. case AF_INET6:
  95. return ((u16 __force)peer->srx.transport.sin6.sin6_port -
  96. (u16 __force)srx->transport.sin6.sin6_port) ?:
  97. memcmp(&peer->srx.transport.sin6.sin6_addr,
  98. &srx->transport.sin6.sin6_addr,
  99. sizeof(struct in6_addr));
  100. #endif
  101. default:
  102. BUG();
  103. }
  104. }
  105. /*
  106. * Look up a remote transport endpoint for the specified address using RCU.
  107. */
  108. static struct rxrpc_peer *__rxrpc_lookup_peer_rcu(
  109. struct rxrpc_local *local,
  110. const struct sockaddr_rxrpc *srx,
  111. unsigned long hash_key)
  112. {
  113. struct rxrpc_peer *peer;
  114. hash_for_each_possible_rcu(rxrpc_peer_hash, peer, hash_link, hash_key) {
  115. if (rxrpc_peer_cmp_key(peer, local, srx, hash_key) == 0) {
  116. if (atomic_read(&peer->usage) == 0)
  117. return NULL;
  118. return peer;
  119. }
  120. }
  121. return NULL;
  122. }
  123. /*
  124. * Look up a remote transport endpoint for the specified address using RCU.
  125. */
  126. struct rxrpc_peer *rxrpc_lookup_peer_rcu(struct rxrpc_local *local,
  127. const struct sockaddr_rxrpc *srx)
  128. {
  129. struct rxrpc_peer *peer;
  130. unsigned long hash_key = rxrpc_peer_hash_key(local, srx);
  131. peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
  132. if (peer) {
  133. _net("PEER %d {%pISp}", peer->debug_id, &peer->srx.transport);
  134. _leave(" = %p {u=%d}", peer, atomic_read(&peer->usage));
  135. }
  136. return peer;
  137. }
  138. /*
  139. * assess the MTU size for the network interface through which this peer is
  140. * reached
  141. */
  142. static void rxrpc_assess_MTU_size(struct rxrpc_peer *peer)
  143. {
  144. struct dst_entry *dst;
  145. struct rtable *rt;
  146. struct flowi fl;
  147. struct flowi4 *fl4 = &fl.u.ip4;
  148. #ifdef CONFIG_AF_RXRPC_IPV6
  149. struct flowi6 *fl6 = &fl.u.ip6;
  150. #endif
  151. peer->if_mtu = 1500;
  152. memset(&fl, 0, sizeof(fl));
  153. switch (peer->srx.transport.family) {
  154. case AF_INET:
  155. rt = ip_route_output_ports(
  156. &init_net, fl4, NULL,
  157. peer->srx.transport.sin.sin_addr.s_addr, 0,
  158. htons(7000), htons(7001), IPPROTO_UDP, 0, 0);
  159. if (IS_ERR(rt)) {
  160. _leave(" [route err %ld]", PTR_ERR(rt));
  161. return;
  162. }
  163. dst = &rt->dst;
  164. break;
  165. #ifdef CONFIG_AF_RXRPC_IPV6
  166. case AF_INET6:
  167. fl6->flowi6_iif = LOOPBACK_IFINDEX;
  168. fl6->flowi6_scope = RT_SCOPE_UNIVERSE;
  169. fl6->flowi6_proto = IPPROTO_UDP;
  170. memcpy(&fl6->daddr, &peer->srx.transport.sin6.sin6_addr,
  171. sizeof(struct in6_addr));
  172. fl6->fl6_dport = htons(7001);
  173. fl6->fl6_sport = htons(7000);
  174. dst = ip6_route_output(&init_net, NULL, fl6);
  175. if (dst->error) {
  176. _leave(" [route err %d]", dst->error);
  177. return;
  178. }
  179. break;
  180. #endif
  181. default:
  182. BUG();
  183. }
  184. peer->if_mtu = dst_mtu(dst);
  185. dst_release(dst);
  186. _leave(" [if_mtu %u]", peer->if_mtu);
  187. }
  188. /*
  189. * Allocate a peer.
  190. */
  191. struct rxrpc_peer *rxrpc_alloc_peer(struct rxrpc_local *local, gfp_t gfp)
  192. {
  193. struct rxrpc_peer *peer;
  194. _enter("");
  195. peer = kzalloc(sizeof(struct rxrpc_peer), gfp);
  196. if (peer) {
  197. atomic_set(&peer->usage, 1);
  198. peer->local = local;
  199. INIT_HLIST_HEAD(&peer->error_targets);
  200. INIT_WORK(&peer->error_distributor,
  201. &rxrpc_peer_error_distributor);
  202. peer->service_conns = RB_ROOT;
  203. seqlock_init(&peer->service_conn_lock);
  204. spin_lock_init(&peer->lock);
  205. peer->debug_id = atomic_inc_return(&rxrpc_debug_id);
  206. }
  207. _leave(" = %p", peer);
  208. return peer;
  209. }
  210. /*
  211. * Initialise peer record.
  212. */
  213. static void rxrpc_init_peer(struct rxrpc_peer *peer, unsigned long hash_key)
  214. {
  215. peer->hash_key = hash_key;
  216. rxrpc_assess_MTU_size(peer);
  217. peer->mtu = peer->if_mtu;
  218. peer->rtt_last_req = ktime_get_real();
  219. switch (peer->srx.transport.family) {
  220. case AF_INET:
  221. peer->hdrsize = sizeof(struct iphdr);
  222. break;
  223. #ifdef CONFIG_AF_RXRPC_IPV6
  224. case AF_INET6:
  225. peer->hdrsize = sizeof(struct ipv6hdr);
  226. break;
  227. #endif
  228. default:
  229. BUG();
  230. }
  231. switch (peer->srx.transport_type) {
  232. case SOCK_DGRAM:
  233. peer->hdrsize += sizeof(struct udphdr);
  234. break;
  235. default:
  236. BUG();
  237. }
  238. peer->hdrsize += sizeof(struct rxrpc_wire_header);
  239. peer->maxdata = peer->mtu - peer->hdrsize;
  240. }
  241. /*
  242. * Set up a new peer.
  243. */
  244. static struct rxrpc_peer *rxrpc_create_peer(struct rxrpc_local *local,
  245. struct sockaddr_rxrpc *srx,
  246. unsigned long hash_key,
  247. gfp_t gfp)
  248. {
  249. struct rxrpc_peer *peer;
  250. _enter("");
  251. peer = rxrpc_alloc_peer(local, gfp);
  252. if (peer) {
  253. memcpy(&peer->srx, srx, sizeof(*srx));
  254. rxrpc_init_peer(peer, hash_key);
  255. }
  256. _leave(" = %p", peer);
  257. return peer;
  258. }
  259. /*
  260. * Set up a new incoming peer. The address is prestored in the preallocated
  261. * peer.
  262. */
  263. struct rxrpc_peer *rxrpc_lookup_incoming_peer(struct rxrpc_local *local,
  264. struct rxrpc_peer *prealloc)
  265. {
  266. struct rxrpc_peer *peer;
  267. unsigned long hash_key;
  268. hash_key = rxrpc_peer_hash_key(local, &prealloc->srx);
  269. prealloc->local = local;
  270. rxrpc_init_peer(prealloc, hash_key);
  271. spin_lock(&rxrpc_peer_hash_lock);
  272. /* Need to check that we aren't racing with someone else */
  273. peer = __rxrpc_lookup_peer_rcu(local, &prealloc->srx, hash_key);
  274. if (peer && !rxrpc_get_peer_maybe(peer))
  275. peer = NULL;
  276. if (!peer) {
  277. peer = prealloc;
  278. hash_add_rcu(rxrpc_peer_hash, &peer->hash_link, hash_key);
  279. }
  280. spin_unlock(&rxrpc_peer_hash_lock);
  281. return peer;
  282. }
  283. /*
  284. * obtain a remote transport endpoint for the specified address
  285. */
  286. struct rxrpc_peer *rxrpc_lookup_peer(struct rxrpc_local *local,
  287. struct sockaddr_rxrpc *srx, gfp_t gfp)
  288. {
  289. struct rxrpc_peer *peer, *candidate;
  290. unsigned long hash_key = rxrpc_peer_hash_key(local, srx);
  291. _enter("{%pISp}", &srx->transport);
  292. /* search the peer list first */
  293. rcu_read_lock();
  294. peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
  295. if (peer && !rxrpc_get_peer_maybe(peer))
  296. peer = NULL;
  297. rcu_read_unlock();
  298. if (!peer) {
  299. /* The peer is not yet present in hash - create a candidate
  300. * for a new record and then redo the search.
  301. */
  302. candidate = rxrpc_create_peer(local, srx, hash_key, gfp);
  303. if (!candidate) {
  304. _leave(" = NULL [nomem]");
  305. return NULL;
  306. }
  307. spin_lock_bh(&rxrpc_peer_hash_lock);
  308. /* Need to check that we aren't racing with someone else */
  309. peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
  310. if (peer && !rxrpc_get_peer_maybe(peer))
  311. peer = NULL;
  312. if (!peer)
  313. hash_add_rcu(rxrpc_peer_hash,
  314. &candidate->hash_link, hash_key);
  315. spin_unlock_bh(&rxrpc_peer_hash_lock);
  316. if (peer)
  317. kfree(candidate);
  318. else
  319. peer = candidate;
  320. }
  321. _net("PEER %d {%pISp}", peer->debug_id, &peer->srx.transport);
  322. _leave(" = %p {u=%d}", peer, atomic_read(&peer->usage));
  323. return peer;
  324. }
  325. /*
  326. * Discard a ref on a remote peer record.
  327. */
  328. void __rxrpc_put_peer(struct rxrpc_peer *peer)
  329. {
  330. ASSERT(hlist_empty(&peer->error_targets));
  331. spin_lock_bh(&rxrpc_peer_hash_lock);
  332. hash_del_rcu(&peer->hash_link);
  333. spin_unlock_bh(&rxrpc_peer_hash_lock);
  334. kfree_rcu(peer, rcu);
  335. }
  336. /**
  337. * rxrpc_kernel_get_peer - Get the peer address of a call
  338. * @sock: The socket on which the call is in progress.
  339. * @call: The call to query
  340. * @_srx: Where to place the result
  341. *
  342. * Get the address of the remote peer in a call.
  343. */
  344. void rxrpc_kernel_get_peer(struct socket *sock, struct rxrpc_call *call,
  345. struct sockaddr_rxrpc *_srx)
  346. {
  347. *_srx = call->peer->srx;
  348. }
  349. EXPORT_SYMBOL(rxrpc_kernel_get_peer);