ar-accept.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 (IS_ERR(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. rxrpc_queue_conn(call->conn);
  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. BUG_ON(ret < 0);
  137. }
  138. spin_unlock(&call->conn->state_lock);
  139. _debug("queued");
  140. }
  141. write_unlock(&rx->call_lock);
  142. _debug("process");
  143. rxrpc_fast_process_packet(call, skb);
  144. _debug("done");
  145. read_unlock_bh(&local->services_lock);
  146. rxrpc_free_skb(notification);
  147. rxrpc_put_call(call);
  148. _leave(" = 0");
  149. return 0;
  150. invalid_service:
  151. _debug("invalid");
  152. read_unlock_bh(&local->services_lock);
  153. read_lock_bh(&call->state_lock);
  154. if (!test_bit(RXRPC_CALL_RELEASE, &call->flags) &&
  155. !test_and_set_bit(RXRPC_CALL_RELEASE, &call->events)) {
  156. rxrpc_get_call(call);
  157. rxrpc_queue_call(call);
  158. }
  159. read_unlock_bh(&call->state_lock);
  160. rxrpc_put_call(call);
  161. ret = -ECONNREFUSED;
  162. error:
  163. rxrpc_free_skb(notification);
  164. _leave(" = %d", ret);
  165. return ret;
  166. }
  167. /*
  168. * accept incoming calls that need peer, transport and/or connection setting up
  169. * - the packets we get are all incoming client DATA packets that have seq == 1
  170. */
  171. void rxrpc_accept_incoming_calls(struct work_struct *work)
  172. {
  173. struct rxrpc_local *local =
  174. container_of(work, struct rxrpc_local, acceptor);
  175. struct rxrpc_skb_priv *sp;
  176. struct sockaddr_rxrpc srx;
  177. struct rxrpc_sock *rx;
  178. struct sk_buff *skb;
  179. __be16 service_id;
  180. int ret;
  181. _enter("%d", local->debug_id);
  182. read_lock_bh(&rxrpc_local_lock);
  183. if (atomic_read(&local->usage) > 0)
  184. rxrpc_get_local(local);
  185. else
  186. local = NULL;
  187. read_unlock_bh(&rxrpc_local_lock);
  188. if (!local) {
  189. _leave(" [local dead]");
  190. return;
  191. }
  192. process_next_packet:
  193. skb = skb_dequeue(&local->accept_queue);
  194. if (!skb) {
  195. rxrpc_put_local(local);
  196. _leave("\n");
  197. return;
  198. }
  199. _net("incoming call skb %p", skb);
  200. sp = rxrpc_skb(skb);
  201. /* determine the remote address */
  202. memset(&srx, 0, sizeof(srx));
  203. srx.srx_family = AF_RXRPC;
  204. srx.transport.family = local->srx.transport.family;
  205. srx.transport_type = local->srx.transport_type;
  206. switch (srx.transport.family) {
  207. case AF_INET:
  208. srx.transport_len = sizeof(struct sockaddr_in);
  209. srx.transport.sin.sin_port = udp_hdr(skb)->source;
  210. srx.transport.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
  211. break;
  212. default:
  213. goto busy;
  214. }
  215. /* get the socket providing the service */
  216. service_id = sp->hdr.serviceId;
  217. read_lock_bh(&local->services_lock);
  218. list_for_each_entry(rx, &local->services, listen_link) {
  219. if (rx->service_id == service_id &&
  220. rx->sk.sk_state != RXRPC_CLOSE)
  221. goto found_service;
  222. }
  223. read_unlock_bh(&local->services_lock);
  224. goto invalid_service;
  225. found_service:
  226. _debug("found service %hd", ntohs(rx->service_id));
  227. if (sk_acceptq_is_full(&rx->sk))
  228. goto backlog_full;
  229. sk_acceptq_added(&rx->sk);
  230. sock_hold(&rx->sk);
  231. read_unlock_bh(&local->services_lock);
  232. ret = rxrpc_accept_incoming_call(local, rx, skb, &srx);
  233. if (ret < 0)
  234. sk_acceptq_removed(&rx->sk);
  235. sock_put(&rx->sk);
  236. switch (ret) {
  237. case -ECONNRESET: /* old calls are ignored */
  238. case -ECONNABORTED: /* aborted calls are reaborted or ignored */
  239. case 0:
  240. goto process_next_packet;
  241. case -ECONNREFUSED:
  242. goto invalid_service;
  243. case -EBUSY:
  244. goto busy;
  245. case -EKEYREJECTED:
  246. goto security_mismatch;
  247. default:
  248. BUG();
  249. }
  250. backlog_full:
  251. read_unlock_bh(&local->services_lock);
  252. busy:
  253. rxrpc_busy(local, &srx, &sp->hdr);
  254. rxrpc_free_skb(skb);
  255. goto process_next_packet;
  256. invalid_service:
  257. skb->priority = RX_INVALID_OPERATION;
  258. rxrpc_reject_packet(local, skb);
  259. goto process_next_packet;
  260. /* can't change connection security type mid-flow */
  261. security_mismatch:
  262. skb->priority = RX_PROTOCOL_ERROR;
  263. rxrpc_reject_packet(local, skb);
  264. goto process_next_packet;
  265. }
  266. /*
  267. * handle acceptance of a call by userspace
  268. * - assign the user call ID to the call at the front of the queue
  269. */
  270. struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *rx,
  271. 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. rxrpc_queue_call(call);
  327. rxrpc_get_call(call);
  328. write_unlock_bh(&call->state_lock);
  329. write_unlock(&rx->call_lock);
  330. _leave(" = %p{%d}", call, call->debug_id);
  331. return call;
  332. /* if the call is already dying or dead, then we leave the socket's ref
  333. * on it to be released by rxrpc_dead_call_expired() as induced by
  334. * rxrpc_release_call() */
  335. out_release:
  336. _debug("release %p", call);
  337. if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
  338. !test_and_set_bit(RXRPC_CALL_RELEASE, &call->events))
  339. rxrpc_queue_call(call);
  340. out_discard:
  341. write_unlock_bh(&call->state_lock);
  342. _debug("discard %p", call);
  343. out:
  344. write_unlock(&rx->call_lock);
  345. _leave(" = %d", ret);
  346. return ERR_PTR(ret);
  347. }
  348. /*
  349. * handle rejectance of a call by userspace
  350. * - reject the call at the front of the queue
  351. */
  352. int rxrpc_reject_call(struct rxrpc_sock *rx)
  353. {
  354. struct rxrpc_call *call;
  355. int ret;
  356. _enter("");
  357. ASSERT(!irqs_disabled());
  358. write_lock(&rx->call_lock);
  359. ret = -ENODATA;
  360. if (list_empty(&rx->acceptq))
  361. goto out;
  362. /* dequeue the first call and check it's still valid */
  363. call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link);
  364. list_del_init(&call->accept_link);
  365. sk_acceptq_removed(&rx->sk);
  366. write_lock_bh(&call->state_lock);
  367. switch (call->state) {
  368. case RXRPC_CALL_SERVER_ACCEPTING:
  369. call->state = RXRPC_CALL_SERVER_BUSY;
  370. if (test_and_set_bit(RXRPC_CALL_REJECT_BUSY, &call->events))
  371. rxrpc_queue_call(call);
  372. ret = 0;
  373. goto out_release;
  374. case RXRPC_CALL_REMOTELY_ABORTED:
  375. case RXRPC_CALL_LOCALLY_ABORTED:
  376. ret = -ECONNABORTED;
  377. goto out_release;
  378. case RXRPC_CALL_NETWORK_ERROR:
  379. ret = call->conn->error;
  380. goto out_release;
  381. case RXRPC_CALL_DEAD:
  382. ret = -ETIME;
  383. goto out_discard;
  384. default:
  385. BUG();
  386. }
  387. /* if the call is already dying or dead, then we leave the socket's ref
  388. * on it to be released by rxrpc_dead_call_expired() as induced by
  389. * rxrpc_release_call() */
  390. out_release:
  391. _debug("release %p", call);
  392. if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
  393. !test_and_set_bit(RXRPC_CALL_RELEASE, &call->events))
  394. rxrpc_queue_call(call);
  395. out_discard:
  396. write_unlock_bh(&call->state_lock);
  397. _debug("discard %p", call);
  398. out:
  399. write_unlock(&rx->call_lock);
  400. _leave(" = %d", ret);
  401. return ret;
  402. }
  403. /**
  404. * rxrpc_kernel_accept_call - Allow a kernel service to accept an incoming call
  405. * @sock: The socket on which the impending call is waiting
  406. * @user_call_ID: The tag to attach to the call
  407. *
  408. * Allow a kernel service to accept an incoming call, assuming the incoming
  409. * call is still valid.
  410. */
  411. struct rxrpc_call *rxrpc_kernel_accept_call(struct socket *sock,
  412. unsigned long user_call_ID)
  413. {
  414. struct rxrpc_call *call;
  415. _enter(",%lx", user_call_ID);
  416. call = rxrpc_accept_call(rxrpc_sk(sock->sk), user_call_ID);
  417. _leave(" = %p", call);
  418. return call;
  419. }
  420. EXPORT_SYMBOL(rxrpc_kernel_accept_call);
  421. /**
  422. * rxrpc_kernel_reject_call - Allow a kernel service to reject an incoming call
  423. * @sock: The socket on which the impending call is waiting
  424. *
  425. * Allow a kernel service to reject an incoming call with a BUSY message,
  426. * assuming the incoming call is still valid.
  427. */
  428. int rxrpc_kernel_reject_call(struct socket *sock)
  429. {
  430. int ret;
  431. _enter("");
  432. ret = rxrpc_reject_call(rxrpc_sk(sock->sk));
  433. _leave(" = %d", ret);
  434. return ret;
  435. }
  436. EXPORT_SYMBOL(rxrpc_kernel_reject_call);