ar-accept.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. 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. 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. rxrpc_queue_call(call);
  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. struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *rx,
  272. unsigned long user_call_ID)
  273. {
  274. struct rxrpc_call *call;
  275. struct rb_node *parent, **pp;
  276. int ret;
  277. _enter(",%lx", user_call_ID);
  278. ASSERT(!irqs_disabled());
  279. write_lock(&rx->call_lock);
  280. ret = -ENODATA;
  281. if (list_empty(&rx->acceptq))
  282. goto out;
  283. /* check the user ID isn't already in use */
  284. ret = -EBADSLT;
  285. pp = &rx->calls.rb_node;
  286. parent = NULL;
  287. while (*pp) {
  288. parent = *pp;
  289. call = rb_entry(parent, struct rxrpc_call, sock_node);
  290. if (user_call_ID < call->user_call_ID)
  291. pp = &(*pp)->rb_left;
  292. else if (user_call_ID > call->user_call_ID)
  293. pp = &(*pp)->rb_right;
  294. else
  295. goto out;
  296. }
  297. /* dequeue the first call and check it's still valid */
  298. call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link);
  299. list_del_init(&call->accept_link);
  300. sk_acceptq_removed(&rx->sk);
  301. write_lock_bh(&call->state_lock);
  302. switch (call->state) {
  303. case RXRPC_CALL_SERVER_ACCEPTING:
  304. call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
  305. break;
  306. case RXRPC_CALL_REMOTELY_ABORTED:
  307. case RXRPC_CALL_LOCALLY_ABORTED:
  308. ret = -ECONNABORTED;
  309. goto out_release;
  310. case RXRPC_CALL_NETWORK_ERROR:
  311. ret = call->conn->error;
  312. goto out_release;
  313. case RXRPC_CALL_DEAD:
  314. ret = -ETIME;
  315. goto out_discard;
  316. default:
  317. BUG();
  318. }
  319. /* formalise the acceptance */
  320. call->user_call_ID = user_call_ID;
  321. rb_link_node(&call->sock_node, parent, pp);
  322. rb_insert_color(&call->sock_node, &rx->calls);
  323. if (test_and_set_bit(RXRPC_CALL_HAS_USERID, &call->flags))
  324. BUG();
  325. if (test_and_set_bit(RXRPC_CALL_ACCEPTED, &call->events))
  326. BUG();
  327. rxrpc_queue_call(call);
  328. rxrpc_get_call(call);
  329. write_unlock_bh(&call->state_lock);
  330. write_unlock(&rx->call_lock);
  331. _leave(" = %p{%d}", call, call->debug_id);
  332. return call;
  333. /* if the call is already dying or dead, then we leave the socket's ref
  334. * on it to be released by rxrpc_dead_call_expired() as induced by
  335. * rxrpc_release_call() */
  336. out_release:
  337. _debug("release %p", call);
  338. if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
  339. !test_and_set_bit(RXRPC_CALL_RELEASE, &call->events))
  340. rxrpc_queue_call(call);
  341. out_discard:
  342. write_unlock_bh(&call->state_lock);
  343. _debug("discard %p", call);
  344. out:
  345. write_unlock(&rx->call_lock);
  346. _leave(" = %d", ret);
  347. return ERR_PTR(ret);
  348. }
  349. /*
  350. * handle rejectance of a call by userspace
  351. * - reject the call at the front of the queue
  352. */
  353. int rxrpc_reject_call(struct rxrpc_sock *rx)
  354. {
  355. struct rxrpc_call *call;
  356. int ret;
  357. _enter("");
  358. ASSERT(!irqs_disabled());
  359. write_lock(&rx->call_lock);
  360. ret = -ENODATA;
  361. if (list_empty(&rx->acceptq))
  362. goto out;
  363. /* dequeue the first call and check it's still valid */
  364. call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link);
  365. list_del_init(&call->accept_link);
  366. sk_acceptq_removed(&rx->sk);
  367. write_lock_bh(&call->state_lock);
  368. switch (call->state) {
  369. case RXRPC_CALL_SERVER_ACCEPTING:
  370. call->state = RXRPC_CALL_SERVER_BUSY;
  371. if (test_and_set_bit(RXRPC_CALL_REJECT_BUSY, &call->events))
  372. rxrpc_queue_call(call);
  373. ret = 0;
  374. goto out_release;
  375. case RXRPC_CALL_REMOTELY_ABORTED:
  376. case RXRPC_CALL_LOCALLY_ABORTED:
  377. ret = -ECONNABORTED;
  378. goto out_release;
  379. case RXRPC_CALL_NETWORK_ERROR:
  380. ret = call->conn->error;
  381. goto out_release;
  382. case RXRPC_CALL_DEAD:
  383. ret = -ETIME;
  384. goto out_discard;
  385. default:
  386. BUG();
  387. }
  388. /* if the call is already dying or dead, then we leave the socket's ref
  389. * on it to be released by rxrpc_dead_call_expired() as induced by
  390. * rxrpc_release_call() */
  391. out_release:
  392. _debug("release %p", call);
  393. if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
  394. !test_and_set_bit(RXRPC_CALL_RELEASE, &call->events))
  395. rxrpc_queue_call(call);
  396. out_discard:
  397. write_unlock_bh(&call->state_lock);
  398. _debug("discard %p", call);
  399. out:
  400. write_unlock(&rx->call_lock);
  401. _leave(" = %d", ret);
  402. return ret;
  403. }
  404. /**
  405. * rxrpc_kernel_accept_call - Allow a kernel service to accept an incoming call
  406. * @sock: The socket on which the impending call is waiting
  407. * @user_call_ID: The tag to attach to the call
  408. *
  409. * Allow a kernel service to accept an incoming call, assuming the incoming
  410. * call is still valid.
  411. */
  412. struct rxrpc_call *rxrpc_kernel_accept_call(struct socket *sock,
  413. unsigned long user_call_ID)
  414. {
  415. struct rxrpc_call *call;
  416. _enter(",%lx", user_call_ID);
  417. call = rxrpc_accept_call(rxrpc_sk(sock->sk), user_call_ID);
  418. _leave(" = %p", call);
  419. return call;
  420. }
  421. EXPORT_SYMBOL(rxrpc_kernel_accept_call);
  422. /**
  423. * rxrpc_kernel_reject_call - Allow a kernel service to reject an incoming call
  424. * @sock: The socket on which the impending call is waiting
  425. *
  426. * Allow a kernel service to reject an incoming call with a BUSY message,
  427. * assuming the incoming call is still valid.
  428. */
  429. int rxrpc_kernel_reject_call(struct socket *sock)
  430. {
  431. int ret;
  432. _enter("");
  433. ret = rxrpc_reject_call(rxrpc_sk(sock->sk));
  434. _leave(" = %d", ret);
  435. return ret;
  436. }
  437. EXPORT_SYMBOL(rxrpc_kernel_reject_call);