• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
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>
15 #include <linux/in.h>
16 #include <linux/in6.h>
17 #include <linux/icmp.h>
18 #include <linux/gfp.h>
19 #include <linux/circ_buf.h>
20 #include <net/sock.h>
21 #include <net/af_rxrpc.h>
22 #include <net/ip.h>
23 #include "ar-internal.h"
24 
rxrpc_dummy_notify(struct sock * sk,struct rxrpc_call * call,unsigned long user_call_ID)25 static void rxrpc_dummy_notify(struct sock *sk, struct rxrpc_call *call,
26 			       unsigned long user_call_ID)
27 {
28 }
29 
30 /*
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.
33  */
rxrpc_service_prealloc_one(struct rxrpc_sock * rx,struct rxrpc_backlog * b,rxrpc_notify_rx_t notify_rx,rxrpc_user_attach_call_t user_attach_call,unsigned long user_call_ID,gfp_t gfp,unsigned int debug_id)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)
40 {
41 	const void *here = __builtin_return_address(0);
42 	struct rxrpc_call *call;
43 	struct rxrpc_net *rxnet = rxrpc_net(sock_net(&rx->sk));
44 	int max, tmp;
45 	unsigned int size = RXRPC_BACKLOG_MAX;
46 	unsigned int head, tail, call_head, call_tail;
47 
48 	max = rx->sk.sk_max_ack_backlog;
49 	tmp = rx->sk.sk_ack_backlog;
50 	if (tmp >= max) {
51 		_leave(" = -ENOBUFS [full %u]", max);
52 		return -ENOBUFS;
53 	}
54 	max -= tmp;
55 
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
58 	 * than calls.
59 	 */
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);
63 	if (tmp >= max) {
64 		_leave(" = -ENOBUFS [enough %u]", tmp);
65 		return -ENOBUFS;
66 	}
67 	max = tmp + 1;
68 
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);
73 		if (!peer)
74 			return -ENOMEM;
75 		b->peer_backlog[head] = peer;
76 		smp_store_release(&b->peer_backlog_head,
77 				  (head + 1) & (size - 1));
78 	}
79 
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;
84 
85 		conn = rxrpc_prealloc_service_connection(rxnet, gfp);
86 		if (!conn)
87 			return -ENOMEM;
88 		b->conn_backlog[head] = conn;
89 		smp_store_release(&b->conn_backlog_head,
90 				  (head + 1) & (size - 1));
91 
92 		trace_rxrpc_conn(conn->debug_id, rxrpc_conn_new_service,
93 				 atomic_read(&conn->usage), here);
94 	}
95 
96 	/* Now it gets complicated, because calls get registered with the
97 	 * socket here, particularly if a user ID is preassigned by the user.
98 	 */
99 	call = rxrpc_alloc_call(rx, gfp, debug_id);
100 	if (!call)
101 		return -ENOMEM;
102 	call->flags |= (1 << RXRPC_CALL_IS_SERVICE);
103 	call->state = RXRPC_CALL_SERVER_PREALLOC;
104 
105 	trace_rxrpc_call(call->debug_id, rxrpc_call_new_service,
106 			 atomic_read(&call->usage),
107 			 here, (const void *)user_call_ID);
108 
109 	write_lock(&rx->call_lock);
110 	if (user_attach_call) {
111 		struct rxrpc_call *xcall;
112 		struct rb_node *parent, **pp;
113 
114 		/* Check the user ID isn't already in use */
115 		pp = &rx->calls.rb_node;
116 		parent = NULL;
117 		while (*pp) {
118 			parent = *pp;
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;
124 			else
125 				goto id_in_use;
126 		}
127 
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);
136 	}
137 
138 	list_add(&call->sock_link, &rx->sock_calls);
139 
140 	write_unlock(&rx->call_lock);
141 
142 	rxnet = call->rxnet;
143 	write_lock(&rxnet->call_lock);
144 	list_add_tail(&call->link, &rxnet->calls);
145 	write_unlock(&rxnet->call_lock);
146 
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);
150 	return 0;
151 
152 id_in_use:
153 	write_unlock(&rx->call_lock);
154 	rxrpc_cleanup_call(call);
155 	_leave(" = -EBADSLT");
156 	return -EBADSLT;
157 }
158 
159 /*
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.
163  *
164  * The backlog is replenished when a connection is accepted or rejected.
165  */
rxrpc_service_prealloc(struct rxrpc_sock * rx,gfp_t gfp)166 int rxrpc_service_prealloc(struct rxrpc_sock *rx, gfp_t gfp)
167 {
168 	struct rxrpc_backlog *b = rx->backlog;
169 
170 	if (!b) {
171 		b = kzalloc(sizeof(struct rxrpc_backlog), gfp);
172 		if (!b)
173 			return -ENOMEM;
174 		rx->backlog = b;
175 	}
176 
177 	if (rx->discard_new_call)
178 		return 0;
179 
180 	while (rxrpc_service_prealloc_one(rx, b, NULL, NULL, 0, gfp,
181 					  atomic_inc_return(&rxrpc_debug_id)) == 0)
182 		;
183 
184 	return 0;
185 }
186 
187 /*
188  * Discard the preallocation on a service.
189  */
rxrpc_discard_prealloc(struct rxrpc_sock * rx)190 void rxrpc_discard_prealloc(struct rxrpc_sock *rx)
191 {
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;
195 
196 	if (!b)
197 		return;
198 	rx->backlog = NULL;
199 
200 	/* Make sure that there aren't any incoming calls in progress before we
201 	 * clear the preallocation buffers.
202 	 */
203 	spin_lock_bh(&rx->incoming_lock);
204 	spin_unlock_bh(&rx->incoming_lock);
205 
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);
211 		kfree(peer);
212 		tail = (tail + 1) & (size - 1);
213 	}
214 
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);
223 		kfree(conn);
224 		if (atomic_dec_and_test(&rxnet->nr_conns))
225 			wake_up_var(&rxnet->nr_conns);
226 		tail = (tail + 1) & (size - 1);
227 	}
228 
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);
237 			if (call->notify_rx)
238 				call->notify_rx = rxrpc_dummy_notify;
239 			rxrpc_put_call(call, rxrpc_call_put_kernel);
240 		}
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);
245 	}
246 
247 	kfree(b);
248 }
249 
250 /*
251  * Ping the other end to fill our RTT cache and to retrieve the rwind
252  * and MTU parameters.
253  */
rxrpc_send_ping(struct rxrpc_call * call,struct sk_buff * skb)254 static void rxrpc_send_ping(struct rxrpc_call *call, struct sk_buff *skb)
255 {
256 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
257 	ktime_t now = skb->tstamp;
258 
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,
262 				  true, true,
263 				  rxrpc_propose_ack_ping_for_params);
264 }
265 
266 /*
267  * Allocate a new incoming call from the prealloc pool, along with a connection
268  * and a peer as necessary.
269  */
rxrpc_alloc_incoming_call(struct rxrpc_sock * rx,struct rxrpc_local * local,struct rxrpc_peer * peer,struct rxrpc_connection * conn,const struct rxrpc_security * sec,struct key * key,struct sk_buff * skb)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,
275 						    struct key *key,
276 						    struct sk_buff *skb)
277 {
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;
283 
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), >=,
295 		  conn_count);
296 
297 	if (call_count == 0)
298 		return NULL;
299 
300 	if (!conn) {
301 		if (peer && !rxrpc_get_peer_maybe(peer))
302 			peer = NULL;
303 		if (!peer) {
304 			peer = b->peer_backlog[peer_tail];
305 			if (rxrpc_extract_addr_from_skb(&peer->srx, skb) < 0)
306 				return NULL;
307 			b->peer_backlog[peer_tail] = NULL;
308 			smp_store_release(&b->peer_backlog_tail,
309 					  (peer_tail + 1) &
310 					  (RXRPC_BACKLOG_MAX - 1));
311 
312 			rxrpc_new_incoming_peer(rx, local, peer);
313 		}
314 
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);
324 	} else {
325 		rxrpc_get_connection(conn);
326 	}
327 
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));
333 
334 	rxrpc_see_call(call);
335 	call->conn = conn;
336 	call->security = conn->security;
337 	call->peer = rxrpc_get_peer(conn->params.peer);
338 	call->cong_cwnd = call->peer->cong_cwnd;
339 	return call;
340 }
341 
342 /*
343  * Set up a new incoming call.  Called in BH context with the RCU read lock
344  * held.
345  *
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.
351  *
352  * If we want to report an error, we mark the skb with the packet type and
353  * abort code and return NULL.
354  *
355  * The call is returned with the user access mutex held.
356  */
rxrpc_new_incoming_call(struct rxrpc_local * local,struct rxrpc_sock * rx,struct sk_buff * skb)357 struct rxrpc_call *rxrpc_new_incoming_call(struct rxrpc_local *local,
358 					   struct rxrpc_sock *rx,
359 					   struct sk_buff *skb)
360 {
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;
367 
368 	_enter("");
369 
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;
377 		goto no_call;
378 	}
379 
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.
384 	 */
385 	conn = rxrpc_find_connection_rcu(local, skb, &peer);
386 
387 	if (!conn && !rxrpc_look_up_server_security(local, rx, &sec, &key, skb))
388 		goto no_call;
389 
390 	call = rxrpc_alloc_incoming_call(rx, local, peer, conn, sec, key, skb);
391 	key_put(key);
392 	if (!call) {
393 		skb->mark = RXRPC_SKB_MARK_REJECT_BUSY;
394 		goto no_call;
395 	}
396 
397 	trace_rxrpc_receive(call, rxrpc_receive_incoming,
398 			    sp->hdr.serial, sp->hdr.seq);
399 
400 	/* Make the call live. */
401 	rxrpc_incoming_call(rx, call, skb);
402 	conn = call->conn;
403 
404 	if (rx->notify_new_call)
405 		rx->notify_new_call(&rx->sk, call, call->user_call_ID);
406 	else
407 		sk_acceptq_added(&rx->sk);
408 
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);
415 		break;
416 
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;
422 			else
423 				call->state = RXRPC_CALL_SERVER_ACCEPTING;
424 		}
425 		write_unlock(&call->state_lock);
426 		break;
427 
428 	case RXRPC_CONN_REMOTELY_ABORTED:
429 		rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
430 					  conn->abort_code, conn->error);
431 		break;
432 	case RXRPC_CONN_LOCALLY_ABORTED:
433 		rxrpc_abort_call("CON", call, sp->hdr.seq,
434 				 conn->abort_code, conn->error);
435 		break;
436 	default:
437 		BUG();
438 	}
439 	spin_unlock(&conn->state_lock);
440 	spin_unlock(&rx->incoming_lock);
441 
442 	rxrpc_send_ping(call, skb);
443 
444 	if (call->state == RXRPC_CALL_SERVER_ACCEPTING)
445 		rxrpc_notify_socket(call);
446 
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.
451 	 */
452 	rxrpc_put_call(call, rxrpc_call_put);
453 
454 	_leave(" = %p{%d}", call, call->debug_id);
455 	return call;
456 
457 no_call:
458 	spin_unlock(&rx->incoming_lock);
459 	_leave(" = NULL [%u]", skb->mark);
460 	return NULL;
461 }
462 
463 /*
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.
467  */
rxrpc_accept_call(struct rxrpc_sock * rx,unsigned long user_call_ID,rxrpc_notify_rx_t notify_rx)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)
473 {
474 	struct rxrpc_call *call;
475 	struct rb_node *parent, **pp;
476 	int ret;
477 
478 	_enter(",%lx", user_call_ID);
479 
480 	ASSERT(!irqs_disabled());
481 
482 	write_lock(&rx->call_lock);
483 
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);
489 	}
490 
491 	/* check the user ID isn't already in use */
492 	pp = &rx->calls.rb_node;
493 	parent = NULL;
494 	while (*pp) {
495 		parent = *pp;
496 		call = rb_entry(parent, struct rxrpc_call, sock_node);
497 
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;
502 		else
503 			goto id_in_use;
504 	}
505 
506 	/* Dequeue the first call and check it's still valid.  We gain
507 	 * responsibility for the queue's reference.
508 	 */
509 	call = list_entry(rx->to_be_accepted.next,
510 			  struct rxrpc_call, accept_link);
511 	write_unlock(&rx->call_lock);
512 
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.
517 	 */
518 	if (mutex_lock_interruptible(&call->user_mutex) < 0) {
519 		release_sock(&rx->sk);
520 		kleave(" = -ERESTARTSYS");
521 		return ERR_PTR(-ERESTARTSYS);
522 	}
523 
524 	write_lock(&rx->call_lock);
525 	list_del_init(&call->accept_link);
526 	sk_acceptq_removed(&rx->sk);
527 	rxrpc_see_call(call);
528 
529 	/* Find the user ID insertion point. */
530 	pp = &rx->calls.rb_node;
531 	parent = NULL;
532 	while (*pp) {
533 		parent = *pp;
534 		call = rb_entry(parent, struct rxrpc_call, sock_node);
535 
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;
540 		else
541 			BUG();
542 	}
543 
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;
548 		break;
549 	case RXRPC_CALL_COMPLETE:
550 		ret = call->error;
551 		goto out_release;
552 	default:
553 		BUG();
554 	}
555 
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))
563 		BUG();
564 
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);
571 	return call;
572 
573 out_release:
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);
579 	goto out;
580 
581 id_in_use:
582 	ret = -EBADSLT;
583 	write_unlock(&rx->call_lock);
584 out:
585 	rxrpc_service_prealloc(rx, GFP_KERNEL);
586 	release_sock(&rx->sk);
587 	_leave(" = %d", ret);
588 	return ERR_PTR(ret);
589 }
590 
591 /*
592  * Handle rejection of a call by userspace
593  * - reject the call at the front of the queue
594  */
rxrpc_reject_call(struct rxrpc_sock * rx)595 int rxrpc_reject_call(struct rxrpc_sock *rx)
596 {
597 	struct rxrpc_call *call;
598 	bool abort = false;
599 	int ret;
600 
601 	_enter("");
602 
603 	ASSERT(!irqs_disabled());
604 
605 	write_lock(&rx->call_lock);
606 
607 	if (list_empty(&rx->to_be_accepted)) {
608 		write_unlock(&rx->call_lock);
609 		return -ENODATA;
610 	}
611 
612 	/* Dequeue the first call and check it's still valid.  We gain
613 	 * responsibility for the queue's reference.
614 	 */
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);
620 
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);
625 		abort = true;
626 		/* fall through */
627 	case RXRPC_CALL_COMPLETE:
628 		ret = call->error;
629 		goto out_discard;
630 	default:
631 		BUG();
632 	}
633 
634 out_discard:
635 	write_unlock_bh(&call->state_lock);
636 	write_unlock(&rx->call_lock);
637 	if (abort) {
638 		rxrpc_send_abort_packet(call);
639 		rxrpc_release_call(rx, call);
640 		rxrpc_put_call(call, rxrpc_call_put);
641 	}
642 	rxrpc_service_prealloc(rx, GFP_KERNEL);
643 	_leave(" = %d", ret);
644 	return ret;
645 }
646 
647 /*
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.
655  *
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.
659  *
660  * Note that the call may be come connected before this function returns.
661  */
rxrpc_kernel_charge_accept(struct socket * sock,rxrpc_notify_rx_t notify_rx,rxrpc_user_attach_call_t user_attach_call,unsigned long user_call_ID,gfp_t gfp,unsigned int debug_id)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)
667 {
668 	struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
669 	struct rxrpc_backlog *b = rx->backlog;
670 
671 	if (sock->sk->sk_state == RXRPC_CLOSE)
672 		return -ESHUTDOWN;
673 
674 	return rxrpc_service_prealloc_one(rx, b, notify_rx,
675 					  user_attach_call, user_call_ID,
676 					  gfp, debug_id);
677 }
678 EXPORT_SYMBOL(rxrpc_kernel_charge_accept);
679