ar-accept.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /* incoming call handling
  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/errqueue.h>
  15. #include <linux/udp.h>
  16. #include <linux/in.h>
  17. #include <linux/in6.h>
  18. #include <linux/icmp.h>
  19. #include <net/sock.h>
  20. #include <net/af_rxrpc.h>
  21. #include <net/ip.h>
  22. #include "ar-internal.h"
  23. /*
  24. * generate a connection-level abort
  25. */
  26. static int rxrpc_busy(struct rxrpc_local *local, struct sockaddr_rxrpc *srx,
  27. struct rxrpc_header *hdr)
  28. {
  29. struct msghdr msg;
  30. struct kvec iov[1];
  31. size_t len;
  32. int ret;
  33. _enter("%d,,", local->debug_id);
  34. msg.msg_name = &srx->transport.sin;
  35. msg.msg_namelen = sizeof(srx->transport.sin);
  36. msg.msg_control = NULL;
  37. msg.msg_controllen = 0;
  38. msg.msg_flags = 0;
  39. hdr->seq = 0;
  40. hdr->type = RXRPC_PACKET_TYPE_BUSY;
  41. hdr->flags = 0;
  42. hdr->userStatus = 0;
  43. hdr->_rsvd = 0;
  44. iov[0].iov_base = hdr;
  45. iov[0].iov_len = sizeof(*hdr);
  46. len = iov[0].iov_len;
  47. hdr->serial = htonl(1);
  48. _proto("Tx BUSY %%%u", ntohl(hdr->serial));
  49. ret = kernel_sendmsg(local->socket, &msg, iov, 1, len);
  50. if (ret < 0) {
  51. _leave(" = -EAGAIN [sendmsg failed: %d]", ret);
  52. return -EAGAIN;
  53. }
  54. _leave(" = 0");
  55. return 0;
  56. }
  57. /*
  58. * accept an incoming call that needs peer, transport and/or connection setting
  59. * up
  60. */
  61. static int rxrpc_accept_incoming_call(struct rxrpc_local *local,
  62. struct rxrpc_sock *rx,
  63. struct sk_buff *skb,
  64. struct sockaddr_rxrpc *srx)
  65. {
  66. struct rxrpc_connection *conn;
  67. struct rxrpc_transport *trans;
  68. struct rxrpc_skb_priv *sp, *nsp;
  69. struct rxrpc_peer *peer;
  70. struct rxrpc_call *call;
  71. struct sk_buff *notification;
  72. int ret;
  73. _enter("");
  74. sp = rxrpc_skb(skb);
  75. /* get a notification message to send to the server app */
  76. notification = alloc_skb(0, GFP_NOFS);
  77. rxrpc_new_skb(notification);
  78. notification->mark = RXRPC_SKB_MARK_NEW_CALL;
  79. peer = rxrpc_get_peer(srx, GFP_NOIO);
  80. if (IS_ERR(peer)) {
  81. _debug("no peer");
  82. ret = -EBUSY;
  83. goto error;
  84. }
  85. trans = rxrpc_get_transport(local, peer, GFP_NOIO);
  86. rxrpc_put_peer(peer);
  87. if (!trans) {
  88. _debug("no trans");
  89. ret = -EBUSY;
  90. goto error;
  91. }
  92. conn = rxrpc_incoming_connection(trans, &sp->hdr, GFP_NOIO);
  93. rxrpc_put_transport(trans);
  94. if (IS_ERR(conn)) {
  95. _debug("no conn");
  96. ret = PTR_ERR(conn);
  97. goto error;
  98. }
  99. call = rxrpc_incoming_call(rx, conn, &sp->hdr, GFP_NOIO);
  100. rxrpc_put_connection(conn);
  101. if (IS_ERR(call)) {
  102. _debug("no call");
  103. ret = PTR_ERR(call);
  104. goto error;
  105. }
  106. /* attach the call to the socket */
  107. read_lock_bh(&local->services_lock);
  108. if (rx->sk.sk_state == RXRPC_CLOSE)
  109. goto invalid_service;
  110. write_lock(&rx->call_lock);
  111. if (!test_and_set_bit(RXRPC_CALL_INIT_ACCEPT, &call->flags)) {
  112. rxrpc_get_call(call);
  113. spin_lock(&call->conn->state_lock);
  114. if (sp->hdr.securityIndex > 0 &&
  115. call->conn->state == RXRPC_CONN_SERVER_UNSECURED) {
  116. _debug("await conn sec");
  117. list_add_tail(&call->accept_link, &rx->secureq);
  118. call->conn->state = RXRPC_CONN_SERVER_CHALLENGING;
  119. atomic_inc(&call->conn->usage);
  120. set_bit(RXRPC_CONN_CHALLENGE, &call->conn->events);
  121. schedule_work(&call->conn->processor);
  122. } else {
  123. _debug("conn ready");
  124. call->state = RXRPC_CALL_SERVER_ACCEPTING;
  125. list_add_tail(&call->accept_link, &rx->acceptq);
  126. rxrpc_get_call(call);
  127. nsp = rxrpc_skb(notification);
  128. nsp->call = call;
  129. ASSERTCMP(atomic_read(&call->usage), >=, 3);
  130. _debug("notify");
  131. spin_lock(&call->lock);
  132. ret = rxrpc_queue_rcv_skb(call, notification, true,
  133. false);
  134. spin_unlock(&call->lock);
  135. notification = NULL;
  136. if (ret < 0)
  137. BUG();
  138. }
  139. spin_unlock(&call->conn->state_lock);
  140. _debug("queued");
  141. }
  142. write_unlock(&rx->call_lock);
  143. _debug("process");
  144. rxrpc_fast_process_packet(call, skb);
  145. _debug("done");
  146. read_unlock_bh(&local->services_lock);
  147. rxrpc_free_skb(notification);
  148. rxrpc_put_call(call);
  149. _leave(" = 0");
  150. return 0;
  151. invalid_service:
  152. _debug("invalid");
  153. read_unlock_bh(&local->services_lock);
  154. read_lock_bh(&call->state_lock);
  155. if (!test_bit(RXRPC_CALL_RELEASE, &call->flags) &&
  156. !test_and_set_bit(RXRPC_CALL_RELEASE, &call->events)) {
  157. rxrpc_get_call(call);
  158. schedule_work(&call->processor);
  159. }
  160. read_unlock_bh(&call->state_lock);
  161. rxrpc_put_call(call);
  162. ret = -ECONNREFUSED;
  163. error:
  164. rxrpc_free_skb(notification);
  165. _leave(" = %d", ret);
  166. return ret;
  167. }
  168. /*
  169. * accept incoming calls that need peer, transport and/or connection setting up
  170. * - the packets we get are all incoming client DATA packets that have seq == 1
  171. */
  172. void rxrpc_accept_incoming_calls(struct work_struct *work)
  173. {
  174. struct rxrpc_local *local =
  175. container_of(work, struct rxrpc_local, acceptor);
  176. struct rxrpc_skb_priv *sp;
  177. struct sockaddr_rxrpc srx;
  178. struct rxrpc_sock *rx;
  179. struct sk_buff *skb;
  180. __be16 service_id;
  181. int ret;
  182. _enter("%d", local->debug_id);
  183. read_lock_bh(&rxrpc_local_lock);
  184. if (atomic_read(&local->usage) > 0)
  185. rxrpc_get_local(local);
  186. else
  187. local = NULL;
  188. read_unlock_bh(&rxrpc_local_lock);
  189. if (!local) {
  190. _leave(" [local dead]");
  191. return;
  192. }
  193. process_next_packet:
  194. skb = skb_dequeue(&local->accept_queue);
  195. if (!skb) {
  196. rxrpc_put_local(local);
  197. _leave("\n");
  198. return;
  199. }
  200. _net("incoming call skb %p", skb);
  201. sp = rxrpc_skb(skb);
  202. /* determine the remote address */
  203. memset(&srx, 0, sizeof(srx));
  204. srx.srx_family = AF_RXRPC;
  205. srx.transport.family = local->srx.transport.family;
  206. srx.transport_type = local->srx.transport_type;
  207. switch (srx.transport.family) {
  208. case AF_INET:
  209. srx.transport_len = sizeof(struct sockaddr_in);
  210. srx.transport.sin.sin_port = udp_hdr(skb)->source;
  211. srx.transport.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
  212. break;
  213. default:
  214. goto busy;
  215. }
  216. /* get the socket providing the service */
  217. service_id = sp->hdr.serviceId;
  218. read_lock_bh(&local->services_lock);
  219. list_for_each_entry(rx, &local->services, listen_link) {
  220. if (rx->service_id == service_id &&
  221. rx->sk.sk_state != RXRPC_CLOSE)
  222. goto found_service;
  223. }
  224. read_unlock_bh(&local->services_lock);
  225. goto invalid_service;
  226. found_service:
  227. _debug("found service %hd", ntohs(rx->service_id));
  228. if (sk_acceptq_is_full(&rx->sk))
  229. goto backlog_full;
  230. sk_acceptq_added(&rx->sk);
  231. sock_hold(&rx->sk);
  232. read_unlock_bh(&local->services_lock);
  233. ret = rxrpc_accept_incoming_call(local, rx, skb, &srx);
  234. if (ret < 0)
  235. sk_acceptq_removed(&rx->sk);
  236. sock_put(&rx->sk);
  237. switch (ret) {
  238. case -ECONNRESET: /* old calls are ignored */
  239. case -ECONNABORTED: /* aborted calls are reaborted or ignored */
  240. case 0:
  241. goto process_next_packet;
  242. case -ECONNREFUSED:
  243. goto invalid_service;
  244. case -EBUSY:
  245. goto busy;
  246. case -EKEYREJECTED:
  247. goto security_mismatch;
  248. default:
  249. BUG();
  250. }
  251. backlog_full:
  252. read_unlock_bh(&local->services_lock);
  253. busy:
  254. rxrpc_busy(local, &srx, &sp->hdr);
  255. rxrpc_free_skb(skb);
  256. goto process_next_packet;
  257. invalid_service:
  258. skb->priority = RX_INVALID_OPERATION;
  259. rxrpc_reject_packet(local, skb);
  260. goto process_next_packet;
  261. /* can't change connection security type mid-flow */
  262. security_mismatch:
  263. skb->priority = RX_PROTOCOL_ERROR;
  264. rxrpc_reject_packet(local, skb);
  265. goto process_next_packet;
  266. }
  267. /*
  268. * handle acceptance of a call by userspace
  269. * - assign the user call ID to the call at the front of the queue
  270. */
  271. int rxrpc_accept_call(struct rxrpc_sock *rx, unsigned long user_call_ID)
  272. {
  273. struct rxrpc_call *call;
  274. struct rb_node *parent, **pp;
  275. int ret;
  276. _enter(",%lx", user_call_ID);
  277. ASSERT(!irqs_disabled());
  278. write_lock(&rx->call_lock);
  279. ret = -ENODATA;
  280. if (list_empty(&rx->acceptq))
  281. goto out;
  282. /* check the user ID isn't already in use */
  283. ret = -EBADSLT;
  284. pp = &rx->calls.rb_node;
  285. parent = NULL;
  286. while (*pp) {
  287. parent = *pp;
  288. call = rb_entry(parent, struct rxrpc_call, sock_node);
  289. if (user_call_ID < call->user_call_ID)
  290. pp = &(*pp)->rb_left;
  291. else if (user_call_ID > call->user_call_ID)
  292. pp = &(*pp)->rb_right;
  293. else
  294. goto out;
  295. }
  296. /* dequeue the first call and check it's still valid */
  297. call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link);
  298. list_del_init(&call->accept_link);
  299. sk_acceptq_removed(&rx->sk);
  300. write_lock_bh(&call->state_lock);
  301. switch (call->state) {
  302. case RXRPC_CALL_SERVER_ACCEPTING:
  303. call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
  304. break;
  305. case RXRPC_CALL_REMOTELY_ABORTED:
  306. case RXRPC_CALL_LOCALLY_ABORTED:
  307. ret = -ECONNABORTED;
  308. goto out_release;
  309. case RXRPC_CALL_NETWORK_ERROR:
  310. ret = call->conn->error;
  311. goto out_release;
  312. case RXRPC_CALL_DEAD:
  313. ret = -ETIME;
  314. goto out_discard;
  315. default:
  316. BUG();
  317. }
  318. /* formalise the acceptance */
  319. call->user_call_ID = user_call_ID;
  320. rb_link_node(&call->sock_node, parent, pp);
  321. rb_insert_color(&call->sock_node, &rx->calls);
  322. if (test_and_set_bit(RXRPC_CALL_HAS_USERID, &call->flags))
  323. BUG();
  324. if (test_and_set_bit(RXRPC_CALL_ACCEPTED, &call->events))
  325. BUG();
  326. schedule_work(&call->processor);
  327. write_unlock_bh(&call->state_lock);
  328. write_unlock(&rx->call_lock);
  329. _leave(" = 0");
  330. return 0;
  331. /* if the call is already dying or dead, then we leave the socket's ref
  332. * on it to be released by rxrpc_dead_call_expired() as induced by
  333. * rxrpc_release_call() */
  334. out_release:
  335. _debug("release %p", call);
  336. if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
  337. !test_and_set_bit(RXRPC_CALL_RELEASE, &call->events))
  338. schedule_work(&call->processor);
  339. out_discard:
  340. write_unlock_bh(&call->state_lock);
  341. _debug("discard %p", call);
  342. out:
  343. write_unlock(&rx->call_lock);
  344. _leave(" = %d", ret);
  345. return ret;
  346. }