1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* incoming call handling
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/net.h>
12 #include <linux/skbuff.h>
13 #include <linux/errqueue.h>
14 #include <linux/udp.h>
16 #include <linux/in6.h>
17 #include <linux/icmp.h>
18 #include <linux/gfp.h>
19 #include <linux/circ_buf.h>
21 #include <net/af_rxrpc.h>
23 #include "ar-internal.h"
25 static void rxrpc_dummy_notify(struct sock *sk, struct rxrpc_call *call,
26 unsigned long user_call_ID)
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.
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)
41 const void *here = __builtin_return_address(0);
42 struct rxrpc_call *call;
43 struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
45 unsigned int size = RXRPC_BACKLOG_MAX;
46 unsigned int head, tail, call_head, call_tail;
48 max = rx->sk.sk_max_ack_backlog;
49 tmp = rx->sk.sk_ack_backlog;
51 _leave(" = -ENOBUFS [full %u]", max);
56 /* We don't need more conns and peers than we have calls, but on the
57 * other hand, we shouldn't ever use more peers than conns or conns
60 call_head = b->call_backlog_head;
61 call_tail = READ_ONCE(b->call_backlog_tail);
62 tmp = CIRC_CNT(call_head, call_tail, size);
64 _leave(" = -ENOBUFS [enough %u]", tmp);
69 head = b->peer_backlog_head;
70 tail = READ_ONCE(b->peer_backlog_tail);
71 if (CIRC_CNT(head, tail, size) < max) {
72 struct rxrpc_peer *peer = rxrpc_alloc_peer(rx->local, gfp);
75 b->peer_backlog[head] = peer;
76 smp_store_release(&b->peer_backlog_head,
77 (head + 1) & (size - 1));
80 head = b->conn_backlog_head;
81 tail = READ_ONCE(b->conn_backlog_tail);
82 if (CIRC_CNT(head, tail, size) < max) {
83 struct rxrpc_connection *conn;
85 conn = rxrpc_prealloc_service_connection(rxnet, gfp);
88 b->conn_backlog[head] = conn;
89 smp_store_release(&b->conn_backlog_head,
90 (head + 1) & (size - 1));
92 trace_rxrpc_conn(conn->debug_id, rxrpc_conn_new_service,
93 atomic_read(&conn->usage), here);
96 /* Now it gets complicated, because calls get registered with the
97 * socket here, particularly if a user ID is preassigned by the user.
99 call = rxrpc_alloc_call(rx, gfp, debug_id);
102 call->flags |= (1 << RXRPC_CALL_IS_SERVICE);
103 call->state = RXRPC_CALL_SERVER_PREALLOC;
105 trace_rxrpc_call(call->debug_id, rxrpc_call_new_service,
106 atomic_read(&call->usage),
107 here, (const void *)user_call_ID);
109 write_lock(&rx->call_lock);
110 if (user_attach_call) {
111 struct rxrpc_call *xcall;
112 struct rb_node *parent, **pp;
114 /* Check the user ID isn't already in use */
115 pp = &rx->calls.rb_node;
119 xcall = rb_entry(parent, struct rxrpc_call, sock_node);
120 if (user_call_ID < xcall->user_call_ID)
121 pp = &(*pp)->rb_left;
122 else if (user_call_ID > xcall->user_call_ID)
123 pp = &(*pp)->rb_right;
128 call->user_call_ID = user_call_ID;
129 call->notify_rx = notify_rx;
130 rxrpc_get_call(call, rxrpc_call_got_kernel);
131 user_attach_call(call, user_call_ID);
132 rxrpc_get_call(call, rxrpc_call_got_userid);
133 rb_link_node(&call->sock_node, parent, pp);
134 rb_insert_color(&call->sock_node, &rx->calls);
135 set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
138 list_add(&call->sock_link, &rx->sock_calls);
140 write_unlock(&rx->call_lock);
143 write_lock(&rxnet->call_lock);
144 list_add_tail(&call->link, &rxnet->calls);
145 write_unlock(&rxnet->call_lock);
147 b->call_backlog[call_head] = call;
148 smp_store_release(&b->call_backlog_head, (call_head + 1) & (size - 1));
149 _leave(" = 0 [%d -> %lx]", call->debug_id, user_call_ID);
153 write_unlock(&rx->call_lock);
154 rxrpc_cleanup_call(call);
155 _leave(" = -EBADSLT");
160 * Preallocate sufficient service connections, calls and peers to cover the
161 * entire backlog of a socket. When a new call comes in, if we don't have
162 * sufficient of each available, the call gets rejected as busy or ignored.
164 * The backlog is replenished when a connection is accepted or rejected.
166 int rxrpc_service_prealloc(struct rxrpc_sock *rx, gfp_t gfp)
168 struct rxrpc_backlog *b = rx->backlog;
171 b = kzalloc(sizeof(struct rxrpc_backlog), gfp);
177 if (rx->discard_new_call)
180 while (rxrpc_service_prealloc_one(rx, b, NULL, NULL, 0, gfp,
181 atomic_inc_return(&rxrpc_debug_id)) == 0)
188 * Discard the preallocation on a service.
190 void rxrpc_discard_prealloc(struct rxrpc_sock *rx)
192 struct rxrpc_backlog *b = rx->backlog;
193 struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
194 unsigned int size = RXRPC_BACKLOG_MAX, head, tail;
200 /* Make sure that there aren't any incoming calls in progress before we
201 * clear the preallocation buffers.
203 spin_lock_bh(&rx->incoming_lock);
204 spin_unlock_bh(&rx->incoming_lock);
206 head = b->peer_backlog_head;
207 tail = b->peer_backlog_tail;
208 while (CIRC_CNT(head, tail, size) > 0) {
209 struct rxrpc_peer *peer = b->peer_backlog[tail];
210 rxrpc_put_local(peer->local);
212 tail = (tail + 1) & (size - 1);
215 head = b->conn_backlog_head;
216 tail = b->conn_backlog_tail;
217 while (CIRC_CNT(head, tail, size) > 0) {
218 struct rxrpc_connection *conn = b->conn_backlog[tail];
219 write_lock(&rxnet->conn_lock);
220 list_del(&conn->link);
221 list_del(&conn->proc_link);
222 write_unlock(&rxnet->conn_lock);
224 if (atomic_dec_and_test(&rxnet->nr_conns))
225 wake_up_var(&rxnet->nr_conns);
226 tail = (tail + 1) & (size - 1);
229 head = b->call_backlog_head;
230 tail = b->call_backlog_tail;
231 while (CIRC_CNT(head, tail, size) > 0) {
232 struct rxrpc_call *call = b->call_backlog[tail];
233 rcu_assign_pointer(call->socket, rx);
234 if (rx->discard_new_call) {
235 _debug("discard %lx", call->user_call_ID);
236 rx->discard_new_call(call, call->user_call_ID);
238 call->notify_rx = rxrpc_dummy_notify;
239 rxrpc_put_call(call, rxrpc_call_put_kernel);
241 rxrpc_call_completed(call);
242 rxrpc_release_call(rx, call);
243 rxrpc_put_call(call, rxrpc_call_put);
244 tail = (tail + 1) & (size - 1);
251 * Ping the other end to fill our RTT cache and to retrieve the rwind
252 * and MTU parameters.
254 static void rxrpc_send_ping(struct rxrpc_call *call, struct sk_buff *skb)
256 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
257 ktime_t now = skb->tstamp;
259 if (call->peer->rtt_count < 3 ||
260 ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000), now))
261 rxrpc_propose_ACK(call, RXRPC_ACK_PING, sp->hdr.serial,
263 rxrpc_propose_ack_ping_for_params);
267 * Allocate a new incoming call from the prealloc pool, along with a connection
268 * and a peer as necessary.
270 static struct rxrpc_call *rxrpc_alloc_incoming_call(struct rxrpc_sock *rx,
271 struct rxrpc_local *local,
272 struct rxrpc_peer *peer,
273 struct rxrpc_connection *conn,
274 const struct rxrpc_security *sec,
278 struct rxrpc_backlog *b = rx->backlog;
279 struct rxrpc_call *call;
280 unsigned short call_head, conn_head, peer_head;
281 unsigned short call_tail, conn_tail, peer_tail;
282 unsigned short call_count, conn_count;
284 /* #calls >= #conns >= #peers must hold true. */
285 call_head = smp_load_acquire(&b->call_backlog_head);
286 call_tail = b->call_backlog_tail;
287 call_count = CIRC_CNT(call_head, call_tail, RXRPC_BACKLOG_MAX);
288 conn_head = smp_load_acquire(&b->conn_backlog_head);
289 conn_tail = b->conn_backlog_tail;
290 conn_count = CIRC_CNT(conn_head, conn_tail, RXRPC_BACKLOG_MAX);
291 ASSERTCMP(conn_count, >=, call_count);
292 peer_head = smp_load_acquire(&b->peer_backlog_head);
293 peer_tail = b->peer_backlog_tail;
294 ASSERTCMP(CIRC_CNT(peer_head, peer_tail, RXRPC_BACKLOG_MAX), >=,
301 if (peer && !rxrpc_get_peer_maybe(peer))
304 peer = b->peer_backlog[peer_tail];
305 if (rxrpc_extract_addr_from_skb(&peer->srx, skb) < 0)
307 b->peer_backlog[peer_tail] = NULL;
308 smp_store_release(&b->peer_backlog_tail,
310 (RXRPC_BACKLOG_MAX - 1));
312 rxrpc_new_incoming_peer(rx, local, peer);
315 /* Now allocate and set up the connection */
316 conn = b->conn_backlog[conn_tail];
317 b->conn_backlog[conn_tail] = NULL;
318 smp_store_release(&b->conn_backlog_tail,
319 (conn_tail + 1) & (RXRPC_BACKLOG_MAX - 1));
320 conn->params.local = rxrpc_get_local(local);
321 conn->params.peer = peer;
322 rxrpc_see_connection(conn);
323 rxrpc_new_incoming_connection(rx, conn, sec, key, skb);
325 rxrpc_get_connection(conn);
328 /* And now we can allocate and set up a new call */
329 call = b->call_backlog[call_tail];
330 b->call_backlog[call_tail] = NULL;
331 smp_store_release(&b->call_backlog_tail,
332 (call_tail + 1) & (RXRPC_BACKLOG_MAX - 1));
334 rxrpc_see_call(call);
336 call->security = conn->security;
337 call->peer = rxrpc_get_peer(conn->params.peer);
338 call->cong_cwnd = call->peer->cong_cwnd;
343 * Set up a new incoming call. Called in BH context with the RCU read lock
346 * If this is for a kernel service, when we allocate the call, it will have
347 * three refs on it: (1) the kernel service, (2) the user_call_ID tree, (3) the
348 * retainer ref obtained from the backlog buffer. Prealloc calls for userspace
349 * services only have the ref from the backlog buffer. We want to pass this
350 * ref to non-BH context to dispose of.
352 * If we want to report an error, we mark the skb with the packet type and
353 * abort code and return NULL.
355 * The call is returned with the user access mutex held.
357 struct rxrpc_call *rxrpc_new_incoming_call(struct rxrpc_local *local,
358 struct rxrpc_sock *rx,
361 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
362 const struct rxrpc_security *sec = NULL;
363 struct rxrpc_connection *conn;
364 struct rxrpc_peer *peer = NULL;
365 struct rxrpc_call *call = NULL;
366 struct key *key = NULL;
370 spin_lock(&rx->incoming_lock);
371 if (rx->sk.sk_state == RXRPC_SERVER_LISTEN_DISABLED ||
372 rx->sk.sk_state == RXRPC_CLOSE) {
373 trace_rxrpc_abort(0, "CLS", sp->hdr.cid, sp->hdr.callNumber,
374 sp->hdr.seq, RX_INVALID_OPERATION, ESHUTDOWN);
375 skb->mark = RXRPC_SKB_MARK_REJECT_ABORT;
376 skb->priority = RX_INVALID_OPERATION;
380 /* The peer, connection and call may all have sprung into existence due
381 * to a duplicate packet being handled on another CPU in parallel, so
382 * we have to recheck the routing. However, we're now holding
383 * rx->incoming_lock, so the values should remain stable.
385 conn = rxrpc_find_connection_rcu(local, skb, &peer);
387 if (!conn && !rxrpc_look_up_server_security(local, rx, &sec, &key, skb))
390 call = rxrpc_alloc_incoming_call(rx, local, peer, conn, sec, key, skb);
393 skb->mark = RXRPC_SKB_MARK_REJECT_BUSY;
397 trace_rxrpc_receive(call, rxrpc_receive_incoming,
398 sp->hdr.serial, sp->hdr.seq);
400 /* Make the call live. */
401 rxrpc_incoming_call(rx, call, skb);
404 if (rx->notify_new_call)
405 rx->notify_new_call(&rx->sk, call, call->user_call_ID);
407 sk_acceptq_added(&rx->sk);
409 spin_lock(&conn->state_lock);
410 switch (conn->state) {
411 case RXRPC_CONN_SERVICE_UNSECURED:
412 conn->state = RXRPC_CONN_SERVICE_CHALLENGING;
413 set_bit(RXRPC_CONN_EV_CHALLENGE, &call->conn->events);
414 rxrpc_queue_conn(call->conn);
417 case RXRPC_CONN_SERVICE:
418 write_lock(&call->state_lock);
419 if (call->state < RXRPC_CALL_COMPLETE) {
420 if (rx->discard_new_call)
421 call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
423 call->state = RXRPC_CALL_SERVER_ACCEPTING;
425 write_unlock(&call->state_lock);
428 case RXRPC_CONN_REMOTELY_ABORTED:
429 rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
430 conn->abort_code, conn->error);
432 case RXRPC_CONN_LOCALLY_ABORTED:
433 rxrpc_abort_call("CON", call, sp->hdr.seq,
434 conn->abort_code, conn->error);
439 spin_unlock(&conn->state_lock);
440 spin_unlock(&rx->incoming_lock);
442 rxrpc_send_ping(call, skb);
444 if (call->state == RXRPC_CALL_SERVER_ACCEPTING)
445 rxrpc_notify_socket(call);
447 /* We have to discard the prealloc queue's ref here and rely on a
448 * combination of the RCU read lock and refs held either by the socket
449 * (recvmsg queue, to-be-accepted queue or user ID tree) or the kernel
450 * service to prevent the call from being deallocated too early.
452 rxrpc_put_call(call, rxrpc_call_put);
454 _leave(" = %p{%d}", call, call->debug_id);
458 spin_unlock(&rx->incoming_lock);
459 _leave(" = NULL [%u]", skb->mark);
464 * handle acceptance of a call by userspace
465 * - assign the user call ID to the call at the front of the queue
466 * - called with the socket locked.
468 struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *rx,
469 unsigned long user_call_ID,
470 rxrpc_notify_rx_t notify_rx)
471 __releases(&rx->sk.sk_lock.slock)
472 __acquires(call->user_mutex)
474 struct rxrpc_call *call;
475 struct rb_node *parent, **pp;
478 _enter(",%lx", user_call_ID);
480 ASSERT(!irqs_disabled());
482 write_lock(&rx->call_lock);
484 if (list_empty(&rx->to_be_accepted)) {
485 write_unlock(&rx->call_lock);
486 release_sock(&rx->sk);
487 kleave(" = -ENODATA [empty]");
488 return ERR_PTR(-ENODATA);
491 /* check the user ID isn't already in use */
492 pp = &rx->calls.rb_node;
496 call = rb_entry(parent, struct rxrpc_call, sock_node);
498 if (user_call_ID < call->user_call_ID)
499 pp = &(*pp)->rb_left;
500 else if (user_call_ID > call->user_call_ID)
501 pp = &(*pp)->rb_right;
506 /* Dequeue the first call and check it's still valid. We gain
507 * responsibility for the queue's reference.
509 call = list_entry(rx->to_be_accepted.next,
510 struct rxrpc_call, accept_link);
511 write_unlock(&rx->call_lock);
513 /* We need to gain the mutex from the interrupt handler without
514 * upsetting lockdep, so we have to release it there and take it here.
515 * We are, however, still holding the socket lock, so other accepts
516 * must wait for us and no one can add the user ID behind our backs.
518 if (mutex_lock_interruptible(&call->user_mutex) < 0) {
519 release_sock(&rx->sk);
520 kleave(" = -ERESTARTSYS");
521 return ERR_PTR(-ERESTARTSYS);
524 write_lock(&rx->call_lock);
525 list_del_init(&call->accept_link);
526 sk_acceptq_removed(&rx->sk);
527 rxrpc_see_call(call);
529 /* Find the user ID insertion point. */
530 pp = &rx->calls.rb_node;
534 call = rb_entry(parent, struct rxrpc_call, sock_node);
536 if (user_call_ID < call->user_call_ID)
537 pp = &(*pp)->rb_left;
538 else if (user_call_ID > call->user_call_ID)
539 pp = &(*pp)->rb_right;
544 write_lock_bh(&call->state_lock);
545 switch (call->state) {
546 case RXRPC_CALL_SERVER_ACCEPTING:
547 call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
549 case RXRPC_CALL_COMPLETE:
556 /* formalise the acceptance */
557 call->notify_rx = notify_rx;
558 call->user_call_ID = user_call_ID;
559 rxrpc_get_call(call, rxrpc_call_got_userid);
560 rb_link_node(&call->sock_node, parent, pp);
561 rb_insert_color(&call->sock_node, &rx->calls);
562 if (test_and_set_bit(RXRPC_CALL_HAS_USERID, &call->flags))
565 write_unlock_bh(&call->state_lock);
566 write_unlock(&rx->call_lock);
567 rxrpc_notify_socket(call);
568 rxrpc_service_prealloc(rx, GFP_KERNEL);
569 release_sock(&rx->sk);
570 _leave(" = %p{%d}", call, call->debug_id);
574 _debug("release %p", call);
575 write_unlock_bh(&call->state_lock);
576 write_unlock(&rx->call_lock);
577 rxrpc_release_call(rx, call);
578 rxrpc_put_call(call, rxrpc_call_put);
583 write_unlock(&rx->call_lock);
585 rxrpc_service_prealloc(rx, GFP_KERNEL);
586 release_sock(&rx->sk);
587 _leave(" = %d", ret);
592 * Handle rejection of a call by userspace
593 * - reject the call at the front of the queue
595 int rxrpc_reject_call(struct rxrpc_sock *rx)
597 struct rxrpc_call *call;
603 ASSERT(!irqs_disabled());
605 write_lock(&rx->call_lock);
607 if (list_empty(&rx->to_be_accepted)) {
608 write_unlock(&rx->call_lock);
612 /* Dequeue the first call and check it's still valid. We gain
613 * responsibility for the queue's reference.
615 call = list_entry(rx->to_be_accepted.next,
616 struct rxrpc_call, accept_link);
617 list_del_init(&call->accept_link);
618 sk_acceptq_removed(&rx->sk);
619 rxrpc_see_call(call);
621 write_lock_bh(&call->state_lock);
622 switch (call->state) {
623 case RXRPC_CALL_SERVER_ACCEPTING:
624 __rxrpc_abort_call("REJ", call, 1, RX_USER_ABORT, -ECONNABORTED);
627 case RXRPC_CALL_COMPLETE:
635 write_unlock_bh(&call->state_lock);
636 write_unlock(&rx->call_lock);
638 rxrpc_send_abort_packet(call);
639 rxrpc_release_call(rx, call);
640 rxrpc_put_call(call, rxrpc_call_put);
642 rxrpc_service_prealloc(rx, GFP_KERNEL);
643 _leave(" = %d", ret);
648 * rxrpc_kernel_charge_accept - Charge up socket with preallocated calls
649 * @sock: The socket on which to preallocate
650 * @notify_rx: Event notification function for the call
651 * @user_attach_call: Func to attach call to user_call_ID
652 * @user_call_ID: The tag to attach to the preallocated call
653 * @gfp: The allocation conditions.
654 * @debug_id: The tracing debug ID.
656 * Charge up the socket with preallocated calls, each with a user ID. A
657 * function should be provided to effect the attachment from the user's side.
658 * The user is given a ref to hold on the call.
660 * Note that the call may be come connected before this function returns.
662 int rxrpc_kernel_charge_accept(struct socket *sock,
663 rxrpc_notify_rx_t notify_rx,
664 rxrpc_user_attach_call_t user_attach_call,
665 unsigned long user_call_ID, gfp_t gfp,
666 unsigned int debug_id)
668 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
669 struct rxrpc_backlog *b = rx->backlog;
671 if (sock->sk->sk_state == RXRPC_CLOSE)
674 return rxrpc_service_prealloc_one(rx, b, notify_rx,
675 user_attach_call, user_call_ID,
678 EXPORT_SYMBOL(rxrpc_kernel_charge_accept);