ar-local.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /* AF_RXRPC local 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/slab.h>
  15. #include <linux/udp.h>
  16. #include <linux/ip.h>
  17. #include <net/sock.h>
  18. #include <net/af_rxrpc.h>
  19. #include <generated/utsrelease.h>
  20. #include "ar-internal.h"
  21. static const char rxrpc_version_string[65] = "linux-" UTS_RELEASE " AF_RXRPC";
  22. static LIST_HEAD(rxrpc_locals);
  23. DEFINE_RWLOCK(rxrpc_local_lock);
  24. static DECLARE_RWSEM(rxrpc_local_sem);
  25. static DECLARE_WAIT_QUEUE_HEAD(rxrpc_local_wq);
  26. static void rxrpc_destroy_local(struct work_struct *work);
  27. static void rxrpc_process_local_events(struct work_struct *work);
  28. /*
  29. * allocate a new local
  30. */
  31. static
  32. struct rxrpc_local *rxrpc_alloc_local(struct sockaddr_rxrpc *srx)
  33. {
  34. struct rxrpc_local *local;
  35. local = kzalloc(sizeof(struct rxrpc_local), GFP_KERNEL);
  36. if (local) {
  37. INIT_WORK(&local->destroyer, &rxrpc_destroy_local);
  38. INIT_WORK(&local->acceptor, &rxrpc_accept_incoming_calls);
  39. INIT_WORK(&local->rejecter, &rxrpc_reject_packets);
  40. INIT_WORK(&local->event_processor, &rxrpc_process_local_events);
  41. INIT_LIST_HEAD(&local->services);
  42. INIT_LIST_HEAD(&local->link);
  43. init_rwsem(&local->defrag_sem);
  44. skb_queue_head_init(&local->accept_queue);
  45. skb_queue_head_init(&local->reject_queue);
  46. skb_queue_head_init(&local->event_queue);
  47. spin_lock_init(&local->lock);
  48. rwlock_init(&local->services_lock);
  49. atomic_set(&local->usage, 1);
  50. local->debug_id = atomic_inc_return(&rxrpc_debug_id);
  51. memcpy(&local->srx, srx, sizeof(*srx));
  52. }
  53. _leave(" = %p", local);
  54. return local;
  55. }
  56. /*
  57. * create the local socket
  58. * - must be called with rxrpc_local_sem writelocked
  59. */
  60. static int rxrpc_create_local(struct rxrpc_local *local)
  61. {
  62. struct sock *sock;
  63. int ret, opt;
  64. _enter("%p{%d}", local, local->srx.transport_type);
  65. /* create a socket to represent the local endpoint */
  66. ret = sock_create_kern(&init_net, PF_INET, local->srx.transport_type,
  67. IPPROTO_UDP, &local->socket);
  68. if (ret < 0) {
  69. _leave(" = %d [socket]", ret);
  70. return ret;
  71. }
  72. /* if a local address was supplied then bind it */
  73. if (local->srx.transport_len > sizeof(sa_family_t)) {
  74. _debug("bind");
  75. ret = kernel_bind(local->socket,
  76. (struct sockaddr *) &local->srx.transport,
  77. local->srx.transport_len);
  78. if (ret < 0) {
  79. _debug("bind failed");
  80. goto error;
  81. }
  82. }
  83. /* we want to receive ICMP errors */
  84. opt = 1;
  85. ret = kernel_setsockopt(local->socket, SOL_IP, IP_RECVERR,
  86. (char *) &opt, sizeof(opt));
  87. if (ret < 0) {
  88. _debug("setsockopt failed");
  89. goto error;
  90. }
  91. /* we want to set the don't fragment bit */
  92. opt = IP_PMTUDISC_DO;
  93. ret = kernel_setsockopt(local->socket, SOL_IP, IP_MTU_DISCOVER,
  94. (char *) &opt, sizeof(opt));
  95. if (ret < 0) {
  96. _debug("setsockopt failed");
  97. goto error;
  98. }
  99. write_lock_bh(&rxrpc_local_lock);
  100. list_add(&local->link, &rxrpc_locals);
  101. write_unlock_bh(&rxrpc_local_lock);
  102. /* set the socket up */
  103. sock = local->socket->sk;
  104. sock->sk_user_data = local;
  105. sock->sk_data_ready = rxrpc_data_ready;
  106. sock->sk_error_report = rxrpc_UDP_error_report;
  107. _leave(" = 0");
  108. return 0;
  109. error:
  110. kernel_sock_shutdown(local->socket, SHUT_RDWR);
  111. local->socket->sk->sk_user_data = NULL;
  112. sock_release(local->socket);
  113. local->socket = NULL;
  114. _leave(" = %d", ret);
  115. return ret;
  116. }
  117. /*
  118. * create a new local endpoint using the specified UDP address
  119. */
  120. struct rxrpc_local *rxrpc_lookup_local(struct sockaddr_rxrpc *srx)
  121. {
  122. struct rxrpc_local *local;
  123. int ret;
  124. _enter("{%d,%u,%pI4+%hu}",
  125. srx->transport_type,
  126. srx->transport.family,
  127. &srx->transport.sin.sin_addr,
  128. ntohs(srx->transport.sin.sin_port));
  129. down_write(&rxrpc_local_sem);
  130. /* see if we have a suitable local local endpoint already */
  131. read_lock_bh(&rxrpc_local_lock);
  132. list_for_each_entry(local, &rxrpc_locals, link) {
  133. _debug("CMP {%d,%u,%pI4+%hu}",
  134. local->srx.transport_type,
  135. local->srx.transport.family,
  136. &local->srx.transport.sin.sin_addr,
  137. ntohs(local->srx.transport.sin.sin_port));
  138. if (local->srx.transport_type != srx->transport_type ||
  139. local->srx.transport.family != srx->transport.family)
  140. continue;
  141. switch (srx->transport.family) {
  142. case AF_INET:
  143. if (local->srx.transport.sin.sin_port !=
  144. srx->transport.sin.sin_port)
  145. continue;
  146. if (memcmp(&local->srx.transport.sin.sin_addr,
  147. &srx->transport.sin.sin_addr,
  148. sizeof(struct in_addr)) != 0)
  149. continue;
  150. goto found_local;
  151. default:
  152. BUG();
  153. }
  154. }
  155. read_unlock_bh(&rxrpc_local_lock);
  156. /* we didn't find one, so we need to create one */
  157. local = rxrpc_alloc_local(srx);
  158. if (!local) {
  159. up_write(&rxrpc_local_sem);
  160. return ERR_PTR(-ENOMEM);
  161. }
  162. ret = rxrpc_create_local(local);
  163. if (ret < 0) {
  164. up_write(&rxrpc_local_sem);
  165. kfree(local);
  166. _leave(" = %d", ret);
  167. return ERR_PTR(ret);
  168. }
  169. up_write(&rxrpc_local_sem);
  170. _net("LOCAL new %d {%d,%u,%pI4+%hu}",
  171. local->debug_id,
  172. local->srx.transport_type,
  173. local->srx.transport.family,
  174. &local->srx.transport.sin.sin_addr,
  175. ntohs(local->srx.transport.sin.sin_port));
  176. _leave(" = %p [new]", local);
  177. return local;
  178. found_local:
  179. rxrpc_get_local(local);
  180. read_unlock_bh(&rxrpc_local_lock);
  181. up_write(&rxrpc_local_sem);
  182. _net("LOCAL old %d {%d,%u,%pI4+%hu}",
  183. local->debug_id,
  184. local->srx.transport_type,
  185. local->srx.transport.family,
  186. &local->srx.transport.sin.sin_addr,
  187. ntohs(local->srx.transport.sin.sin_port));
  188. _leave(" = %p [reuse]", local);
  189. return local;
  190. }
  191. /*
  192. * release a local endpoint
  193. */
  194. void rxrpc_put_local(struct rxrpc_local *local)
  195. {
  196. _enter("%p{u=%d}", local, atomic_read(&local->usage));
  197. ASSERTCMP(atomic_read(&local->usage), >, 0);
  198. /* to prevent a race, the decrement and the dequeue must be effectively
  199. * atomic */
  200. write_lock_bh(&rxrpc_local_lock);
  201. if (unlikely(atomic_dec_and_test(&local->usage))) {
  202. _debug("destroy local");
  203. rxrpc_queue_work(&local->destroyer);
  204. }
  205. write_unlock_bh(&rxrpc_local_lock);
  206. _leave("");
  207. }
  208. /*
  209. * destroy a local endpoint
  210. */
  211. static void rxrpc_destroy_local(struct work_struct *work)
  212. {
  213. struct rxrpc_local *local =
  214. container_of(work, struct rxrpc_local, destroyer);
  215. _enter("%p{%d}", local, atomic_read(&local->usage));
  216. down_write(&rxrpc_local_sem);
  217. write_lock_bh(&rxrpc_local_lock);
  218. if (atomic_read(&local->usage) > 0) {
  219. write_unlock_bh(&rxrpc_local_lock);
  220. up_read(&rxrpc_local_sem);
  221. _leave(" [resurrected]");
  222. return;
  223. }
  224. list_del(&local->link);
  225. local->socket->sk->sk_user_data = NULL;
  226. write_unlock_bh(&rxrpc_local_lock);
  227. downgrade_write(&rxrpc_local_sem);
  228. ASSERT(list_empty(&local->services));
  229. ASSERT(!work_pending(&local->acceptor));
  230. ASSERT(!work_pending(&local->rejecter));
  231. ASSERT(!work_pending(&local->event_processor));
  232. /* finish cleaning up the local descriptor */
  233. rxrpc_purge_queue(&local->accept_queue);
  234. rxrpc_purge_queue(&local->reject_queue);
  235. rxrpc_purge_queue(&local->event_queue);
  236. kernel_sock_shutdown(local->socket, SHUT_RDWR);
  237. sock_release(local->socket);
  238. up_read(&rxrpc_local_sem);
  239. _net("DESTROY LOCAL %d", local->debug_id);
  240. kfree(local);
  241. if (list_empty(&rxrpc_locals))
  242. wake_up_all(&rxrpc_local_wq);
  243. _leave("");
  244. }
  245. /*
  246. * preemptively destroy all local local endpoint rather than waiting for
  247. * them to be destroyed
  248. */
  249. void __exit rxrpc_destroy_all_locals(void)
  250. {
  251. DECLARE_WAITQUEUE(myself,current);
  252. _enter("");
  253. /* we simply have to wait for them to go away */
  254. if (!list_empty(&rxrpc_locals)) {
  255. set_current_state(TASK_UNINTERRUPTIBLE);
  256. add_wait_queue(&rxrpc_local_wq, &myself);
  257. while (!list_empty(&rxrpc_locals)) {
  258. schedule();
  259. set_current_state(TASK_UNINTERRUPTIBLE);
  260. }
  261. remove_wait_queue(&rxrpc_local_wq, &myself);
  262. set_current_state(TASK_RUNNING);
  263. }
  264. _leave("");
  265. }
  266. /*
  267. * Reply to a version request
  268. */
  269. static void rxrpc_send_version_request(struct rxrpc_local *local,
  270. struct rxrpc_header *hdr,
  271. struct sk_buff *skb)
  272. {
  273. struct sockaddr_in sin;
  274. struct msghdr msg;
  275. struct kvec iov[2];
  276. size_t len;
  277. int ret;
  278. _enter("");
  279. sin.sin_family = AF_INET;
  280. sin.sin_port = udp_hdr(skb)->source;
  281. sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
  282. msg.msg_name = &sin;
  283. msg.msg_namelen = sizeof(sin);
  284. msg.msg_control = NULL;
  285. msg.msg_controllen = 0;
  286. msg.msg_flags = 0;
  287. hdr->seq = 0;
  288. hdr->serial = 0;
  289. hdr->type = RXRPC_PACKET_TYPE_VERSION;
  290. hdr->flags = RXRPC_LAST_PACKET | (~hdr->flags & RXRPC_CLIENT_INITIATED);
  291. hdr->userStatus = 0;
  292. hdr->_rsvd = 0;
  293. iov[0].iov_base = hdr;
  294. iov[0].iov_len = sizeof(*hdr);
  295. iov[1].iov_base = (char *)rxrpc_version_string;
  296. iov[1].iov_len = sizeof(rxrpc_version_string);
  297. len = iov[0].iov_len + iov[1].iov_len;
  298. _proto("Tx VERSION (reply)");
  299. ret = kernel_sendmsg(local->socket, &msg, iov, 2, len);
  300. if (ret < 0)
  301. _debug("sendmsg failed: %d", ret);
  302. _leave("");
  303. }
  304. /*
  305. * Process event packets targetted at a local endpoint.
  306. */
  307. static void rxrpc_process_local_events(struct work_struct *work)
  308. {
  309. struct rxrpc_local *local = container_of(work, struct rxrpc_local, event_processor);
  310. struct sk_buff *skb;
  311. char v;
  312. _enter("");
  313. atomic_inc(&local->usage);
  314. while ((skb = skb_dequeue(&local->event_queue))) {
  315. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  316. kdebug("{%d},{%u}", local->debug_id, sp->hdr.type);
  317. switch (sp->hdr.type) {
  318. case RXRPC_PACKET_TYPE_VERSION:
  319. if (skb_copy_bits(skb, 0, &v, 1) < 0)
  320. return;
  321. _proto("Rx VERSION { %02x }", v);
  322. if (v == 0)
  323. rxrpc_send_version_request(local, &sp->hdr, skb);
  324. break;
  325. default:
  326. /* Just ignore anything we don't understand */
  327. break;
  328. }
  329. rxrpc_put_local(local);
  330. rxrpc_free_skb(skb);
  331. }
  332. rxrpc_put_local(local);
  333. _leave("");
  334. }