blob: 26c293ef98eb53a9c5f69dae3c8e23327e0b6741 [file] [log] [blame]
David Howells17926a72007-04-26 15:48:28 -07001/* 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
Joe Perches9b6d5392016-06-02 12:08:52 -070012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
David Howells17926a72007-04-26 15:48:28 -070014#include <linux/module.h>
15#include <linux/net.h>
16#include <linux/skbuff.h>
17#include <linux/errqueue.h>
18#include <linux/udp.h>
19#include <linux/in.h>
20#include <linux/in6.h>
21#include <linux/icmp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
David Howells00e90712016-09-08 11:10:12 +010023#include <linux/circ_buf.h>
David Howells17926a72007-04-26 15:48:28 -070024#include <net/sock.h>
25#include <net/af_rxrpc.h>
26#include <net/ip.h>
27#include "ar-internal.h"
28
29/*
David Howells00e90712016-09-08 11:10:12 +010030 * Preallocate a single service call, connection and peer and, if possible,
31 * give them a user ID and attach the user's side of the ID to them.
32 */
33static int rxrpc_service_prealloc_one(struct rxrpc_sock *rx,
34 struct rxrpc_backlog *b,
35 rxrpc_notify_rx_t notify_rx,
36 rxrpc_user_attach_call_t user_attach_call,
37 unsigned long user_call_ID, gfp_t gfp)
38{
39 const void *here = __builtin_return_address(0);
40 struct rxrpc_call *call;
41 int max, tmp;
42 unsigned int size = RXRPC_BACKLOG_MAX;
43 unsigned int head, tail, call_head, call_tail;
44
45 max = rx->sk.sk_max_ack_backlog;
46 tmp = rx->sk.sk_ack_backlog;
47 if (tmp >= max) {
48 _leave(" = -ENOBUFS [full %u]", max);
49 return -ENOBUFS;
50 }
51 max -= tmp;
52
53 /* We don't need more conns and peers than we have calls, but on the
54 * other hand, we shouldn't ever use more peers than conns or conns
55 * than calls.
56 */
57 call_head = b->call_backlog_head;
58 call_tail = READ_ONCE(b->call_backlog_tail);
59 tmp = CIRC_CNT(call_head, call_tail, size);
60 if (tmp >= max) {
61 _leave(" = -ENOBUFS [enough %u]", tmp);
62 return -ENOBUFS;
63 }
64 max = tmp + 1;
65
66 head = b->peer_backlog_head;
67 tail = READ_ONCE(b->peer_backlog_tail);
68 if (CIRC_CNT(head, tail, size) < max) {
69 struct rxrpc_peer *peer = rxrpc_alloc_peer(rx->local, gfp);
70 if (!peer)
71 return -ENOMEM;
72 b->peer_backlog[head] = peer;
73 smp_store_release(&b->peer_backlog_head,
74 (head + 1) & (size - 1));
75 }
76
77 head = b->conn_backlog_head;
78 tail = READ_ONCE(b->conn_backlog_tail);
79 if (CIRC_CNT(head, tail, size) < max) {
80 struct rxrpc_connection *conn;
81
82 conn = rxrpc_prealloc_service_connection(gfp);
83 if (!conn)
84 return -ENOMEM;
85 b->conn_backlog[head] = conn;
86 smp_store_release(&b->conn_backlog_head,
87 (head + 1) & (size - 1));
88 }
89
90 /* Now it gets complicated, because calls get registered with the
91 * socket here, particularly if a user ID is preassigned by the user.
92 */
93 call = rxrpc_alloc_call(gfp);
94 if (!call)
95 return -ENOMEM;
96 call->flags |= (1 << RXRPC_CALL_IS_SERVICE);
97 call->state = RXRPC_CALL_SERVER_PREALLOC;
98
99 trace_rxrpc_call(call, rxrpc_call_new_service,
100 atomic_read(&call->usage),
101 here, (const void *)user_call_ID);
102
103 write_lock(&rx->call_lock);
104 if (user_attach_call) {
105 struct rxrpc_call *xcall;
106 struct rb_node *parent, **pp;
107
108 /* Check the user ID isn't already in use */
109 pp = &rx->calls.rb_node;
110 parent = NULL;
111 while (*pp) {
112 parent = *pp;
113 xcall = rb_entry(parent, struct rxrpc_call, sock_node);
114 if (user_call_ID < call->user_call_ID)
115 pp = &(*pp)->rb_left;
116 else if (user_call_ID > call->user_call_ID)
117 pp = &(*pp)->rb_right;
118 else
119 goto id_in_use;
120 }
121
122 call->user_call_ID = user_call_ID;
123 call->notify_rx = notify_rx;
David Howellscbd00892016-09-13 09:12:34 +0100124 rxrpc_get_call(call, rxrpc_call_got_kernel);
David Howells00e90712016-09-08 11:10:12 +0100125 user_attach_call(call, user_call_ID);
126 rxrpc_get_call(call, rxrpc_call_got_userid);
127 rb_link_node(&call->sock_node, parent, pp);
128 rb_insert_color(&call->sock_node, &rx->calls);
129 set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
130 }
131
David Howells248f2192016-09-08 11:10:12 +0100132 list_add(&call->sock_link, &rx->sock_calls);
133
David Howells00e90712016-09-08 11:10:12 +0100134 write_unlock(&rx->call_lock);
135
136 write_lock(&rxrpc_call_lock);
137 list_add_tail(&call->link, &rxrpc_calls);
138 write_unlock(&rxrpc_call_lock);
139
140 b->call_backlog[call_head] = call;
141 smp_store_release(&b->call_backlog_head, (call_head + 1) & (size - 1));
142 _leave(" = 0 [%d -> %lx]", call->debug_id, user_call_ID);
143 return 0;
144
145id_in_use:
146 write_unlock(&rx->call_lock);
147 rxrpc_cleanup_call(call);
148 _leave(" = -EBADSLT");
149 return -EBADSLT;
150}
151
152/*
153 * Preallocate sufficient service connections, calls and peers to cover the
154 * entire backlog of a socket. When a new call comes in, if we don't have
155 * sufficient of each available, the call gets rejected as busy or ignored.
156 *
157 * The backlog is replenished when a connection is accepted or rejected.
158 */
159int rxrpc_service_prealloc(struct rxrpc_sock *rx, gfp_t gfp)
160{
161 struct rxrpc_backlog *b = rx->backlog;
162
163 if (!b) {
164 b = kzalloc(sizeof(struct rxrpc_backlog), gfp);
165 if (!b)
166 return -ENOMEM;
167 rx->backlog = b;
168 }
169
170 if (rx->discard_new_call)
171 return 0;
172
173 while (rxrpc_service_prealloc_one(rx, b, NULL, NULL, 0, gfp) == 0)
174 ;
175
176 return 0;
177}
178
179/*
180 * Discard the preallocation on a service.
181 */
182void rxrpc_discard_prealloc(struct rxrpc_sock *rx)
183{
184 struct rxrpc_backlog *b = rx->backlog;
185 unsigned int size = RXRPC_BACKLOG_MAX, head, tail;
186
187 if (!b)
188 return;
189 rx->backlog = NULL;
190
David Howells248f2192016-09-08 11:10:12 +0100191 /* Make sure that there aren't any incoming calls in progress before we
192 * clear the preallocation buffers.
193 */
194 spin_lock_bh(&rx->incoming_lock);
195 spin_unlock_bh(&rx->incoming_lock);
196
David Howells00e90712016-09-08 11:10:12 +0100197 head = b->peer_backlog_head;
198 tail = b->peer_backlog_tail;
199 while (CIRC_CNT(head, tail, size) > 0) {
200 struct rxrpc_peer *peer = b->peer_backlog[tail];
201 kfree(peer);
202 tail = (tail + 1) & (size - 1);
203 }
204
205 head = b->conn_backlog_head;
206 tail = b->conn_backlog_tail;
207 while (CIRC_CNT(head, tail, size) > 0) {
208 struct rxrpc_connection *conn = b->conn_backlog[tail];
209 write_lock(&rxrpc_connection_lock);
210 list_del(&conn->link);
211 list_del(&conn->proc_link);
212 write_unlock(&rxrpc_connection_lock);
213 kfree(conn);
214 tail = (tail + 1) & (size - 1);
215 }
216
217 head = b->call_backlog_head;
218 tail = b->call_backlog_tail;
219 while (CIRC_CNT(head, tail, size) > 0) {
220 struct rxrpc_call *call = b->call_backlog[tail];
221 if (rx->discard_new_call) {
222 _debug("discard %lx", call->user_call_ID);
223 rx->discard_new_call(call, call->user_call_ID);
David Howells3432a752016-09-13 09:05:14 +0100224 rxrpc_put_call(call, rxrpc_call_put_kernel);
David Howells00e90712016-09-08 11:10:12 +0100225 }
226 rxrpc_call_completed(call);
227 rxrpc_release_call(rx, call);
228 rxrpc_put_call(call, rxrpc_call_put);
229 tail = (tail + 1) & (size - 1);
230 }
231
232 kfree(b);
233}
234
235/*
David Howells248f2192016-09-08 11:10:12 +0100236 * Allocate a new incoming call from the prealloc pool, along with a connection
237 * and a peer as necessary.
David Howells17926a72007-04-26 15:48:28 -0700238 */
David Howells248f2192016-09-08 11:10:12 +0100239static struct rxrpc_call *rxrpc_alloc_incoming_call(struct rxrpc_sock *rx,
240 struct rxrpc_local *local,
241 struct rxrpc_connection *conn,
242 struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700243{
David Howells248f2192016-09-08 11:10:12 +0100244 struct rxrpc_backlog *b = rx->backlog;
245 struct rxrpc_peer *peer, *xpeer;
246 struct rxrpc_call *call;
247 unsigned short call_head, conn_head, peer_head;
248 unsigned short call_tail, conn_tail, peer_tail;
249 unsigned short call_count, conn_count;
David Howells17926a72007-04-26 15:48:28 -0700250
David Howells248f2192016-09-08 11:10:12 +0100251 /* #calls >= #conns >= #peers must hold true. */
252 call_head = smp_load_acquire(&b->call_backlog_head);
253 call_tail = b->call_backlog_tail;
254 call_count = CIRC_CNT(call_head, call_tail, RXRPC_BACKLOG_MAX);
255 conn_head = smp_load_acquire(&b->conn_backlog_head);
256 conn_tail = b->conn_backlog_tail;
257 conn_count = CIRC_CNT(conn_head, conn_tail, RXRPC_BACKLOG_MAX);
258 ASSERTCMP(conn_count, >=, call_count);
259 peer_head = smp_load_acquire(&b->peer_backlog_head);
260 peer_tail = b->peer_backlog_tail;
261 ASSERTCMP(CIRC_CNT(peer_head, peer_tail, RXRPC_BACKLOG_MAX), >=,
262 conn_count);
David Howells17926a72007-04-26 15:48:28 -0700263
David Howells248f2192016-09-08 11:10:12 +0100264 if (call_count == 0)
265 return NULL;
David Howells0d12f8a2016-03-04 15:53:46 +0000266
David Howells248f2192016-09-08 11:10:12 +0100267 if (!conn) {
268 /* No connection. We're going to need a peer to start off
269 * with. If one doesn't yet exist, use a spare from the
270 * preallocation set. We dump the address into the spare in
271 * anticipation - and to save on stack space.
272 */
273 xpeer = b->peer_backlog[peer_tail];
274 if (rxrpc_extract_addr_from_skb(&xpeer->srx, skb) < 0)
275 return NULL;
David Howells17926a72007-04-26 15:48:28 -0700276
David Howells248f2192016-09-08 11:10:12 +0100277 peer = rxrpc_lookup_incoming_peer(local, xpeer);
278 if (peer == xpeer) {
279 b->peer_backlog[peer_tail] = NULL;
280 smp_store_release(&b->peer_backlog_tail,
281 (peer_tail + 1) &
282 (RXRPC_BACKLOG_MAX - 1));
283 }
David Howells17926a72007-04-26 15:48:28 -0700284
David Howells248f2192016-09-08 11:10:12 +0100285 /* Now allocate and set up the connection */
286 conn = b->conn_backlog[conn_tail];
287 b->conn_backlog[conn_tail] = NULL;
288 smp_store_release(&b->conn_backlog_tail,
289 (conn_tail + 1) & (RXRPC_BACKLOG_MAX - 1));
290 rxrpc_get_local(local);
291 conn->params.local = local;
292 conn->params.peer = peer;
293 rxrpc_new_incoming_connection(conn, skb);
294 } else {
295 rxrpc_get_connection(conn);
David Howells17926a72007-04-26 15:48:28 -0700296 }
297
David Howells248f2192016-09-08 11:10:12 +0100298 /* And now we can allocate and set up a new call */
299 call = b->call_backlog[call_tail];
300 b->call_backlog[call_tail] = NULL;
301 smp_store_release(&b->call_backlog_tail,
302 (call_tail + 1) & (RXRPC_BACKLOG_MAX - 1));
303
David Howellscbd00892016-09-13 09:12:34 +0100304 rxrpc_see_call(call);
David Howells248f2192016-09-08 11:10:12 +0100305 call->conn = conn;
306 call->peer = rxrpc_get_peer(conn->params.peer);
307 return call;
David Howells17926a72007-04-26 15:48:28 -0700308}
309
310/*
David Howells248f2192016-09-08 11:10:12 +0100311 * Set up a new incoming call. Called in BH context with the RCU read lock
312 * held.
313 *
314 * If this is for a kernel service, when we allocate the call, it will have
315 * three refs on it: (1) the kernel service, (2) the user_call_ID tree, (3) the
316 * retainer ref obtained from the backlog buffer. Prealloc calls for userspace
317 * services only have the ref from the backlog buffer. We want to pass this
318 * ref to non-BH context to dispose of.
319 *
320 * If we want to report an error, we mark the skb with the packet type and
321 * abort code and return NULL.
David Howells17926a72007-04-26 15:48:28 -0700322 */
David Howells248f2192016-09-08 11:10:12 +0100323struct rxrpc_call *rxrpc_new_incoming_call(struct rxrpc_local *local,
324 struct rxrpc_connection *conn,
325 struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700326{
David Howells248f2192016-09-08 11:10:12 +0100327 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
328 struct rxrpc_sock *rx;
David Howells17926a72007-04-26 15:48:28 -0700329 struct rxrpc_call *call;
David Howells17926a72007-04-26 15:48:28 -0700330
331 _enter("");
332
David Howells248f2192016-09-08 11:10:12 +0100333 /* Get the socket providing the service */
334 hlist_for_each_entry_rcu_bh(rx, &local->services, listen_link) {
335 if (rx->srx.srx_service == sp->hdr.serviceId)
David Howells17926a72007-04-26 15:48:28 -0700336 goto found_service;
337 }
David Howells248f2192016-09-08 11:10:12 +0100338
339 trace_rxrpc_abort("INV", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
340 RX_INVALID_OPERATION, EOPNOTSUPP);
341 skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
342 skb->priority = RX_INVALID_OPERATION;
343 _leave(" = NULL [service]");
344 return NULL;
David Howells17926a72007-04-26 15:48:28 -0700345
346found_service:
David Howells248f2192016-09-08 11:10:12 +0100347 spin_lock(&rx->incoming_lock);
348 if (rx->sk.sk_state == RXRPC_CLOSE) {
349 trace_rxrpc_abort("CLS", sp->hdr.cid, sp->hdr.callNumber,
350 sp->hdr.seq, RX_INVALID_OPERATION, ESHUTDOWN);
351 skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT;
352 skb->priority = RX_INVALID_OPERATION;
353 _leave(" = NULL [close]");
354 call = NULL;
355 goto out;
356 }
David Howells17926a72007-04-26 15:48:28 -0700357
David Howells248f2192016-09-08 11:10:12 +0100358 call = rxrpc_alloc_incoming_call(rx, local, conn, skb);
359 if (!call) {
360 skb->mark = RXRPC_SKB_MARK_BUSY;
361 _leave(" = NULL [busy]");
362 call = NULL;
363 goto out;
364 }
365
366 /* Make the call live. */
367 rxrpc_incoming_call(rx, call, skb);
368 conn = call->conn;
369
370 if (rx->notify_new_call)
371 rx->notify_new_call(&rx->sk, call, call->user_call_ID);
372
373 spin_lock(&conn->state_lock);
374 switch (conn->state) {
375 case RXRPC_CONN_SERVICE_UNSECURED:
376 conn->state = RXRPC_CONN_SERVICE_CHALLENGING;
377 set_bit(RXRPC_CONN_EV_CHALLENGE, &call->conn->events);
378 rxrpc_queue_conn(call->conn);
379 break;
380
381 case RXRPC_CONN_SERVICE:
382 write_lock(&call->state_lock);
383 if (rx->discard_new_call)
384 call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
385 else
386 call->state = RXRPC_CALL_SERVER_ACCEPTING;
387 write_unlock(&call->state_lock);
388 break;
389
390 case RXRPC_CONN_REMOTELY_ABORTED:
391 rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
392 conn->remote_abort, ECONNABORTED);
393 break;
394 case RXRPC_CONN_LOCALLY_ABORTED:
395 rxrpc_abort_call("CON", call, sp->hdr.seq,
396 conn->local_abort, ECONNABORTED);
397 break;
David Howells17926a72007-04-26 15:48:28 -0700398 default:
399 BUG();
400 }
David Howells248f2192016-09-08 11:10:12 +0100401 spin_unlock(&conn->state_lock);
David Howells17926a72007-04-26 15:48:28 -0700402
David Howells248f2192016-09-08 11:10:12 +0100403 if (call->state == RXRPC_CALL_SERVER_ACCEPTING)
404 rxrpc_notify_socket(call);
David Howells17926a72007-04-26 15:48:28 -0700405
David Howells3432a752016-09-13 09:05:14 +0100406 /* We have to discard the prealloc queue's ref here and rely on a
407 * combination of the RCU read lock and refs held either by the socket
408 * (recvmsg queue, to-be-accepted queue or user ID tree) or the kernel
409 * service to prevent the call from being deallocated too early.
410 */
411 rxrpc_put_call(call, rxrpc_call_put);
412
David Howells248f2192016-09-08 11:10:12 +0100413 _leave(" = %p{%d}", call, call->debug_id);
414out:
415 spin_unlock(&rx->incoming_lock);
416 return call;
David Howells17926a72007-04-26 15:48:28 -0700417}
418
419/*
420 * handle acceptance of a call by userspace
421 * - assign the user call ID to the call at the front of the queue
422 */
David Howells651350d2007-04-26 15:50:17 -0700423struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *rx,
David Howellsd0016482016-08-30 20:42:14 +0100424 unsigned long user_call_ID,
425 rxrpc_notify_rx_t notify_rx)
David Howells17926a72007-04-26 15:48:28 -0700426{
427 struct rxrpc_call *call;
428 struct rb_node *parent, **pp;
429 int ret;
430
431 _enter(",%lx", user_call_ID);
432
433 ASSERT(!irqs_disabled());
434
435 write_lock(&rx->call_lock);
436
David Howellsb25de362016-09-13 22:36:22 +0100437 if (list_empty(&rx->to_be_accepted)) {
438 write_unlock(&rx->call_lock);
439 kleave(" = -ENODATA [empty]");
440 return ERR_PTR(-ENODATA);
441 }
David Howells17926a72007-04-26 15:48:28 -0700442
443 /* check the user ID isn't already in use */
David Howells17926a72007-04-26 15:48:28 -0700444 pp = &rx->calls.rb_node;
445 parent = NULL;
446 while (*pp) {
447 parent = *pp;
448 call = rb_entry(parent, struct rxrpc_call, sock_node);
449
450 if (user_call_ID < call->user_call_ID)
451 pp = &(*pp)->rb_left;
452 else if (user_call_ID > call->user_call_ID)
453 pp = &(*pp)->rb_right;
454 else
David Howells248f2192016-09-08 11:10:12 +0100455 goto id_in_use;
David Howells17926a72007-04-26 15:48:28 -0700456 }
457
David Howells248f2192016-09-08 11:10:12 +0100458 /* Dequeue the first call and check it's still valid. We gain
459 * responsibility for the queue's reference.
460 */
461 call = list_entry(rx->to_be_accepted.next,
462 struct rxrpc_call, accept_link);
David Howells17926a72007-04-26 15:48:28 -0700463 list_del_init(&call->accept_link);
464 sk_acceptq_removed(&rx->sk);
David Howellse34d4232016-08-30 09:49:29 +0100465 rxrpc_see_call(call);
David Howells17926a72007-04-26 15:48:28 -0700466
467 write_lock_bh(&call->state_lock);
468 switch (call->state) {
469 case RXRPC_CALL_SERVER_ACCEPTING:
470 call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
471 break;
David Howellsf5c17aa2016-08-30 09:49:28 +0100472 case RXRPC_CALL_COMPLETE:
473 ret = call->error;
David Howells17926a72007-04-26 15:48:28 -0700474 goto out_release;
David Howells17926a72007-04-26 15:48:28 -0700475 default:
476 BUG();
477 }
478
479 /* formalise the acceptance */
David Howellsd0016482016-08-30 20:42:14 +0100480 call->notify_rx = notify_rx;
David Howells17926a72007-04-26 15:48:28 -0700481 call->user_call_ID = user_call_ID;
David Howells248f2192016-09-08 11:10:12 +0100482 rxrpc_get_call(call, rxrpc_call_got_userid);
David Howells17926a72007-04-26 15:48:28 -0700483 rb_link_node(&call->sock_node, parent, pp);
484 rb_insert_color(&call->sock_node, &rx->calls);
485 if (test_and_set_bit(RXRPC_CALL_HAS_USERID, &call->flags))
486 BUG();
David Howells17926a72007-04-26 15:48:28 -0700487
488 write_unlock_bh(&call->state_lock);
489 write_unlock(&rx->call_lock);
David Howells248f2192016-09-08 11:10:12 +0100490 rxrpc_notify_socket(call);
491 rxrpc_service_prealloc(rx, GFP_KERNEL);
David Howells651350d2007-04-26 15:50:17 -0700492 _leave(" = %p{%d}", call, call->debug_id);
493 return call;
David Howells17926a72007-04-26 15:48:28 -0700494
David Howells17926a72007-04-26 15:48:28 -0700495out_release:
David Howells248f2192016-09-08 11:10:12 +0100496 _debug("release %p", call);
David Howells651350d2007-04-26 15:50:17 -0700497 write_unlock_bh(&call->state_lock);
David Howells8d94aa32016-09-07 09:19:31 +0100498 write_unlock(&rx->call_lock);
David Howells8d94aa32016-09-07 09:19:31 +0100499 rxrpc_release_call(rx, call);
David Howells248f2192016-09-08 11:10:12 +0100500 rxrpc_put_call(call, rxrpc_call_put);
501 goto out;
502
503id_in_use:
504 ret = -EBADSLT;
David Howells651350d2007-04-26 15:50:17 -0700505 write_unlock(&rx->call_lock);
David Howells248f2192016-09-08 11:10:12 +0100506out:
507 rxrpc_service_prealloc(rx, GFP_KERNEL);
David Howells651350d2007-04-26 15:50:17 -0700508 _leave(" = %d", ret);
509 return ERR_PTR(ret);
510}
511
512/*
David Howellsb4f13422016-03-04 15:56:19 +0000513 * Handle rejection of a call by userspace
David Howells651350d2007-04-26 15:50:17 -0700514 * - reject the call at the front of the queue
515 */
516int rxrpc_reject_call(struct rxrpc_sock *rx)
517{
518 struct rxrpc_call *call;
David Howells248f2192016-09-08 11:10:12 +0100519 bool abort = false;
David Howells651350d2007-04-26 15:50:17 -0700520 int ret;
521
522 _enter("");
523
524 ASSERT(!irqs_disabled());
525
526 write_lock(&rx->call_lock);
527
David Howells248f2192016-09-08 11:10:12 +0100528 if (list_empty(&rx->to_be_accepted)) {
David Howells8d94aa32016-09-07 09:19:31 +0100529 write_unlock(&rx->call_lock);
David Howells8d94aa32016-09-07 09:19:31 +0100530 return -ENODATA;
531 }
David Howells651350d2007-04-26 15:50:17 -0700532
David Howells248f2192016-09-08 11:10:12 +0100533 /* Dequeue the first call and check it's still valid. We gain
534 * responsibility for the queue's reference.
535 */
536 call = list_entry(rx->to_be_accepted.next,
537 struct rxrpc_call, accept_link);
David Howells651350d2007-04-26 15:50:17 -0700538 list_del_init(&call->accept_link);
539 sk_acceptq_removed(&rx->sk);
David Howellse34d4232016-08-30 09:49:29 +0100540 rxrpc_see_call(call);
David Howells651350d2007-04-26 15:50:17 -0700541
542 write_lock_bh(&call->state_lock);
543 switch (call->state) {
544 case RXRPC_CALL_SERVER_ACCEPTING:
David Howells248f2192016-09-08 11:10:12 +0100545 __rxrpc_abort_call("REJ", call, 1, RX_USER_ABORT, ECONNABORTED);
546 abort = true;
547 /* fall through */
David Howellsf5c17aa2016-08-30 09:49:28 +0100548 case RXRPC_CALL_COMPLETE:
549 ret = call->error;
David Howells248f2192016-09-08 11:10:12 +0100550 goto out_discard;
David Howells651350d2007-04-26 15:50:17 -0700551 default:
552 BUG();
553 }
554
David Howells248f2192016-09-08 11:10:12 +0100555out_discard:
David Howells17926a72007-04-26 15:48:28 -0700556 write_unlock_bh(&call->state_lock);
David Howells17926a72007-04-26 15:48:28 -0700557 write_unlock(&rx->call_lock);
David Howells248f2192016-09-08 11:10:12 +0100558 if (abort) {
559 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ABORT);
560 rxrpc_release_call(rx, call);
561 rxrpc_put_call(call, rxrpc_call_put);
562 }
563 rxrpc_service_prealloc(rx, GFP_KERNEL);
David Howells17926a72007-04-26 15:48:28 -0700564 _leave(" = %d", ret);
565 return ret;
566}
David Howells651350d2007-04-26 15:50:17 -0700567
David Howells00e90712016-09-08 11:10:12 +0100568/*
569 * rxrpc_kernel_charge_accept - Charge up socket with preallocated calls
570 * @sock: The socket on which to preallocate
571 * @notify_rx: Event notification function for the call
572 * @user_attach_call: Func to attach call to user_call_ID
573 * @user_call_ID: The tag to attach to the preallocated call
574 * @gfp: The allocation conditions.
575 *
576 * Charge up the socket with preallocated calls, each with a user ID. A
577 * function should be provided to effect the attachment from the user's side.
578 * The user is given a ref to hold on the call.
579 *
580 * Note that the call may be come connected before this function returns.
581 */
582int rxrpc_kernel_charge_accept(struct socket *sock,
583 rxrpc_notify_rx_t notify_rx,
584 rxrpc_user_attach_call_t user_attach_call,
585 unsigned long user_call_ID, gfp_t gfp)
586{
587 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
588 struct rxrpc_backlog *b = rx->backlog;
589
590 if (sock->sk->sk_state == RXRPC_CLOSE)
591 return -ESHUTDOWN;
592
593 return rxrpc_service_prealloc_one(rx, b, notify_rx,
594 user_attach_call, user_call_ID,
595 gfp);
596}
597EXPORT_SYMBOL(rxrpc_kernel_charge_accept);