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