blob: 9b3b48abe12ff86ff3487d473dec8731a40fd3dc [file] [log] [blame]
David Howells17926a72007-04-26 15:48:28 -07001/* RxRPC individual remote procedure 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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
David Howells17926a72007-04-26 15:48:28 -070015#include <linux/module.h>
16#include <linux/circ_buf.h>
Tim Smith77276402014-03-03 23:04:45 +000017#include <linux/hashtable.h>
18#include <linux/spinlock_types.h>
David Howells17926a72007-04-26 15:48:28 -070019#include <net/sock.h>
20#include <net/af_rxrpc.h>
21#include "ar-internal.h"
22
David Howells5873c082014-02-07 18:58:44 +000023/*
24 * Maximum lifetime of a call (in jiffies).
25 */
David Howellsdad8aff2016-03-09 23:22:56 +000026unsigned int rxrpc_max_call_lifetime = 60 * HZ;
David Howells5873c082014-02-07 18:58:44 +000027
28/*
29 * Time till dead call expires after last use (in jiffies).
30 */
David Howellsdad8aff2016-03-09 23:22:56 +000031unsigned int rxrpc_dead_call_expiry = 2 * HZ;
David Howells5873c082014-02-07 18:58:44 +000032
David Howells5b8848d2016-03-04 15:53:46 +000033const char *const rxrpc_call_states[NR__RXRPC_CALL_STATES] = {
David Howells999b69f2016-06-17 15:42:35 +010034 [RXRPC_CALL_UNINITIALISED] = "Uninit",
35 [RXRPC_CALL_CLIENT_AWAIT_CONN] = "ClWtConn",
David Howells1f8481d2007-05-22 16:14:24 -070036 [RXRPC_CALL_CLIENT_SEND_REQUEST] = "ClSndReq",
37 [RXRPC_CALL_CLIENT_AWAIT_REPLY] = "ClAwtRpl",
38 [RXRPC_CALL_CLIENT_RECV_REPLY] = "ClRcvRpl",
39 [RXRPC_CALL_CLIENT_FINAL_ACK] = "ClFnlACK",
40 [RXRPC_CALL_SERVER_SECURING] = "SvSecure",
41 [RXRPC_CALL_SERVER_ACCEPTING] = "SvAccept",
42 [RXRPC_CALL_SERVER_RECV_REQUEST] = "SvRcvReq",
43 [RXRPC_CALL_SERVER_ACK_REQUEST] = "SvAckReq",
44 [RXRPC_CALL_SERVER_SEND_REPLY] = "SvSndRpl",
45 [RXRPC_CALL_SERVER_AWAIT_ACK] = "SvAwtACK",
46 [RXRPC_CALL_COMPLETE] = "Complete",
47 [RXRPC_CALL_SERVER_BUSY] = "SvBusy ",
48 [RXRPC_CALL_REMOTELY_ABORTED] = "RmtAbort",
49 [RXRPC_CALL_LOCALLY_ABORTED] = "LocAbort",
50 [RXRPC_CALL_NETWORK_ERROR] = "NetError",
51 [RXRPC_CALL_DEAD] = "Dead ",
52};
53
David Howells17926a72007-04-26 15:48:28 -070054struct kmem_cache *rxrpc_call_jar;
55LIST_HEAD(rxrpc_calls);
56DEFINE_RWLOCK(rxrpc_call_lock);
David Howells17926a72007-04-26 15:48:28 -070057
58static void rxrpc_destroy_call(struct work_struct *work);
59static void rxrpc_call_life_expired(unsigned long _call);
60static void rxrpc_dead_call_expired(unsigned long _call);
61static void rxrpc_ack_time_expired(unsigned long _call);
62static void rxrpc_resend_time_expired(unsigned long _call);
63
Tim Smith77276402014-03-03 23:04:45 +000064static DEFINE_SPINLOCK(rxrpc_call_hash_lock);
65static DEFINE_HASHTABLE(rxrpc_call_hash, 10);
66
67/*
68 * Hash function for rxrpc_call_hash
69 */
70static unsigned long rxrpc_call_hashfunc(
David Howells0d12f8a2016-03-04 15:53:46 +000071 u8 in_clientflag,
72 u32 cid,
73 u32 call_id,
74 u32 epoch,
75 u16 service_id,
David Howells19ffa012016-04-04 14:00:36 +010076 sa_family_t family,
Tim Smith77276402014-03-03 23:04:45 +000077 void *localptr,
78 unsigned int addr_size,
79 const u8 *peer_addr)
80{
81 const u16 *p;
82 unsigned int i;
83 unsigned long key;
Tim Smith77276402014-03-03 23:04:45 +000084
85 _enter("");
86
87 key = (unsigned long)localptr;
88 /* We just want to add up the __be32 values, so forcing the
89 * cast should be okay.
90 */
David Howells0d12f8a2016-03-04 15:53:46 +000091 key += epoch;
92 key += service_id;
93 key += call_id;
94 key += (cid & RXRPC_CIDMASK) >> RXRPC_CIDSHIFT;
95 key += cid & RXRPC_CHANNELMASK;
96 key += in_clientflag;
David Howells19ffa012016-04-04 14:00:36 +010097 key += family;
Tim Smith77276402014-03-03 23:04:45 +000098 /* Step through the peer address in 16-bit portions for speed */
99 for (i = 0, p = (const u16 *)peer_addr; i < addr_size >> 1; i++, p++)
100 key += *p;
101 _leave(" key = 0x%lx", key);
102 return key;
103}
104
105/*
106 * Add a call to the hashtable
107 */
108static void rxrpc_call_hash_add(struct rxrpc_call *call)
109{
110 unsigned long key;
111 unsigned int addr_size = 0;
112
113 _enter("");
David Howells19ffa012016-04-04 14:00:36 +0100114 switch (call->family) {
Tim Smith77276402014-03-03 23:04:45 +0000115 case AF_INET:
116 addr_size = sizeof(call->peer_ip.ipv4_addr);
117 break;
118 case AF_INET6:
119 addr_size = sizeof(call->peer_ip.ipv6_addr);
120 break;
121 default:
122 break;
123 }
124 key = rxrpc_call_hashfunc(call->in_clientflag, call->cid,
125 call->call_id, call->epoch,
David Howells19ffa012016-04-04 14:00:36 +0100126 call->service_id, call->family,
David Howells85f32272016-04-04 14:00:36 +0100127 call->conn->params.local, addr_size,
Tim Smith77276402014-03-03 23:04:45 +0000128 call->peer_ip.ipv6_addr);
129 /* Store the full key in the call */
130 call->hash_key = key;
131 spin_lock(&rxrpc_call_hash_lock);
132 hash_add_rcu(rxrpc_call_hash, &call->hash_node, key);
133 spin_unlock(&rxrpc_call_hash_lock);
134 _leave("");
135}
136
137/*
138 * Remove a call from the hashtable
139 */
140static void rxrpc_call_hash_del(struct rxrpc_call *call)
141{
142 _enter("");
143 spin_lock(&rxrpc_call_hash_lock);
144 hash_del_rcu(&call->hash_node);
145 spin_unlock(&rxrpc_call_hash_lock);
146 _leave("");
147}
148
149/*
150 * Find a call in the hashtable and return it, or NULL if it
151 * isn't there.
152 */
153struct rxrpc_call *rxrpc_find_call_hash(
David Howells0d12f8a2016-03-04 15:53:46 +0000154 struct rxrpc_host_header *hdr,
Tim Smith77276402014-03-03 23:04:45 +0000155 void *localptr,
David Howells19ffa012016-04-04 14:00:36 +0100156 sa_family_t family,
David Howells0d12f8a2016-03-04 15:53:46 +0000157 const void *peer_addr)
Tim Smith77276402014-03-03 23:04:45 +0000158{
159 unsigned long key;
160 unsigned int addr_size = 0;
161 struct rxrpc_call *call = NULL;
162 struct rxrpc_call *ret = NULL;
David Howells0d12f8a2016-03-04 15:53:46 +0000163 u8 in_clientflag = hdr->flags & RXRPC_CLIENT_INITIATED;
Tim Smith77276402014-03-03 23:04:45 +0000164
165 _enter("");
David Howells19ffa012016-04-04 14:00:36 +0100166 switch (family) {
Tim Smith77276402014-03-03 23:04:45 +0000167 case AF_INET:
168 addr_size = sizeof(call->peer_ip.ipv4_addr);
169 break;
170 case AF_INET6:
171 addr_size = sizeof(call->peer_ip.ipv6_addr);
172 break;
173 default:
174 break;
175 }
176
David Howells0d12f8a2016-03-04 15:53:46 +0000177 key = rxrpc_call_hashfunc(in_clientflag, hdr->cid, hdr->callNumber,
178 hdr->epoch, hdr->serviceId,
David Howells19ffa012016-04-04 14:00:36 +0100179 family, localptr, addr_size,
Tim Smith77276402014-03-03 23:04:45 +0000180 peer_addr);
181 hash_for_each_possible_rcu(rxrpc_call_hash, call, hash_node, key) {
182 if (call->hash_key == key &&
David Howells0d12f8a2016-03-04 15:53:46 +0000183 call->call_id == hdr->callNumber &&
184 call->cid == hdr->cid &&
185 call->in_clientflag == in_clientflag &&
186 call->service_id == hdr->serviceId &&
David Howells19ffa012016-04-04 14:00:36 +0100187 call->family == family &&
Tim Smith77276402014-03-03 23:04:45 +0000188 call->local == localptr &&
189 memcmp(call->peer_ip.ipv6_addr, peer_addr,
David Howells0d12f8a2016-03-04 15:53:46 +0000190 addr_size) == 0 &&
191 call->epoch == hdr->epoch) {
Tim Smith77276402014-03-03 23:04:45 +0000192 ret = call;
193 break;
194 }
195 }
196 _leave(" = %p", ret);
197 return ret;
198}
199
David Howells17926a72007-04-26 15:48:28 -0700200/*
David Howells2341e072016-06-09 23:02:51 +0100201 * find an extant server call
202 * - called in process context with IRQs enabled
203 */
204struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx,
205 unsigned long user_call_ID)
206{
207 struct rxrpc_call *call;
208 struct rb_node *p;
209
210 _enter("%p,%lx", rx, user_call_ID);
211
212 read_lock(&rx->call_lock);
213
214 p = rx->calls.rb_node;
215 while (p) {
216 call = rb_entry(p, struct rxrpc_call, sock_node);
217
218 if (user_call_ID < call->user_call_ID)
219 p = p->rb_left;
220 else if (user_call_ID > call->user_call_ID)
221 p = p->rb_right;
222 else
223 goto found_extant_call;
224 }
225
226 read_unlock(&rx->call_lock);
227 _leave(" = NULL");
228 return NULL;
229
230found_extant_call:
231 rxrpc_get_call(call);
232 read_unlock(&rx->call_lock);
233 _leave(" = %p [%d]", call, atomic_read(&call->usage));
234 return call;
235}
236
237/*
David Howells17926a72007-04-26 15:48:28 -0700238 * allocate a new call
239 */
240static struct rxrpc_call *rxrpc_alloc_call(gfp_t gfp)
241{
242 struct rxrpc_call *call;
243
244 call = kmem_cache_zalloc(rxrpc_call_jar, gfp);
245 if (!call)
246 return NULL;
247
248 call->acks_winsz = 16;
249 call->acks_window = kmalloc(call->acks_winsz * sizeof(unsigned long),
250 gfp);
251 if (!call->acks_window) {
252 kmem_cache_free(rxrpc_call_jar, call);
253 return NULL;
254 }
255
256 setup_timer(&call->lifetimer, &rxrpc_call_life_expired,
257 (unsigned long) call);
258 setup_timer(&call->deadspan, &rxrpc_dead_call_expired,
259 (unsigned long) call);
260 setup_timer(&call->ack_timer, &rxrpc_ack_time_expired,
261 (unsigned long) call);
262 setup_timer(&call->resend_timer, &rxrpc_resend_time_expired,
263 (unsigned long) call);
264 INIT_WORK(&call->destroyer, &rxrpc_destroy_call);
265 INIT_WORK(&call->processor, &rxrpc_process_call);
David Howells999b69f2016-06-17 15:42:35 +0100266 INIT_LIST_HEAD(&call->link);
David Howells17926a72007-04-26 15:48:28 -0700267 INIT_LIST_HEAD(&call->accept_link);
268 skb_queue_head_init(&call->rx_queue);
269 skb_queue_head_init(&call->rx_oos_queue);
270 init_waitqueue_head(&call->tx_waitq);
271 spin_lock_init(&call->lock);
272 rwlock_init(&call->state_lock);
273 atomic_set(&call->usage, 1);
274 call->debug_id = atomic_inc_return(&rxrpc_debug_id);
David Howells17926a72007-04-26 15:48:28 -0700275
276 memset(&call->sock_node, 0xed, sizeof(call->sock_node));
277
278 call->rx_data_expect = 1;
279 call->rx_data_eaten = 0;
280 call->rx_first_oos = 0;
David Howells817913d2014-02-07 18:10:30 +0000281 call->ackr_win_top = call->rx_data_eaten + 1 + rxrpc_rx_window_size;
David Howells17926a72007-04-26 15:48:28 -0700282 call->creation_jif = jiffies;
283 return call;
284}
285
286/*
David Howells999b69f2016-06-17 15:42:35 +0100287 * Allocate a new client call.
David Howells17926a72007-04-26 15:48:28 -0700288 */
289static struct rxrpc_call *rxrpc_alloc_client_call(
290 struct rxrpc_sock *rx,
David Howells19ffa012016-04-04 14:00:36 +0100291 struct rxrpc_conn_parameters *cp,
David Howells999b69f2016-06-17 15:42:35 +0100292 struct sockaddr_rxrpc *srx,
David Howells17926a72007-04-26 15:48:28 -0700293 gfp_t gfp)
294{
295 struct rxrpc_call *call;
David Howells17926a72007-04-26 15:48:28 -0700296
297 _enter("");
298
David Howells999b69f2016-06-17 15:42:35 +0100299 ASSERT(rx->local != NULL);
David Howells17926a72007-04-26 15:48:28 -0700300
301 call = rxrpc_alloc_call(gfp);
302 if (!call)
303 return ERR_PTR(-ENOMEM);
David Howells999b69f2016-06-17 15:42:35 +0100304 call->state = RXRPC_CALL_CLIENT_AWAIT_CONN;
David Howells17926a72007-04-26 15:48:28 -0700305
306 sock_hold(&rx->sk);
307 call->socket = rx;
308 call->rx_data_post = 1;
309
Tim Smith77276402014-03-03 23:04:45 +0000310 /* Record copies of information for hashtable lookup */
David Howells19ffa012016-04-04 14:00:36 +0100311 call->family = rx->family;
David Howells999b69f2016-06-17 15:42:35 +0100312 call->local = rx->local;
David Howells19ffa012016-04-04 14:00:36 +0100313 switch (call->family) {
Tim Smith77276402014-03-03 23:04:45 +0000314 case AF_INET:
David Howells999b69f2016-06-17 15:42:35 +0100315 call->peer_ip.ipv4_addr = srx->transport.sin.sin_addr.s_addr;
Tim Smith77276402014-03-03 23:04:45 +0000316 break;
317 case AF_INET6:
318 memcpy(call->peer_ip.ipv6_addr,
David Howells999b69f2016-06-17 15:42:35 +0100319 srx->transport.sin6.sin6_addr.in6_u.u6_addr8,
Tim Smith77276402014-03-03 23:04:45 +0000320 sizeof(call->peer_ip.ipv6_addr));
321 break;
322 }
David Howells999b69f2016-06-17 15:42:35 +0100323
324 call->service_id = srx->srx_service;
325 call->in_clientflag = 0;
326
327 _leave(" = %p", call);
328 return call;
329}
330
331/*
332 * Begin client call.
333 */
334static int rxrpc_begin_client_call(struct rxrpc_call *call,
335 struct rxrpc_conn_parameters *cp,
336 struct rxrpc_transport *trans,
337 struct sockaddr_rxrpc *srx,
338 gfp_t gfp)
339{
340 int ret;
341
342 /* Set up or get a connection record and set the protocol parameters,
343 * including channel number and call ID.
344 */
345 ret = rxrpc_connect_call(call, cp, trans, srx, gfp);
346 if (ret < 0)
347 return ret;
348
349 call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
350
Tim Smith77276402014-03-03 23:04:45 +0000351 /* Add the new call to the hashtable */
352 rxrpc_call_hash_add(call);
353
David Howells85f32272016-04-04 14:00:36 +0100354 spin_lock(&call->conn->params.peer->lock);
355 hlist_add_head(&call->error_link, &call->conn->params.peer->error_targets);
356 spin_unlock(&call->conn->params.peer->lock);
David Howells17926a72007-04-26 15:48:28 -0700357
David Howells5873c082014-02-07 18:58:44 +0000358 call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
David Howells17926a72007-04-26 15:48:28 -0700359 add_timer(&call->lifetimer);
David Howells999b69f2016-06-17 15:42:35 +0100360 return 0;
David Howells17926a72007-04-26 15:48:28 -0700361}
362
363/*
364 * set up a call for the given data
365 * - called in process context with IRQs enabled
366 */
David Howells2341e072016-06-09 23:02:51 +0100367struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
David Howells19ffa012016-04-04 14:00:36 +0100368 struct rxrpc_conn_parameters *cp,
David Howells17926a72007-04-26 15:48:28 -0700369 struct rxrpc_transport *trans,
David Howells999b69f2016-06-17 15:42:35 +0100370 struct sockaddr_rxrpc *srx,
David Howells17926a72007-04-26 15:48:28 -0700371 unsigned long user_call_ID,
David Howells17926a72007-04-26 15:48:28 -0700372 gfp_t gfp)
373{
David Howells2341e072016-06-09 23:02:51 +0100374 struct rxrpc_call *call, *xcall;
375 struct rb_node *parent, **pp;
David Howells999b69f2016-06-17 15:42:35 +0100376 int ret;
David Howells17926a72007-04-26 15:48:28 -0700377
David Howells999b69f2016-06-17 15:42:35 +0100378 _enter("%p,%lx", rx, user_call_ID);
David Howells17926a72007-04-26 15:48:28 -0700379
David Howells999b69f2016-06-17 15:42:35 +0100380 call = rxrpc_alloc_client_call(rx, cp, srx, gfp);
David Howells2341e072016-06-09 23:02:51 +0100381 if (IS_ERR(call)) {
382 _leave(" = %ld", PTR_ERR(call));
383 return call;
David Howells17926a72007-04-26 15:48:28 -0700384 }
385
David Howells999b69f2016-06-17 15:42:35 +0100386 /* Publish the call, even though it is incompletely set up as yet */
David Howells2341e072016-06-09 23:02:51 +0100387 call->user_call_ID = user_call_ID;
388 __set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
David Howells17926a72007-04-26 15:48:28 -0700389
390 write_lock(&rx->call_lock);
391
392 pp = &rx->calls.rb_node;
393 parent = NULL;
394 while (*pp) {
395 parent = *pp;
David Howells2341e072016-06-09 23:02:51 +0100396 xcall = rb_entry(parent, struct rxrpc_call, sock_node);
David Howells17926a72007-04-26 15:48:28 -0700397
David Howells2341e072016-06-09 23:02:51 +0100398 if (user_call_ID < xcall->user_call_ID)
David Howells17926a72007-04-26 15:48:28 -0700399 pp = &(*pp)->rb_left;
David Howells2341e072016-06-09 23:02:51 +0100400 else if (user_call_ID > xcall->user_call_ID)
David Howells17926a72007-04-26 15:48:28 -0700401 pp = &(*pp)->rb_right;
402 else
David Howells2341e072016-06-09 23:02:51 +0100403 goto found_user_ID_now_present;
David Howells17926a72007-04-26 15:48:28 -0700404 }
405
David Howells17926a72007-04-26 15:48:28 -0700406 rxrpc_get_call(call);
407
408 rb_link_node(&call->sock_node, parent, pp);
409 rb_insert_color(&call->sock_node, &rx->calls);
410 write_unlock(&rx->call_lock);
411
412 write_lock_bh(&rxrpc_call_lock);
413 list_add_tail(&call->link, &rxrpc_calls);
414 write_unlock_bh(&rxrpc_call_lock);
415
David Howells999b69f2016-06-17 15:42:35 +0100416 ret = rxrpc_begin_client_call(call, cp, trans, srx, gfp);
417 if (ret < 0)
418 goto error;
419
David Howells17926a72007-04-26 15:48:28 -0700420 _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
421
422 _leave(" = %p [new]", call);
423 return call;
424
David Howells999b69f2016-06-17 15:42:35 +0100425error:
426 write_lock(&rx->call_lock);
427 rb_erase(&call->sock_node, &rx->calls);
428 write_unlock(&rx->call_lock);
429 rxrpc_put_call(call);
430
431 write_lock_bh(&rxrpc_call_lock);
432 list_del(&call->link);
433 write_unlock_bh(&rxrpc_call_lock);
434
435 rxrpc_put_call(call);
436 _leave(" = %d", ret);
437 return ERR_PTR(ret);
438
David Howells2341e072016-06-09 23:02:51 +0100439 /* We unexpectedly found the user ID in the list after taking
440 * the call_lock. This shouldn't happen unless the user races
441 * with itself and tries to add the same user ID twice at the
442 * same time in different threads.
443 */
444found_user_ID_now_present:
David Howells17926a72007-04-26 15:48:28 -0700445 write_unlock(&rx->call_lock);
David Howells2341e072016-06-09 23:02:51 +0100446 rxrpc_put_call(call);
447 _leave(" = -EEXIST [%p]", call);
448 return ERR_PTR(-EEXIST);
David Howells17926a72007-04-26 15:48:28 -0700449}
450
451/*
452 * set up an incoming call
453 * - called in process context with IRQs enabled
454 */
455struct rxrpc_call *rxrpc_incoming_call(struct rxrpc_sock *rx,
456 struct rxrpc_connection *conn,
David Howells42886ff2016-06-16 13:31:07 +0100457 struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700458{
David Howells42886ff2016-06-16 13:31:07 +0100459 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700460 struct rxrpc_call *call, *candidate;
461 struct rb_node **p, *parent;
David Howells0d12f8a2016-03-04 15:53:46 +0000462 u32 call_id;
David Howells17926a72007-04-26 15:48:28 -0700463
David Howells843099c2016-04-07 17:23:37 +0100464 _enter(",%d", conn->debug_id);
David Howells17926a72007-04-26 15:48:28 -0700465
466 ASSERT(rx != NULL);
467
David Howells843099c2016-04-07 17:23:37 +0100468 candidate = rxrpc_alloc_call(GFP_NOIO);
David Howells17926a72007-04-26 15:48:28 -0700469 if (!candidate)
470 return ERR_PTR(-EBUSY);
471
David Howells42886ff2016-06-16 13:31:07 +0100472 candidate->socket = rx;
473 candidate->conn = conn;
474 candidate->cid = sp->hdr.cid;
475 candidate->call_id = sp->hdr.callNumber;
476 candidate->channel = sp->hdr.cid & RXRPC_CHANNELMASK;
477 candidate->rx_data_post = 0;
478 candidate->state = RXRPC_CALL_SERVER_ACCEPTING;
David Howells17926a72007-04-26 15:48:28 -0700479 if (conn->security_ix > 0)
480 candidate->state = RXRPC_CALL_SERVER_SECURING;
481
482 write_lock_bh(&conn->lock);
483
484 /* set the channel for this call */
485 call = conn->channels[candidate->channel];
486 _debug("channel[%u] is %p", candidate->channel, call);
David Howells42886ff2016-06-16 13:31:07 +0100487 if (call && call->call_id == sp->hdr.callNumber) {
David Howells17926a72007-04-26 15:48:28 -0700488 /* already set; must've been a duplicate packet */
489 _debug("extant call [%d]", call->state);
490 ASSERTCMP(call->conn, ==, conn);
491
492 read_lock(&call->state_lock);
493 switch (call->state) {
494 case RXRPC_CALL_LOCALLY_ABORTED:
David Howells4c198ad2016-03-04 15:53:46 +0000495 if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700496 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700497 case RXRPC_CALL_REMOTELY_ABORTED:
498 read_unlock(&call->state_lock);
499 goto aborted_call;
500 default:
501 rxrpc_get_call(call);
502 read_unlock(&call->state_lock);
503 goto extant_call;
504 }
505 }
506
507 if (call) {
508 /* it seems the channel is still in use from the previous call
509 * - ditch the old binding if its call is now complete */
510 _debug("CALL: %u { %s }",
511 call->debug_id, rxrpc_call_states[call->state]);
512
513 if (call->state >= RXRPC_CALL_COMPLETE) {
514 conn->channels[call->channel] = NULL;
515 } else {
516 write_unlock_bh(&conn->lock);
517 kmem_cache_free(rxrpc_call_jar, candidate);
518 _leave(" = -EBUSY");
519 return ERR_PTR(-EBUSY);
520 }
521 }
522
523 /* check the call number isn't duplicate */
524 _debug("check dup");
David Howells42886ff2016-06-16 13:31:07 +0100525 call_id = sp->hdr.callNumber;
David Howells17926a72007-04-26 15:48:28 -0700526 p = &conn->calls.rb_node;
527 parent = NULL;
528 while (*p) {
529 parent = *p;
530 call = rb_entry(parent, struct rxrpc_call, conn_node);
531
Tim Smith77276402014-03-03 23:04:45 +0000532 /* The tree is sorted in order of the __be32 value without
533 * turning it into host order.
534 */
David Howells0d12f8a2016-03-04 15:53:46 +0000535 if (call_id < call->call_id)
David Howells17926a72007-04-26 15:48:28 -0700536 p = &(*p)->rb_left;
David Howells0d12f8a2016-03-04 15:53:46 +0000537 else if (call_id > call->call_id)
David Howells17926a72007-04-26 15:48:28 -0700538 p = &(*p)->rb_right;
539 else
540 goto old_call;
541 }
542
543 /* make the call available */
544 _debug("new call");
545 call = candidate;
546 candidate = NULL;
547 rb_link_node(&call->conn_node, parent, p);
548 rb_insert_color(&call->conn_node, &conn->calls);
549 conn->channels[call->channel] = call;
550 sock_hold(&rx->sk);
David Howells5627cc82016-04-04 14:00:38 +0100551 rxrpc_get_connection(conn);
David Howells17926a72007-04-26 15:48:28 -0700552 write_unlock_bh(&conn->lock);
553
David Howells85f32272016-04-04 14:00:36 +0100554 spin_lock(&conn->params.peer->lock);
555 hlist_add_head(&call->error_link, &conn->params.peer->error_targets);
556 spin_unlock(&conn->params.peer->lock);
David Howells17926a72007-04-26 15:48:28 -0700557
558 write_lock_bh(&rxrpc_call_lock);
559 list_add_tail(&call->link, &rxrpc_calls);
560 write_unlock_bh(&rxrpc_call_lock);
561
Tim Smith77276402014-03-03 23:04:45 +0000562 /* Record copies of information for hashtable lookup */
David Howells19ffa012016-04-04 14:00:36 +0100563 call->family = rx->family;
David Howells85f32272016-04-04 14:00:36 +0100564 call->local = conn->params.local;
David Howells19ffa012016-04-04 14:00:36 +0100565 switch (call->family) {
Tim Smith77276402014-03-03 23:04:45 +0000566 case AF_INET:
567 call->peer_ip.ipv4_addr =
David Howells85f32272016-04-04 14:00:36 +0100568 conn->params.peer->srx.transport.sin.sin_addr.s_addr;
Tim Smith77276402014-03-03 23:04:45 +0000569 break;
570 case AF_INET6:
571 memcpy(call->peer_ip.ipv6_addr,
David Howells85f32272016-04-04 14:00:36 +0100572 conn->params.peer->srx.transport.sin6.sin6_addr.in6_u.u6_addr8,
Tim Smith77276402014-03-03 23:04:45 +0000573 sizeof(call->peer_ip.ipv6_addr));
574 break;
575 default:
576 break;
577 }
David Howells19ffa012016-04-04 14:00:36 +0100578 call->epoch = conn->proto.epoch;
579 call->service_id = conn->params.service_id;
580 call->in_clientflag = conn->proto.in_clientflag;
Tim Smith77276402014-03-03 23:04:45 +0000581 /* Add the new call to the hashtable */
582 rxrpc_call_hash_add(call);
583
David Howells17926a72007-04-26 15:48:28 -0700584 _net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
585
David Howells5873c082014-02-07 18:58:44 +0000586 call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
David Howells17926a72007-04-26 15:48:28 -0700587 add_timer(&call->lifetimer);
588 _leave(" = %p {%d} [new]", call, call->debug_id);
589 return call;
590
591extant_call:
592 write_unlock_bh(&conn->lock);
593 kmem_cache_free(rxrpc_call_jar, candidate);
594 _leave(" = %p {%d} [extant]", call, call ? call->debug_id : -1);
595 return call;
596
597aborted_call:
598 write_unlock_bh(&conn->lock);
599 kmem_cache_free(rxrpc_call_jar, candidate);
600 _leave(" = -ECONNABORTED");
601 return ERR_PTR(-ECONNABORTED);
602
603old_call:
604 write_unlock_bh(&conn->lock);
605 kmem_cache_free(rxrpc_call_jar, candidate);
606 _leave(" = -ECONNRESET [old]");
607 return ERR_PTR(-ECONNRESET);
608}
609
610/*
David Howells17926a72007-04-26 15:48:28 -0700611 * detach a call from a socket and set up for release
612 */
613void rxrpc_release_call(struct rxrpc_call *call)
614{
David Howells651350d2007-04-26 15:50:17 -0700615 struct rxrpc_connection *conn = call->conn;
David Howells17926a72007-04-26 15:48:28 -0700616 struct rxrpc_sock *rx = call->socket;
617
618 _enter("{%d,%d,%d,%d}",
619 call->debug_id, atomic_read(&call->usage),
620 atomic_read(&call->ackr_not_idle),
621 call->rx_first_oos);
622
623 spin_lock_bh(&call->lock);
624 if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
625 BUG();
626 spin_unlock_bh(&call->lock);
627
628 /* dissociate from the socket
629 * - the socket's ref on the call is passed to the death timer
630 */
David Howells651350d2007-04-26 15:50:17 -0700631 _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
David Howells17926a72007-04-26 15:48:28 -0700632
633 write_lock_bh(&rx->call_lock);
634 if (!list_empty(&call->accept_link)) {
635 _debug("unlinking once-pending call %p { e=%lx f=%lx }",
636 call, call->events, call->flags);
637 ASSERT(!test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
638 list_del_init(&call->accept_link);
639 sk_acceptq_removed(&rx->sk);
640 } else if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
641 rb_erase(&call->sock_node, &rx->calls);
642 memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
643 clear_bit(RXRPC_CALL_HAS_USERID, &call->flags);
644 }
645 write_unlock_bh(&rx->call_lock);
646
David Howells17926a72007-04-26 15:48:28 -0700647 /* free up the channel for reuse */
David Howells999b69f2016-06-17 15:42:35 +0100648 spin_lock(&conn->channel_lock);
David Howells651350d2007-04-26 15:50:17 -0700649 write_lock_bh(&conn->lock);
650 write_lock(&call->state_lock);
651
David Howells999b69f2016-06-17 15:42:35 +0100652 rxrpc_disconnect_call(call);
David Howells651350d2007-04-26 15:50:17 -0700653
David Howells999b69f2016-06-17 15:42:35 +0100654 spin_unlock(&conn->channel_lock);
David Howells17926a72007-04-26 15:48:28 -0700655
656 if (call->state < RXRPC_CALL_COMPLETE &&
657 call->state != RXRPC_CALL_CLIENT_FINAL_ACK) {
658 _debug("+++ ABORTING STATE %d +++\n", call->state);
659 call->state = RXRPC_CALL_LOCALLY_ABORTED;
David Howellsdc44b3a2016-04-07 17:23:30 +0100660 call->local_abort = RX_CALL_DEAD;
David Howells4c198ad2016-03-04 15:53:46 +0000661 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
David Howells651350d2007-04-26 15:50:17 -0700662 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700663 }
664 write_unlock(&call->state_lock);
David Howells651350d2007-04-26 15:50:17 -0700665 write_unlock_bh(&conn->lock);
David Howells17926a72007-04-26 15:48:28 -0700666
David Howells651350d2007-04-26 15:50:17 -0700667 /* clean up the Rx queue */
David Howells17926a72007-04-26 15:48:28 -0700668 if (!skb_queue_empty(&call->rx_queue) ||
669 !skb_queue_empty(&call->rx_oos_queue)) {
670 struct rxrpc_skb_priv *sp;
671 struct sk_buff *skb;
672
673 _debug("purge Rx queues");
674
675 spin_lock_bh(&call->lock);
676 while ((skb = skb_dequeue(&call->rx_queue)) ||
677 (skb = skb_dequeue(&call->rx_oos_queue))) {
678 sp = rxrpc_skb(skb);
679 if (sp->call) {
680 ASSERTCMP(sp->call, ==, call);
681 rxrpc_put_call(call);
682 sp->call = NULL;
683 }
684 skb->destructor = NULL;
685 spin_unlock_bh(&call->lock);
686
687 _debug("- zap %s %%%u #%u",
688 rxrpc_pkts[sp->hdr.type],
David Howells0d12f8a2016-03-04 15:53:46 +0000689 sp->hdr.serial, sp->hdr.seq);
David Howells17926a72007-04-26 15:48:28 -0700690 rxrpc_free_skb(skb);
691 spin_lock_bh(&call->lock);
692 }
693 spin_unlock_bh(&call->lock);
694
695 ASSERTCMP(call->state, !=, RXRPC_CALL_COMPLETE);
696 }
697
698 del_timer_sync(&call->resend_timer);
699 del_timer_sync(&call->ack_timer);
700 del_timer_sync(&call->lifetimer);
David Howells5873c082014-02-07 18:58:44 +0000701 call->deadspan.expires = jiffies + rxrpc_dead_call_expiry;
David Howells17926a72007-04-26 15:48:28 -0700702 add_timer(&call->deadspan);
703
704 _leave("");
705}
706
707/*
708 * handle a dead call being ready for reaping
709 */
710static void rxrpc_dead_call_expired(unsigned long _call)
711{
712 struct rxrpc_call *call = (struct rxrpc_call *) _call;
713
714 _enter("{%d}", call->debug_id);
715
716 write_lock_bh(&call->state_lock);
717 call->state = RXRPC_CALL_DEAD;
718 write_unlock_bh(&call->state_lock);
719 rxrpc_put_call(call);
720}
721
722/*
723 * mark a call as to be released, aborting it if it's still in progress
724 * - called with softirqs disabled
725 */
726static void rxrpc_mark_call_released(struct rxrpc_call *call)
727{
728 bool sched;
729
730 write_lock(&call->state_lock);
731 if (call->state < RXRPC_CALL_DEAD) {
732 sched = false;
733 if (call->state < RXRPC_CALL_COMPLETE) {
734 _debug("abort call %p", call);
735 call->state = RXRPC_CALL_LOCALLY_ABORTED;
David Howellsdc44b3a2016-04-07 17:23:30 +0100736 call->local_abort = RX_CALL_DEAD;
David Howells4c198ad2016-03-04 15:53:46 +0000737 if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events))
David Howells17926a72007-04-26 15:48:28 -0700738 sched = true;
739 }
David Howells4c198ad2016-03-04 15:53:46 +0000740 if (!test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
David Howells17926a72007-04-26 15:48:28 -0700741 sched = true;
742 if (sched)
David Howells651350d2007-04-26 15:50:17 -0700743 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700744 }
745 write_unlock(&call->state_lock);
746}
747
748/*
749 * release all the calls associated with a socket
750 */
751void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
752{
753 struct rxrpc_call *call;
754 struct rb_node *p;
755
756 _enter("%p", rx);
757
758 read_lock_bh(&rx->call_lock);
759
760 /* mark all the calls as no longer wanting incoming packets */
761 for (p = rb_first(&rx->calls); p; p = rb_next(p)) {
762 call = rb_entry(p, struct rxrpc_call, sock_node);
763 rxrpc_mark_call_released(call);
764 }
765
766 /* kill the not-yet-accepted incoming calls */
767 list_for_each_entry(call, &rx->secureq, accept_link) {
768 rxrpc_mark_call_released(call);
769 }
770
771 list_for_each_entry(call, &rx->acceptq, accept_link) {
772 rxrpc_mark_call_released(call);
773 }
774
775 read_unlock_bh(&rx->call_lock);
776 _leave("");
777}
778
779/*
780 * release a call
781 */
782void __rxrpc_put_call(struct rxrpc_call *call)
783{
784 ASSERT(call != NULL);
785
786 _enter("%p{u=%d}", call, atomic_read(&call->usage));
787
788 ASSERTCMP(atomic_read(&call->usage), >, 0);
789
790 if (atomic_dec_and_test(&call->usage)) {
791 _debug("call %d dead", call->debug_id);
792 ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
David Howells651350d2007-04-26 15:50:17 -0700793 rxrpc_queue_work(&call->destroyer);
David Howells17926a72007-04-26 15:48:28 -0700794 }
795 _leave("");
796}
797
798/*
799 * clean up a call
800 */
801static void rxrpc_cleanup_call(struct rxrpc_call *call)
802{
803 _net("DESTROY CALL %d", call->debug_id);
804
805 ASSERT(call->socket);
806
807 memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
808
809 del_timer_sync(&call->lifetimer);
810 del_timer_sync(&call->deadspan);
811 del_timer_sync(&call->ack_timer);
812 del_timer_sync(&call->resend_timer);
813
814 ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
815 ASSERTCMP(call->events, ==, 0);
816 if (work_pending(&call->processor)) {
817 _debug("defer destroy");
David Howells651350d2007-04-26 15:50:17 -0700818 rxrpc_queue_work(&call->destroyer);
David Howells17926a72007-04-26 15:48:28 -0700819 return;
820 }
821
822 if (call->conn) {
David Howells85f32272016-04-04 14:00:36 +0100823 spin_lock(&call->conn->params.peer->lock);
David Howellsf66d7492016-04-04 14:00:34 +0100824 hlist_del_init(&call->error_link);
David Howells85f32272016-04-04 14:00:36 +0100825 spin_unlock(&call->conn->params.peer->lock);
David Howells17926a72007-04-26 15:48:28 -0700826
827 write_lock_bh(&call->conn->lock);
828 rb_erase(&call->conn_node, &call->conn->calls);
829 write_unlock_bh(&call->conn->lock);
830 rxrpc_put_connection(call->conn);
831 }
832
Tim Smith77276402014-03-03 23:04:45 +0000833 /* Remove the call from the hash */
834 rxrpc_call_hash_del(call);
835
David Howells17926a72007-04-26 15:48:28 -0700836 if (call->acks_window) {
837 _debug("kill Tx window %d",
838 CIRC_CNT(call->acks_head, call->acks_tail,
839 call->acks_winsz));
840 smp_mb();
841 while (CIRC_CNT(call->acks_head, call->acks_tail,
842 call->acks_winsz) > 0) {
843 struct rxrpc_skb_priv *sp;
844 unsigned long _skb;
845
846 _skb = call->acks_window[call->acks_tail] & ~1;
David Howells0d12f8a2016-03-04 15:53:46 +0000847 sp = rxrpc_skb((struct sk_buff *)_skb);
848 _debug("+++ clear Tx %u", sp->hdr.seq);
849 rxrpc_free_skb((struct sk_buff *)_skb);
David Howells17926a72007-04-26 15:48:28 -0700850 call->acks_tail =
851 (call->acks_tail + 1) & (call->acks_winsz - 1);
852 }
853
854 kfree(call->acks_window);
855 }
856
857 rxrpc_free_skb(call->tx_pending);
858
859 rxrpc_purge_queue(&call->rx_queue);
860 ASSERT(skb_queue_empty(&call->rx_oos_queue));
861 sock_put(&call->socket->sk);
862 kmem_cache_free(rxrpc_call_jar, call);
863}
864
865/*
866 * destroy a call
867 */
868static void rxrpc_destroy_call(struct work_struct *work)
869{
870 struct rxrpc_call *call =
871 container_of(work, struct rxrpc_call, destroyer);
872
873 _enter("%p{%d,%d,%p}",
874 call, atomic_read(&call->usage), call->channel, call->conn);
875
876 ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
877
878 write_lock_bh(&rxrpc_call_lock);
879 list_del_init(&call->link);
880 write_unlock_bh(&rxrpc_call_lock);
881
882 rxrpc_cleanup_call(call);
883 _leave("");
884}
885
886/*
887 * preemptively destroy all the call records from a transport endpoint rather
888 * than waiting for them to time out
889 */
890void __exit rxrpc_destroy_all_calls(void)
891{
892 struct rxrpc_call *call;
893
894 _enter("");
895 write_lock_bh(&rxrpc_call_lock);
896
897 while (!list_empty(&rxrpc_calls)) {
898 call = list_entry(rxrpc_calls.next, struct rxrpc_call, link);
899 _debug("Zapping call %p", call);
900
901 list_del_init(&call->link);
902
903 switch (atomic_read(&call->usage)) {
904 case 0:
905 ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
906 break;
907 case 1:
908 if (del_timer_sync(&call->deadspan) != 0 &&
909 call->state != RXRPC_CALL_DEAD)
910 rxrpc_dead_call_expired((unsigned long) call);
911 if (call->state != RXRPC_CALL_DEAD)
912 break;
913 default:
Joe Perches9b6d5392016-06-02 12:08:52 -0700914 pr_err("Call %p still in use (%d,%d,%s,%lx,%lx)!\n",
David Howells17926a72007-04-26 15:48:28 -0700915 call, atomic_read(&call->usage),
916 atomic_read(&call->ackr_not_idle),
917 rxrpc_call_states[call->state],
918 call->flags, call->events);
919 if (!skb_queue_empty(&call->rx_queue))
Joe Perches9b6d5392016-06-02 12:08:52 -0700920 pr_err("Rx queue occupied\n");
David Howells17926a72007-04-26 15:48:28 -0700921 if (!skb_queue_empty(&call->rx_oos_queue))
Joe Perches9b6d5392016-06-02 12:08:52 -0700922 pr_err("OOS queue occupied\n");
David Howells17926a72007-04-26 15:48:28 -0700923 break;
924 }
925
926 write_unlock_bh(&rxrpc_call_lock);
927 cond_resched();
928 write_lock_bh(&rxrpc_call_lock);
929 }
930
931 write_unlock_bh(&rxrpc_call_lock);
932 _leave("");
933}
934
935/*
936 * handle call lifetime being exceeded
937 */
938static void rxrpc_call_life_expired(unsigned long _call)
939{
940 struct rxrpc_call *call = (struct rxrpc_call *) _call;
941
942 if (call->state >= RXRPC_CALL_COMPLETE)
943 return;
944
945 _enter("{%d}", call->debug_id);
946 read_lock_bh(&call->state_lock);
947 if (call->state < RXRPC_CALL_COMPLETE) {
David Howells4c198ad2016-03-04 15:53:46 +0000948 set_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events);
David Howells651350d2007-04-26 15:50:17 -0700949 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700950 }
951 read_unlock_bh(&call->state_lock);
952}
953
954/*
955 * handle resend timer expiry
David Howells3b5bac22010-08-04 02:34:17 +0000956 * - may not take call->state_lock as this can deadlock against del_timer_sync()
David Howells17926a72007-04-26 15:48:28 -0700957 */
958static void rxrpc_resend_time_expired(unsigned long _call)
959{
960 struct rxrpc_call *call = (struct rxrpc_call *) _call;
961
962 _enter("{%d}", call->debug_id);
963
964 if (call->state >= RXRPC_CALL_COMPLETE)
965 return;
966
David Howells17926a72007-04-26 15:48:28 -0700967 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
David Howells4c198ad2016-03-04 15:53:46 +0000968 if (!test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700969 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700970}
971
972/*
973 * handle ACK timer expiry
974 */
975static void rxrpc_ack_time_expired(unsigned long _call)
976{
977 struct rxrpc_call *call = (struct rxrpc_call *) _call;
978
979 _enter("{%d}", call->debug_id);
980
981 if (call->state >= RXRPC_CALL_COMPLETE)
982 return;
983
984 read_lock_bh(&call->state_lock);
985 if (call->state < RXRPC_CALL_COMPLETE &&
David Howells4c198ad2016-03-04 15:53:46 +0000986 !test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700987 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700988 read_unlock_bh(&call->state_lock);
989}