call_accept.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* incoming call handling
  3. *
  4. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/module.h>
  9. #include <linux/net.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/errqueue.h>
  12. #include <linux/udp.h>
  13. #include <linux/in.h>
  14. #include <linux/in6.h>
  15. #include <linux/icmp.h>
  16. #include <linux/gfp.h>
  17. #include <linux/circ_buf.h>
  18. #include <net/sock.h>
  19. #include <net/af_rxrpc.h>
  20. #include <net/ip.h>
  21. #include "ar-internal.h"
  22. static void rxrpc_dummy_notify(struct sock *sk, struct rxrpc_call *call,
  23. unsigned long user_call_ID)
  24. {
  25. }
  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, *xcall;
  39. struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
  40. struct rb_node *parent, **pp;
  41. int max, tmp;
  42. unsigned int size = RXRPC_BACKLOG_MAX;
  43. unsigned int head, tail, call_head, call_tail;
  44. max = rx->sk.sk_max_ack_backlog;
  45. tmp = rx->sk.sk_ack_backlog;
  46. if (tmp >= max) {
  47. _leave(" = -ENOBUFS [full %u]", max);
  48. return -ENOBUFS;
  49. }
  50. max -= tmp;
  51. /* We don't need more conns and peers than we have calls, but on the
  52. * other hand, we shouldn't ever use more peers than conns or conns
  53. * than calls.
  54. */
  55. call_head = b->call_backlog_head;
  56. call_tail = READ_ONCE(b->call_backlog_tail);
  57. tmp = CIRC_CNT(call_head, call_tail, size);
  58. if (tmp >= max) {
  59. _leave(" = -ENOBUFS [enough %u]", tmp);
  60. return -ENOBUFS;
  61. }
  62. max = tmp + 1;
  63. head = b->peer_backlog_head;
  64. tail = READ_ONCE(b->peer_backlog_tail);
  65. if (CIRC_CNT(head, tail, size) < max) {
  66. struct rxrpc_peer *peer = rxrpc_alloc_peer(rx->local, gfp);
  67. if (!peer)
  68. return -ENOMEM;
  69. b->peer_backlog[head] = peer;
  70. smp_store_release(&b->peer_backlog_head,
  71. (head + 1) & (size - 1));
  72. }
  73. head = b->conn_backlog_head;
  74. tail = READ_ONCE(b->conn_backlog_tail);
  75. if (CIRC_CNT(head, tail, size) < max) {
  76. struct rxrpc_connection *conn;
  77. conn = rxrpc_prealloc_service_connection(rxnet, gfp);
  78. if (!conn)
  79. return -ENOMEM;
  80. b->conn_backlog[head] = conn;
  81. smp_store_release(&b->conn_backlog_head,
  82. (head + 1) & (size - 1));
  83. trace_rxrpc_conn(conn->debug_id, rxrpc_conn_new_service,
  84. atomic_read(&conn->usage), here);
  85. }
  86. /* Now it gets complicated, because calls get registered with the
  87. * socket here, with a user ID preassigned by the user.
  88. */
  89. call = rxrpc_alloc_call(rx, gfp, debug_id);
  90. if (!call)
  91. return -ENOMEM;
  92. call->flags |= (1 << RXRPC_CALL_IS_SERVICE);
  93. call->state = RXRPC_CALL_SERVER_PREALLOC;
  94. trace_rxrpc_call(call->debug_id, rxrpc_call_new_service,
  95. atomic_read(&call->usage),
  96. here, (const void *)user_call_ID);
  97. write_lock(&rx->call_lock);
  98. /* Check the user ID isn't already in use */
  99. pp = &rx->calls.rb_node;
  100. parent = NULL;
  101. while (*pp) {
  102. parent = *pp;
  103. xcall = rb_entry(parent, struct rxrpc_call, sock_node);
  104. if (user_call_ID < xcall->user_call_ID)
  105. pp = &(*pp)->rb_left;
  106. else if (user_call_ID > xcall->user_call_ID)
  107. pp = &(*pp)->rb_right;
  108. else
  109. goto id_in_use;
  110. }
  111. call->user_call_ID = user_call_ID;
  112. call->notify_rx = notify_rx;
  113. if (user_attach_call) {
  114. rxrpc_get_call(call, rxrpc_call_got_kernel);
  115. user_attach_call(call, user_call_ID);
  116. }
  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. list_add(&call->sock_link, &rx->sock_calls);
  122. write_unlock(&rx->call_lock);
  123. rxnet = call->rxnet;
  124. write_lock(&rxnet->call_lock);
  125. list_add_tail(&call->link, &rxnet->calls);
  126. write_unlock(&rxnet->call_lock);
  127. b->call_backlog[call_head] = call;
  128. smp_store_release(&b->call_backlog_head, (call_head + 1) & (size - 1));
  129. _leave(" = 0 [%d -> %lx]", call->debug_id, user_call_ID);
  130. return 0;
  131. id_in_use:
  132. write_unlock(&rx->call_lock);
  133. rxrpc_cleanup_call(call);
  134. _leave(" = -EBADSLT");
  135. return -EBADSLT;
  136. }
  137. /*
  138. * Allocate the preallocation buffers for incoming service calls. These must
  139. * be charged manually.
  140. */
  141. int rxrpc_service_prealloc(struct rxrpc_sock *rx, gfp_t gfp)
  142. {
  143. struct rxrpc_backlog *b = rx->backlog;
  144. if (!b) {
  145. b = kzalloc(sizeof(struct rxrpc_backlog), gfp);
  146. if (!b)
  147. return -ENOMEM;
  148. rx->backlog = b;
  149. }
  150. return 0;
  151. }
  152. /*
  153. * Discard the preallocation on a service.
  154. */
  155. void rxrpc_discard_prealloc(struct rxrpc_sock *rx)
  156. {
  157. struct rxrpc_backlog *b = rx->backlog;
  158. struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
  159. unsigned int size = RXRPC_BACKLOG_MAX, head, tail;
  160. if (!b)
  161. return;
  162. rx->backlog = NULL;
  163. /* Make sure that there aren't any incoming calls in progress before we
  164. * clear the preallocation buffers.
  165. */
  166. spin_lock_bh(&rx->incoming_lock);
  167. spin_unlock_bh(&rx->incoming_lock);
  168. head = b->peer_backlog_head;
  169. tail = b->peer_backlog_tail;
  170. while (CIRC_CNT(head, tail, size) > 0) {
  171. struct rxrpc_peer *peer = b->peer_backlog[tail];
  172. rxrpc_put_local(peer->local);
  173. kfree(peer);
  174. tail = (tail + 1) & (size - 1);
  175. }
  176. head = b->conn_backlog_head;
  177. tail = b->conn_backlog_tail;
  178. while (CIRC_CNT(head, tail, size) > 0) {
  179. struct rxrpc_connection *conn = b->conn_backlog[tail];
  180. write_lock(&rxnet->conn_lock);
  181. list_del(&conn->link);
  182. list_del(&conn->proc_link);
  183. write_unlock(&rxnet->conn_lock);
  184. kfree(conn);
  185. if (atomic_dec_and_test(&rxnet->nr_conns))
  186. wake_up_var(&rxnet->nr_conns);
  187. tail = (tail + 1) & (size - 1);
  188. }
  189. head = b->call_backlog_head;
  190. tail = b->call_backlog_tail;
  191. while (CIRC_CNT(head, tail, size) > 0) {
  192. struct rxrpc_call *call = b->call_backlog[tail];
  193. rcu_assign_pointer(call->socket, rx);
  194. if (rx->discard_new_call) {
  195. _debug("discard %lx", call->user_call_ID);
  196. rx->discard_new_call(call, call->user_call_ID);
  197. if (call->notify_rx)
  198. call->notify_rx = rxrpc_dummy_notify;
  199. rxrpc_put_call(call, rxrpc_call_put_kernel);
  200. }
  201. rxrpc_call_completed(call);
  202. rxrpc_release_call(rx, call);
  203. rxrpc_put_call(call, rxrpc_call_put);
  204. tail = (tail + 1) & (size - 1);
  205. }
  206. kfree(b);
  207. }
  208. /*
  209. * Ping the other end to fill our RTT cache and to retrieve the rwind
  210. * and MTU parameters.
  211. */
  212. static void rxrpc_send_ping(struct rxrpc_call *call, struct sk_buff *skb)
  213. {
  214. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  215. ktime_t now = skb->tstamp;
  216. if (call->peer->rtt_count < 3 ||
  217. ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000), now))
  218. rxrpc_propose_ACK(call, RXRPC_ACK_PING, sp->hdr.serial,
  219. true, true,
  220. rxrpc_propose_ack_ping_for_params);
  221. }
  222. /*
  223. * Allocate a new incoming call from the prealloc pool, along with a connection
  224. * and a peer as necessary.
  225. */
  226. static struct rxrpc_call *rxrpc_alloc_incoming_call(struct rxrpc_sock *rx,
  227. struct rxrpc_local *local,
  228. struct rxrpc_peer *peer,
  229. struct rxrpc_connection *conn,
  230. const struct rxrpc_security *sec,
  231. struct key *key,
  232. struct sk_buff *skb)
  233. {
  234. struct rxrpc_backlog *b = rx->backlog;
  235. struct rxrpc_call *call;
  236. unsigned short call_head, conn_head, peer_head;
  237. unsigned short call_tail, conn_tail, peer_tail;
  238. unsigned short call_count, conn_count;
  239. /* #calls >= #conns >= #peers must hold true. */
  240. call_head = smp_load_acquire(&b->call_backlog_head);
  241. call_tail = b->call_backlog_tail;
  242. call_count = CIRC_CNT(call_head, call_tail, RXRPC_BACKLOG_MAX);
  243. conn_head = smp_load_acquire(&b->conn_backlog_head);
  244. conn_tail = b->conn_backlog_tail;
  245. conn_count = CIRC_CNT(conn_head, conn_tail, RXRPC_BACKLOG_MAX);
  246. ASSERTCMP(conn_count, >=, call_count);
  247. peer_head = smp_load_acquire(&b->peer_backlog_head);
  248. peer_tail = b->peer_backlog_tail;
  249. ASSERTCMP(CIRC_CNT(peer_head, peer_tail, RXRPC_BACKLOG_MAX), >=,
  250. conn_count);
  251. if (call_count == 0)
  252. return NULL;
  253. if (!conn) {
  254. if (peer && !rxrpc_get_peer_maybe(peer))
  255. peer = NULL;
  256. if (!peer) {
  257. peer = b->peer_backlog[peer_tail];
  258. if (rxrpc_extract_addr_from_skb(&peer->srx, skb) < 0)
  259. return NULL;
  260. b->peer_backlog[peer_tail] = NULL;
  261. smp_store_release(&b->peer_backlog_tail,
  262. (peer_tail + 1) &
  263. (RXRPC_BACKLOG_MAX - 1));
  264. rxrpc_new_incoming_peer(rx, local, peer);
  265. }
  266. /* Now allocate and set up the connection */
  267. conn = b->conn_backlog[conn_tail];
  268. b->conn_backlog[conn_tail] = NULL;
  269. smp_store_release(&b->conn_backlog_tail,
  270. (conn_tail + 1) & (RXRPC_BACKLOG_MAX - 1));
  271. conn->params.local = rxrpc_get_local(local);
  272. conn->params.peer = peer;
  273. rxrpc_see_connection(conn);
  274. rxrpc_new_incoming_connection(rx, conn, sec, key, skb);
  275. } else {
  276. rxrpc_get_connection(conn);
  277. }
  278. /* And now we can allocate and set up a new call */
  279. call = b->call_backlog[call_tail];
  280. b->call_backlog[call_tail] = NULL;
  281. smp_store_release(&b->call_backlog_tail,
  282. (call_tail + 1) & (RXRPC_BACKLOG_MAX - 1));
  283. rxrpc_see_call(call);
  284. call->conn = conn;
  285. call->security = conn->security;
  286. call->security_ix = conn->security_ix;
  287. call->peer = rxrpc_get_peer(conn->params.peer);
  288. call->cong_cwnd = call->peer->cong_cwnd;
  289. return call;
  290. }
  291. /*
  292. * Set up a new incoming call. Called in BH context with the RCU read lock
  293. * held.
  294. *
  295. * If this is for a kernel service, when we allocate the call, it will have
  296. * three refs on it: (1) the kernel service, (2) the user_call_ID tree, (3) the
  297. * retainer ref obtained from the backlog buffer. Prealloc calls for userspace
  298. * services only have the ref from the backlog buffer. We want to pass this
  299. * ref to non-BH context to dispose of.
  300. *
  301. * If we want to report an error, we mark the skb with the packet type and
  302. * abort code and return NULL.
  303. *
  304. * The call is returned with the user access mutex held.
  305. */
  306. struct rxrpc_call *rxrpc_new_incoming_call(struct rxrpc_local *local,
  307. struct rxrpc_sock *rx,
  308. struct sk_buff *skb)
  309. {
  310. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  311. const struct rxrpc_security *sec = NULL;
  312. struct rxrpc_connection *conn;
  313. struct rxrpc_peer *peer = NULL;
  314. struct rxrpc_call *call = NULL;
  315. struct key *key = NULL;
  316. _enter("");
  317. spin_lock(&rx->incoming_lock);
  318. if (rx->sk.sk_state == RXRPC_SERVER_LISTEN_DISABLED ||
  319. rx->sk.sk_state == RXRPC_CLOSE) {
  320. trace_rxrpc_abort(0, "CLS", sp->hdr.cid, sp->hdr.callNumber,
  321. sp->hdr.seq, RX_INVALID_OPERATION, ESHUTDOWN);
  322. skb->mark = RXRPC_SKB_MARK_REJECT_ABORT;
  323. skb->priority = RX_INVALID_OPERATION;
  324. goto no_call;
  325. }
  326. /* The peer, connection and call may all have sprung into existence due
  327. * to a duplicate packet being handled on another CPU in parallel, so
  328. * we have to recheck the routing. However, we're now holding
  329. * rx->incoming_lock, so the values should remain stable.
  330. */
  331. conn = rxrpc_find_connection_rcu(local, skb, &peer);
  332. if (!conn && !rxrpc_look_up_server_security(local, rx, &sec, &key, skb))
  333. goto no_call;
  334. call = rxrpc_alloc_incoming_call(rx, local, peer, conn, sec, key, skb);
  335. key_put(key);
  336. if (!call) {
  337. skb->mark = RXRPC_SKB_MARK_REJECT_BUSY;
  338. goto no_call;
  339. }
  340. trace_rxrpc_receive(call, rxrpc_receive_incoming,
  341. sp->hdr.serial, sp->hdr.seq);
  342. /* Make the call live. */
  343. rxrpc_incoming_call(rx, call, skb);
  344. conn = call->conn;
  345. if (rx->notify_new_call)
  346. rx->notify_new_call(&rx->sk, call, call->user_call_ID);
  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. call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
  358. write_unlock(&call->state_lock);
  359. break;
  360. case RXRPC_CONN_REMOTELY_ABORTED:
  361. rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
  362. conn->abort_code, conn->error);
  363. break;
  364. case RXRPC_CONN_LOCALLY_ABORTED:
  365. rxrpc_abort_call("CON", call, sp->hdr.seq,
  366. conn->abort_code, conn->error);
  367. break;
  368. default:
  369. BUG();
  370. }
  371. spin_unlock(&conn->state_lock);
  372. spin_unlock(&rx->incoming_lock);
  373. rxrpc_send_ping(call, skb);
  374. /* We have to discard the prealloc queue's ref here and rely on a
  375. * combination of the RCU read lock and refs held either by the socket
  376. * (recvmsg queue, to-be-accepted queue or user ID tree) or the kernel
  377. * service to prevent the call from being deallocated too early.
  378. */
  379. rxrpc_put_call(call, rxrpc_call_put);
  380. _leave(" = %p{%d}", call, call->debug_id);
  381. return call;
  382. no_call:
  383. spin_unlock(&rx->incoming_lock);
  384. _leave(" = NULL [%u]", skb->mark);
  385. return NULL;
  386. }
  387. /*
  388. * Charge up socket with preallocated calls, attaching user call IDs.
  389. */
  390. int rxrpc_user_charge_accept(struct rxrpc_sock *rx, unsigned long user_call_ID)
  391. {
  392. struct rxrpc_backlog *b = rx->backlog;
  393. if (rx->sk.sk_state == RXRPC_CLOSE)
  394. return -ESHUTDOWN;
  395. return rxrpc_service_prealloc_one(rx, b, NULL, NULL, user_call_ID,
  396. GFP_KERNEL,
  397. atomic_inc_return(&rxrpc_debug_id));
  398. }
  399. /*
  400. * rxrpc_kernel_charge_accept - Charge up socket with preallocated calls
  401. * @sock: The socket on which to preallocate
  402. * @notify_rx: Event notification function for the call
  403. * @user_attach_call: Func to attach call to user_call_ID
  404. * @user_call_ID: The tag to attach to the preallocated call
  405. * @gfp: The allocation conditions.
  406. * @debug_id: The tracing debug ID.
  407. *
  408. * Charge up the socket with preallocated calls, each with a user ID. A
  409. * function should be provided to effect the attachment from the user's side.
  410. * The user is given a ref to hold on the call.
  411. *
  412. * Note that the call may be come connected before this function returns.
  413. */
  414. int rxrpc_kernel_charge_accept(struct socket *sock,
  415. rxrpc_notify_rx_t notify_rx,
  416. rxrpc_user_attach_call_t user_attach_call,
  417. unsigned long user_call_ID, gfp_t gfp,
  418. unsigned int debug_id)
  419. {
  420. struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
  421. struct rxrpc_backlog *b = rx->backlog;
  422. if (sock->sk->sk_state == RXRPC_CLOSE)
  423. return -ESHUTDOWN;
  424. return rxrpc_service_prealloc_one(rx, b, notify_rx,
  425. user_attach_call, user_call_ID,
  426. gfp, debug_id);
  427. }
  428. EXPORT_SYMBOL(rxrpc_kernel_charge_accept);