call_accept.c 16 KB

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