call_accept.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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 <linux/circ_buf.h>
  22. #include <net/sock.h>
  23. #include <net/af_rxrpc.h>
  24. #include <net/ip.h>
  25. #include "ar-internal.h"
  26. /*
  27. * Preallocate a single service call, connection and peer and, if possible,
  28. * give them a user ID and attach the user's side of the ID to them.
  29. */
  30. static int rxrpc_service_prealloc_one(struct rxrpc_sock *rx,
  31. struct rxrpc_backlog *b,
  32. rxrpc_notify_rx_t notify_rx,
  33. rxrpc_user_attach_call_t user_attach_call,
  34. unsigned long user_call_ID, gfp_t gfp)
  35. {
  36. const void *here = __builtin_return_address(0);
  37. struct rxrpc_call *call;
  38. struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
  39. int max, tmp;
  40. unsigned int size = RXRPC_BACKLOG_MAX;
  41. unsigned int head, tail, call_head, call_tail;
  42. max = rx->sk.sk_max_ack_backlog;
  43. tmp = rx->sk.sk_ack_backlog;
  44. if (tmp >= max) {
  45. _leave(" = -ENOBUFS [full %u]", max);
  46. return -ENOBUFS;
  47. }
  48. max -= tmp;
  49. /* We don't need more conns and peers than we have calls, but on the
  50. * other hand, we shouldn't ever use more peers than conns or conns
  51. * than calls.
  52. */
  53. call_head = b->call_backlog_head;
  54. call_tail = READ_ONCE(b->call_backlog_tail);
  55. tmp = CIRC_CNT(call_head, call_tail, size);
  56. if (tmp >= max) {
  57. _leave(" = -ENOBUFS [enough %u]", tmp);
  58. return -ENOBUFS;
  59. }
  60. max = tmp + 1;
  61. head = b->peer_backlog_head;
  62. tail = READ_ONCE(b->peer_backlog_tail);
  63. if (CIRC_CNT(head, tail, size) < max) {
  64. struct rxrpc_peer *peer = rxrpc_alloc_peer(rx->local, gfp);
  65. if (!peer)
  66. return -ENOMEM;
  67. b->peer_backlog[head] = peer;
  68. smp_store_release(&b->peer_backlog_head,
  69. (head + 1) & (size - 1));
  70. }
  71. head = b->conn_backlog_head;
  72. tail = READ_ONCE(b->conn_backlog_tail);
  73. if (CIRC_CNT(head, tail, size) < max) {
  74. struct rxrpc_connection *conn;
  75. conn = rxrpc_prealloc_service_connection(rxnet, gfp);
  76. if (!conn)
  77. return -ENOMEM;
  78. b->conn_backlog[head] = conn;
  79. smp_store_release(&b->conn_backlog_head,
  80. (head + 1) & (size - 1));
  81. trace_rxrpc_conn(conn, rxrpc_conn_new_service,
  82. atomic_read(&conn->usage), here);
  83. }
  84. /* Now it gets complicated, because calls get registered with the
  85. * socket here, particularly if a user ID is preassigned by the user.
  86. */
  87. call = rxrpc_alloc_call(gfp);
  88. if (!call)
  89. return -ENOMEM;
  90. call->flags |= (1 << RXRPC_CALL_IS_SERVICE);
  91. call->state = RXRPC_CALL_SERVER_PREALLOC;
  92. trace_rxrpc_call(call, rxrpc_call_new_service,
  93. atomic_read(&call->usage),
  94. here, (const void *)user_call_ID);
  95. write_lock(&rx->call_lock);
  96. if (user_attach_call) {
  97. struct rxrpc_call *xcall;
  98. struct rb_node *parent, **pp;
  99. /* Check the user ID isn't already in use */
  100. pp = &rx->calls.rb_node;
  101. parent = NULL;
  102. while (*pp) {
  103. parent = *pp;
  104. xcall = rb_entry(parent, struct rxrpc_call, sock_node);
  105. if (user_call_ID < call->user_call_ID)
  106. pp = &(*pp)->rb_left;
  107. else if (user_call_ID > call->user_call_ID)
  108. pp = &(*pp)->rb_right;
  109. else
  110. goto id_in_use;
  111. }
  112. call->user_call_ID = user_call_ID;
  113. call->notify_rx = notify_rx;
  114. rxrpc_get_call(call, rxrpc_call_got_kernel);
  115. user_attach_call(call, user_call_ID);
  116. rxrpc_get_call(call, rxrpc_call_got_userid);
  117. rb_link_node(&call->sock_node, parent, pp);
  118. rb_insert_color(&call->sock_node, &rx->calls);
  119. set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
  120. }
  121. list_add(&call->sock_link, &rx->sock_calls);
  122. write_unlock(&rx->call_lock);
  123. write_lock(&rxnet->call_lock);
  124. list_add_tail(&call->link, &rxnet->calls);
  125. write_unlock(&rxnet->call_lock);
  126. b->call_backlog[call_head] = call;
  127. smp_store_release(&b->call_backlog_head, (call_head + 1) & (size - 1));
  128. _leave(" = 0 [%d -> %lx]", call->debug_id, user_call_ID);
  129. return 0;
  130. id_in_use:
  131. write_unlock(&rx->call_lock);
  132. rxrpc_cleanup_call(call);
  133. _leave(" = -EBADSLT");
  134. return -EBADSLT;
  135. }
  136. /*
  137. * Preallocate sufficient service connections, calls and peers to cover the
  138. * entire backlog of a socket. When a new call comes in, if we don't have
  139. * sufficient of each available, the call gets rejected as busy or ignored.
  140. *
  141. * The backlog is replenished when a connection is accepted or rejected.
  142. */
  143. int rxrpc_service_prealloc(struct rxrpc_sock *rx, gfp_t gfp)
  144. {
  145. struct rxrpc_backlog *b = rx->backlog;
  146. if (!b) {
  147. b = kzalloc(sizeof(struct rxrpc_backlog), gfp);
  148. if (!b)
  149. return -ENOMEM;
  150. rx->backlog = b;
  151. }
  152. if (rx->discard_new_call)
  153. return 0;
  154. while (rxrpc_service_prealloc_one(rx, b, NULL, NULL, 0, gfp) == 0)
  155. ;
  156. return 0;
  157. }
  158. /*
  159. * Discard the preallocation on a service.
  160. */
  161. void rxrpc_discard_prealloc(struct rxrpc_sock *rx)
  162. {
  163. struct rxrpc_backlog *b = rx->backlog;
  164. struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
  165. unsigned int size = RXRPC_BACKLOG_MAX, head, tail;
  166. if (!b)
  167. return;
  168. rx->backlog = NULL;
  169. /* Make sure that there aren't any incoming calls in progress before we
  170. * clear the preallocation buffers.
  171. */
  172. spin_lock_bh(&rx->incoming_lock);
  173. spin_unlock_bh(&rx->incoming_lock);
  174. head = b->peer_backlog_head;
  175. tail = b->peer_backlog_tail;
  176. while (CIRC_CNT(head, tail, size) > 0) {
  177. struct rxrpc_peer *peer = b->peer_backlog[tail];
  178. kfree(peer);
  179. tail = (tail + 1) & (size - 1);
  180. }
  181. head = b->conn_backlog_head;
  182. tail = b->conn_backlog_tail;
  183. while (CIRC_CNT(head, tail, size) > 0) {
  184. struct rxrpc_connection *conn = b->conn_backlog[tail];
  185. write_lock(&rxnet->conn_lock);
  186. list_del(&conn->link);
  187. list_del(&conn->proc_link);
  188. write_unlock(&rxnet->conn_lock);
  189. kfree(conn);
  190. tail = (tail + 1) & (size - 1);
  191. }
  192. head = b->call_backlog_head;
  193. tail = b->call_backlog_tail;
  194. while (CIRC_CNT(head, tail, size) > 0) {
  195. struct rxrpc_call *call = b->call_backlog[tail];
  196. if (rx->discard_new_call) {
  197. _debug("discard %lx", call->user_call_ID);
  198. rx->discard_new_call(call, call->user_call_ID);
  199. rxrpc_put_call(call, rxrpc_call_put_kernel);
  200. }
  201. rxrpc_call_completed(call);
  202. rxrpc_release_call(rx, call);
  203. rxrpc_put_call(call, rxrpc_call_put);
  204. tail = (tail + 1) & (size - 1);
  205. }
  206. kfree(b);
  207. }
  208. /*
  209. * Allocate a new incoming call from the prealloc pool, along with a connection
  210. * and a peer as necessary.
  211. */
  212. static struct rxrpc_call *rxrpc_alloc_incoming_call(struct rxrpc_sock *rx,
  213. struct rxrpc_local *local,
  214. struct rxrpc_connection *conn,
  215. struct sk_buff *skb)
  216. {
  217. struct rxrpc_backlog *b = rx->backlog;
  218. struct rxrpc_peer *peer, *xpeer;
  219. struct rxrpc_call *call;
  220. unsigned short call_head, conn_head, peer_head;
  221. unsigned short call_tail, conn_tail, peer_tail;
  222. unsigned short call_count, conn_count;
  223. /* #calls >= #conns >= #peers must hold true. */
  224. call_head = smp_load_acquire(&b->call_backlog_head);
  225. call_tail = b->call_backlog_tail;
  226. call_count = CIRC_CNT(call_head, call_tail, RXRPC_BACKLOG_MAX);
  227. conn_head = smp_load_acquire(&b->conn_backlog_head);
  228. conn_tail = b->conn_backlog_tail;
  229. conn_count = CIRC_CNT(conn_head, conn_tail, RXRPC_BACKLOG_MAX);
  230. ASSERTCMP(conn_count, >=, call_count);
  231. peer_head = smp_load_acquire(&b->peer_backlog_head);
  232. peer_tail = b->peer_backlog_tail;
  233. ASSERTCMP(CIRC_CNT(peer_head, peer_tail, RXRPC_BACKLOG_MAX), >=,
  234. conn_count);
  235. if (call_count == 0)
  236. return NULL;
  237. if (!conn) {
  238. /* No connection. We're going to need a peer to start off
  239. * with. If one doesn't yet exist, use a spare from the
  240. * preallocation set. We dump the address into the spare in
  241. * anticipation - and to save on stack space.
  242. */
  243. xpeer = b->peer_backlog[peer_tail];
  244. if (rxrpc_extract_addr_from_skb(&xpeer->srx, skb) < 0)
  245. return NULL;
  246. peer = rxrpc_lookup_incoming_peer(local, xpeer);
  247. if (peer == xpeer) {
  248. b->peer_backlog[peer_tail] = NULL;
  249. smp_store_release(&b->peer_backlog_tail,
  250. (peer_tail + 1) &
  251. (RXRPC_BACKLOG_MAX - 1));
  252. }
  253. /* Now allocate and set up the connection */
  254. conn = b->conn_backlog[conn_tail];
  255. b->conn_backlog[conn_tail] = NULL;
  256. smp_store_release(&b->conn_backlog_tail,
  257. (conn_tail + 1) & (RXRPC_BACKLOG_MAX - 1));
  258. rxrpc_get_local(local);
  259. conn->params.local = local;
  260. conn->params.peer = peer;
  261. rxrpc_see_connection(conn);
  262. rxrpc_new_incoming_connection(rx, conn, skb);
  263. } else {
  264. rxrpc_get_connection(conn);
  265. }
  266. /* And now we can allocate and set up a new call */
  267. call = b->call_backlog[call_tail];
  268. b->call_backlog[call_tail] = NULL;
  269. smp_store_release(&b->call_backlog_tail,
  270. (call_tail + 1) & (RXRPC_BACKLOG_MAX - 1));
  271. rxrpc_see_call(call);
  272. call->conn = conn;
  273. call->peer = rxrpc_get_peer(conn->params.peer);
  274. call->cong_cwnd = call->peer->cong_cwnd;
  275. return call;
  276. }
  277. /*
  278. * Set up a new incoming call. Called in BH context with the RCU read lock
  279. * held.
  280. *
  281. * If this is for a kernel service, when we allocate the call, it will have
  282. * three refs on it: (1) the kernel service, (2) the user_call_ID tree, (3) the
  283. * retainer ref obtained from the backlog buffer. Prealloc calls for userspace
  284. * services only have the ref from the backlog buffer. We want to pass this
  285. * ref to non-BH context to dispose of.
  286. *
  287. * If we want to report an error, we mark the skb with the packet type and
  288. * abort code and return NULL.
  289. *
  290. * The call is returned with the user access mutex held.
  291. */
  292. struct rxrpc_call *rxrpc_new_incoming_call(struct rxrpc_local *local,
  293. struct rxrpc_connection *conn,
  294. struct sk_buff *skb)
  295. {
  296. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  297. struct rxrpc_sock *rx;
  298. struct rxrpc_call *call;
  299. u16 service_id = sp->hdr.serviceId;
  300. _enter("");
  301. /* Get the socket providing the service */
  302. rx = rcu_dereference(local->service);
  303. if (rx && (service_id == rx->srx.srx_service ||
  304. service_id == rx->second_service))
  305. goto found_service;
  306. trace_rxrpc_abort("INV", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
  307. RX_INVALID_OPERATION, EOPNOTSUPP);
  308. skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
  309. skb->priority = RX_INVALID_OPERATION;
  310. _leave(" = NULL [service]");
  311. return NULL;
  312. found_service:
  313. spin_lock(&rx->incoming_lock);
  314. if (rx->sk.sk_state == RXRPC_SERVER_LISTEN_DISABLED ||
  315. rx->sk.sk_state == RXRPC_CLOSE) {
  316. trace_rxrpc_abort("CLS", sp->hdr.cid, sp->hdr.callNumber,
  317. sp->hdr.seq, RX_INVALID_OPERATION, ESHUTDOWN);
  318. skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
  319. skb->priority = RX_INVALID_OPERATION;
  320. _leave(" = NULL [close]");
  321. call = NULL;
  322. goto out;
  323. }
  324. call = rxrpc_alloc_incoming_call(rx, local, conn, skb);
  325. if (!call) {
  326. skb->mark = RXRPC_SKB_MARK_BUSY;
  327. _leave(" = NULL [busy]");
  328. call = NULL;
  329. goto out;
  330. }
  331. trace_rxrpc_receive(call, rxrpc_receive_incoming,
  332. sp->hdr.serial, sp->hdr.seq);
  333. /* Lock the call to prevent rxrpc_kernel_send/recv_data() and
  334. * sendmsg()/recvmsg() inconveniently stealing the mutex once the
  335. * notification is generated.
  336. *
  337. * The BUG should never happen because the kernel should be well
  338. * behaved enough not to access the call before the first notification
  339. * event and userspace is prevented from doing so until the state is
  340. * appropriate.
  341. */
  342. if (!mutex_trylock(&call->user_mutex))
  343. BUG();
  344. /* Make the call live. */
  345. rxrpc_incoming_call(rx, call, skb);
  346. conn = call->conn;
  347. if (rx->notify_new_call)
  348. rx->notify_new_call(&rx->sk, call, call->user_call_ID);
  349. else
  350. sk_acceptq_added(&rx->sk);
  351. spin_lock(&conn->state_lock);
  352. switch (conn->state) {
  353. case RXRPC_CONN_SERVICE_UNSECURED:
  354. conn->state = RXRPC_CONN_SERVICE_CHALLENGING;
  355. set_bit(RXRPC_CONN_EV_CHALLENGE, &call->conn->events);
  356. rxrpc_queue_conn(call->conn);
  357. break;
  358. case RXRPC_CONN_SERVICE:
  359. write_lock(&call->state_lock);
  360. if (rx->discard_new_call)
  361. call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
  362. else
  363. call->state = RXRPC_CALL_SERVER_ACCEPTING;
  364. write_unlock(&call->state_lock);
  365. break;
  366. case RXRPC_CONN_REMOTELY_ABORTED:
  367. rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
  368. conn->remote_abort, -ECONNABORTED);
  369. break;
  370. case RXRPC_CONN_LOCALLY_ABORTED:
  371. rxrpc_abort_call("CON", call, sp->hdr.seq,
  372. conn->local_abort, -ECONNABORTED);
  373. break;
  374. default:
  375. BUG();
  376. }
  377. spin_unlock(&conn->state_lock);
  378. if (call->state == RXRPC_CALL_SERVER_ACCEPTING)
  379. rxrpc_notify_socket(call);
  380. /* We have to discard the prealloc queue's ref here and rely on a
  381. * combination of the RCU read lock and refs held either by the socket
  382. * (recvmsg queue, to-be-accepted queue or user ID tree) or the kernel
  383. * service to prevent the call from being deallocated too early.
  384. */
  385. rxrpc_put_call(call, rxrpc_call_put);
  386. _leave(" = %p{%d}", call, call->debug_id);
  387. out:
  388. spin_unlock(&rx->incoming_lock);
  389. return call;
  390. }
  391. /*
  392. * handle acceptance of a call by userspace
  393. * - assign the user call ID to the call at the front of the queue
  394. * - called with the socket locked.
  395. */
  396. struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *rx,
  397. unsigned long user_call_ID,
  398. rxrpc_notify_rx_t notify_rx)
  399. __releases(&rx->sk.sk_lock.slock)
  400. {
  401. struct rxrpc_call *call;
  402. struct rb_node *parent, **pp;
  403. int ret;
  404. _enter(",%lx", user_call_ID);
  405. ASSERT(!irqs_disabled());
  406. write_lock(&rx->call_lock);
  407. if (list_empty(&rx->to_be_accepted)) {
  408. write_unlock(&rx->call_lock);
  409. release_sock(&rx->sk);
  410. kleave(" = -ENODATA [empty]");
  411. return ERR_PTR(-ENODATA);
  412. }
  413. /* check the user ID isn't already in use */
  414. pp = &rx->calls.rb_node;
  415. parent = NULL;
  416. while (*pp) {
  417. parent = *pp;
  418. call = rb_entry(parent, struct rxrpc_call, sock_node);
  419. if (user_call_ID < call->user_call_ID)
  420. pp = &(*pp)->rb_left;
  421. else if (user_call_ID > call->user_call_ID)
  422. pp = &(*pp)->rb_right;
  423. else
  424. goto id_in_use;
  425. }
  426. /* Dequeue the first call and check it's still valid. We gain
  427. * responsibility for the queue's reference.
  428. */
  429. call = list_entry(rx->to_be_accepted.next,
  430. struct rxrpc_call, accept_link);
  431. write_unlock(&rx->call_lock);
  432. /* We need to gain the mutex from the interrupt handler without
  433. * upsetting lockdep, so we have to release it there and take it here.
  434. * We are, however, still holding the socket lock, so other accepts
  435. * must wait for us and no one can add the user ID behind our backs.
  436. */
  437. if (mutex_lock_interruptible(&call->user_mutex) < 0) {
  438. release_sock(&rx->sk);
  439. kleave(" = -ERESTARTSYS");
  440. return ERR_PTR(-ERESTARTSYS);
  441. }
  442. write_lock(&rx->call_lock);
  443. list_del_init(&call->accept_link);
  444. sk_acceptq_removed(&rx->sk);
  445. rxrpc_see_call(call);
  446. /* Find the user ID insertion point. */
  447. pp = &rx->calls.rb_node;
  448. parent = NULL;
  449. while (*pp) {
  450. parent = *pp;
  451. call = rb_entry(parent, struct rxrpc_call, sock_node);
  452. if (user_call_ID < call->user_call_ID)
  453. pp = &(*pp)->rb_left;
  454. else if (user_call_ID > call->user_call_ID)
  455. pp = &(*pp)->rb_right;
  456. else
  457. BUG();
  458. }
  459. write_lock_bh(&call->state_lock);
  460. switch (call->state) {
  461. case RXRPC_CALL_SERVER_ACCEPTING:
  462. call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
  463. break;
  464. case RXRPC_CALL_COMPLETE:
  465. ret = call->error;
  466. goto out_release;
  467. default:
  468. BUG();
  469. }
  470. /* formalise the acceptance */
  471. call->notify_rx = notify_rx;
  472. call->user_call_ID = user_call_ID;
  473. rxrpc_get_call(call, rxrpc_call_got_userid);
  474. rb_link_node(&call->sock_node, parent, pp);
  475. rb_insert_color(&call->sock_node, &rx->calls);
  476. if (test_and_set_bit(RXRPC_CALL_HAS_USERID, &call->flags))
  477. BUG();
  478. write_unlock_bh(&call->state_lock);
  479. write_unlock(&rx->call_lock);
  480. rxrpc_notify_socket(call);
  481. rxrpc_service_prealloc(rx, GFP_KERNEL);
  482. release_sock(&rx->sk);
  483. _leave(" = %p{%d}", call, call->debug_id);
  484. return call;
  485. out_release:
  486. _debug("release %p", call);
  487. write_unlock_bh(&call->state_lock);
  488. write_unlock(&rx->call_lock);
  489. rxrpc_release_call(rx, call);
  490. rxrpc_put_call(call, rxrpc_call_put);
  491. goto out;
  492. id_in_use:
  493. ret = -EBADSLT;
  494. write_unlock(&rx->call_lock);
  495. out:
  496. rxrpc_service_prealloc(rx, GFP_KERNEL);
  497. release_sock(&rx->sk);
  498. _leave(" = %d", ret);
  499. return ERR_PTR(ret);
  500. }
  501. /*
  502. * Handle rejection of a call by userspace
  503. * - reject the call at the front of the queue
  504. */
  505. int rxrpc_reject_call(struct rxrpc_sock *rx)
  506. {
  507. struct rxrpc_call *call;
  508. bool abort = false;
  509. int ret;
  510. _enter("");
  511. ASSERT(!irqs_disabled());
  512. write_lock(&rx->call_lock);
  513. if (list_empty(&rx->to_be_accepted)) {
  514. write_unlock(&rx->call_lock);
  515. return -ENODATA;
  516. }
  517. /* Dequeue the first call and check it's still valid. We gain
  518. * responsibility for the queue's reference.
  519. */
  520. call = list_entry(rx->to_be_accepted.next,
  521. struct rxrpc_call, accept_link);
  522. list_del_init(&call->accept_link);
  523. sk_acceptq_removed(&rx->sk);
  524. rxrpc_see_call(call);
  525. write_lock_bh(&call->state_lock);
  526. switch (call->state) {
  527. case RXRPC_CALL_SERVER_ACCEPTING:
  528. __rxrpc_abort_call("REJ", call, 1, RX_USER_ABORT, -ECONNABORTED);
  529. abort = true;
  530. /* fall through */
  531. case RXRPC_CALL_COMPLETE:
  532. ret = call->error;
  533. goto out_discard;
  534. default:
  535. BUG();
  536. }
  537. out_discard:
  538. write_unlock_bh(&call->state_lock);
  539. write_unlock(&rx->call_lock);
  540. if (abort) {
  541. rxrpc_send_abort_packet(call);
  542. rxrpc_release_call(rx, call);
  543. rxrpc_put_call(call, rxrpc_call_put);
  544. }
  545. rxrpc_service_prealloc(rx, GFP_KERNEL);
  546. _leave(" = %d", ret);
  547. return ret;
  548. }
  549. /*
  550. * rxrpc_kernel_charge_accept - Charge up socket with preallocated calls
  551. * @sock: The socket on which to preallocate
  552. * @notify_rx: Event notification function for the call
  553. * @user_attach_call: Func to attach call to user_call_ID
  554. * @user_call_ID: The tag to attach to the preallocated call
  555. * @gfp: The allocation conditions.
  556. *
  557. * Charge up the socket with preallocated calls, each with a user ID. A
  558. * function should be provided to effect the attachment from the user's side.
  559. * The user is given a ref to hold on the call.
  560. *
  561. * Note that the call may be come connected before this function returns.
  562. */
  563. int rxrpc_kernel_charge_accept(struct socket *sock,
  564. rxrpc_notify_rx_t notify_rx,
  565. rxrpc_user_attach_call_t user_attach_call,
  566. unsigned long user_call_ID, gfp_t gfp)
  567. {
  568. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  569. struct rxrpc_backlog *b = rx->backlog;
  570. if (sock->sk->sk_state == RXRPC_CLOSE)
  571. return -ESHUTDOWN;
  572. return rxrpc_service_prealloc_one(rx, b, notify_rx,
  573. user_attach_call, user_call_ID,
  574. gfp);
  575. }
  576. EXPORT_SYMBOL(rxrpc_kernel_charge_accept);