ar-peer.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* RxRPC remote transport endpoint management
  2. *
  3. * Copyright (C) 2007 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. #include <linux/module.h>
  12. #include <linux/net.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/udp.h>
  15. #include <linux/in.h>
  16. #include <linux/in6.h>
  17. #include <linux/icmp.h>
  18. #include <linux/slab.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 LIST_HEAD(rxrpc_peers);
  25. static DEFINE_RWLOCK(rxrpc_peer_lock);
  26. static DECLARE_WAIT_QUEUE_HEAD(rxrpc_peer_wq);
  27. static void rxrpc_destroy_peer(struct work_struct *work);
  28. /*
  29. * assess the MTU size for the network interface through which this peer is
  30. * reached
  31. */
  32. static void rxrpc_assess_MTU_size(struct rxrpc_peer *peer)
  33. {
  34. struct rtable *rt;
  35. struct flowi4 fl4;
  36. peer->if_mtu = 1500;
  37. rt = ip_route_output_ports(&init_net, &fl4, NULL,
  38. peer->srx.transport.sin.sin_addr.s_addr, 0,
  39. htons(7000), htons(7001),
  40. IPPROTO_UDP, 0, 0);
  41. if (IS_ERR(rt)) {
  42. _leave(" [route err %ld]", PTR_ERR(rt));
  43. return;
  44. }
  45. peer->if_mtu = dst_mtu(&rt->dst);
  46. dst_release(&rt->dst);
  47. _leave(" [if_mtu %u]", peer->if_mtu);
  48. }
  49. /*
  50. * allocate a new peer
  51. */
  52. static struct rxrpc_peer *rxrpc_alloc_peer(struct sockaddr_rxrpc *srx,
  53. gfp_t gfp)
  54. {
  55. struct rxrpc_peer *peer;
  56. _enter("");
  57. peer = kzalloc(sizeof(struct rxrpc_peer), gfp);
  58. if (peer) {
  59. INIT_WORK(&peer->destroyer, &rxrpc_destroy_peer);
  60. INIT_LIST_HEAD(&peer->link);
  61. INIT_LIST_HEAD(&peer->error_targets);
  62. spin_lock_init(&peer->lock);
  63. atomic_set(&peer->usage, 1);
  64. peer->debug_id = atomic_inc_return(&rxrpc_debug_id);
  65. memcpy(&peer->srx, srx, sizeof(*srx));
  66. rxrpc_assess_MTU_size(peer);
  67. peer->mtu = peer->if_mtu;
  68. if (srx->transport.family == AF_INET) {
  69. peer->hdrsize = sizeof(struct iphdr);
  70. switch (srx->transport_type) {
  71. case SOCK_DGRAM:
  72. peer->hdrsize += sizeof(struct udphdr);
  73. break;
  74. default:
  75. BUG();
  76. break;
  77. }
  78. } else {
  79. BUG();
  80. }
  81. peer->hdrsize += sizeof(struct rxrpc_header);
  82. peer->maxdata = peer->mtu - peer->hdrsize;
  83. }
  84. _leave(" = %p", peer);
  85. return peer;
  86. }
  87. /*
  88. * obtain a remote transport endpoint for the specified address
  89. */
  90. struct rxrpc_peer *rxrpc_get_peer(struct sockaddr_rxrpc *srx, gfp_t gfp)
  91. {
  92. struct rxrpc_peer *peer, *candidate;
  93. const char *new = "old";
  94. int usage;
  95. _enter("{%d,%d,%pI4+%hu}",
  96. srx->transport_type,
  97. srx->transport_len,
  98. &srx->transport.sin.sin_addr,
  99. ntohs(srx->transport.sin.sin_port));
  100. /* search the peer list first */
  101. read_lock_bh(&rxrpc_peer_lock);
  102. list_for_each_entry(peer, &rxrpc_peers, link) {
  103. _debug("check PEER %d { u=%d t=%d l=%d }",
  104. peer->debug_id,
  105. atomic_read(&peer->usage),
  106. peer->srx.transport_type,
  107. peer->srx.transport_len);
  108. if (atomic_read(&peer->usage) > 0 &&
  109. peer->srx.transport_type == srx->transport_type &&
  110. peer->srx.transport_len == srx->transport_len &&
  111. memcmp(&peer->srx.transport,
  112. &srx->transport,
  113. srx->transport_len) == 0)
  114. goto found_extant_peer;
  115. }
  116. read_unlock_bh(&rxrpc_peer_lock);
  117. /* not yet present - create a candidate for a new record and then
  118. * redo the search */
  119. candidate = rxrpc_alloc_peer(srx, gfp);
  120. if (!candidate) {
  121. _leave(" = -ENOMEM");
  122. return ERR_PTR(-ENOMEM);
  123. }
  124. write_lock_bh(&rxrpc_peer_lock);
  125. list_for_each_entry(peer, &rxrpc_peers, link) {
  126. if (atomic_read(&peer->usage) > 0 &&
  127. peer->srx.transport_type == srx->transport_type &&
  128. peer->srx.transport_len == srx->transport_len &&
  129. memcmp(&peer->srx.transport,
  130. &srx->transport,
  131. srx->transport_len) == 0)
  132. goto found_extant_second;
  133. }
  134. /* we can now add the new candidate to the list */
  135. peer = candidate;
  136. candidate = NULL;
  137. usage = atomic_read(&peer->usage);
  138. list_add_tail(&peer->link, &rxrpc_peers);
  139. write_unlock_bh(&rxrpc_peer_lock);
  140. new = "new";
  141. success:
  142. _net("PEER %s %d {%d,%u,%pI4+%hu}",
  143. new,
  144. peer->debug_id,
  145. peer->srx.transport_type,
  146. peer->srx.transport.family,
  147. &peer->srx.transport.sin.sin_addr,
  148. ntohs(peer->srx.transport.sin.sin_port));
  149. _leave(" = %p {u=%d}", peer, usage);
  150. return peer;
  151. /* we found the peer in the list immediately */
  152. found_extant_peer:
  153. usage = atomic_inc_return(&peer->usage);
  154. read_unlock_bh(&rxrpc_peer_lock);
  155. goto success;
  156. /* we found the peer on the second time through the list */
  157. found_extant_second:
  158. usage = atomic_inc_return(&peer->usage);
  159. write_unlock_bh(&rxrpc_peer_lock);
  160. kfree(candidate);
  161. goto success;
  162. }
  163. /*
  164. * find the peer associated with a packet
  165. */
  166. struct rxrpc_peer *rxrpc_find_peer(struct rxrpc_local *local,
  167. __be32 addr, __be16 port)
  168. {
  169. struct rxrpc_peer *peer;
  170. _enter("");
  171. /* search the peer list */
  172. read_lock_bh(&rxrpc_peer_lock);
  173. if (local->srx.transport.family == AF_INET &&
  174. local->srx.transport_type == SOCK_DGRAM
  175. ) {
  176. list_for_each_entry(peer, &rxrpc_peers, link) {
  177. if (atomic_read(&peer->usage) > 0 &&
  178. peer->srx.transport_type == SOCK_DGRAM &&
  179. peer->srx.transport.family == AF_INET &&
  180. peer->srx.transport.sin.sin_port == port &&
  181. peer->srx.transport.sin.sin_addr.s_addr == addr)
  182. goto found_UDP_peer;
  183. }
  184. goto new_UDP_peer;
  185. }
  186. read_unlock_bh(&rxrpc_peer_lock);
  187. _leave(" = -EAFNOSUPPORT");
  188. return ERR_PTR(-EAFNOSUPPORT);
  189. found_UDP_peer:
  190. _net("Rx UDP DGRAM from peer %d", peer->debug_id);
  191. atomic_inc(&peer->usage);
  192. read_unlock_bh(&rxrpc_peer_lock);
  193. _leave(" = %p", peer);
  194. return peer;
  195. new_UDP_peer:
  196. _net("Rx UDP DGRAM from NEW peer");
  197. read_unlock_bh(&rxrpc_peer_lock);
  198. _leave(" = -EBUSY [new]");
  199. return ERR_PTR(-EBUSY);
  200. }
  201. /*
  202. * release a remote transport endpoint
  203. */
  204. void rxrpc_put_peer(struct rxrpc_peer *peer)
  205. {
  206. _enter("%p{u=%d}", peer, atomic_read(&peer->usage));
  207. ASSERTCMP(atomic_read(&peer->usage), >, 0);
  208. if (likely(!atomic_dec_and_test(&peer->usage))) {
  209. _leave(" [in use]");
  210. return;
  211. }
  212. rxrpc_queue_work(&peer->destroyer);
  213. _leave("");
  214. }
  215. /*
  216. * destroy a remote transport endpoint
  217. */
  218. static void rxrpc_destroy_peer(struct work_struct *work)
  219. {
  220. struct rxrpc_peer *peer =
  221. container_of(work, struct rxrpc_peer, destroyer);
  222. _enter("%p{%d}", peer, atomic_read(&peer->usage));
  223. write_lock_bh(&rxrpc_peer_lock);
  224. list_del(&peer->link);
  225. write_unlock_bh(&rxrpc_peer_lock);
  226. _net("DESTROY PEER %d", peer->debug_id);
  227. kfree(peer);
  228. if (list_empty(&rxrpc_peers))
  229. wake_up_all(&rxrpc_peer_wq);
  230. _leave("");
  231. }
  232. /*
  233. * preemptively destroy all the peer records from a transport endpoint rather
  234. * than waiting for them to time out
  235. */
  236. void __exit rxrpc_destroy_all_peers(void)
  237. {
  238. DECLARE_WAITQUEUE(myself,current);
  239. _enter("");
  240. /* we simply have to wait for them to go away */
  241. if (!list_empty(&rxrpc_peers)) {
  242. set_current_state(TASK_UNINTERRUPTIBLE);
  243. add_wait_queue(&rxrpc_peer_wq, &myself);
  244. while (!list_empty(&rxrpc_peers)) {
  245. schedule();
  246. set_current_state(TASK_UNINTERRUPTIBLE);
  247. }
  248. remove_wait_queue(&rxrpc_peer_wq, &myself);
  249. set_current_state(TASK_RUNNING);
  250. }
  251. _leave("");
  252. }