call_accept.c 18 KB

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