1 /* incoming call handling
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/module.h>
15 #include <linux/net.h>
16 #include <linux/skbuff.h>
17 #include <linux/errqueue.h>
18 #include <linux/udp.h>
20 #include <linux/in6.h>
21 #include <linux/icmp.h>
22 #include <linux/gfp.h>
23 #include <linux/circ_buf.h>
25 #include <net/af_rxrpc.h>
27 #include "ar-internal.h"
29 static void rxrpc_dummy_notify(struct sock *sk, struct rxrpc_call *call,
30 unsigned long user_call_ID)
35 * Preallocate a single service call, connection and peer and, if possible,
36 * give them a user ID and attach the user's side of the ID to them.
38 static int rxrpc_service_prealloc_one(struct rxrpc_sock *rx,
39 struct rxrpc_backlog *b,
40 rxrpc_notify_rx_t notify_rx,
41 rxrpc_user_attach_call_t user_attach_call,
42 unsigned long user_call_ID, gfp_t gfp)
44 const void *here = __builtin_return_address(0);
45 struct rxrpc_call *call;
46 struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
48 unsigned int size = RXRPC_BACKLOG_MAX;
49 unsigned int head, tail, call_head, call_tail;
51 max = rx->sk.sk_max_ack_backlog;
52 tmp = rx->sk.sk_ack_backlog;
54 _leave(" = -ENOBUFS [full %u]", max);
59 /* We don't need more conns and peers than we have calls, but on the
60 * other hand, we shouldn't ever use more peers than conns or conns
63 call_head = b->call_backlog_head;
64 call_tail = READ_ONCE(b->call_backlog_tail);
65 tmp = CIRC_CNT(call_head, call_tail, size);
67 _leave(" = -ENOBUFS [enough %u]", tmp);
72 head = b->peer_backlog_head;
73 tail = READ_ONCE(b->peer_backlog_tail);
74 if (CIRC_CNT(head, tail, size) < max) {
75 struct rxrpc_peer *peer = rxrpc_alloc_peer(rx->local, gfp);
78 b->peer_backlog[head] = peer;
79 smp_store_release(&b->peer_backlog_head,
80 (head + 1) & (size - 1));
83 head = b->conn_backlog_head;
84 tail = READ_ONCE(b->conn_backlog_tail);
85 if (CIRC_CNT(head, tail, size) < max) {
86 struct rxrpc_connection *conn;
88 conn = rxrpc_prealloc_service_connection(rxnet, gfp);
91 b->conn_backlog[head] = conn;
92 smp_store_release(&b->conn_backlog_head,
93 (head + 1) & (size - 1));
95 trace_rxrpc_conn(conn, rxrpc_conn_new_service,
96 atomic_read(&conn->usage), here);
99 /* Now it gets complicated, because calls get registered with the
100 * socket here, particularly if a user ID is preassigned by the user.
102 call = rxrpc_alloc_call(rx, gfp);
105 call->flags |= (1 << RXRPC_CALL_IS_SERVICE);
106 call->state = RXRPC_CALL_SERVER_PREALLOC;
108 trace_rxrpc_call(call, rxrpc_call_new_service,
109 atomic_read(&call->usage),
110 here, (const void *)user_call_ID);
112 write_lock(&rx->call_lock);
113 if (user_attach_call) {
114 struct rxrpc_call *xcall;
115 struct rb_node *parent, **pp;
117 /* Check the user ID isn't already in use */
118 pp = &rx->calls.rb_node;
122 xcall = rb_entry(parent, struct rxrpc_call, sock_node);
123 if (user_call_ID < xcall->user_call_ID)
124 pp = &(*pp)->rb_left;
125 else if (user_call_ID > xcall->user_call_ID)
126 pp = &(*pp)->rb_right;
131 call->user_call_ID = user_call_ID;
132 call->notify_rx = notify_rx;
133 rxrpc_get_call(call, rxrpc_call_got_kernel);
134 user_attach_call(call, user_call_ID);
135 rxrpc_get_call(call, rxrpc_call_got_userid);
136 rb_link_node(&call->sock_node, parent, pp);
137 rb_insert_color(&call->sock_node, &rx->calls);
138 set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
141 list_add(&call->sock_link, &rx->sock_calls);
143 write_unlock(&rx->call_lock);
145 write_lock(&rxnet->call_lock);
146 list_add_tail(&call->link, &rxnet->calls);
147 write_unlock(&rxnet->call_lock);
149 b->call_backlog[call_head] = call;
150 smp_store_release(&b->call_backlog_head, (call_head + 1) & (size - 1));
151 _leave(" = 0 [%d -> %lx]", call->debug_id, user_call_ID);
155 write_unlock(&rx->call_lock);
156 rxrpc_cleanup_call(call);
157 _leave(" = -EBADSLT");
162 * Preallocate sufficient service connections, calls and peers to cover the
163 * entire backlog of a socket. When a new call comes in, if we don't have
164 * sufficient of each available, the call gets rejected as busy or ignored.
166 * The backlog is replenished when a connection is accepted or rejected.
168 int rxrpc_service_prealloc(struct rxrpc_sock *rx, gfp_t gfp)
170 struct rxrpc_backlog *b = rx->backlog;
173 b = kzalloc(sizeof(struct rxrpc_backlog), gfp);
179 if (rx->discard_new_call)
182 while (rxrpc_service_prealloc_one(rx, b, NULL, NULL, 0, gfp) == 0)
189 * Discard the preallocation on a service.
191 void rxrpc_discard_prealloc(struct rxrpc_sock *rx)
193 struct rxrpc_backlog *b = rx->backlog;
194 struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
195 unsigned int size = RXRPC_BACKLOG_MAX, head, tail;
201 /* Make sure that there aren't any incoming calls in progress before we
202 * clear the preallocation buffers.
204 spin_lock_bh(&rx->incoming_lock);
205 spin_unlock_bh(&rx->incoming_lock);
207 head = b->peer_backlog_head;
208 tail = b->peer_backlog_tail;
209 while (CIRC_CNT(head, tail, size) > 0) {
210 struct rxrpc_peer *peer = b->peer_backlog[tail];
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 tail = (tail + 1) & (size - 1);
227 head = b->call_backlog_head;
228 tail = b->call_backlog_tail;
229 while (CIRC_CNT(head, tail, size) > 0) {
230 struct rxrpc_call *call = b->call_backlog[tail];
232 if (rx->discard_new_call) {
233 _debug("discard %lx", call->user_call_ID);
234 rx->discard_new_call(call, call->user_call_ID);
236 call->notify_rx = rxrpc_dummy_notify;
237 rxrpc_put_call(call, rxrpc_call_put_kernel);
239 rxrpc_call_completed(call);
240 rxrpc_release_call(rx, call);
241 rxrpc_put_call(call, rxrpc_call_put);
242 tail = (tail + 1) & (size - 1);
249 * Allocate a new incoming call from the prealloc pool, along with a connection
250 * and a peer as necessary.
252 static struct rxrpc_call *rxrpc_alloc_incoming_call(struct rxrpc_sock *rx,
253 struct rxrpc_local *local,
254 struct rxrpc_connection *conn,
257 struct rxrpc_backlog *b = rx->backlog;
258 struct rxrpc_peer *peer, *xpeer;
259 struct rxrpc_call *call;
260 unsigned short call_head, conn_head, peer_head;
261 unsigned short call_tail, conn_tail, peer_tail;
262 unsigned short call_count, conn_count;
264 /* #calls >= #conns >= #peers must hold true. */
265 call_head = smp_load_acquire(&b->call_backlog_head);
266 call_tail = b->call_backlog_tail;
267 call_count = CIRC_CNT(call_head, call_tail, RXRPC_BACKLOG_MAX);
268 conn_head = smp_load_acquire(&b->conn_backlog_head);
269 conn_tail = b->conn_backlog_tail;
270 conn_count = CIRC_CNT(conn_head, conn_tail, RXRPC_BACKLOG_MAX);
271 ASSERTCMP(conn_count, >=, call_count);
272 peer_head = smp_load_acquire(&b->peer_backlog_head);
273 peer_tail = b->peer_backlog_tail;
274 ASSERTCMP(CIRC_CNT(peer_head, peer_tail, RXRPC_BACKLOG_MAX), >=,
281 /* No connection. We're going to need a peer to start off
282 * with. If one doesn't yet exist, use a spare from the
283 * preallocation set. We dump the address into the spare in
284 * anticipation - and to save on stack space.
286 xpeer = b->peer_backlog[peer_tail];
287 if (rxrpc_extract_addr_from_skb(local, &xpeer->srx, skb) < 0)
290 peer = rxrpc_lookup_incoming_peer(local, xpeer);
292 b->peer_backlog[peer_tail] = NULL;
293 smp_store_release(&b->peer_backlog_tail,
295 (RXRPC_BACKLOG_MAX - 1));
298 /* Now allocate and set up the connection */
299 conn = b->conn_backlog[conn_tail];
300 b->conn_backlog[conn_tail] = NULL;
301 smp_store_release(&b->conn_backlog_tail,
302 (conn_tail + 1) & (RXRPC_BACKLOG_MAX - 1));
303 rxrpc_get_local(local);
304 conn->params.local = local;
305 conn->params.peer = peer;
306 rxrpc_see_connection(conn);
307 rxrpc_new_incoming_connection(rx, conn, skb);
309 rxrpc_get_connection(conn);
312 /* And now we can allocate and set up a new call */
313 call = b->call_backlog[call_tail];
314 b->call_backlog[call_tail] = NULL;
315 smp_store_release(&b->call_backlog_tail,
316 (call_tail + 1) & (RXRPC_BACKLOG_MAX - 1));
318 rxrpc_see_call(call);
320 call->peer = rxrpc_get_peer(conn->params.peer);
321 call->cong_cwnd = call->peer->cong_cwnd;
326 * Set up a new incoming call. Called in BH context with the RCU read lock
329 * If this is for a kernel service, when we allocate the call, it will have
330 * three refs on it: (1) the kernel service, (2) the user_call_ID tree, (3) the
331 * retainer ref obtained from the backlog buffer. Prealloc calls for userspace
332 * services only have the ref from the backlog buffer. We want to pass this
333 * ref to non-BH context to dispose of.
335 * If we want to report an error, we mark the skb with the packet type and
336 * abort code and return NULL.
338 * The call is returned with the user access mutex held.
340 struct rxrpc_call *rxrpc_new_incoming_call(struct rxrpc_local *local,
341 struct rxrpc_connection *conn,
344 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
345 struct rxrpc_sock *rx;
346 struct rxrpc_call *call;
347 u16 service_id = sp->hdr.serviceId;
351 /* Get the socket providing the service */
352 rx = rcu_dereference(local->service);
353 if (rx && (service_id == rx->srx.srx_service ||
354 service_id == rx->second_service))
357 trace_rxrpc_abort("INV", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
358 RX_INVALID_OPERATION, EOPNOTSUPP);
359 skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
360 skb->priority = RX_INVALID_OPERATION;
361 _leave(" = NULL [service]");
365 spin_lock(&rx->incoming_lock);
366 if (rx->sk.sk_state == RXRPC_SERVER_LISTEN_DISABLED ||
367 rx->sk.sk_state == RXRPC_CLOSE) {
368 trace_rxrpc_abort("CLS", sp->hdr.cid, sp->hdr.callNumber,
369 sp->hdr.seq, RX_INVALID_OPERATION, ESHUTDOWN);
370 skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
371 skb->priority = RX_INVALID_OPERATION;
372 _leave(" = NULL [close]");
377 call = rxrpc_alloc_incoming_call(rx, local, conn, skb);
379 skb->mark = RXRPC_SKB_MARK_BUSY;
380 _leave(" = NULL [busy]");
385 trace_rxrpc_receive(call, rxrpc_receive_incoming,
386 sp->hdr.serial, sp->hdr.seq);
388 /* Lock the call to prevent rxrpc_kernel_send/recv_data() and
389 * sendmsg()/recvmsg() inconveniently stealing the mutex once the
390 * notification is generated.
392 * The BUG should never happen because the kernel should be well
393 * behaved enough not to access the call before the first notification
394 * event and userspace is prevented from doing so until the state is
397 if (!mutex_trylock(&call->user_mutex))
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 (rx->discard_new_call)
420 call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
422 call->state = RXRPC_CALL_SERVER_ACCEPTING;
423 write_unlock(&call->state_lock);
426 case RXRPC_CONN_REMOTELY_ABORTED:
427 rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
428 conn->abort_code, conn->error);
430 case RXRPC_CONN_LOCALLY_ABORTED:
431 rxrpc_abort_call("CON", call, sp->hdr.seq,
432 conn->abort_code, conn->error);
437 spin_unlock(&conn->state_lock);
439 if (call->state == RXRPC_CALL_SERVER_ACCEPTING)
440 rxrpc_notify_socket(call);
442 /* We have to discard the prealloc queue's ref here and rely on a
443 * combination of the RCU read lock and refs held either by the socket
444 * (recvmsg queue, to-be-accepted queue or user ID tree) or the kernel
445 * service to prevent the call from being deallocated too early.
447 rxrpc_put_call(call, rxrpc_call_put);
449 _leave(" = %p{%d}", call, call->debug_id);
451 spin_unlock(&rx->incoming_lock);
456 * handle acceptance of a call by userspace
457 * - assign the user call ID to the call at the front of the queue
458 * - called with the socket locked.
460 struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *rx,
461 unsigned long user_call_ID,
462 rxrpc_notify_rx_t notify_rx)
463 __releases(&rx->sk.sk_lock.slock)
465 struct rxrpc_call *call;
466 struct rb_node *parent, **pp;
469 _enter(",%lx", user_call_ID);
471 ASSERT(!irqs_disabled());
473 write_lock(&rx->call_lock);
475 if (list_empty(&rx->to_be_accepted)) {
476 write_unlock(&rx->call_lock);
477 release_sock(&rx->sk);
478 kleave(" = -ENODATA [empty]");
479 return ERR_PTR(-ENODATA);
482 /* check the user ID isn't already in use */
483 pp = &rx->calls.rb_node;
487 call = rb_entry(parent, struct rxrpc_call, sock_node);
489 if (user_call_ID < call->user_call_ID)
490 pp = &(*pp)->rb_left;
491 else if (user_call_ID > call->user_call_ID)
492 pp = &(*pp)->rb_right;
497 /* Dequeue the first call and check it's still valid. We gain
498 * responsibility for the queue's reference.
500 call = list_entry(rx->to_be_accepted.next,
501 struct rxrpc_call, accept_link);
502 write_unlock(&rx->call_lock);
504 /* We need to gain the mutex from the interrupt handler without
505 * upsetting lockdep, so we have to release it there and take it here.
506 * We are, however, still holding the socket lock, so other accepts
507 * must wait for us and no one can add the user ID behind our backs.
509 if (mutex_lock_interruptible(&call->user_mutex) < 0) {
510 release_sock(&rx->sk);
511 kleave(" = -ERESTARTSYS");
512 return ERR_PTR(-ERESTARTSYS);
515 write_lock(&rx->call_lock);
516 list_del_init(&call->accept_link);
517 sk_acceptq_removed(&rx->sk);
518 rxrpc_see_call(call);
520 /* Find the user ID insertion point. */
521 pp = &rx->calls.rb_node;
525 call = rb_entry(parent, struct rxrpc_call, sock_node);
527 if (user_call_ID < call->user_call_ID)
528 pp = &(*pp)->rb_left;
529 else if (user_call_ID > call->user_call_ID)
530 pp = &(*pp)->rb_right;
535 write_lock_bh(&call->state_lock);
536 switch (call->state) {
537 case RXRPC_CALL_SERVER_ACCEPTING:
538 call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
540 case RXRPC_CALL_COMPLETE:
547 /* formalise the acceptance */
548 call->notify_rx = notify_rx;
549 call->user_call_ID = user_call_ID;
550 rxrpc_get_call(call, rxrpc_call_got_userid);
551 rb_link_node(&call->sock_node, parent, pp);
552 rb_insert_color(&call->sock_node, &rx->calls);
553 if (test_and_set_bit(RXRPC_CALL_HAS_USERID, &call->flags))
556 write_unlock_bh(&call->state_lock);
557 write_unlock(&rx->call_lock);
558 rxrpc_notify_socket(call);
559 rxrpc_service_prealloc(rx, GFP_KERNEL);
560 release_sock(&rx->sk);
561 _leave(" = %p{%d}", call, call->debug_id);
565 _debug("release %p", call);
566 write_unlock_bh(&call->state_lock);
567 write_unlock(&rx->call_lock);
568 rxrpc_release_call(rx, call);
569 rxrpc_put_call(call, rxrpc_call_put);
574 write_unlock(&rx->call_lock);
576 rxrpc_service_prealloc(rx, GFP_KERNEL);
577 release_sock(&rx->sk);
578 _leave(" = %d", ret);
583 * Handle rejection of a call by userspace
584 * - reject the call at the front of the queue
586 int rxrpc_reject_call(struct rxrpc_sock *rx)
588 struct rxrpc_call *call;
594 ASSERT(!irqs_disabled());
596 write_lock(&rx->call_lock);
598 if (list_empty(&rx->to_be_accepted)) {
599 write_unlock(&rx->call_lock);
603 /* Dequeue the first call and check it's still valid. We gain
604 * responsibility for the queue's reference.
606 call = list_entry(rx->to_be_accepted.next,
607 struct rxrpc_call, accept_link);
608 list_del_init(&call->accept_link);
609 sk_acceptq_removed(&rx->sk);
610 rxrpc_see_call(call);
612 write_lock_bh(&call->state_lock);
613 switch (call->state) {
614 case RXRPC_CALL_SERVER_ACCEPTING:
615 __rxrpc_abort_call("REJ", call, 1, RX_USER_ABORT, -ECONNABORTED);
618 case RXRPC_CALL_COMPLETE:
626 write_unlock_bh(&call->state_lock);
627 write_unlock(&rx->call_lock);
629 rxrpc_send_abort_packet(call);
630 rxrpc_release_call(rx, call);
631 rxrpc_put_call(call, rxrpc_call_put);
633 rxrpc_service_prealloc(rx, GFP_KERNEL);
634 _leave(" = %d", ret);
639 * rxrpc_kernel_charge_accept - Charge up socket with preallocated calls
640 * @sock: The socket on which to preallocate
641 * @notify_rx: Event notification function for the call
642 * @user_attach_call: Func to attach call to user_call_ID
643 * @user_call_ID: The tag to attach to the preallocated call
644 * @gfp: The allocation conditions.
646 * Charge up the socket with preallocated calls, each with a user ID. A
647 * function should be provided to effect the attachment from the user's side.
648 * The user is given a ref to hold on the call.
650 * Note that the call may be come connected before this function returns.
652 int rxrpc_kernel_charge_accept(struct socket *sock,
653 rxrpc_notify_rx_t notify_rx,
654 rxrpc_user_attach_call_t user_attach_call,
655 unsigned long user_call_ID, gfp_t gfp)
657 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
658 struct rxrpc_backlog *b = rx->backlog;
660 if (sock->sk->sk_state == RXRPC_CLOSE)
663 return rxrpc_service_prealloc_one(rx, b, notify_rx,
664 user_attach_call, user_call_ID,
667 EXPORT_SYMBOL(rxrpc_kernel_charge_accept);