call_accept.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/module.h>
  13. #include <linux/net.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/errqueue.h>
  16. #include <linux/udp.h>
  17. #include <linux/in.h>
  18. #include <linux/in6.h>
  19. #include <linux/icmp.h>
  20. #include <linux/gfp.h>
  21. #include <net/sock.h>
  22. #include <net/af_rxrpc.h>
  23. #include <net/ip.h>
  24. #include "ar-internal.h"
  25. /*
  26. * generate a connection-level abort
  27. */
  28. static int rxrpc_busy(struct rxrpc_local *local, struct sockaddr_rxrpc *srx,
  29. struct rxrpc_wire_header *whdr)
  30. {
  31. struct msghdr msg;
  32. struct kvec iov[1];
  33. size_t len;
  34. int ret;
  35. _enter("%d,,", local->debug_id);
  36. whdr->type = RXRPC_PACKET_TYPE_BUSY;
  37. whdr->serial = htonl(1);
  38. msg.msg_name = &srx->transport.sin;
  39. msg.msg_namelen = sizeof(srx->transport.sin);
  40. msg.msg_control = NULL;
  41. msg.msg_controllen = 0;
  42. msg.msg_flags = 0;
  43. iov[0].iov_base = whdr;
  44. iov[0].iov_len = sizeof(*whdr);
  45. len = iov[0].iov_len;
  46. _proto("Tx BUSY %%1");
  47. ret = kernel_sendmsg(local->socket, &msg, iov, 1, len);
  48. if (ret < 0) {
  49. _leave(" = -EAGAIN [sendmsg failed: %d]", ret);
  50. return -EAGAIN;
  51. }
  52. _leave(" = 0");
  53. return 0;
  54. }
  55. /*
  56. * accept an incoming call that needs peer, transport and/or connection setting
  57. * up
  58. */
  59. static int rxrpc_accept_incoming_call(struct rxrpc_local *local,
  60. struct rxrpc_sock *rx,
  61. struct sk_buff *skb,
  62. struct sockaddr_rxrpc *srx)
  63. {
  64. struct rxrpc_connection *conn;
  65. struct rxrpc_skb_priv *sp, *nsp;
  66. struct rxrpc_call *call;
  67. struct sk_buff *notification;
  68. int ret;
  69. _enter("");
  70. sp = rxrpc_skb(skb);
  71. /* get a notification message to send to the server app */
  72. notification = alloc_skb(0, GFP_NOFS);
  73. if (!notification) {
  74. _debug("no memory");
  75. ret = -ENOMEM;
  76. goto error_nofree;
  77. }
  78. rxrpc_new_skb(notification);
  79. notification->mark = RXRPC_SKB_MARK_NEW_CALL;
  80. conn = rxrpc_incoming_connection(local, srx, skb);
  81. if (IS_ERR(conn)) {
  82. _debug("no conn");
  83. ret = PTR_ERR(conn);
  84. goto error;
  85. }
  86. call = rxrpc_incoming_call(rx, conn, skb);
  87. rxrpc_put_connection(conn);
  88. if (IS_ERR(call)) {
  89. _debug("no call");
  90. ret = PTR_ERR(call);
  91. goto error;
  92. }
  93. /* attach the call to the socket */
  94. read_lock_bh(&local->services_lock);
  95. if (rx->sk.sk_state == RXRPC_CLOSE)
  96. goto invalid_service;
  97. write_lock(&rx->call_lock);
  98. if (!test_and_set_bit(RXRPC_CALL_INIT_ACCEPT, &call->flags)) {
  99. rxrpc_get_call(call);
  100. spin_lock(&call->conn->state_lock);
  101. if (sp->hdr.securityIndex > 0 &&
  102. call->conn->state == RXRPC_CONN_SERVICE_UNSECURED) {
  103. _debug("await conn sec");
  104. list_add_tail(&call->accept_link, &rx->secureq);
  105. call->conn->state = RXRPC_CONN_SERVICE_CHALLENGING;
  106. set_bit(RXRPC_CONN_EV_CHALLENGE, &call->conn->events);
  107. rxrpc_queue_conn(call->conn);
  108. } else {
  109. _debug("conn ready");
  110. call->state = RXRPC_CALL_SERVER_ACCEPTING;
  111. list_add_tail(&call->accept_link, &rx->acceptq);
  112. rxrpc_get_call(call);
  113. nsp = rxrpc_skb(notification);
  114. nsp->call = call;
  115. ASSERTCMP(atomic_read(&call->usage), >=, 3);
  116. _debug("notify");
  117. spin_lock(&call->lock);
  118. ret = rxrpc_queue_rcv_skb(call, notification, true,
  119. false);
  120. spin_unlock(&call->lock);
  121. notification = NULL;
  122. BUG_ON(ret < 0);
  123. }
  124. spin_unlock(&call->conn->state_lock);
  125. _debug("queued");
  126. }
  127. write_unlock(&rx->call_lock);
  128. _debug("process");
  129. rxrpc_fast_process_packet(call, skb);
  130. _debug("done");
  131. read_unlock_bh(&local->services_lock);
  132. rxrpc_free_skb(notification);
  133. rxrpc_put_call(call);
  134. _leave(" = 0");
  135. return 0;
  136. invalid_service:
  137. _debug("invalid");
  138. read_unlock_bh(&local->services_lock);
  139. read_lock_bh(&call->state_lock);
  140. if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
  141. !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events)) {
  142. rxrpc_get_call(call);
  143. rxrpc_queue_call(call);
  144. }
  145. read_unlock_bh(&call->state_lock);
  146. rxrpc_put_call(call);
  147. ret = -ECONNREFUSED;
  148. error:
  149. rxrpc_free_skb(notification);
  150. error_nofree:
  151. _leave(" = %d", ret);
  152. return ret;
  153. }
  154. /*
  155. * accept incoming calls that need peer, transport and/or connection setting up
  156. * - the packets we get are all incoming client DATA packets that have seq == 1
  157. */
  158. void rxrpc_accept_incoming_calls(struct rxrpc_local *local)
  159. {
  160. struct rxrpc_skb_priv *sp;
  161. struct sockaddr_rxrpc srx;
  162. struct rxrpc_sock *rx;
  163. struct rxrpc_wire_header whdr;
  164. struct sk_buff *skb;
  165. int ret;
  166. _enter("%d", local->debug_id);
  167. skb = skb_dequeue(&local->accept_queue);
  168. if (!skb) {
  169. _leave("\n");
  170. return;
  171. }
  172. _net("incoming call skb %p", skb);
  173. sp = rxrpc_skb(skb);
  174. /* Set up a response packet header in case we need it */
  175. whdr.epoch = htonl(sp->hdr.epoch);
  176. whdr.cid = htonl(sp->hdr.cid);
  177. whdr.callNumber = htonl(sp->hdr.callNumber);
  178. whdr.seq = htonl(sp->hdr.seq);
  179. whdr.serial = 0;
  180. whdr.flags = 0;
  181. whdr.type = 0;
  182. whdr.userStatus = 0;
  183. whdr.securityIndex = sp->hdr.securityIndex;
  184. whdr._rsvd = 0;
  185. whdr.serviceId = htons(sp->hdr.serviceId);
  186. if (rxrpc_extract_addr_from_skb(&srx, skb) < 0)
  187. goto drop;
  188. /* get the socket providing the service */
  189. read_lock_bh(&local->services_lock);
  190. list_for_each_entry(rx, &local->services, listen_link) {
  191. if (rx->srx.srx_service == sp->hdr.serviceId &&
  192. rx->sk.sk_state != RXRPC_CLOSE)
  193. goto found_service;
  194. }
  195. read_unlock_bh(&local->services_lock);
  196. goto invalid_service;
  197. found_service:
  198. _debug("found service %hd", rx->srx.srx_service);
  199. if (sk_acceptq_is_full(&rx->sk))
  200. goto backlog_full;
  201. sk_acceptq_added(&rx->sk);
  202. sock_hold(&rx->sk);
  203. read_unlock_bh(&local->services_lock);
  204. ret = rxrpc_accept_incoming_call(local, rx, skb, &srx);
  205. if (ret < 0)
  206. sk_acceptq_removed(&rx->sk);
  207. sock_put(&rx->sk);
  208. switch (ret) {
  209. case -ECONNRESET: /* old calls are ignored */
  210. case -ECONNABORTED: /* aborted calls are reaborted or ignored */
  211. case 0:
  212. return;
  213. case -ECONNREFUSED:
  214. goto invalid_service;
  215. case -EBUSY:
  216. goto busy;
  217. case -EKEYREJECTED:
  218. goto security_mismatch;
  219. default:
  220. BUG();
  221. }
  222. backlog_full:
  223. read_unlock_bh(&local->services_lock);
  224. busy:
  225. rxrpc_busy(local, &srx, &whdr);
  226. rxrpc_free_skb(skb);
  227. return;
  228. drop:
  229. rxrpc_free_skb(skb);
  230. return;
  231. invalid_service:
  232. skb->priority = RX_INVALID_OPERATION;
  233. rxrpc_reject_packet(local, skb);
  234. return;
  235. /* can't change connection security type mid-flow */
  236. security_mismatch:
  237. skb->priority = RX_PROTOCOL_ERROR;
  238. rxrpc_reject_packet(local, skb);
  239. return;
  240. }
  241. /*
  242. * handle acceptance of a call by userspace
  243. * - assign the user call ID to the call at the front of the queue
  244. */
  245. struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *rx,
  246. unsigned long user_call_ID)
  247. {
  248. struct rxrpc_call *call;
  249. struct rb_node *parent, **pp;
  250. int ret;
  251. _enter(",%lx", user_call_ID);
  252. ASSERT(!irqs_disabled());
  253. write_lock(&rx->call_lock);
  254. ret = -ENODATA;
  255. if (list_empty(&rx->acceptq))
  256. goto out;
  257. /* check the user ID isn't already in use */
  258. ret = -EBADSLT;
  259. pp = &rx->calls.rb_node;
  260. parent = NULL;
  261. while (*pp) {
  262. parent = *pp;
  263. call = rb_entry(parent, struct rxrpc_call, sock_node);
  264. if (user_call_ID < call->user_call_ID)
  265. pp = &(*pp)->rb_left;
  266. else if (user_call_ID > call->user_call_ID)
  267. pp = &(*pp)->rb_right;
  268. else
  269. goto out;
  270. }
  271. /* dequeue the first call and check it's still valid */
  272. call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link);
  273. list_del_init(&call->accept_link);
  274. sk_acceptq_removed(&rx->sk);
  275. write_lock_bh(&call->state_lock);
  276. switch (call->state) {
  277. case RXRPC_CALL_SERVER_ACCEPTING:
  278. call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
  279. break;
  280. case RXRPC_CALL_REMOTELY_ABORTED:
  281. case RXRPC_CALL_LOCALLY_ABORTED:
  282. ret = -ECONNABORTED;
  283. goto out_release;
  284. case RXRPC_CALL_NETWORK_ERROR:
  285. ret = call->conn->error;
  286. goto out_release;
  287. case RXRPC_CALL_DEAD:
  288. ret = -ETIME;
  289. goto out_discard;
  290. default:
  291. BUG();
  292. }
  293. /* formalise the acceptance */
  294. call->user_call_ID = user_call_ID;
  295. rb_link_node(&call->sock_node, parent, pp);
  296. rb_insert_color(&call->sock_node, &rx->calls);
  297. if (test_and_set_bit(RXRPC_CALL_HAS_USERID, &call->flags))
  298. BUG();
  299. if (test_and_set_bit(RXRPC_CALL_EV_ACCEPTED, &call->events))
  300. BUG();
  301. rxrpc_queue_call(call);
  302. rxrpc_get_call(call);
  303. write_unlock_bh(&call->state_lock);
  304. write_unlock(&rx->call_lock);
  305. _leave(" = %p{%d}", call, call->debug_id);
  306. return call;
  307. /* if the call is already dying or dead, then we leave the socket's ref
  308. * on it to be released by rxrpc_dead_call_expired() as induced by
  309. * rxrpc_release_call() */
  310. out_release:
  311. _debug("release %p", call);
  312. if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
  313. !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
  314. rxrpc_queue_call(call);
  315. out_discard:
  316. write_unlock_bh(&call->state_lock);
  317. _debug("discard %p", call);
  318. out:
  319. write_unlock(&rx->call_lock);
  320. _leave(" = %d", ret);
  321. return ERR_PTR(ret);
  322. }
  323. /*
  324. * Handle rejection of a call by userspace
  325. * - reject the call at the front of the queue
  326. */
  327. int rxrpc_reject_call(struct rxrpc_sock *rx)
  328. {
  329. struct rxrpc_call *call;
  330. int ret;
  331. _enter("");
  332. ASSERT(!irqs_disabled());
  333. write_lock(&rx->call_lock);
  334. ret = -ENODATA;
  335. if (list_empty(&rx->acceptq))
  336. goto out;
  337. /* dequeue the first call and check it's still valid */
  338. call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link);
  339. list_del_init(&call->accept_link);
  340. sk_acceptq_removed(&rx->sk);
  341. write_lock_bh(&call->state_lock);
  342. switch (call->state) {
  343. case RXRPC_CALL_SERVER_ACCEPTING:
  344. call->state = RXRPC_CALL_SERVER_BUSY;
  345. if (test_and_set_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events))
  346. rxrpc_queue_call(call);
  347. ret = 0;
  348. goto out_release;
  349. case RXRPC_CALL_REMOTELY_ABORTED:
  350. case RXRPC_CALL_LOCALLY_ABORTED:
  351. ret = -ECONNABORTED;
  352. goto out_release;
  353. case RXRPC_CALL_NETWORK_ERROR:
  354. ret = call->conn->error;
  355. goto out_release;
  356. case RXRPC_CALL_DEAD:
  357. ret = -ETIME;
  358. goto out_discard;
  359. default:
  360. BUG();
  361. }
  362. /* if the call is already dying or dead, then we leave the socket's ref
  363. * on it to be released by rxrpc_dead_call_expired() as induced by
  364. * rxrpc_release_call() */
  365. out_release:
  366. _debug("release %p", call);
  367. if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
  368. !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
  369. rxrpc_queue_call(call);
  370. out_discard:
  371. write_unlock_bh(&call->state_lock);
  372. _debug("discard %p", call);
  373. out:
  374. write_unlock(&rx->call_lock);
  375. _leave(" = %d", ret);
  376. return ret;
  377. }
  378. /**
  379. * rxrpc_kernel_accept_call - Allow a kernel service to accept an incoming call
  380. * @sock: The socket on which the impending call is waiting
  381. * @user_call_ID: The tag to attach to the call
  382. *
  383. * Allow a kernel service to accept an incoming call, assuming the incoming
  384. * call is still valid.
  385. */
  386. struct rxrpc_call *rxrpc_kernel_accept_call(struct socket *sock,
  387. unsigned long user_call_ID)
  388. {
  389. struct rxrpc_call *call;
  390. _enter(",%lx", user_call_ID);
  391. call = rxrpc_accept_call(rxrpc_sk(sock->sk), user_call_ID);
  392. _leave(" = %p", call);
  393. return call;
  394. }
  395. EXPORT_SYMBOL(rxrpc_kernel_accept_call);
  396. /**
  397. * rxrpc_kernel_reject_call - Allow a kernel service to reject an incoming call
  398. * @sock: The socket on which the impending call is waiting
  399. *
  400. * Allow a kernel service to reject an incoming call with a BUSY message,
  401. * assuming the incoming call is still valid.
  402. */
  403. int rxrpc_kernel_reject_call(struct socket *sock)
  404. {
  405. int ret;
  406. _enter("");
  407. ret = rxrpc_reject_call(rxrpc_sk(sock->sk));
  408. _leave(" = %d", ret);
  409. return ret;
  410. }
  411. EXPORT_SYMBOL(rxrpc_kernel_reject_call);