xref: /openbmc/linux/net/rxrpc/call_object.c (revision 999b69f8)
18c3e34a4SDavid Howells /* RxRPC individual remote procedure call handling
28c3e34a4SDavid Howells  *
38c3e34a4SDavid Howells  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
48c3e34a4SDavid Howells  * Written by David Howells (dhowells@redhat.com)
58c3e34a4SDavid Howells  *
68c3e34a4SDavid Howells  * This program is free software; you can redistribute it and/or
78c3e34a4SDavid Howells  * modify it under the terms of the GNU General Public License
88c3e34a4SDavid Howells  * as published by the Free Software Foundation; either version
98c3e34a4SDavid Howells  * 2 of the License, or (at your option) any later version.
108c3e34a4SDavid Howells  */
118c3e34a4SDavid Howells 
128c3e34a4SDavid Howells #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
138c3e34a4SDavid Howells 
148c3e34a4SDavid Howells #include <linux/slab.h>
158c3e34a4SDavid Howells #include <linux/module.h>
168c3e34a4SDavid Howells #include <linux/circ_buf.h>
178c3e34a4SDavid Howells #include <linux/hashtable.h>
188c3e34a4SDavid Howells #include <linux/spinlock_types.h>
198c3e34a4SDavid Howells #include <net/sock.h>
208c3e34a4SDavid Howells #include <net/af_rxrpc.h>
218c3e34a4SDavid Howells #include "ar-internal.h"
228c3e34a4SDavid Howells 
238c3e34a4SDavid Howells /*
248c3e34a4SDavid Howells  * Maximum lifetime of a call (in jiffies).
258c3e34a4SDavid Howells  */
268c3e34a4SDavid Howells unsigned int rxrpc_max_call_lifetime = 60 * HZ;
278c3e34a4SDavid Howells 
288c3e34a4SDavid Howells /*
298c3e34a4SDavid Howells  * Time till dead call expires after last use (in jiffies).
308c3e34a4SDavid Howells  */
318c3e34a4SDavid Howells unsigned int rxrpc_dead_call_expiry = 2 * HZ;
328c3e34a4SDavid Howells 
338c3e34a4SDavid Howells const char *const rxrpc_call_states[NR__RXRPC_CALL_STATES] = {
34999b69f8SDavid Howells 	[RXRPC_CALL_UNINITIALISED]		= "Uninit",
35999b69f8SDavid Howells 	[RXRPC_CALL_CLIENT_AWAIT_CONN]		= "ClWtConn",
368c3e34a4SDavid Howells 	[RXRPC_CALL_CLIENT_SEND_REQUEST]	= "ClSndReq",
378c3e34a4SDavid Howells 	[RXRPC_CALL_CLIENT_AWAIT_REPLY]		= "ClAwtRpl",
388c3e34a4SDavid Howells 	[RXRPC_CALL_CLIENT_RECV_REPLY]		= "ClRcvRpl",
398c3e34a4SDavid Howells 	[RXRPC_CALL_CLIENT_FINAL_ACK]		= "ClFnlACK",
408c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_SECURING]		= "SvSecure",
418c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_ACCEPTING]		= "SvAccept",
428c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_RECV_REQUEST]	= "SvRcvReq",
438c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_ACK_REQUEST]		= "SvAckReq",
448c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_SEND_REPLY]		= "SvSndRpl",
458c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_AWAIT_ACK]		= "SvAwtACK",
468c3e34a4SDavid Howells 	[RXRPC_CALL_COMPLETE]			= "Complete",
478c3e34a4SDavid Howells 	[RXRPC_CALL_SERVER_BUSY]		= "SvBusy  ",
488c3e34a4SDavid Howells 	[RXRPC_CALL_REMOTELY_ABORTED]		= "RmtAbort",
498c3e34a4SDavid Howells 	[RXRPC_CALL_LOCALLY_ABORTED]		= "LocAbort",
508c3e34a4SDavid Howells 	[RXRPC_CALL_NETWORK_ERROR]		= "NetError",
518c3e34a4SDavid Howells 	[RXRPC_CALL_DEAD]			= "Dead    ",
528c3e34a4SDavid Howells };
538c3e34a4SDavid Howells 
548c3e34a4SDavid Howells struct kmem_cache *rxrpc_call_jar;
558c3e34a4SDavid Howells LIST_HEAD(rxrpc_calls);
568c3e34a4SDavid Howells DEFINE_RWLOCK(rxrpc_call_lock);
578c3e34a4SDavid Howells 
588c3e34a4SDavid Howells static void rxrpc_destroy_call(struct work_struct *work);
598c3e34a4SDavid Howells static void rxrpc_call_life_expired(unsigned long _call);
608c3e34a4SDavid Howells static void rxrpc_dead_call_expired(unsigned long _call);
618c3e34a4SDavid Howells static void rxrpc_ack_time_expired(unsigned long _call);
628c3e34a4SDavid Howells static void rxrpc_resend_time_expired(unsigned long _call);
638c3e34a4SDavid Howells 
648c3e34a4SDavid Howells static DEFINE_SPINLOCK(rxrpc_call_hash_lock);
658c3e34a4SDavid Howells static DEFINE_HASHTABLE(rxrpc_call_hash, 10);
668c3e34a4SDavid Howells 
678c3e34a4SDavid Howells /*
688c3e34a4SDavid Howells  * Hash function for rxrpc_call_hash
698c3e34a4SDavid Howells  */
708c3e34a4SDavid Howells static unsigned long rxrpc_call_hashfunc(
718c3e34a4SDavid Howells 	u8		in_clientflag,
728c3e34a4SDavid Howells 	u32		cid,
738c3e34a4SDavid Howells 	u32		call_id,
748c3e34a4SDavid Howells 	u32		epoch,
758c3e34a4SDavid Howells 	u16		service_id,
7619ffa01cSDavid Howells 	sa_family_t	family,
778c3e34a4SDavid Howells 	void		*localptr,
788c3e34a4SDavid Howells 	unsigned int	addr_size,
798c3e34a4SDavid Howells 	const u8	*peer_addr)
808c3e34a4SDavid Howells {
818c3e34a4SDavid Howells 	const u16 *p;
828c3e34a4SDavid Howells 	unsigned int i;
838c3e34a4SDavid Howells 	unsigned long key;
848c3e34a4SDavid Howells 
858c3e34a4SDavid Howells 	_enter("");
868c3e34a4SDavid Howells 
878c3e34a4SDavid Howells 	key = (unsigned long)localptr;
888c3e34a4SDavid Howells 	/* We just want to add up the __be32 values, so forcing the
898c3e34a4SDavid Howells 	 * cast should be okay.
908c3e34a4SDavid Howells 	 */
918c3e34a4SDavid Howells 	key += epoch;
928c3e34a4SDavid Howells 	key += service_id;
938c3e34a4SDavid Howells 	key += call_id;
948c3e34a4SDavid Howells 	key += (cid & RXRPC_CIDMASK) >> RXRPC_CIDSHIFT;
958c3e34a4SDavid Howells 	key += cid & RXRPC_CHANNELMASK;
968c3e34a4SDavid Howells 	key += in_clientflag;
9719ffa01cSDavid Howells 	key += family;
988c3e34a4SDavid Howells 	/* Step through the peer address in 16-bit portions for speed */
998c3e34a4SDavid Howells 	for (i = 0, p = (const u16 *)peer_addr; i < addr_size >> 1; i++, p++)
1008c3e34a4SDavid Howells 		key += *p;
1018c3e34a4SDavid Howells 	_leave(" key = 0x%lx", key);
1028c3e34a4SDavid Howells 	return key;
1038c3e34a4SDavid Howells }
1048c3e34a4SDavid Howells 
1058c3e34a4SDavid Howells /*
1068c3e34a4SDavid Howells  * Add a call to the hashtable
1078c3e34a4SDavid Howells  */
1088c3e34a4SDavid Howells static void rxrpc_call_hash_add(struct rxrpc_call *call)
1098c3e34a4SDavid Howells {
1108c3e34a4SDavid Howells 	unsigned long key;
1118c3e34a4SDavid Howells 	unsigned int addr_size = 0;
1128c3e34a4SDavid Howells 
1138c3e34a4SDavid Howells 	_enter("");
11419ffa01cSDavid Howells 	switch (call->family) {
1158c3e34a4SDavid Howells 	case AF_INET:
1168c3e34a4SDavid Howells 		addr_size = sizeof(call->peer_ip.ipv4_addr);
1178c3e34a4SDavid Howells 		break;
1188c3e34a4SDavid Howells 	case AF_INET6:
1198c3e34a4SDavid Howells 		addr_size = sizeof(call->peer_ip.ipv6_addr);
1208c3e34a4SDavid Howells 		break;
1218c3e34a4SDavid Howells 	default:
1228c3e34a4SDavid Howells 		break;
1238c3e34a4SDavid Howells 	}
1248c3e34a4SDavid Howells 	key = rxrpc_call_hashfunc(call->in_clientflag, call->cid,
1258c3e34a4SDavid Howells 				  call->call_id, call->epoch,
12619ffa01cSDavid Howells 				  call->service_id, call->family,
12785f32278SDavid Howells 				  call->conn->params.local, addr_size,
1288c3e34a4SDavid Howells 				  call->peer_ip.ipv6_addr);
1298c3e34a4SDavid Howells 	/* Store the full key in the call */
1308c3e34a4SDavid Howells 	call->hash_key = key;
1318c3e34a4SDavid Howells 	spin_lock(&rxrpc_call_hash_lock);
1328c3e34a4SDavid Howells 	hash_add_rcu(rxrpc_call_hash, &call->hash_node, key);
1338c3e34a4SDavid Howells 	spin_unlock(&rxrpc_call_hash_lock);
1348c3e34a4SDavid Howells 	_leave("");
1358c3e34a4SDavid Howells }
1368c3e34a4SDavid Howells 
1378c3e34a4SDavid Howells /*
1388c3e34a4SDavid Howells  * Remove a call from the hashtable
1398c3e34a4SDavid Howells  */
1408c3e34a4SDavid Howells static void rxrpc_call_hash_del(struct rxrpc_call *call)
1418c3e34a4SDavid Howells {
1428c3e34a4SDavid Howells 	_enter("");
1438c3e34a4SDavid Howells 	spin_lock(&rxrpc_call_hash_lock);
1448c3e34a4SDavid Howells 	hash_del_rcu(&call->hash_node);
1458c3e34a4SDavid Howells 	spin_unlock(&rxrpc_call_hash_lock);
1468c3e34a4SDavid Howells 	_leave("");
1478c3e34a4SDavid Howells }
1488c3e34a4SDavid Howells 
1498c3e34a4SDavid Howells /*
1508c3e34a4SDavid Howells  * Find a call in the hashtable and return it, or NULL if it
1518c3e34a4SDavid Howells  * isn't there.
1528c3e34a4SDavid Howells  */
1538c3e34a4SDavid Howells struct rxrpc_call *rxrpc_find_call_hash(
1548c3e34a4SDavid Howells 	struct rxrpc_host_header *hdr,
1558c3e34a4SDavid Howells 	void		*localptr,
15619ffa01cSDavid Howells 	sa_family_t	family,
1578c3e34a4SDavid Howells 	const void	*peer_addr)
1588c3e34a4SDavid Howells {
1598c3e34a4SDavid Howells 	unsigned long key;
1608c3e34a4SDavid Howells 	unsigned int addr_size = 0;
1618c3e34a4SDavid Howells 	struct rxrpc_call *call = NULL;
1628c3e34a4SDavid Howells 	struct rxrpc_call *ret = NULL;
1638c3e34a4SDavid Howells 	u8 in_clientflag = hdr->flags & RXRPC_CLIENT_INITIATED;
1648c3e34a4SDavid Howells 
1658c3e34a4SDavid Howells 	_enter("");
16619ffa01cSDavid Howells 	switch (family) {
1678c3e34a4SDavid Howells 	case AF_INET:
1688c3e34a4SDavid Howells 		addr_size = sizeof(call->peer_ip.ipv4_addr);
1698c3e34a4SDavid Howells 		break;
1708c3e34a4SDavid Howells 	case AF_INET6:
1718c3e34a4SDavid Howells 		addr_size = sizeof(call->peer_ip.ipv6_addr);
1728c3e34a4SDavid Howells 		break;
1738c3e34a4SDavid Howells 	default:
1748c3e34a4SDavid Howells 		break;
1758c3e34a4SDavid Howells 	}
1768c3e34a4SDavid Howells 
1778c3e34a4SDavid Howells 	key = rxrpc_call_hashfunc(in_clientflag, hdr->cid, hdr->callNumber,
1788c3e34a4SDavid Howells 				  hdr->epoch, hdr->serviceId,
17919ffa01cSDavid Howells 				  family, localptr, addr_size,
1808c3e34a4SDavid Howells 				  peer_addr);
1818c3e34a4SDavid Howells 	hash_for_each_possible_rcu(rxrpc_call_hash, call, hash_node, key) {
1828c3e34a4SDavid Howells 		if (call->hash_key == key &&
1838c3e34a4SDavid Howells 		    call->call_id == hdr->callNumber &&
1848c3e34a4SDavid Howells 		    call->cid == hdr->cid &&
1858c3e34a4SDavid Howells 		    call->in_clientflag == in_clientflag &&
1868c3e34a4SDavid Howells 		    call->service_id == hdr->serviceId &&
18719ffa01cSDavid Howells 		    call->family == family &&
1888c3e34a4SDavid Howells 		    call->local == localptr &&
1898c3e34a4SDavid Howells 		    memcmp(call->peer_ip.ipv6_addr, peer_addr,
1908c3e34a4SDavid Howells 			   addr_size) == 0 &&
1918c3e34a4SDavid Howells 		    call->epoch == hdr->epoch) {
1928c3e34a4SDavid Howells 			ret = call;
1938c3e34a4SDavid Howells 			break;
1948c3e34a4SDavid Howells 		}
1958c3e34a4SDavid Howells 	}
1968c3e34a4SDavid Howells 	_leave(" = %p", ret);
1978c3e34a4SDavid Howells 	return ret;
1988c3e34a4SDavid Howells }
1998c3e34a4SDavid Howells 
2008c3e34a4SDavid Howells /*
2018c3e34a4SDavid Howells  * find an extant server call
2028c3e34a4SDavid Howells  * - called in process context with IRQs enabled
2038c3e34a4SDavid Howells  */
2048c3e34a4SDavid Howells struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx,
2058c3e34a4SDavid Howells 					      unsigned long user_call_ID)
2068c3e34a4SDavid Howells {
2078c3e34a4SDavid Howells 	struct rxrpc_call *call;
2088c3e34a4SDavid Howells 	struct rb_node *p;
2098c3e34a4SDavid Howells 
2108c3e34a4SDavid Howells 	_enter("%p,%lx", rx, user_call_ID);
2118c3e34a4SDavid Howells 
2128c3e34a4SDavid Howells 	read_lock(&rx->call_lock);
2138c3e34a4SDavid Howells 
2148c3e34a4SDavid Howells 	p = rx->calls.rb_node;
2158c3e34a4SDavid Howells 	while (p) {
2168c3e34a4SDavid Howells 		call = rb_entry(p, struct rxrpc_call, sock_node);
2178c3e34a4SDavid Howells 
2188c3e34a4SDavid Howells 		if (user_call_ID < call->user_call_ID)
2198c3e34a4SDavid Howells 			p = p->rb_left;
2208c3e34a4SDavid Howells 		else if (user_call_ID > call->user_call_ID)
2218c3e34a4SDavid Howells 			p = p->rb_right;
2228c3e34a4SDavid Howells 		else
2238c3e34a4SDavid Howells 			goto found_extant_call;
2248c3e34a4SDavid Howells 	}
2258c3e34a4SDavid Howells 
2268c3e34a4SDavid Howells 	read_unlock(&rx->call_lock);
2278c3e34a4SDavid Howells 	_leave(" = NULL");
2288c3e34a4SDavid Howells 	return NULL;
2298c3e34a4SDavid Howells 
2308c3e34a4SDavid Howells found_extant_call:
2318c3e34a4SDavid Howells 	rxrpc_get_call(call);
2328c3e34a4SDavid Howells 	read_unlock(&rx->call_lock);
2338c3e34a4SDavid Howells 	_leave(" = %p [%d]", call, atomic_read(&call->usage));
2348c3e34a4SDavid Howells 	return call;
2358c3e34a4SDavid Howells }
2368c3e34a4SDavid Howells 
2378c3e34a4SDavid Howells /*
2388c3e34a4SDavid Howells  * allocate a new call
2398c3e34a4SDavid Howells  */
2408c3e34a4SDavid Howells static struct rxrpc_call *rxrpc_alloc_call(gfp_t gfp)
2418c3e34a4SDavid Howells {
2428c3e34a4SDavid Howells 	struct rxrpc_call *call;
2438c3e34a4SDavid Howells 
2448c3e34a4SDavid Howells 	call = kmem_cache_zalloc(rxrpc_call_jar, gfp);
2458c3e34a4SDavid Howells 	if (!call)
2468c3e34a4SDavid Howells 		return NULL;
2478c3e34a4SDavid Howells 
2488c3e34a4SDavid Howells 	call->acks_winsz = 16;
2498c3e34a4SDavid Howells 	call->acks_window = kmalloc(call->acks_winsz * sizeof(unsigned long),
2508c3e34a4SDavid Howells 				    gfp);
2518c3e34a4SDavid Howells 	if (!call->acks_window) {
2528c3e34a4SDavid Howells 		kmem_cache_free(rxrpc_call_jar, call);
2538c3e34a4SDavid Howells 		return NULL;
2548c3e34a4SDavid Howells 	}
2558c3e34a4SDavid Howells 
2568c3e34a4SDavid Howells 	setup_timer(&call->lifetimer, &rxrpc_call_life_expired,
2578c3e34a4SDavid Howells 		    (unsigned long) call);
2588c3e34a4SDavid Howells 	setup_timer(&call->deadspan, &rxrpc_dead_call_expired,
2598c3e34a4SDavid Howells 		    (unsigned long) call);
2608c3e34a4SDavid Howells 	setup_timer(&call->ack_timer, &rxrpc_ack_time_expired,
2618c3e34a4SDavid Howells 		    (unsigned long) call);
2628c3e34a4SDavid Howells 	setup_timer(&call->resend_timer, &rxrpc_resend_time_expired,
2638c3e34a4SDavid Howells 		    (unsigned long) call);
2648c3e34a4SDavid Howells 	INIT_WORK(&call->destroyer, &rxrpc_destroy_call);
2658c3e34a4SDavid Howells 	INIT_WORK(&call->processor, &rxrpc_process_call);
266999b69f8SDavid Howells 	INIT_LIST_HEAD(&call->link);
2678c3e34a4SDavid Howells 	INIT_LIST_HEAD(&call->accept_link);
2688c3e34a4SDavid Howells 	skb_queue_head_init(&call->rx_queue);
2698c3e34a4SDavid Howells 	skb_queue_head_init(&call->rx_oos_queue);
2708c3e34a4SDavid Howells 	init_waitqueue_head(&call->tx_waitq);
2718c3e34a4SDavid Howells 	spin_lock_init(&call->lock);
2728c3e34a4SDavid Howells 	rwlock_init(&call->state_lock);
2738c3e34a4SDavid Howells 	atomic_set(&call->usage, 1);
2748c3e34a4SDavid Howells 	call->debug_id = atomic_inc_return(&rxrpc_debug_id);
2758c3e34a4SDavid Howells 
2768c3e34a4SDavid Howells 	memset(&call->sock_node, 0xed, sizeof(call->sock_node));
2778c3e34a4SDavid Howells 
2788c3e34a4SDavid Howells 	call->rx_data_expect = 1;
2798c3e34a4SDavid Howells 	call->rx_data_eaten = 0;
2808c3e34a4SDavid Howells 	call->rx_first_oos = 0;
2818c3e34a4SDavid Howells 	call->ackr_win_top = call->rx_data_eaten + 1 + rxrpc_rx_window_size;
2828c3e34a4SDavid Howells 	call->creation_jif = jiffies;
2838c3e34a4SDavid Howells 	return call;
2848c3e34a4SDavid Howells }
2858c3e34a4SDavid Howells 
2868c3e34a4SDavid Howells /*
287999b69f8SDavid Howells  * Allocate a new client call.
2888c3e34a4SDavid Howells  */
2898c3e34a4SDavid Howells static struct rxrpc_call *rxrpc_alloc_client_call(
2908c3e34a4SDavid Howells 	struct rxrpc_sock *rx,
29119ffa01cSDavid Howells 	struct rxrpc_conn_parameters *cp,
292999b69f8SDavid Howells 	struct sockaddr_rxrpc *srx,
2938c3e34a4SDavid Howells 	gfp_t gfp)
2948c3e34a4SDavid Howells {
2958c3e34a4SDavid Howells 	struct rxrpc_call *call;
2968c3e34a4SDavid Howells 
2978c3e34a4SDavid Howells 	_enter("");
2988c3e34a4SDavid Howells 
299999b69f8SDavid Howells 	ASSERT(rx->local != NULL);
3008c3e34a4SDavid Howells 
3018c3e34a4SDavid Howells 	call = rxrpc_alloc_call(gfp);
3028c3e34a4SDavid Howells 	if (!call)
3038c3e34a4SDavid Howells 		return ERR_PTR(-ENOMEM);
304999b69f8SDavid Howells 	call->state = RXRPC_CALL_CLIENT_AWAIT_CONN;
3058c3e34a4SDavid Howells 
3068c3e34a4SDavid Howells 	sock_hold(&rx->sk);
3078c3e34a4SDavid Howells 	call->socket = rx;
3088c3e34a4SDavid Howells 	call->rx_data_post = 1;
3098c3e34a4SDavid Howells 
3108c3e34a4SDavid Howells 	/* Record copies of information for hashtable lookup */
31119ffa01cSDavid Howells 	call->family = rx->family;
312999b69f8SDavid Howells 	call->local = rx->local;
31319ffa01cSDavid Howells 	switch (call->family) {
3148c3e34a4SDavid Howells 	case AF_INET:
315999b69f8SDavid Howells 		call->peer_ip.ipv4_addr = srx->transport.sin.sin_addr.s_addr;
3168c3e34a4SDavid Howells 		break;
3178c3e34a4SDavid Howells 	case AF_INET6:
3188c3e34a4SDavid Howells 		memcpy(call->peer_ip.ipv6_addr,
319999b69f8SDavid Howells 		       srx->transport.sin6.sin6_addr.in6_u.u6_addr8,
3208c3e34a4SDavid Howells 		       sizeof(call->peer_ip.ipv6_addr));
3218c3e34a4SDavid Howells 		break;
3228c3e34a4SDavid Howells 	}
323999b69f8SDavid Howells 
324999b69f8SDavid Howells 	call->service_id = srx->srx_service;
325999b69f8SDavid Howells 	call->in_clientflag = 0;
326999b69f8SDavid Howells 
327999b69f8SDavid Howells 	_leave(" = %p", call);
328999b69f8SDavid Howells 	return call;
329999b69f8SDavid Howells }
330999b69f8SDavid Howells 
331999b69f8SDavid Howells /*
332999b69f8SDavid Howells  * Begin client call.
333999b69f8SDavid Howells  */
334999b69f8SDavid Howells static int rxrpc_begin_client_call(struct rxrpc_call *call,
335999b69f8SDavid Howells 				   struct rxrpc_conn_parameters *cp,
336999b69f8SDavid Howells 				   struct rxrpc_transport *trans,
337999b69f8SDavid Howells 				   struct sockaddr_rxrpc *srx,
338999b69f8SDavid Howells 				   gfp_t gfp)
339999b69f8SDavid Howells {
340999b69f8SDavid Howells 	int ret;
341999b69f8SDavid Howells 
342999b69f8SDavid Howells 	/* Set up or get a connection record and set the protocol parameters,
343999b69f8SDavid Howells 	 * including channel number and call ID.
344999b69f8SDavid Howells 	 */
345999b69f8SDavid Howells 	ret = rxrpc_connect_call(call, cp, trans, srx, gfp);
346999b69f8SDavid Howells 	if (ret < 0)
347999b69f8SDavid Howells 		return ret;
348999b69f8SDavid Howells 
349999b69f8SDavid Howells 	call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
350999b69f8SDavid Howells 
3518c3e34a4SDavid Howells 	/* Add the new call to the hashtable */
3528c3e34a4SDavid Howells 	rxrpc_call_hash_add(call);
3538c3e34a4SDavid Howells 
35485f32278SDavid Howells 	spin_lock(&call->conn->params.peer->lock);
35585f32278SDavid Howells 	hlist_add_head(&call->error_link, &call->conn->params.peer->error_targets);
35685f32278SDavid Howells 	spin_unlock(&call->conn->params.peer->lock);
3578c3e34a4SDavid Howells 
3588c3e34a4SDavid Howells 	call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
3598c3e34a4SDavid Howells 	add_timer(&call->lifetimer);
360999b69f8SDavid Howells 	return 0;
3618c3e34a4SDavid Howells }
3628c3e34a4SDavid Howells 
3638c3e34a4SDavid Howells /*
3648c3e34a4SDavid Howells  * set up a call for the given data
3658c3e34a4SDavid Howells  * - called in process context with IRQs enabled
3668c3e34a4SDavid Howells  */
3678c3e34a4SDavid Howells struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
36819ffa01cSDavid Howells 					 struct rxrpc_conn_parameters *cp,
3698c3e34a4SDavid Howells 					 struct rxrpc_transport *trans,
370999b69f8SDavid Howells 					 struct sockaddr_rxrpc *srx,
3718c3e34a4SDavid Howells 					 unsigned long user_call_ID,
3728c3e34a4SDavid Howells 					 gfp_t gfp)
3738c3e34a4SDavid Howells {
3748c3e34a4SDavid Howells 	struct rxrpc_call *call, *xcall;
3758c3e34a4SDavid Howells 	struct rb_node *parent, **pp;
376999b69f8SDavid Howells 	int ret;
3778c3e34a4SDavid Howells 
378999b69f8SDavid Howells 	_enter("%p,%lx", rx, user_call_ID);
3798c3e34a4SDavid Howells 
380999b69f8SDavid Howells 	call = rxrpc_alloc_client_call(rx, cp, srx, gfp);
3818c3e34a4SDavid Howells 	if (IS_ERR(call)) {
3828c3e34a4SDavid Howells 		_leave(" = %ld", PTR_ERR(call));
3838c3e34a4SDavid Howells 		return call;
3848c3e34a4SDavid Howells 	}
3858c3e34a4SDavid Howells 
386999b69f8SDavid Howells 	/* Publish the call, even though it is incompletely set up as yet */
3878c3e34a4SDavid Howells 	call->user_call_ID = user_call_ID;
3888c3e34a4SDavid Howells 	__set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
3898c3e34a4SDavid Howells 
3908c3e34a4SDavid Howells 	write_lock(&rx->call_lock);
3918c3e34a4SDavid Howells 
3928c3e34a4SDavid Howells 	pp = &rx->calls.rb_node;
3938c3e34a4SDavid Howells 	parent = NULL;
3948c3e34a4SDavid Howells 	while (*pp) {
3958c3e34a4SDavid Howells 		parent = *pp;
3968c3e34a4SDavid Howells 		xcall = rb_entry(parent, struct rxrpc_call, sock_node);
3978c3e34a4SDavid Howells 
3988c3e34a4SDavid Howells 		if (user_call_ID < xcall->user_call_ID)
3998c3e34a4SDavid Howells 			pp = &(*pp)->rb_left;
4008c3e34a4SDavid Howells 		else if (user_call_ID > xcall->user_call_ID)
4018c3e34a4SDavid Howells 			pp = &(*pp)->rb_right;
4028c3e34a4SDavid Howells 		else
4038c3e34a4SDavid Howells 			goto found_user_ID_now_present;
4048c3e34a4SDavid Howells 	}
4058c3e34a4SDavid Howells 
4068c3e34a4SDavid Howells 	rxrpc_get_call(call);
4078c3e34a4SDavid Howells 
4088c3e34a4SDavid Howells 	rb_link_node(&call->sock_node, parent, pp);
4098c3e34a4SDavid Howells 	rb_insert_color(&call->sock_node, &rx->calls);
4108c3e34a4SDavid Howells 	write_unlock(&rx->call_lock);
4118c3e34a4SDavid Howells 
4128c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
4138c3e34a4SDavid Howells 	list_add_tail(&call->link, &rxrpc_calls);
4148c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
4158c3e34a4SDavid Howells 
416999b69f8SDavid Howells 	ret = rxrpc_begin_client_call(call, cp, trans, srx, gfp);
417999b69f8SDavid Howells 	if (ret < 0)
418999b69f8SDavid Howells 		goto error;
419999b69f8SDavid Howells 
4208c3e34a4SDavid Howells 	_net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
4218c3e34a4SDavid Howells 
4228c3e34a4SDavid Howells 	_leave(" = %p [new]", call);
4238c3e34a4SDavid Howells 	return call;
4248c3e34a4SDavid Howells 
425999b69f8SDavid Howells error:
426999b69f8SDavid Howells 	write_lock(&rx->call_lock);
427999b69f8SDavid Howells 	rb_erase(&call->sock_node, &rx->calls);
428999b69f8SDavid Howells 	write_unlock(&rx->call_lock);
429999b69f8SDavid Howells 	rxrpc_put_call(call);
430999b69f8SDavid Howells 
431999b69f8SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
432999b69f8SDavid Howells 	list_del(&call->link);
433999b69f8SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
434999b69f8SDavid Howells 
435999b69f8SDavid Howells 	rxrpc_put_call(call);
436999b69f8SDavid Howells 	_leave(" = %d", ret);
437999b69f8SDavid Howells 	return ERR_PTR(ret);
438999b69f8SDavid Howells 
4398c3e34a4SDavid Howells 	/* We unexpectedly found the user ID in the list after taking
4408c3e34a4SDavid Howells 	 * the call_lock.  This shouldn't happen unless the user races
4418c3e34a4SDavid Howells 	 * with itself and tries to add the same user ID twice at the
4428c3e34a4SDavid Howells 	 * same time in different threads.
4438c3e34a4SDavid Howells 	 */
4448c3e34a4SDavid Howells found_user_ID_now_present:
4458c3e34a4SDavid Howells 	write_unlock(&rx->call_lock);
4468c3e34a4SDavid Howells 	rxrpc_put_call(call);
4478c3e34a4SDavid Howells 	_leave(" = -EEXIST [%p]", call);
4488c3e34a4SDavid Howells 	return ERR_PTR(-EEXIST);
4498c3e34a4SDavid Howells }
4508c3e34a4SDavid Howells 
4518c3e34a4SDavid Howells /*
4528c3e34a4SDavid Howells  * set up an incoming call
4538c3e34a4SDavid Howells  * - called in process context with IRQs enabled
4548c3e34a4SDavid Howells  */
4558c3e34a4SDavid Howells struct rxrpc_call *rxrpc_incoming_call(struct rxrpc_sock *rx,
4568c3e34a4SDavid Howells 				       struct rxrpc_connection *conn,
45742886ffeSDavid Howells 				       struct sk_buff *skb)
4588c3e34a4SDavid Howells {
45942886ffeSDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
4608c3e34a4SDavid Howells 	struct rxrpc_call *call, *candidate;
4618c3e34a4SDavid Howells 	struct rb_node **p, *parent;
4628c3e34a4SDavid Howells 	u32 call_id;
4638c3e34a4SDavid Howells 
4648c3e34a4SDavid Howells 	_enter(",%d", conn->debug_id);
4658c3e34a4SDavid Howells 
4668c3e34a4SDavid Howells 	ASSERT(rx != NULL);
4678c3e34a4SDavid Howells 
4688c3e34a4SDavid Howells 	candidate = rxrpc_alloc_call(GFP_NOIO);
4698c3e34a4SDavid Howells 	if (!candidate)
4708c3e34a4SDavid Howells 		return ERR_PTR(-EBUSY);
4718c3e34a4SDavid Howells 
4728c3e34a4SDavid Howells 	candidate->socket	= rx;
4738c3e34a4SDavid Howells 	candidate->conn		= conn;
47442886ffeSDavid Howells 	candidate->cid		= sp->hdr.cid;
47542886ffeSDavid Howells 	candidate->call_id	= sp->hdr.callNumber;
47642886ffeSDavid Howells 	candidate->channel	= sp->hdr.cid & RXRPC_CHANNELMASK;
4778c3e34a4SDavid Howells 	candidate->rx_data_post	= 0;
4788c3e34a4SDavid Howells 	candidate->state	= RXRPC_CALL_SERVER_ACCEPTING;
4798c3e34a4SDavid Howells 	if (conn->security_ix > 0)
4808c3e34a4SDavid Howells 		candidate->state = RXRPC_CALL_SERVER_SECURING;
4818c3e34a4SDavid Howells 
4828c3e34a4SDavid Howells 	write_lock_bh(&conn->lock);
4838c3e34a4SDavid Howells 
4848c3e34a4SDavid Howells 	/* set the channel for this call */
4858c3e34a4SDavid Howells 	call = conn->channels[candidate->channel];
4868c3e34a4SDavid Howells 	_debug("channel[%u] is %p", candidate->channel, call);
48742886ffeSDavid Howells 	if (call && call->call_id == sp->hdr.callNumber) {
4888c3e34a4SDavid Howells 		/* already set; must've been a duplicate packet */
4898c3e34a4SDavid Howells 		_debug("extant call [%d]", call->state);
4908c3e34a4SDavid Howells 		ASSERTCMP(call->conn, ==, conn);
4918c3e34a4SDavid Howells 
4928c3e34a4SDavid Howells 		read_lock(&call->state_lock);
4938c3e34a4SDavid Howells 		switch (call->state) {
4948c3e34a4SDavid Howells 		case RXRPC_CALL_LOCALLY_ABORTED:
4958c3e34a4SDavid Howells 			if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events))
4968c3e34a4SDavid Howells 				rxrpc_queue_call(call);
4978c3e34a4SDavid Howells 		case RXRPC_CALL_REMOTELY_ABORTED:
4988c3e34a4SDavid Howells 			read_unlock(&call->state_lock);
4998c3e34a4SDavid Howells 			goto aborted_call;
5008c3e34a4SDavid Howells 		default:
5018c3e34a4SDavid Howells 			rxrpc_get_call(call);
5028c3e34a4SDavid Howells 			read_unlock(&call->state_lock);
5038c3e34a4SDavid Howells 			goto extant_call;
5048c3e34a4SDavid Howells 		}
5058c3e34a4SDavid Howells 	}
5068c3e34a4SDavid Howells 
5078c3e34a4SDavid Howells 	if (call) {
5088c3e34a4SDavid Howells 		/* it seems the channel is still in use from the previous call
5098c3e34a4SDavid Howells 		 * - ditch the old binding if its call is now complete */
5108c3e34a4SDavid Howells 		_debug("CALL: %u { %s }",
5118c3e34a4SDavid Howells 		       call->debug_id, rxrpc_call_states[call->state]);
5128c3e34a4SDavid Howells 
5138c3e34a4SDavid Howells 		if (call->state >= RXRPC_CALL_COMPLETE) {
5148c3e34a4SDavid Howells 			conn->channels[call->channel] = NULL;
5158c3e34a4SDavid Howells 		} else {
5168c3e34a4SDavid Howells 			write_unlock_bh(&conn->lock);
5178c3e34a4SDavid Howells 			kmem_cache_free(rxrpc_call_jar, candidate);
5188c3e34a4SDavid Howells 			_leave(" = -EBUSY");
5198c3e34a4SDavid Howells 			return ERR_PTR(-EBUSY);
5208c3e34a4SDavid Howells 		}
5218c3e34a4SDavid Howells 	}
5228c3e34a4SDavid Howells 
5238c3e34a4SDavid Howells 	/* check the call number isn't duplicate */
5248c3e34a4SDavid Howells 	_debug("check dup");
52542886ffeSDavid Howells 	call_id = sp->hdr.callNumber;
5268c3e34a4SDavid Howells 	p = &conn->calls.rb_node;
5278c3e34a4SDavid Howells 	parent = NULL;
5288c3e34a4SDavid Howells 	while (*p) {
5298c3e34a4SDavid Howells 		parent = *p;
5308c3e34a4SDavid Howells 		call = rb_entry(parent, struct rxrpc_call, conn_node);
5318c3e34a4SDavid Howells 
5328c3e34a4SDavid Howells 		/* The tree is sorted in order of the __be32 value without
5338c3e34a4SDavid Howells 		 * turning it into host order.
5348c3e34a4SDavid Howells 		 */
5358c3e34a4SDavid Howells 		if (call_id < call->call_id)
5368c3e34a4SDavid Howells 			p = &(*p)->rb_left;
5378c3e34a4SDavid Howells 		else if (call_id > call->call_id)
5388c3e34a4SDavid Howells 			p = &(*p)->rb_right;
5398c3e34a4SDavid Howells 		else
5408c3e34a4SDavid Howells 			goto old_call;
5418c3e34a4SDavid Howells 	}
5428c3e34a4SDavid Howells 
5438c3e34a4SDavid Howells 	/* make the call available */
5448c3e34a4SDavid Howells 	_debug("new call");
5458c3e34a4SDavid Howells 	call = candidate;
5468c3e34a4SDavid Howells 	candidate = NULL;
5478c3e34a4SDavid Howells 	rb_link_node(&call->conn_node, parent, p);
5488c3e34a4SDavid Howells 	rb_insert_color(&call->conn_node, &conn->calls);
5498c3e34a4SDavid Howells 	conn->channels[call->channel] = call;
5508c3e34a4SDavid Howells 	sock_hold(&rx->sk);
5515627cc8bSDavid Howells 	rxrpc_get_connection(conn);
5528c3e34a4SDavid Howells 	write_unlock_bh(&conn->lock);
5538c3e34a4SDavid Howells 
55485f32278SDavid Howells 	spin_lock(&conn->params.peer->lock);
55585f32278SDavid Howells 	hlist_add_head(&call->error_link, &conn->params.peer->error_targets);
55685f32278SDavid Howells 	spin_unlock(&conn->params.peer->lock);
5578c3e34a4SDavid Howells 
5588c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
5598c3e34a4SDavid Howells 	list_add_tail(&call->link, &rxrpc_calls);
5608c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
5618c3e34a4SDavid Howells 
5628c3e34a4SDavid Howells 	/* Record copies of information for hashtable lookup */
56319ffa01cSDavid Howells 	call->family = rx->family;
56485f32278SDavid Howells 	call->local = conn->params.local;
56519ffa01cSDavid Howells 	switch (call->family) {
5668c3e34a4SDavid Howells 	case AF_INET:
5678c3e34a4SDavid Howells 		call->peer_ip.ipv4_addr =
56885f32278SDavid Howells 			conn->params.peer->srx.transport.sin.sin_addr.s_addr;
5698c3e34a4SDavid Howells 		break;
5708c3e34a4SDavid Howells 	case AF_INET6:
5718c3e34a4SDavid Howells 		memcpy(call->peer_ip.ipv6_addr,
57285f32278SDavid Howells 		       conn->params.peer->srx.transport.sin6.sin6_addr.in6_u.u6_addr8,
5738c3e34a4SDavid Howells 		       sizeof(call->peer_ip.ipv6_addr));
5748c3e34a4SDavid Howells 		break;
5758c3e34a4SDavid Howells 	default:
5768c3e34a4SDavid Howells 		break;
5778c3e34a4SDavid Howells 	}
57819ffa01cSDavid Howells 	call->epoch = conn->proto.epoch;
57919ffa01cSDavid Howells 	call->service_id = conn->params.service_id;
58019ffa01cSDavid Howells 	call->in_clientflag = conn->proto.in_clientflag;
5818c3e34a4SDavid Howells 	/* Add the new call to the hashtable */
5828c3e34a4SDavid Howells 	rxrpc_call_hash_add(call);
5838c3e34a4SDavid Howells 
5848c3e34a4SDavid Howells 	_net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
5858c3e34a4SDavid Howells 
5868c3e34a4SDavid Howells 	call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
5878c3e34a4SDavid Howells 	add_timer(&call->lifetimer);
5888c3e34a4SDavid Howells 	_leave(" = %p {%d} [new]", call, call->debug_id);
5898c3e34a4SDavid Howells 	return call;
5908c3e34a4SDavid Howells 
5918c3e34a4SDavid Howells extant_call:
5928c3e34a4SDavid Howells 	write_unlock_bh(&conn->lock);
5938c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, candidate);
5948c3e34a4SDavid Howells 	_leave(" = %p {%d} [extant]", call, call ? call->debug_id : -1);
5958c3e34a4SDavid Howells 	return call;
5968c3e34a4SDavid Howells 
5978c3e34a4SDavid Howells aborted_call:
5988c3e34a4SDavid Howells 	write_unlock_bh(&conn->lock);
5998c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, candidate);
6008c3e34a4SDavid Howells 	_leave(" = -ECONNABORTED");
6018c3e34a4SDavid Howells 	return ERR_PTR(-ECONNABORTED);
6028c3e34a4SDavid Howells 
6038c3e34a4SDavid Howells old_call:
6048c3e34a4SDavid Howells 	write_unlock_bh(&conn->lock);
6058c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, candidate);
6068c3e34a4SDavid Howells 	_leave(" = -ECONNRESET [old]");
6078c3e34a4SDavid Howells 	return ERR_PTR(-ECONNRESET);
6088c3e34a4SDavid Howells }
6098c3e34a4SDavid Howells 
6108c3e34a4SDavid Howells /*
6118c3e34a4SDavid Howells  * detach a call from a socket and set up for release
6128c3e34a4SDavid Howells  */
6138c3e34a4SDavid Howells void rxrpc_release_call(struct rxrpc_call *call)
6148c3e34a4SDavid Howells {
6158c3e34a4SDavid Howells 	struct rxrpc_connection *conn = call->conn;
6168c3e34a4SDavid Howells 	struct rxrpc_sock *rx = call->socket;
6178c3e34a4SDavid Howells 
6188c3e34a4SDavid Howells 	_enter("{%d,%d,%d,%d}",
6198c3e34a4SDavid Howells 	       call->debug_id, atomic_read(&call->usage),
6208c3e34a4SDavid Howells 	       atomic_read(&call->ackr_not_idle),
6218c3e34a4SDavid Howells 	       call->rx_first_oos);
6228c3e34a4SDavid Howells 
6238c3e34a4SDavid Howells 	spin_lock_bh(&call->lock);
6248c3e34a4SDavid Howells 	if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
6258c3e34a4SDavid Howells 		BUG();
6268c3e34a4SDavid Howells 	spin_unlock_bh(&call->lock);
6278c3e34a4SDavid Howells 
6288c3e34a4SDavid Howells 	/* dissociate from the socket
6298c3e34a4SDavid Howells 	 * - the socket's ref on the call is passed to the death timer
6308c3e34a4SDavid Howells 	 */
6318c3e34a4SDavid Howells 	_debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
6328c3e34a4SDavid Howells 
6338c3e34a4SDavid Howells 	write_lock_bh(&rx->call_lock);
6348c3e34a4SDavid Howells 	if (!list_empty(&call->accept_link)) {
6358c3e34a4SDavid Howells 		_debug("unlinking once-pending call %p { e=%lx f=%lx }",
6368c3e34a4SDavid Howells 		       call, call->events, call->flags);
6378c3e34a4SDavid Howells 		ASSERT(!test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
6388c3e34a4SDavid Howells 		list_del_init(&call->accept_link);
6398c3e34a4SDavid Howells 		sk_acceptq_removed(&rx->sk);
6408c3e34a4SDavid Howells 	} else if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
6418c3e34a4SDavid Howells 		rb_erase(&call->sock_node, &rx->calls);
6428c3e34a4SDavid Howells 		memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
6438c3e34a4SDavid Howells 		clear_bit(RXRPC_CALL_HAS_USERID, &call->flags);
6448c3e34a4SDavid Howells 	}
6458c3e34a4SDavid Howells 	write_unlock_bh(&rx->call_lock);
6468c3e34a4SDavid Howells 
6478c3e34a4SDavid Howells 	/* free up the channel for reuse */
648999b69f8SDavid Howells 	spin_lock(&conn->channel_lock);
6498c3e34a4SDavid Howells 	write_lock_bh(&conn->lock);
6508c3e34a4SDavid Howells 	write_lock(&call->state_lock);
6518c3e34a4SDavid Howells 
652999b69f8SDavid Howells 	rxrpc_disconnect_call(call);
6538c3e34a4SDavid Howells 
654999b69f8SDavid Howells 	spin_unlock(&conn->channel_lock);
6558c3e34a4SDavid Howells 
6568c3e34a4SDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE &&
6578c3e34a4SDavid Howells 	    call->state != RXRPC_CALL_CLIENT_FINAL_ACK) {
6588c3e34a4SDavid Howells 		_debug("+++ ABORTING STATE %d +++\n", call->state);
6598c3e34a4SDavid Howells 		call->state = RXRPC_CALL_LOCALLY_ABORTED;
6608c3e34a4SDavid Howells 		call->local_abort = RX_CALL_DEAD;
6618c3e34a4SDavid Howells 		set_bit(RXRPC_CALL_EV_ABORT, &call->events);
6628c3e34a4SDavid Howells 		rxrpc_queue_call(call);
6638c3e34a4SDavid Howells 	}
6648c3e34a4SDavid Howells 	write_unlock(&call->state_lock);
6658c3e34a4SDavid Howells 	write_unlock_bh(&conn->lock);
6668c3e34a4SDavid Howells 
6678c3e34a4SDavid Howells 	/* clean up the Rx queue */
6688c3e34a4SDavid Howells 	if (!skb_queue_empty(&call->rx_queue) ||
6698c3e34a4SDavid Howells 	    !skb_queue_empty(&call->rx_oos_queue)) {
6708c3e34a4SDavid Howells 		struct rxrpc_skb_priv *sp;
6718c3e34a4SDavid Howells 		struct sk_buff *skb;
6728c3e34a4SDavid Howells 
6738c3e34a4SDavid Howells 		_debug("purge Rx queues");
6748c3e34a4SDavid Howells 
6758c3e34a4SDavid Howells 		spin_lock_bh(&call->lock);
6768c3e34a4SDavid Howells 		while ((skb = skb_dequeue(&call->rx_queue)) ||
6778c3e34a4SDavid Howells 		       (skb = skb_dequeue(&call->rx_oos_queue))) {
6788c3e34a4SDavid Howells 			sp = rxrpc_skb(skb);
6798c3e34a4SDavid Howells 			if (sp->call) {
6808c3e34a4SDavid Howells 				ASSERTCMP(sp->call, ==, call);
6818c3e34a4SDavid Howells 				rxrpc_put_call(call);
6828c3e34a4SDavid Howells 				sp->call = NULL;
6838c3e34a4SDavid Howells 			}
6848c3e34a4SDavid Howells 			skb->destructor = NULL;
6858c3e34a4SDavid Howells 			spin_unlock_bh(&call->lock);
6868c3e34a4SDavid Howells 
6878c3e34a4SDavid Howells 			_debug("- zap %s %%%u #%u",
6888c3e34a4SDavid Howells 			       rxrpc_pkts[sp->hdr.type],
6898c3e34a4SDavid Howells 			       sp->hdr.serial, sp->hdr.seq);
6908c3e34a4SDavid Howells 			rxrpc_free_skb(skb);
6918c3e34a4SDavid Howells 			spin_lock_bh(&call->lock);
6928c3e34a4SDavid Howells 		}
6938c3e34a4SDavid Howells 		spin_unlock_bh(&call->lock);
6948c3e34a4SDavid Howells 
6958c3e34a4SDavid Howells 		ASSERTCMP(call->state, !=, RXRPC_CALL_COMPLETE);
6968c3e34a4SDavid Howells 	}
6978c3e34a4SDavid Howells 
6988c3e34a4SDavid Howells 	del_timer_sync(&call->resend_timer);
6998c3e34a4SDavid Howells 	del_timer_sync(&call->ack_timer);
7008c3e34a4SDavid Howells 	del_timer_sync(&call->lifetimer);
7018c3e34a4SDavid Howells 	call->deadspan.expires = jiffies + rxrpc_dead_call_expiry;
7028c3e34a4SDavid Howells 	add_timer(&call->deadspan);
7038c3e34a4SDavid Howells 
7048c3e34a4SDavid Howells 	_leave("");
7058c3e34a4SDavid Howells }
7068c3e34a4SDavid Howells 
7078c3e34a4SDavid Howells /*
7088c3e34a4SDavid Howells  * handle a dead call being ready for reaping
7098c3e34a4SDavid Howells  */
7108c3e34a4SDavid Howells static void rxrpc_dead_call_expired(unsigned long _call)
7118c3e34a4SDavid Howells {
7128c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
7138c3e34a4SDavid Howells 
7148c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
7158c3e34a4SDavid Howells 
7168c3e34a4SDavid Howells 	write_lock_bh(&call->state_lock);
7178c3e34a4SDavid Howells 	call->state = RXRPC_CALL_DEAD;
7188c3e34a4SDavid Howells 	write_unlock_bh(&call->state_lock);
7198c3e34a4SDavid Howells 	rxrpc_put_call(call);
7208c3e34a4SDavid Howells }
7218c3e34a4SDavid Howells 
7228c3e34a4SDavid Howells /*
7238c3e34a4SDavid Howells  * mark a call as to be released, aborting it if it's still in progress
7248c3e34a4SDavid Howells  * - called with softirqs disabled
7258c3e34a4SDavid Howells  */
7268c3e34a4SDavid Howells static void rxrpc_mark_call_released(struct rxrpc_call *call)
7278c3e34a4SDavid Howells {
7288c3e34a4SDavid Howells 	bool sched;
7298c3e34a4SDavid Howells 
7308c3e34a4SDavid Howells 	write_lock(&call->state_lock);
7318c3e34a4SDavid Howells 	if (call->state < RXRPC_CALL_DEAD) {
7328c3e34a4SDavid Howells 		sched = false;
7338c3e34a4SDavid Howells 		if (call->state < RXRPC_CALL_COMPLETE) {
7348c3e34a4SDavid Howells 			_debug("abort call %p", call);
7358c3e34a4SDavid Howells 			call->state = RXRPC_CALL_LOCALLY_ABORTED;
7368c3e34a4SDavid Howells 			call->local_abort = RX_CALL_DEAD;
7378c3e34a4SDavid Howells 			if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events))
7388c3e34a4SDavid Howells 				sched = true;
7398c3e34a4SDavid Howells 		}
7408c3e34a4SDavid Howells 		if (!test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
7418c3e34a4SDavid Howells 			sched = true;
7428c3e34a4SDavid Howells 		if (sched)
7438c3e34a4SDavid Howells 			rxrpc_queue_call(call);
7448c3e34a4SDavid Howells 	}
7458c3e34a4SDavid Howells 	write_unlock(&call->state_lock);
7468c3e34a4SDavid Howells }
7478c3e34a4SDavid Howells 
7488c3e34a4SDavid Howells /*
7498c3e34a4SDavid Howells  * release all the calls associated with a socket
7508c3e34a4SDavid Howells  */
7518c3e34a4SDavid Howells void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
7528c3e34a4SDavid Howells {
7538c3e34a4SDavid Howells 	struct rxrpc_call *call;
7548c3e34a4SDavid Howells 	struct rb_node *p;
7558c3e34a4SDavid Howells 
7568c3e34a4SDavid Howells 	_enter("%p", rx);
7578c3e34a4SDavid Howells 
7588c3e34a4SDavid Howells 	read_lock_bh(&rx->call_lock);
7598c3e34a4SDavid Howells 
7608c3e34a4SDavid Howells 	/* mark all the calls as no longer wanting incoming packets */
7618c3e34a4SDavid Howells 	for (p = rb_first(&rx->calls); p; p = rb_next(p)) {
7628c3e34a4SDavid Howells 		call = rb_entry(p, struct rxrpc_call, sock_node);
7638c3e34a4SDavid Howells 		rxrpc_mark_call_released(call);
7648c3e34a4SDavid Howells 	}
7658c3e34a4SDavid Howells 
7668c3e34a4SDavid Howells 	/* kill the not-yet-accepted incoming calls */
7678c3e34a4SDavid Howells 	list_for_each_entry(call, &rx->secureq, accept_link) {
7688c3e34a4SDavid Howells 		rxrpc_mark_call_released(call);
7698c3e34a4SDavid Howells 	}
7708c3e34a4SDavid Howells 
7718c3e34a4SDavid Howells 	list_for_each_entry(call, &rx->acceptq, accept_link) {
7728c3e34a4SDavid Howells 		rxrpc_mark_call_released(call);
7738c3e34a4SDavid Howells 	}
7748c3e34a4SDavid Howells 
7758c3e34a4SDavid Howells 	read_unlock_bh(&rx->call_lock);
7768c3e34a4SDavid Howells 	_leave("");
7778c3e34a4SDavid Howells }
7788c3e34a4SDavid Howells 
7798c3e34a4SDavid Howells /*
7808c3e34a4SDavid Howells  * release a call
7818c3e34a4SDavid Howells  */
7828c3e34a4SDavid Howells void __rxrpc_put_call(struct rxrpc_call *call)
7838c3e34a4SDavid Howells {
7848c3e34a4SDavid Howells 	ASSERT(call != NULL);
7858c3e34a4SDavid Howells 
7868c3e34a4SDavid Howells 	_enter("%p{u=%d}", call, atomic_read(&call->usage));
7878c3e34a4SDavid Howells 
7888c3e34a4SDavid Howells 	ASSERTCMP(atomic_read(&call->usage), >, 0);
7898c3e34a4SDavid Howells 
7908c3e34a4SDavid Howells 	if (atomic_dec_and_test(&call->usage)) {
7918c3e34a4SDavid Howells 		_debug("call %d dead", call->debug_id);
7928c3e34a4SDavid Howells 		ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
7938c3e34a4SDavid Howells 		rxrpc_queue_work(&call->destroyer);
7948c3e34a4SDavid Howells 	}
7958c3e34a4SDavid Howells 	_leave("");
7968c3e34a4SDavid Howells }
7978c3e34a4SDavid Howells 
7988c3e34a4SDavid Howells /*
7998c3e34a4SDavid Howells  * clean up a call
8008c3e34a4SDavid Howells  */
8018c3e34a4SDavid Howells static void rxrpc_cleanup_call(struct rxrpc_call *call)
8028c3e34a4SDavid Howells {
8038c3e34a4SDavid Howells 	_net("DESTROY CALL %d", call->debug_id);
8048c3e34a4SDavid Howells 
8058c3e34a4SDavid Howells 	ASSERT(call->socket);
8068c3e34a4SDavid Howells 
8078c3e34a4SDavid Howells 	memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
8088c3e34a4SDavid Howells 
8098c3e34a4SDavid Howells 	del_timer_sync(&call->lifetimer);
8108c3e34a4SDavid Howells 	del_timer_sync(&call->deadspan);
8118c3e34a4SDavid Howells 	del_timer_sync(&call->ack_timer);
8128c3e34a4SDavid Howells 	del_timer_sync(&call->resend_timer);
8138c3e34a4SDavid Howells 
8148c3e34a4SDavid Howells 	ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
8158c3e34a4SDavid Howells 	ASSERTCMP(call->events, ==, 0);
8168c3e34a4SDavid Howells 	if (work_pending(&call->processor)) {
8178c3e34a4SDavid Howells 		_debug("defer destroy");
8188c3e34a4SDavid Howells 		rxrpc_queue_work(&call->destroyer);
8198c3e34a4SDavid Howells 		return;
8208c3e34a4SDavid Howells 	}
8218c3e34a4SDavid Howells 
8228c3e34a4SDavid Howells 	if (call->conn) {
82385f32278SDavid Howells 		spin_lock(&call->conn->params.peer->lock);
824f66d7490SDavid Howells 		hlist_del_init(&call->error_link);
82585f32278SDavid Howells 		spin_unlock(&call->conn->params.peer->lock);
8268c3e34a4SDavid Howells 
8278c3e34a4SDavid Howells 		write_lock_bh(&call->conn->lock);
8288c3e34a4SDavid Howells 		rb_erase(&call->conn_node, &call->conn->calls);
8298c3e34a4SDavid Howells 		write_unlock_bh(&call->conn->lock);
8308c3e34a4SDavid Howells 		rxrpc_put_connection(call->conn);
8318c3e34a4SDavid Howells 	}
8328c3e34a4SDavid Howells 
8338c3e34a4SDavid Howells 	/* Remove the call from the hash */
8348c3e34a4SDavid Howells 	rxrpc_call_hash_del(call);
8358c3e34a4SDavid Howells 
8368c3e34a4SDavid Howells 	if (call->acks_window) {
8378c3e34a4SDavid Howells 		_debug("kill Tx window %d",
8388c3e34a4SDavid Howells 		       CIRC_CNT(call->acks_head, call->acks_tail,
8398c3e34a4SDavid Howells 				call->acks_winsz));
8408c3e34a4SDavid Howells 		smp_mb();
8418c3e34a4SDavid Howells 		while (CIRC_CNT(call->acks_head, call->acks_tail,
8428c3e34a4SDavid Howells 				call->acks_winsz) > 0) {
8438c3e34a4SDavid Howells 			struct rxrpc_skb_priv *sp;
8448c3e34a4SDavid Howells 			unsigned long _skb;
8458c3e34a4SDavid Howells 
8468c3e34a4SDavid Howells 			_skb = call->acks_window[call->acks_tail] & ~1;
8478c3e34a4SDavid Howells 			sp = rxrpc_skb((struct sk_buff *)_skb);
8488c3e34a4SDavid Howells 			_debug("+++ clear Tx %u", sp->hdr.seq);
8498c3e34a4SDavid Howells 			rxrpc_free_skb((struct sk_buff *)_skb);
8508c3e34a4SDavid Howells 			call->acks_tail =
8518c3e34a4SDavid Howells 				(call->acks_tail + 1) & (call->acks_winsz - 1);
8528c3e34a4SDavid Howells 		}
8538c3e34a4SDavid Howells 
8548c3e34a4SDavid Howells 		kfree(call->acks_window);
8558c3e34a4SDavid Howells 	}
8568c3e34a4SDavid Howells 
8578c3e34a4SDavid Howells 	rxrpc_free_skb(call->tx_pending);
8588c3e34a4SDavid Howells 
8598c3e34a4SDavid Howells 	rxrpc_purge_queue(&call->rx_queue);
8608c3e34a4SDavid Howells 	ASSERT(skb_queue_empty(&call->rx_oos_queue));
8618c3e34a4SDavid Howells 	sock_put(&call->socket->sk);
8628c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, call);
8638c3e34a4SDavid Howells }
8648c3e34a4SDavid Howells 
8658c3e34a4SDavid Howells /*
8668c3e34a4SDavid Howells  * destroy a call
8678c3e34a4SDavid Howells  */
8688c3e34a4SDavid Howells static void rxrpc_destroy_call(struct work_struct *work)
8698c3e34a4SDavid Howells {
8708c3e34a4SDavid Howells 	struct rxrpc_call *call =
8718c3e34a4SDavid Howells 		container_of(work, struct rxrpc_call, destroyer);
8728c3e34a4SDavid Howells 
8738c3e34a4SDavid Howells 	_enter("%p{%d,%d,%p}",
8748c3e34a4SDavid Howells 	       call, atomic_read(&call->usage), call->channel, call->conn);
8758c3e34a4SDavid Howells 
8768c3e34a4SDavid Howells 	ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
8778c3e34a4SDavid Howells 
8788c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
8798c3e34a4SDavid Howells 	list_del_init(&call->link);
8808c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
8818c3e34a4SDavid Howells 
8828c3e34a4SDavid Howells 	rxrpc_cleanup_call(call);
8838c3e34a4SDavid Howells 	_leave("");
8848c3e34a4SDavid Howells }
8858c3e34a4SDavid Howells 
8868c3e34a4SDavid Howells /*
8878c3e34a4SDavid Howells  * preemptively destroy all the call records from a transport endpoint rather
8888c3e34a4SDavid Howells  * than waiting for them to time out
8898c3e34a4SDavid Howells  */
8908c3e34a4SDavid Howells void __exit rxrpc_destroy_all_calls(void)
8918c3e34a4SDavid Howells {
8928c3e34a4SDavid Howells 	struct rxrpc_call *call;
8938c3e34a4SDavid Howells 
8948c3e34a4SDavid Howells 	_enter("");
8958c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
8968c3e34a4SDavid Howells 
8978c3e34a4SDavid Howells 	while (!list_empty(&rxrpc_calls)) {
8988c3e34a4SDavid Howells 		call = list_entry(rxrpc_calls.next, struct rxrpc_call, link);
8998c3e34a4SDavid Howells 		_debug("Zapping call %p", call);
9008c3e34a4SDavid Howells 
9018c3e34a4SDavid Howells 		list_del_init(&call->link);
9028c3e34a4SDavid Howells 
9038c3e34a4SDavid Howells 		switch (atomic_read(&call->usage)) {
9048c3e34a4SDavid Howells 		case 0:
9058c3e34a4SDavid Howells 			ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
9068c3e34a4SDavid Howells 			break;
9078c3e34a4SDavid Howells 		case 1:
9088c3e34a4SDavid Howells 			if (del_timer_sync(&call->deadspan) != 0 &&
9098c3e34a4SDavid Howells 			    call->state != RXRPC_CALL_DEAD)
9108c3e34a4SDavid Howells 				rxrpc_dead_call_expired((unsigned long) call);
9118c3e34a4SDavid Howells 			if (call->state != RXRPC_CALL_DEAD)
9128c3e34a4SDavid Howells 				break;
9138c3e34a4SDavid Howells 		default:
9148c3e34a4SDavid Howells 			pr_err("Call %p still in use (%d,%d,%s,%lx,%lx)!\n",
9158c3e34a4SDavid Howells 			       call, atomic_read(&call->usage),
9168c3e34a4SDavid Howells 			       atomic_read(&call->ackr_not_idle),
9178c3e34a4SDavid Howells 			       rxrpc_call_states[call->state],
9188c3e34a4SDavid Howells 			       call->flags, call->events);
9198c3e34a4SDavid Howells 			if (!skb_queue_empty(&call->rx_queue))
9208c3e34a4SDavid Howells 				pr_err("Rx queue occupied\n");
9218c3e34a4SDavid Howells 			if (!skb_queue_empty(&call->rx_oos_queue))
9228c3e34a4SDavid Howells 				pr_err("OOS queue occupied\n");
9238c3e34a4SDavid Howells 			break;
9248c3e34a4SDavid Howells 		}
9258c3e34a4SDavid Howells 
9268c3e34a4SDavid Howells 		write_unlock_bh(&rxrpc_call_lock);
9278c3e34a4SDavid Howells 		cond_resched();
9288c3e34a4SDavid Howells 		write_lock_bh(&rxrpc_call_lock);
9298c3e34a4SDavid Howells 	}
9308c3e34a4SDavid Howells 
9318c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
9328c3e34a4SDavid Howells 	_leave("");
9338c3e34a4SDavid Howells }
9348c3e34a4SDavid Howells 
9358c3e34a4SDavid Howells /*
9368c3e34a4SDavid Howells  * handle call lifetime being exceeded
9378c3e34a4SDavid Howells  */
9388c3e34a4SDavid Howells static void rxrpc_call_life_expired(unsigned long _call)
9398c3e34a4SDavid Howells {
9408c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
9418c3e34a4SDavid Howells 
9428c3e34a4SDavid Howells 	if (call->state >= RXRPC_CALL_COMPLETE)
9438c3e34a4SDavid Howells 		return;
9448c3e34a4SDavid Howells 
9458c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
9468c3e34a4SDavid Howells 	read_lock_bh(&call->state_lock);
9478c3e34a4SDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE) {
9488c3e34a4SDavid Howells 		set_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events);
9498c3e34a4SDavid Howells 		rxrpc_queue_call(call);
9508c3e34a4SDavid Howells 	}
9518c3e34a4SDavid Howells 	read_unlock_bh(&call->state_lock);
9528c3e34a4SDavid Howells }
9538c3e34a4SDavid Howells 
9548c3e34a4SDavid Howells /*
9558c3e34a4SDavid Howells  * handle resend timer expiry
9568c3e34a4SDavid Howells  * - may not take call->state_lock as this can deadlock against del_timer_sync()
9578c3e34a4SDavid Howells  */
9588c3e34a4SDavid Howells static void rxrpc_resend_time_expired(unsigned long _call)
9598c3e34a4SDavid Howells {
9608c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
9618c3e34a4SDavid Howells 
9628c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
9638c3e34a4SDavid Howells 
9648c3e34a4SDavid Howells 	if (call->state >= RXRPC_CALL_COMPLETE)
9658c3e34a4SDavid Howells 		return;
9668c3e34a4SDavid Howells 
9678c3e34a4SDavid Howells 	clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
9688c3e34a4SDavid Howells 	if (!test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
9698c3e34a4SDavid Howells 		rxrpc_queue_call(call);
9708c3e34a4SDavid Howells }
9718c3e34a4SDavid Howells 
9728c3e34a4SDavid Howells /*
9738c3e34a4SDavid Howells  * handle ACK timer expiry
9748c3e34a4SDavid Howells  */
9758c3e34a4SDavid Howells static void rxrpc_ack_time_expired(unsigned long _call)
9768c3e34a4SDavid Howells {
9778c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
9788c3e34a4SDavid Howells 
9798c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
9808c3e34a4SDavid Howells 
9818c3e34a4SDavid Howells 	if (call->state >= RXRPC_CALL_COMPLETE)
9828c3e34a4SDavid Howells 		return;
9838c3e34a4SDavid Howells 
9848c3e34a4SDavid Howells 	read_lock_bh(&call->state_lock);
9858c3e34a4SDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE &&
9868c3e34a4SDavid Howells 	    !test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events))
9878c3e34a4SDavid Howells 		rxrpc_queue_call(call);
9888c3e34a4SDavid Howells 	read_unlock_bh(&call->state_lock);
9898c3e34a4SDavid Howells }
990