call_accept.c 19 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. /*
  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, 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_connection *conn,
  221. struct sk_buff *skb)
  222. {
  223. struct rxrpc_backlog *b = rx->backlog;
  224. struct rxrpc_peer *peer, *xpeer;
  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. /* No connection. We're going to need a peer to start off
  245. * with. If one doesn't yet exist, use a spare from the
  246. * preallocation set. We dump the address into the spare in
  247. * anticipation - and to save on stack space.
  248. */
  249. xpeer = b->peer_backlog[peer_tail];
  250. if (rxrpc_extract_addr_from_skb(local, &xpeer->srx, skb) < 0)
  251. return NULL;
  252. peer = rxrpc_lookup_incoming_peer(local, xpeer);
  253. if (peer == xpeer) {
  254. b->peer_backlog[peer_tail] = NULL;
  255. smp_store_release(&b->peer_backlog_tail,
  256. (peer_tail + 1) &
  257. (RXRPC_BACKLOG_MAX - 1));
  258. }
  259. /* Now allocate and set up the connection */
  260. conn = b->conn_backlog[conn_tail];
  261. b->conn_backlog[conn_tail] = NULL;
  262. smp_store_release(&b->conn_backlog_tail,
  263. (conn_tail + 1) & (RXRPC_BACKLOG_MAX - 1));
  264. conn->params.local = rxrpc_get_local(local);
  265. conn->params.peer = peer;
  266. rxrpc_see_connection(conn);
  267. rxrpc_new_incoming_connection(rx, conn, skb);
  268. } else {
  269. rxrpc_get_connection(conn);
  270. }
  271. /* And now we can allocate and set up a new call */
  272. call = b->call_backlog[call_tail];
  273. b->call_backlog[call_tail] = NULL;
  274. smp_store_release(&b->call_backlog_tail,
  275. (call_tail + 1) & (RXRPC_BACKLOG_MAX - 1));
  276. rxrpc_see_call(call);
  277. call->conn = conn;
  278. call->peer = rxrpc_get_peer(conn->params.peer);
  279. call->cong_cwnd = call->peer->cong_cwnd;
  280. return call;
  281. }
  282. /*
  283. * Set up a new incoming call. Called in BH context with the RCU read lock
  284. * held.
  285. *
  286. * If this is for a kernel service, when we allocate the call, it will have
  287. * three refs on it: (1) the kernel service, (2) the user_call_ID tree, (3) the
  288. * retainer ref obtained from the backlog buffer. Prealloc calls for userspace
  289. * services only have the ref from the backlog buffer. We want to pass this
  290. * ref to non-BH context to dispose of.
  291. *
  292. * If we want to report an error, we mark the skb with the packet type and
  293. * abort code and return NULL.
  294. *
  295. * The call is returned with the user access mutex held.
  296. */
  297. struct rxrpc_call *rxrpc_new_incoming_call(struct rxrpc_local *local,
  298. struct rxrpc_connection *conn,
  299. struct sk_buff *skb)
  300. {
  301. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  302. struct rxrpc_sock *rx;
  303. struct rxrpc_call *call;
  304. u16 service_id = sp->hdr.serviceId;
  305. _enter("");
  306. /* Get the socket providing the service */
  307. rx = rcu_dereference(local->service);
  308. if (rx && (service_id == rx->srx.srx_service ||
  309. service_id == rx->second_service))
  310. goto found_service;
  311. trace_rxrpc_abort(0, "INV", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
  312. RX_INVALID_OPERATION, EOPNOTSUPP);
  313. skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
  314. skb->priority = RX_INVALID_OPERATION;
  315. _leave(" = NULL [service]");
  316. return NULL;
  317. found_service:
  318. spin_lock(&rx->incoming_lock);
  319. if (rx->sk.sk_state == RXRPC_SERVER_LISTEN_DISABLED ||
  320. rx->sk.sk_state == RXRPC_CLOSE) {
  321. trace_rxrpc_abort(0, "CLS", sp->hdr.cid, sp->hdr.callNumber,
  322. sp->hdr.seq, RX_INVALID_OPERATION, ESHUTDOWN);
  323. skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
  324. skb->priority = RX_INVALID_OPERATION;
  325. _leave(" = NULL [close]");
  326. call = NULL;
  327. goto out;
  328. }
  329. call = rxrpc_alloc_incoming_call(rx, local, conn, skb);
  330. if (!call) {
  331. skb->mark = RXRPC_SKB_MARK_BUSY;
  332. _leave(" = NULL [busy]");
  333. call = NULL;
  334. goto out;
  335. }
  336. trace_rxrpc_receive(call, rxrpc_receive_incoming,
  337. sp->hdr.serial, sp->hdr.seq);
  338. /* Lock the call to prevent rxrpc_kernel_send/recv_data() and
  339. * sendmsg()/recvmsg() inconveniently stealing the mutex once the
  340. * notification is generated.
  341. *
  342. * The BUG should never happen because the kernel should be well
  343. * behaved enough not to access the call before the first notification
  344. * event and userspace is prevented from doing so until the state is
  345. * appropriate.
  346. */
  347. if (!mutex_trylock(&call->user_mutex))
  348. BUG();
  349. /* Make the call live. */
  350. rxrpc_incoming_call(rx, call, skb);
  351. conn = call->conn;
  352. if (rx->notify_new_call)
  353. rx->notify_new_call(&rx->sk, call, call->user_call_ID);
  354. else
  355. sk_acceptq_added(&rx->sk);
  356. spin_lock(&conn->state_lock);
  357. switch (conn->state) {
  358. case RXRPC_CONN_SERVICE_UNSECURED:
  359. conn->state = RXRPC_CONN_SERVICE_CHALLENGING;
  360. set_bit(RXRPC_CONN_EV_CHALLENGE, &call->conn->events);
  361. rxrpc_queue_conn(call->conn);
  362. break;
  363. case RXRPC_CONN_SERVICE:
  364. write_lock(&call->state_lock);
  365. if (rx->discard_new_call)
  366. call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
  367. else
  368. call->state = RXRPC_CALL_SERVER_ACCEPTING;
  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->remote_abort, -ECONNABORTED);
  374. break;
  375. case RXRPC_CONN_LOCALLY_ABORTED:
  376. rxrpc_abort_call("CON", call, sp->hdr.seq,
  377. conn->local_abort, -ECONNABORTED);
  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);