call_accept.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. call->socket = rx;
  197. if (rx->discard_new_call) {
  198. _debug("discard %lx", call->user_call_ID);
  199. rx->discard_new_call(call, call->user_call_ID);
  200. rxrpc_put_call(call, rxrpc_call_put_kernel);
  201. }
  202. rxrpc_call_completed(call);
  203. rxrpc_release_call(rx, call);
  204. rxrpc_put_call(call, rxrpc_call_put);
  205. tail = (tail + 1) & (size - 1);
  206. }
  207. kfree(b);
  208. }
  209. /*
  210. * Allocate a new incoming call from the prealloc pool, along with a connection
  211. * and a peer as necessary.
  212. */
  213. static struct rxrpc_call *rxrpc_alloc_incoming_call(struct rxrpc_sock *rx,
  214. struct rxrpc_local *local,
  215. struct rxrpc_connection *conn,
  216. struct sk_buff *skb)
  217. {
  218. struct rxrpc_backlog *b = rx->backlog;
  219. struct rxrpc_peer *peer, *xpeer;
  220. struct rxrpc_call *call;
  221. unsigned short call_head, conn_head, peer_head;
  222. unsigned short call_tail, conn_tail, peer_tail;
  223. unsigned short call_count, conn_count;
  224. /* #calls >= #conns >= #peers must hold true. */
  225. call_head = smp_load_acquire(&b->call_backlog_head);
  226. call_tail = b->call_backlog_tail;
  227. call_count = CIRC_CNT(call_head, call_tail, RXRPC_BACKLOG_MAX);
  228. conn_head = smp_load_acquire(&b->conn_backlog_head);
  229. conn_tail = b->conn_backlog_tail;
  230. conn_count = CIRC_CNT(conn_head, conn_tail, RXRPC_BACKLOG_MAX);
  231. ASSERTCMP(conn_count, >=, call_count);
  232. peer_head = smp_load_acquire(&b->peer_backlog_head);
  233. peer_tail = b->peer_backlog_tail;
  234. ASSERTCMP(CIRC_CNT(peer_head, peer_tail, RXRPC_BACKLOG_MAX), >=,
  235. conn_count);
  236. if (call_count == 0)
  237. return NULL;
  238. if (!conn) {
  239. /* No connection. We're going to need a peer to start off
  240. * with. If one doesn't yet exist, use a spare from the
  241. * preallocation set. We dump the address into the spare in
  242. * anticipation - and to save on stack space.
  243. */
  244. xpeer = b->peer_backlog[peer_tail];
  245. if (rxrpc_extract_addr_from_skb(local, &xpeer->srx, skb) < 0)
  246. return NULL;
  247. peer = rxrpc_lookup_incoming_peer(local, xpeer);
  248. if (peer == xpeer) {
  249. b->peer_backlog[peer_tail] = NULL;
  250. smp_store_release(&b->peer_backlog_tail,
  251. (peer_tail + 1) &
  252. (RXRPC_BACKLOG_MAX - 1));
  253. }
  254. /* Now allocate and set up the connection */
  255. conn = b->conn_backlog[conn_tail];
  256. b->conn_backlog[conn_tail] = NULL;
  257. smp_store_release(&b->conn_backlog_tail,
  258. (conn_tail + 1) & (RXRPC_BACKLOG_MAX - 1));
  259. rxrpc_get_local(local);
  260. conn->params.local = local;
  261. conn->params.peer = peer;
  262. rxrpc_see_connection(conn);
  263. rxrpc_new_incoming_connection(rx, conn, skb);
  264. } else {
  265. rxrpc_get_connection(conn);
  266. }
  267. /* And now we can allocate and set up a new call */
  268. call = b->call_backlog[call_tail];
  269. b->call_backlog[call_tail] = NULL;
  270. smp_store_release(&b->call_backlog_tail,
  271. (call_tail + 1) & (RXRPC_BACKLOG_MAX - 1));
  272. rxrpc_see_call(call);
  273. call->conn = conn;
  274. call->peer = rxrpc_get_peer(conn->params.peer);
  275. call->cong_cwnd = call->peer->cong_cwnd;
  276. return call;
  277. }
  278. /*
  279. * Set up a new incoming call. Called in BH context with the RCU read lock
  280. * held.
  281. *
  282. * If this is for a kernel service, when we allocate the call, it will have
  283. * three refs on it: (1) the kernel service, (2) the user_call_ID tree, (3) the
  284. * retainer ref obtained from the backlog buffer. Prealloc calls for userspace
  285. * services only have the ref from the backlog buffer. We want to pass this
  286. * ref to non-BH context to dispose of.
  287. *
  288. * If we want to report an error, we mark the skb with the packet type and
  289. * abort code and return NULL.
  290. *
  291. * The call is returned with the user access mutex held.
  292. */
  293. struct rxrpc_call *rxrpc_new_incoming_call(struct rxrpc_local *local,
  294. struct rxrpc_connection *conn,
  295. struct sk_buff *skb)
  296. {
  297. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  298. struct rxrpc_sock *rx;
  299. struct rxrpc_call *call;
  300. u16 service_id = sp->hdr.serviceId;
  301. _enter("");
  302. /* Get the socket providing the service */
  303. rx = rcu_dereference(local->service);
  304. if (rx && (service_id == rx->srx.srx_service ||
  305. service_id == rx->second_service))
  306. goto found_service;
  307. trace_rxrpc_abort("INV", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
  308. RX_INVALID_OPERATION, EOPNOTSUPP);
  309. skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
  310. skb->priority = RX_INVALID_OPERATION;
  311. _leave(" = NULL [service]");
  312. return NULL;
  313. found_service:
  314. spin_lock(&rx->incoming_lock);
  315. if (rx->sk.sk_state == RXRPC_SERVER_LISTEN_DISABLED ||
  316. rx->sk.sk_state == RXRPC_CLOSE) {
  317. trace_rxrpc_abort("CLS", sp->hdr.cid, sp->hdr.callNumber,
  318. sp->hdr.seq, RX_INVALID_OPERATION, ESHUTDOWN);
  319. skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
  320. skb->priority = RX_INVALID_OPERATION;
  321. _leave(" = NULL [close]");
  322. call = NULL;
  323. goto out;
  324. }
  325. call = rxrpc_alloc_incoming_call(rx, local, conn, skb);
  326. if (!call) {
  327. skb->mark = RXRPC_SKB_MARK_BUSY;
  328. _leave(" = NULL [busy]");
  329. call = NULL;
  330. goto out;
  331. }
  332. trace_rxrpc_receive(call, rxrpc_receive_incoming,
  333. sp->hdr.serial, sp->hdr.seq);
  334. /* Lock the call to prevent rxrpc_kernel_send/recv_data() and
  335. * sendmsg()/recvmsg() inconveniently stealing the mutex once the
  336. * notification is generated.
  337. *
  338. * The BUG should never happen because the kernel should be well
  339. * behaved enough not to access the call before the first notification
  340. * event and userspace is prevented from doing so until the state is
  341. * appropriate.
  342. */
  343. if (!mutex_trylock(&call->user_mutex))
  344. BUG();
  345. /* Make the call live. */
  346. rxrpc_incoming_call(rx, call, skb);
  347. conn = call->conn;
  348. if (rx->notify_new_call)
  349. rx->notify_new_call(&rx->sk, call, call->user_call_ID);
  350. else
  351. sk_acceptq_added(&rx->sk);
  352. spin_lock(&conn->state_lock);
  353. switch (conn->state) {
  354. case RXRPC_CONN_SERVICE_UNSECURED:
  355. conn->state = RXRPC_CONN_SERVICE_CHALLENGING;
  356. set_bit(RXRPC_CONN_EV_CHALLENGE, &call->conn->events);
  357. rxrpc_queue_conn(call->conn);
  358. break;
  359. case RXRPC_CONN_SERVICE:
  360. write_lock(&call->state_lock);
  361. if (rx->discard_new_call)
  362. call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
  363. else
  364. call->state = RXRPC_CALL_SERVER_ACCEPTING;
  365. write_unlock(&call->state_lock);
  366. break;
  367. case RXRPC_CONN_REMOTELY_ABORTED:
  368. rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
  369. conn->remote_abort, -ECONNABORTED);
  370. break;
  371. case RXRPC_CONN_LOCALLY_ABORTED:
  372. rxrpc_abort_call("CON", call, sp->hdr.seq,
  373. conn->local_abort, -ECONNABORTED);
  374. break;
  375. default:
  376. BUG();
  377. }
  378. spin_unlock(&conn->state_lock);
  379. if (call->state == RXRPC_CALL_SERVER_ACCEPTING)
  380. rxrpc_notify_socket(call);
  381. /* We have to discard the prealloc queue's ref here and rely on a
  382. * combination of the RCU read lock and refs held either by the socket
  383. * (recvmsg queue, to-be-accepted queue or user ID tree) or the kernel
  384. * service to prevent the call from being deallocated too early.
  385. */
  386. rxrpc_put_call(call, rxrpc_call_put);
  387. _leave(" = %p{%d}", call, call->debug_id);
  388. out:
  389. spin_unlock(&rx->incoming_lock);
  390. return call;
  391. }
  392. /*
  393. * handle acceptance of a call by userspace
  394. * - assign the user call ID to the call at the front of the queue
  395. * - called with the socket locked.
  396. */
  397. struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *rx,
  398. unsigned long user_call_ID,
  399. rxrpc_notify_rx_t notify_rx)
  400. __releases(&rx->sk.sk_lock.slock)
  401. {
  402. struct rxrpc_call *call;
  403. struct rb_node *parent, **pp;
  404. int ret;
  405. _enter(",%lx", user_call_ID);
  406. ASSERT(!irqs_disabled());
  407. write_lock(&rx->call_lock);
  408. if (list_empty(&rx->to_be_accepted)) {
  409. write_unlock(&rx->call_lock);
  410. release_sock(&rx->sk);
  411. kleave(" = -ENODATA [empty]");
  412. return ERR_PTR(-ENODATA);
  413. }
  414. /* check the user ID isn't already in use */
  415. pp = &rx->calls.rb_node;
  416. parent = NULL;
  417. while (*pp) {
  418. parent = *pp;
  419. call = rb_entry(parent, struct rxrpc_call, sock_node);
  420. if (user_call_ID < call->user_call_ID)
  421. pp = &(*pp)->rb_left;
  422. else if (user_call_ID > call->user_call_ID)
  423. pp = &(*pp)->rb_right;
  424. else
  425. goto id_in_use;
  426. }
  427. /* Dequeue the first call and check it's still valid. We gain
  428. * responsibility for the queue's reference.
  429. */
  430. call = list_entry(rx->to_be_accepted.next,
  431. struct rxrpc_call, accept_link);
  432. write_unlock(&rx->call_lock);
  433. /* We need to gain the mutex from the interrupt handler without
  434. * upsetting lockdep, so we have to release it there and take it here.
  435. * We are, however, still holding the socket lock, so other accepts
  436. * must wait for us and no one can add the user ID behind our backs.
  437. */
  438. if (mutex_lock_interruptible(&call->user_mutex) < 0) {
  439. release_sock(&rx->sk);
  440. kleave(" = -ERESTARTSYS");
  441. return ERR_PTR(-ERESTARTSYS);
  442. }
  443. write_lock(&rx->call_lock);
  444. list_del_init(&call->accept_link);
  445. sk_acceptq_removed(&rx->sk);
  446. rxrpc_see_call(call);
  447. /* Find the user ID insertion point. */
  448. pp = &rx->calls.rb_node;
  449. parent = NULL;
  450. while (*pp) {
  451. parent = *pp;
  452. call = rb_entry(parent, struct rxrpc_call, sock_node);
  453. if (user_call_ID < call->user_call_ID)
  454. pp = &(*pp)->rb_left;
  455. else if (user_call_ID > call->user_call_ID)
  456. pp = &(*pp)->rb_right;
  457. else
  458. BUG();
  459. }
  460. write_lock_bh(&call->state_lock);
  461. switch (call->state) {
  462. case RXRPC_CALL_SERVER_ACCEPTING:
  463. call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
  464. break;
  465. case RXRPC_CALL_COMPLETE:
  466. ret = call->error;
  467. goto out_release;
  468. default:
  469. BUG();
  470. }
  471. /* formalise the acceptance */
  472. call->notify_rx = notify_rx;
  473. call->user_call_ID = user_call_ID;
  474. rxrpc_get_call(call, rxrpc_call_got_userid);
  475. rb_link_node(&call->sock_node, parent, pp);
  476. rb_insert_color(&call->sock_node, &rx->calls);
  477. if (test_and_set_bit(RXRPC_CALL_HAS_USERID, &call->flags))
  478. BUG();
  479. write_unlock_bh(&call->state_lock);
  480. write_unlock(&rx->call_lock);
  481. rxrpc_notify_socket(call);
  482. rxrpc_service_prealloc(rx, GFP_KERNEL);
  483. release_sock(&rx->sk);
  484. _leave(" = %p{%d}", call, call->debug_id);
  485. return call;
  486. out_release:
  487. _debug("release %p", call);
  488. write_unlock_bh(&call->state_lock);
  489. write_unlock(&rx->call_lock);
  490. rxrpc_release_call(rx, call);
  491. rxrpc_put_call(call, rxrpc_call_put);
  492. goto out;
  493. id_in_use:
  494. ret = -EBADSLT;
  495. write_unlock(&rx->call_lock);
  496. out:
  497. rxrpc_service_prealloc(rx, GFP_KERNEL);
  498. release_sock(&rx->sk);
  499. _leave(" = %d", ret);
  500. return ERR_PTR(ret);
  501. }
  502. /*
  503. * Handle rejection of a call by userspace
  504. * - reject the call at the front of the queue
  505. */
  506. int rxrpc_reject_call(struct rxrpc_sock *rx)
  507. {
  508. struct rxrpc_call *call;
  509. bool abort = false;
  510. int ret;
  511. _enter("");
  512. ASSERT(!irqs_disabled());
  513. write_lock(&rx->call_lock);
  514. if (list_empty(&rx->to_be_accepted)) {
  515. write_unlock(&rx->call_lock);
  516. return -ENODATA;
  517. }
  518. /* Dequeue the first call and check it's still valid. We gain
  519. * responsibility for the queue's reference.
  520. */
  521. call = list_entry(rx->to_be_accepted.next,
  522. struct rxrpc_call, accept_link);
  523. list_del_init(&call->accept_link);
  524. sk_acceptq_removed(&rx->sk);
  525. rxrpc_see_call(call);
  526. write_lock_bh(&call->state_lock);
  527. switch (call->state) {
  528. case RXRPC_CALL_SERVER_ACCEPTING:
  529. __rxrpc_abort_call("REJ", call, 1, RX_USER_ABORT, -ECONNABORTED);
  530. abort = true;
  531. /* fall through */
  532. case RXRPC_CALL_COMPLETE:
  533. ret = call->error;
  534. goto out_discard;
  535. default:
  536. BUG();
  537. }
  538. out_discard:
  539. write_unlock_bh(&call->state_lock);
  540. write_unlock(&rx->call_lock);
  541. if (abort) {
  542. rxrpc_send_abort_packet(call);
  543. rxrpc_release_call(rx, call);
  544. rxrpc_put_call(call, rxrpc_call_put);
  545. }
  546. rxrpc_service_prealloc(rx, GFP_KERNEL);
  547. _leave(" = %d", ret);
  548. return ret;
  549. }
  550. /*
  551. * rxrpc_kernel_charge_accept - Charge up socket with preallocated calls
  552. * @sock: The socket on which to preallocate
  553. * @notify_rx: Event notification function for the call
  554. * @user_attach_call: Func to attach call to user_call_ID
  555. * @user_call_ID: The tag to attach to the preallocated call
  556. * @gfp: The allocation conditions.
  557. *
  558. * Charge up the socket with preallocated calls, each with a user ID. A
  559. * function should be provided to effect the attachment from the user's side.
  560. * The user is given a ref to hold on the call.
  561. *
  562. * Note that the call may be come connected before this function returns.
  563. */
  564. int rxrpc_kernel_charge_accept(struct socket *sock,
  565. rxrpc_notify_rx_t notify_rx,
  566. rxrpc_user_attach_call_t user_attach_call,
  567. unsigned long user_call_ID, gfp_t gfp)
  568. {
  569. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  570. struct rxrpc_backlog *b = rx->backlog;
  571. if (sock->sk->sk_state == RXRPC_CLOSE)
  572. return -ESHUTDOWN;
  573. return rxrpc_service_prealloc_one(rx, b, notify_rx,
  574. user_attach_call, user_call_ID,
  575. gfp);
  576. }
  577. EXPORT_SYMBOL(rxrpc_kernel_charge_accept);