peer_object.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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/slab.h>
  18. #include <linux/hashtable.h>
  19. #include <net/sock.h>
  20. #include <net/af_rxrpc.h>
  21. #include <net/ip.h>
  22. #include <net/route.h>
  23. #include "ar-internal.h"
  24. static DEFINE_HASHTABLE(rxrpc_peer_hash, 10);
  25. static DEFINE_SPINLOCK(rxrpc_peer_hash_lock);
  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. default:
  47. WARN(1, "AF_RXRPC: Unsupported transport address family\n");
  48. return 0;
  49. }
  50. /* Step through the peer address in 16-bit portions for speed */
  51. for (i = 0; i < size; i += sizeof(*p), p++)
  52. hash_key += *p;
  53. _leave(" 0x%lx", hash_key);
  54. return hash_key;
  55. }
  56. /*
  57. * Compare a peer to a key. Return -ve, 0 or +ve to indicate less than, same
  58. * or greater than.
  59. *
  60. * Unfortunately, the primitives in linux/hashtable.h don't allow for sorted
  61. * buckets and mid-bucket insertion, so we don't make full use of this
  62. * information at this point.
  63. */
  64. static long rxrpc_peer_cmp_key(const struct rxrpc_peer *peer,
  65. struct rxrpc_local *local,
  66. const struct sockaddr_rxrpc *srx,
  67. unsigned long hash_key)
  68. {
  69. long diff;
  70. diff = ((peer->hash_key - hash_key) ?:
  71. ((unsigned long)peer->local - (unsigned long)local) ?:
  72. (peer->srx.transport_type - srx->transport_type) ?:
  73. (peer->srx.transport_len - srx->transport_len) ?:
  74. (peer->srx.transport.family - srx->transport.family));
  75. if (diff != 0)
  76. return diff;
  77. switch (srx->transport.family) {
  78. case AF_INET:
  79. return ((u16 __force)peer->srx.transport.sin.sin_port -
  80. (u16 __force)srx->transport.sin.sin_port) ?:
  81. memcmp(&peer->srx.transport.sin.sin_addr,
  82. &srx->transport.sin.sin_addr,
  83. sizeof(struct in_addr));
  84. default:
  85. BUG();
  86. }
  87. }
  88. /*
  89. * Look up a remote transport endpoint for the specified address using RCU.
  90. */
  91. static struct rxrpc_peer *__rxrpc_lookup_peer_rcu(
  92. struct rxrpc_local *local,
  93. const struct sockaddr_rxrpc *srx,
  94. unsigned long hash_key)
  95. {
  96. struct rxrpc_peer *peer;
  97. hash_for_each_possible_rcu(rxrpc_peer_hash, peer, hash_link, hash_key) {
  98. if (rxrpc_peer_cmp_key(peer, local, srx, hash_key) == 0) {
  99. if (atomic_read(&peer->usage) == 0)
  100. return NULL;
  101. return peer;
  102. }
  103. }
  104. return NULL;
  105. }
  106. /*
  107. * Look up a remote transport endpoint for the specified address using RCU.
  108. */
  109. struct rxrpc_peer *rxrpc_lookup_peer_rcu(struct rxrpc_local *local,
  110. const struct sockaddr_rxrpc *srx)
  111. {
  112. struct rxrpc_peer *peer;
  113. unsigned long hash_key = rxrpc_peer_hash_key(local, srx);
  114. peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
  115. if (peer) {
  116. switch (srx->transport.family) {
  117. case AF_INET:
  118. _net("PEER %d {%d,%u,%pI4+%hu}",
  119. peer->debug_id,
  120. peer->srx.transport_type,
  121. peer->srx.transport.family,
  122. &peer->srx.transport.sin.sin_addr,
  123. ntohs(peer->srx.transport.sin.sin_port));
  124. break;
  125. }
  126. _leave(" = %p {u=%d}", peer, atomic_read(&peer->usage));
  127. }
  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_peer *peer)
  135. {
  136. struct rtable *rt;
  137. struct flowi4 fl4;
  138. peer->if_mtu = 1500;
  139. rt = ip_route_output_ports(&init_net, &fl4, NULL,
  140. peer->srx.transport.sin.sin_addr.s_addr, 0,
  141. htons(7000), htons(7001),
  142. IPPROTO_UDP, 0, 0);
  143. if (IS_ERR(rt)) {
  144. _leave(" [route err %ld]", PTR_ERR(rt));
  145. return;
  146. }
  147. peer->if_mtu = dst_mtu(&rt->dst);
  148. dst_release(&rt->dst);
  149. _leave(" [if_mtu %u]", peer->if_mtu);
  150. }
  151. /*
  152. * Allocate a peer.
  153. */
  154. struct rxrpc_peer *rxrpc_alloc_peer(struct rxrpc_local *local, gfp_t gfp)
  155. {
  156. struct rxrpc_peer *peer;
  157. _enter("");
  158. peer = kzalloc(sizeof(struct rxrpc_peer), gfp);
  159. if (peer) {
  160. atomic_set(&peer->usage, 1);
  161. peer->local = local;
  162. INIT_HLIST_HEAD(&peer->error_targets);
  163. INIT_WORK(&peer->error_distributor,
  164. &rxrpc_peer_error_distributor);
  165. peer->service_conns = RB_ROOT;
  166. seqlock_init(&peer->service_conn_lock);
  167. spin_lock_init(&peer->lock);
  168. peer->debug_id = atomic_inc_return(&rxrpc_debug_id);
  169. }
  170. _leave(" = %p", peer);
  171. return peer;
  172. }
  173. /*
  174. * Set up a new peer.
  175. */
  176. static struct rxrpc_peer *rxrpc_create_peer(struct rxrpc_local *local,
  177. struct sockaddr_rxrpc *srx,
  178. unsigned long hash_key,
  179. gfp_t gfp)
  180. {
  181. struct rxrpc_peer *peer;
  182. _enter("");
  183. peer = rxrpc_alloc_peer(local, gfp);
  184. if (peer) {
  185. peer->hash_key = hash_key;
  186. memcpy(&peer->srx, srx, sizeof(*srx));
  187. rxrpc_assess_MTU_size(peer);
  188. peer->mtu = peer->if_mtu;
  189. if (srx->transport.family == AF_INET) {
  190. peer->hdrsize = sizeof(struct iphdr);
  191. switch (srx->transport_type) {
  192. case SOCK_DGRAM:
  193. peer->hdrsize += sizeof(struct udphdr);
  194. break;
  195. default:
  196. BUG();
  197. break;
  198. }
  199. } else {
  200. BUG();
  201. }
  202. peer->hdrsize += sizeof(struct rxrpc_wire_header);
  203. peer->maxdata = peer->mtu - peer->hdrsize;
  204. }
  205. _leave(" = %p", peer);
  206. return peer;
  207. }
  208. /*
  209. * obtain a remote transport endpoint for the specified address
  210. */
  211. struct rxrpc_peer *rxrpc_lookup_peer(struct rxrpc_local *local,
  212. struct sockaddr_rxrpc *srx, gfp_t gfp)
  213. {
  214. struct rxrpc_peer *peer, *candidate;
  215. unsigned long hash_key = rxrpc_peer_hash_key(local, srx);
  216. _enter("{%d,%d,%pI4+%hu}",
  217. srx->transport_type,
  218. srx->transport_len,
  219. &srx->transport.sin.sin_addr,
  220. ntohs(srx->transport.sin.sin_port));
  221. /* search the peer list first */
  222. rcu_read_lock();
  223. peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
  224. if (peer && !rxrpc_get_peer_maybe(peer))
  225. peer = NULL;
  226. rcu_read_unlock();
  227. if (!peer) {
  228. /* The peer is not yet present in hash - create a candidate
  229. * for a new record and then redo the search.
  230. */
  231. candidate = rxrpc_create_peer(local, srx, hash_key, gfp);
  232. if (!candidate) {
  233. _leave(" = NULL [nomem]");
  234. return NULL;
  235. }
  236. spin_lock(&rxrpc_peer_hash_lock);
  237. /* Need to check that we aren't racing with someone else */
  238. peer = __rxrpc_lookup_peer_rcu(local, srx, hash_key);
  239. if (peer && !rxrpc_get_peer_maybe(peer))
  240. peer = NULL;
  241. if (!peer)
  242. hash_add_rcu(rxrpc_peer_hash,
  243. &candidate->hash_link, hash_key);
  244. spin_unlock(&rxrpc_peer_hash_lock);
  245. if (peer)
  246. kfree(candidate);
  247. else
  248. peer = candidate;
  249. }
  250. _net("PEER %d {%d,%pI4+%hu}",
  251. peer->debug_id,
  252. peer->srx.transport_type,
  253. &peer->srx.transport.sin.sin_addr,
  254. ntohs(peer->srx.transport.sin.sin_port));
  255. _leave(" = %p {u=%d}", peer, atomic_read(&peer->usage));
  256. return peer;
  257. }
  258. /*
  259. * Discard a ref on a remote peer record.
  260. */
  261. void __rxrpc_put_peer(struct rxrpc_peer *peer)
  262. {
  263. ASSERT(hlist_empty(&peer->error_targets));
  264. spin_lock(&rxrpc_peer_hash_lock);
  265. hash_del_rcu(&peer->hash_link);
  266. spin_unlock(&rxrpc_peer_hash_lock);
  267. kfree_rcu(peer, rcu);
  268. }