call_accept.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. atomic_inc(&call->skb_count);
  114. nsp = rxrpc_skb(notification);
  115. nsp->call = call;
  116. ASSERTCMP(atomic_read(&call->usage), >=, 3);
  117. _debug("notify");
  118. spin_lock(&call->lock);
  119. ret = rxrpc_queue_rcv_skb(call, notification, true,
  120. false);
  121. spin_unlock(&call->lock);
  122. notification = NULL;
  123. BUG_ON(ret < 0);
  124. }
  125. spin_unlock(&call->conn->state_lock);
  126. _debug("queued");
  127. }
  128. write_unlock(&rx->call_lock);
  129. _debug("process");
  130. rxrpc_fast_process_packet(call, skb);
  131. _debug("done");
  132. read_unlock_bh(&local->services_lock);
  133. rxrpc_free_skb(notification);
  134. rxrpc_put_call(call);
  135. _leave(" = 0");
  136. return 0;
  137. invalid_service:
  138. _debug("invalid");
  139. read_unlock_bh(&local->services_lock);
  140. read_lock_bh(&call->state_lock);
  141. if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
  142. !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events)) {
  143. rxrpc_get_call(call);
  144. rxrpc_queue_call(call);
  145. }
  146. read_unlock_bh(&call->state_lock);
  147. rxrpc_put_call(call);
  148. ret = -ECONNREFUSED;
  149. error:
  150. rxrpc_free_skb(notification);
  151. error_nofree:
  152. _leave(" = %d", ret);
  153. return ret;
  154. }
  155. /*
  156. * accept incoming calls that need peer, transport and/or connection setting up
  157. * - the packets we get are all incoming client DATA packets that have seq == 1
  158. */
  159. void rxrpc_accept_incoming_calls(struct rxrpc_local *local)
  160. {
  161. struct rxrpc_skb_priv *sp;
  162. struct sockaddr_rxrpc srx;
  163. struct rxrpc_sock *rx;
  164. struct rxrpc_wire_header whdr;
  165. struct sk_buff *skb;
  166. int ret;
  167. _enter("%d", local->debug_id);
  168. skb = skb_dequeue(&local->accept_queue);
  169. if (!skb) {
  170. _leave("\n");
  171. return;
  172. }
  173. _net("incoming call skb %p", skb);
  174. sp = rxrpc_skb(skb);
  175. /* Set up a response packet header in case we need it */
  176. whdr.epoch = htonl(sp->hdr.epoch);
  177. whdr.cid = htonl(sp->hdr.cid);
  178. whdr.callNumber = htonl(sp->hdr.callNumber);
  179. whdr.seq = htonl(sp->hdr.seq);
  180. whdr.serial = 0;
  181. whdr.flags = 0;
  182. whdr.type = 0;
  183. whdr.userStatus = 0;
  184. whdr.securityIndex = sp->hdr.securityIndex;
  185. whdr._rsvd = 0;
  186. whdr.serviceId = htons(sp->hdr.serviceId);
  187. if (rxrpc_extract_addr_from_skb(&srx, skb) < 0)
  188. goto drop;
  189. /* get the socket providing the service */
  190. read_lock_bh(&local->services_lock);
  191. list_for_each_entry(rx, &local->services, listen_link) {
  192. if (rx->srx.srx_service == sp->hdr.serviceId &&
  193. rx->sk.sk_state != RXRPC_CLOSE)
  194. goto found_service;
  195. }
  196. read_unlock_bh(&local->services_lock);
  197. goto invalid_service;
  198. found_service:
  199. _debug("found service %hd", rx->srx.srx_service);
  200. if (sk_acceptq_is_full(&rx->sk))
  201. goto backlog_full;
  202. sk_acceptq_added(&rx->sk);
  203. sock_hold(&rx->sk);
  204. read_unlock_bh(&local->services_lock);
  205. ret = rxrpc_accept_incoming_call(local, rx, skb, &srx);
  206. if (ret < 0)
  207. sk_acceptq_removed(&rx->sk);
  208. sock_put(&rx->sk);
  209. switch (ret) {
  210. case -ECONNRESET: /* old calls are ignored */
  211. case -ECONNABORTED: /* aborted calls are reaborted or ignored */
  212. case 0:
  213. return;
  214. case -ECONNREFUSED:
  215. goto invalid_service;
  216. case -EBUSY:
  217. goto busy;
  218. case -EKEYREJECTED:
  219. goto security_mismatch;
  220. default:
  221. BUG();
  222. }
  223. backlog_full:
  224. read_unlock_bh(&local->services_lock);
  225. busy:
  226. rxrpc_busy(local, &srx, &whdr);
  227. rxrpc_free_skb(skb);
  228. return;
  229. drop:
  230. rxrpc_free_skb(skb);
  231. return;
  232. invalid_service:
  233. skb->priority = RX_INVALID_OPERATION;
  234. rxrpc_reject_packet(local, skb);
  235. return;
  236. /* can't change connection security type mid-flow */
  237. security_mismatch:
  238. skb->priority = RX_PROTOCOL_ERROR;
  239. rxrpc_reject_packet(local, skb);
  240. return;
  241. }
  242. /*
  243. * handle acceptance of a call by userspace
  244. * - assign the user call ID to the call at the front of the queue
  245. */
  246. struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *rx,
  247. unsigned long user_call_ID)
  248. {
  249. struct rxrpc_call *call;
  250. struct rb_node *parent, **pp;
  251. int ret;
  252. _enter(",%lx", user_call_ID);
  253. ASSERT(!irqs_disabled());
  254. write_lock(&rx->call_lock);
  255. ret = -ENODATA;
  256. if (list_empty(&rx->acceptq))
  257. goto out;
  258. /* check the user ID isn't already in use */
  259. ret = -EBADSLT;
  260. pp = &rx->calls.rb_node;
  261. parent = NULL;
  262. while (*pp) {
  263. parent = *pp;
  264. call = rb_entry(parent, struct rxrpc_call, sock_node);
  265. if (user_call_ID < call->user_call_ID)
  266. pp = &(*pp)->rb_left;
  267. else if (user_call_ID > call->user_call_ID)
  268. pp = &(*pp)->rb_right;
  269. else
  270. goto out;
  271. }
  272. /* dequeue the first call and check it's still valid */
  273. call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link);
  274. list_del_init(&call->accept_link);
  275. sk_acceptq_removed(&rx->sk);
  276. write_lock_bh(&call->state_lock);
  277. switch (call->state) {
  278. case RXRPC_CALL_SERVER_ACCEPTING:
  279. call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
  280. break;
  281. case RXRPC_CALL_REMOTELY_ABORTED:
  282. case RXRPC_CALL_LOCALLY_ABORTED:
  283. ret = -ECONNABORTED;
  284. goto out_release;
  285. case RXRPC_CALL_NETWORK_ERROR:
  286. ret = call->conn->error;
  287. goto out_release;
  288. case RXRPC_CALL_DEAD:
  289. ret = -ETIME;
  290. goto out_discard;
  291. default:
  292. BUG();
  293. }
  294. /* formalise the acceptance */
  295. call->user_call_ID = user_call_ID;
  296. rb_link_node(&call->sock_node, parent, pp);
  297. rb_insert_color(&call->sock_node, &rx->calls);
  298. if (test_and_set_bit(RXRPC_CALL_HAS_USERID, &call->flags))
  299. BUG();
  300. if (test_and_set_bit(RXRPC_CALL_EV_ACCEPTED, &call->events))
  301. BUG();
  302. rxrpc_queue_call(call);
  303. rxrpc_get_call(call);
  304. write_unlock_bh(&call->state_lock);
  305. write_unlock(&rx->call_lock);
  306. _leave(" = %p{%d}", call, call->debug_id);
  307. return call;
  308. /* if the call is already dying or dead, then we leave the socket's ref
  309. * on it to be released by rxrpc_dead_call_expired() as induced by
  310. * rxrpc_release_call() */
  311. out_release:
  312. _debug("release %p", call);
  313. if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
  314. !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
  315. rxrpc_queue_call(call);
  316. out_discard:
  317. write_unlock_bh(&call->state_lock);
  318. _debug("discard %p", call);
  319. out:
  320. write_unlock(&rx->call_lock);
  321. _leave(" = %d", ret);
  322. return ERR_PTR(ret);
  323. }
  324. /*
  325. * Handle rejection of a call by userspace
  326. * - reject the call at the front of the queue
  327. */
  328. int rxrpc_reject_call(struct rxrpc_sock *rx)
  329. {
  330. struct rxrpc_call *call;
  331. int ret;
  332. _enter("");
  333. ASSERT(!irqs_disabled());
  334. write_lock(&rx->call_lock);
  335. ret = -ENODATA;
  336. if (list_empty(&rx->acceptq))
  337. goto out;
  338. /* dequeue the first call and check it's still valid */
  339. call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link);
  340. list_del_init(&call->accept_link);
  341. sk_acceptq_removed(&rx->sk);
  342. write_lock_bh(&call->state_lock);
  343. switch (call->state) {
  344. case RXRPC_CALL_SERVER_ACCEPTING:
  345. call->state = RXRPC_CALL_SERVER_BUSY;
  346. if (test_and_set_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events))
  347. rxrpc_queue_call(call);
  348. ret = 0;
  349. goto out_release;
  350. case RXRPC_CALL_REMOTELY_ABORTED:
  351. case RXRPC_CALL_LOCALLY_ABORTED:
  352. ret = -ECONNABORTED;
  353. goto out_release;
  354. case RXRPC_CALL_NETWORK_ERROR:
  355. ret = call->conn->error;
  356. goto out_release;
  357. case RXRPC_CALL_DEAD:
  358. ret = -ETIME;
  359. goto out_discard;
  360. default:
  361. BUG();
  362. }
  363. /* if the call is already dying or dead, then we leave the socket's ref
  364. * on it to be released by rxrpc_dead_call_expired() as induced by
  365. * rxrpc_release_call() */
  366. out_release:
  367. _debug("release %p", call);
  368. if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
  369. !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
  370. rxrpc_queue_call(call);
  371. out_discard:
  372. write_unlock_bh(&call->state_lock);
  373. _debug("discard %p", call);
  374. out:
  375. write_unlock(&rx->call_lock);
  376. _leave(" = %d", ret);
  377. return ret;
  378. }
  379. /**
  380. * rxrpc_kernel_accept_call - Allow a kernel service to accept an incoming call
  381. * @sock: The socket on which the impending call is waiting
  382. * @user_call_ID: The tag to attach to the call
  383. *
  384. * Allow a kernel service to accept an incoming call, assuming the incoming
  385. * call is still valid.
  386. */
  387. struct rxrpc_call *rxrpc_kernel_accept_call(struct socket *sock,
  388. unsigned long user_call_ID)
  389. {
  390. struct rxrpc_call *call;
  391. _enter(",%lx", user_call_ID);
  392. call = rxrpc_accept_call(rxrpc_sk(sock->sk), user_call_ID);
  393. _leave(" = %p", call);
  394. return call;
  395. }
  396. EXPORT_SYMBOL(rxrpc_kernel_accept_call);
  397. /**
  398. * rxrpc_kernel_reject_call - Allow a kernel service to reject an incoming call
  399. * @sock: The socket on which the impending call is waiting
  400. *
  401. * Allow a kernel service to reject an incoming call with a BUSY message,
  402. * assuming the incoming call is still valid.
  403. */
  404. int rxrpc_kernel_reject_call(struct socket *sock)
  405. {
  406. int ret;
  407. _enter("");
  408. ret = rxrpc_reject_call(rxrpc_sk(sock->sk));
  409. _leave(" = %d", ret);
  410. return ret;
  411. }
  412. EXPORT_SYMBOL(rxrpc_kernel_reject_call);