xref: /openbmc/linux/net/rxrpc/call_object.c (revision e8d70ce1)
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  */
289aa390bbeSDavid Howells static struct rxrpc_call *rxrpc_alloc_client_call(struct rxrpc_sock *rx,
290999b69f8SDavid Howells 						  struct sockaddr_rxrpc *srx,
2918c3e34a4SDavid Howells 						  gfp_t gfp)
2928c3e34a4SDavid Howells {
2938c3e34a4SDavid Howells 	struct rxrpc_call *call;
2948c3e34a4SDavid Howells 
2958c3e34a4SDavid Howells 	_enter("");
2968c3e34a4SDavid Howells 
297999b69f8SDavid Howells 	ASSERT(rx->local != NULL);
2988c3e34a4SDavid Howells 
2998c3e34a4SDavid Howells 	call = rxrpc_alloc_call(gfp);
3008c3e34a4SDavid Howells 	if (!call)
3018c3e34a4SDavid Howells 		return ERR_PTR(-ENOMEM);
302999b69f8SDavid Howells 	call->state = RXRPC_CALL_CLIENT_AWAIT_CONN;
3038c3e34a4SDavid Howells 
3048c3e34a4SDavid Howells 	sock_hold(&rx->sk);
3058c3e34a4SDavid Howells 	call->socket = rx;
3068c3e34a4SDavid Howells 	call->rx_data_post = 1;
3078c3e34a4SDavid Howells 
3088c3e34a4SDavid Howells 	/* Record copies of information for hashtable lookup */
30919ffa01cSDavid Howells 	call->family = rx->family;
310999b69f8SDavid Howells 	call->local = rx->local;
31119ffa01cSDavid Howells 	switch (call->family) {
3128c3e34a4SDavid Howells 	case AF_INET:
313999b69f8SDavid Howells 		call->peer_ip.ipv4_addr = srx->transport.sin.sin_addr.s_addr;
3148c3e34a4SDavid Howells 		break;
3158c3e34a4SDavid Howells 	case AF_INET6:
3168c3e34a4SDavid Howells 		memcpy(call->peer_ip.ipv6_addr,
317999b69f8SDavid Howells 		       srx->transport.sin6.sin6_addr.in6_u.u6_addr8,
3188c3e34a4SDavid Howells 		       sizeof(call->peer_ip.ipv6_addr));
3198c3e34a4SDavid Howells 		break;
3208c3e34a4SDavid Howells 	}
321999b69f8SDavid Howells 
322999b69f8SDavid Howells 	call->service_id = srx->srx_service;
323999b69f8SDavid Howells 	call->in_clientflag = 0;
324999b69f8SDavid Howells 
325999b69f8SDavid Howells 	_leave(" = %p", call);
326999b69f8SDavid Howells 	return call;
327999b69f8SDavid Howells }
328999b69f8SDavid Howells 
329999b69f8SDavid Howells /*
330999b69f8SDavid Howells  * Begin client call.
331999b69f8SDavid Howells  */
332999b69f8SDavid Howells static int rxrpc_begin_client_call(struct rxrpc_call *call,
333999b69f8SDavid Howells 				   struct rxrpc_conn_parameters *cp,
334999b69f8SDavid Howells 				   struct sockaddr_rxrpc *srx,
335999b69f8SDavid Howells 				   gfp_t gfp)
336999b69f8SDavid Howells {
337999b69f8SDavid Howells 	int ret;
338999b69f8SDavid Howells 
339999b69f8SDavid Howells 	/* Set up or get a connection record and set the protocol parameters,
340999b69f8SDavid Howells 	 * including channel number and call ID.
341999b69f8SDavid Howells 	 */
342aa390bbeSDavid Howells 	ret = rxrpc_connect_call(call, cp, srx, gfp);
343999b69f8SDavid Howells 	if (ret < 0)
344999b69f8SDavid Howells 		return ret;
345999b69f8SDavid Howells 
346999b69f8SDavid Howells 	call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
347999b69f8SDavid Howells 
3488c3e34a4SDavid Howells 	/* Add the new call to the hashtable */
3498c3e34a4SDavid Howells 	rxrpc_call_hash_add(call);
3508c3e34a4SDavid Howells 
35185f32278SDavid Howells 	spin_lock(&call->conn->params.peer->lock);
35285f32278SDavid Howells 	hlist_add_head(&call->error_link, &call->conn->params.peer->error_targets);
35385f32278SDavid Howells 	spin_unlock(&call->conn->params.peer->lock);
3548c3e34a4SDavid Howells 
3558c3e34a4SDavid Howells 	call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
3568c3e34a4SDavid Howells 	add_timer(&call->lifetimer);
357999b69f8SDavid Howells 	return 0;
3588c3e34a4SDavid Howells }
3598c3e34a4SDavid Howells 
3608c3e34a4SDavid Howells /*
3618c3e34a4SDavid Howells  * set up a call for the given data
3628c3e34a4SDavid Howells  * - called in process context with IRQs enabled
3638c3e34a4SDavid Howells  */
3648c3e34a4SDavid Howells struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx,
36519ffa01cSDavid Howells 					 struct rxrpc_conn_parameters *cp,
366999b69f8SDavid Howells 					 struct sockaddr_rxrpc *srx,
3678c3e34a4SDavid Howells 					 unsigned long user_call_ID,
3688c3e34a4SDavid Howells 					 gfp_t gfp)
3698c3e34a4SDavid Howells {
3708c3e34a4SDavid Howells 	struct rxrpc_call *call, *xcall;
3718c3e34a4SDavid Howells 	struct rb_node *parent, **pp;
372999b69f8SDavid Howells 	int ret;
3738c3e34a4SDavid Howells 
374999b69f8SDavid Howells 	_enter("%p,%lx", rx, user_call_ID);
3758c3e34a4SDavid Howells 
376aa390bbeSDavid Howells 	call = rxrpc_alloc_client_call(rx, srx, gfp);
3778c3e34a4SDavid Howells 	if (IS_ERR(call)) {
3788c3e34a4SDavid Howells 		_leave(" = %ld", PTR_ERR(call));
3798c3e34a4SDavid Howells 		return call;
3808c3e34a4SDavid Howells 	}
3818c3e34a4SDavid Howells 
382999b69f8SDavid Howells 	/* Publish the call, even though it is incompletely set up as yet */
3838c3e34a4SDavid Howells 	call->user_call_ID = user_call_ID;
3848c3e34a4SDavid Howells 	__set_bit(RXRPC_CALL_HAS_USERID, &call->flags);
3858c3e34a4SDavid Howells 
3868c3e34a4SDavid Howells 	write_lock(&rx->call_lock);
3878c3e34a4SDavid Howells 
3888c3e34a4SDavid Howells 	pp = &rx->calls.rb_node;
3898c3e34a4SDavid Howells 	parent = NULL;
3908c3e34a4SDavid Howells 	while (*pp) {
3918c3e34a4SDavid Howells 		parent = *pp;
3928c3e34a4SDavid Howells 		xcall = rb_entry(parent, struct rxrpc_call, sock_node);
3938c3e34a4SDavid Howells 
3948c3e34a4SDavid Howells 		if (user_call_ID < xcall->user_call_ID)
3958c3e34a4SDavid Howells 			pp = &(*pp)->rb_left;
3968c3e34a4SDavid Howells 		else if (user_call_ID > xcall->user_call_ID)
3978c3e34a4SDavid Howells 			pp = &(*pp)->rb_right;
3988c3e34a4SDavid Howells 		else
3998c3e34a4SDavid Howells 			goto found_user_ID_now_present;
4008c3e34a4SDavid Howells 	}
4018c3e34a4SDavid Howells 
4028c3e34a4SDavid Howells 	rxrpc_get_call(call);
4038c3e34a4SDavid Howells 
4048c3e34a4SDavid Howells 	rb_link_node(&call->sock_node, parent, pp);
4058c3e34a4SDavid Howells 	rb_insert_color(&call->sock_node, &rx->calls);
4068c3e34a4SDavid Howells 	write_unlock(&rx->call_lock);
4078c3e34a4SDavid Howells 
4088c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
4098c3e34a4SDavid Howells 	list_add_tail(&call->link, &rxrpc_calls);
4108c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
4118c3e34a4SDavid Howells 
412aa390bbeSDavid Howells 	ret = rxrpc_begin_client_call(call, cp, srx, gfp);
413999b69f8SDavid Howells 	if (ret < 0)
414999b69f8SDavid Howells 		goto error;
415999b69f8SDavid Howells 
4168c3e34a4SDavid Howells 	_net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
4178c3e34a4SDavid Howells 
4188c3e34a4SDavid Howells 	_leave(" = %p [new]", call);
4198c3e34a4SDavid Howells 	return call;
4208c3e34a4SDavid Howells 
421999b69f8SDavid Howells error:
422999b69f8SDavid Howells 	write_lock(&rx->call_lock);
423999b69f8SDavid Howells 	rb_erase(&call->sock_node, &rx->calls);
424999b69f8SDavid Howells 	write_unlock(&rx->call_lock);
425999b69f8SDavid Howells 	rxrpc_put_call(call);
426999b69f8SDavid Howells 
427999b69f8SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
428d1e858c5SDavid Howells 	list_del_init(&call->link);
429999b69f8SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
430999b69f8SDavid Howells 
431d1e858c5SDavid Howells 	call->state = RXRPC_CALL_DEAD;
432999b69f8SDavid Howells 	rxrpc_put_call(call);
433999b69f8SDavid Howells 	_leave(" = %d", ret);
434999b69f8SDavid Howells 	return ERR_PTR(ret);
435999b69f8SDavid Howells 
4368c3e34a4SDavid Howells 	/* We unexpectedly found the user ID in the list after taking
4378c3e34a4SDavid Howells 	 * the call_lock.  This shouldn't happen unless the user races
4388c3e34a4SDavid Howells 	 * with itself and tries to add the same user ID twice at the
4398c3e34a4SDavid Howells 	 * same time in different threads.
4408c3e34a4SDavid Howells 	 */
4418c3e34a4SDavid Howells found_user_ID_now_present:
4428c3e34a4SDavid Howells 	write_unlock(&rx->call_lock);
443d1e858c5SDavid Howells 	call->state = RXRPC_CALL_DEAD;
4448c3e34a4SDavid Howells 	rxrpc_put_call(call);
4458c3e34a4SDavid Howells 	_leave(" = -EEXIST [%p]", call);
4468c3e34a4SDavid Howells 	return ERR_PTR(-EEXIST);
4478c3e34a4SDavid Howells }
4488c3e34a4SDavid Howells 
4498c3e34a4SDavid Howells /*
4508c3e34a4SDavid Howells  * set up an incoming call
4518c3e34a4SDavid Howells  * - called in process context with IRQs enabled
4528c3e34a4SDavid Howells  */
4538c3e34a4SDavid Howells struct rxrpc_call *rxrpc_incoming_call(struct rxrpc_sock *rx,
4548c3e34a4SDavid Howells 				       struct rxrpc_connection *conn,
45542886ffeSDavid Howells 				       struct sk_buff *skb)
4568c3e34a4SDavid Howells {
45742886ffeSDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
4588c3e34a4SDavid Howells 	struct rxrpc_call *call, *candidate;
459a1399f8bSDavid Howells 	u32 call_id, chan;
4608c3e34a4SDavid Howells 
4618c3e34a4SDavid Howells 	_enter(",%d", conn->debug_id);
4628c3e34a4SDavid Howells 
4638c3e34a4SDavid Howells 	ASSERT(rx != NULL);
4648c3e34a4SDavid Howells 
4658c3e34a4SDavid Howells 	candidate = rxrpc_alloc_call(GFP_NOIO);
4668c3e34a4SDavid Howells 	if (!candidate)
4678c3e34a4SDavid Howells 		return ERR_PTR(-EBUSY);
4688c3e34a4SDavid Howells 
469a1399f8bSDavid Howells 	chan = sp->hdr.cid & RXRPC_CHANNELMASK;
4708c3e34a4SDavid Howells 	candidate->socket	= rx;
4718c3e34a4SDavid Howells 	candidate->conn		= conn;
47242886ffeSDavid Howells 	candidate->cid		= sp->hdr.cid;
47342886ffeSDavid Howells 	candidate->call_id	= sp->hdr.callNumber;
474a1399f8bSDavid Howells 	candidate->channel	= chan;
4758c3e34a4SDavid Howells 	candidate->rx_data_post	= 0;
4768c3e34a4SDavid Howells 	candidate->state	= RXRPC_CALL_SERVER_ACCEPTING;
4778c3e34a4SDavid Howells 	if (conn->security_ix > 0)
4788c3e34a4SDavid Howells 		candidate->state = RXRPC_CALL_SERVER_SECURING;
4798c3e34a4SDavid Howells 
480a1399f8bSDavid Howells 	spin_lock(&conn->channel_lock);
4818c3e34a4SDavid Howells 
4828c3e34a4SDavid Howells 	/* set the channel for this call */
483a1399f8bSDavid Howells 	call = rcu_dereference_protected(conn->channels[chan].call,
484a1399f8bSDavid Howells 					 lockdep_is_held(&conn->channel_lock));
485a1399f8bSDavid Howells 
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) {
514a1399f8bSDavid Howells 			__rxrpc_disconnect_call(call);
5158c3e34a4SDavid Howells 		} else {
516a1399f8bSDavid Howells 			spin_unlock(&conn->channel_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 
527a1399f8bSDavid Howells 	/* We just ignore calls prior to the current call ID.  Terminated calls
528a1399f8bSDavid Howells 	 * are handled via the connection.
5298c3e34a4SDavid Howells 	 */
530a1399f8bSDavid Howells 	if (call_id <= conn->channels[chan].call_counter)
531a1399f8bSDavid Howells 		goto old_call; /* TODO: Just drop packet */
5328c3e34a4SDavid Howells 
5338c3e34a4SDavid Howells 	/* make the call available */
5348c3e34a4SDavid Howells 	_debug("new call");
5358c3e34a4SDavid Howells 	call = candidate;
5368c3e34a4SDavid Howells 	candidate = NULL;
537a1399f8bSDavid Howells 	conn->channels[chan].call_counter = call_id;
538a1399f8bSDavid Howells 	rcu_assign_pointer(conn->channels[chan].call, call);
5398c3e34a4SDavid Howells 	sock_hold(&rx->sk);
5405627cc8bSDavid Howells 	rxrpc_get_connection(conn);
541a1399f8bSDavid Howells 	spin_unlock(&conn->channel_lock);
5428c3e34a4SDavid Howells 
54385f32278SDavid Howells 	spin_lock(&conn->params.peer->lock);
54485f32278SDavid Howells 	hlist_add_head(&call->error_link, &conn->params.peer->error_targets);
54585f32278SDavid Howells 	spin_unlock(&conn->params.peer->lock);
5468c3e34a4SDavid Howells 
5478c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
5488c3e34a4SDavid Howells 	list_add_tail(&call->link, &rxrpc_calls);
5498c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
5508c3e34a4SDavid Howells 
5518c3e34a4SDavid Howells 	/* Record copies of information for hashtable lookup */
55219ffa01cSDavid Howells 	call->family = rx->family;
55385f32278SDavid Howells 	call->local = conn->params.local;
55419ffa01cSDavid Howells 	switch (call->family) {
5558c3e34a4SDavid Howells 	case AF_INET:
5568c3e34a4SDavid Howells 		call->peer_ip.ipv4_addr =
55785f32278SDavid Howells 			conn->params.peer->srx.transport.sin.sin_addr.s_addr;
5588c3e34a4SDavid Howells 		break;
5598c3e34a4SDavid Howells 	case AF_INET6:
5608c3e34a4SDavid Howells 		memcpy(call->peer_ip.ipv6_addr,
56185f32278SDavid Howells 		       conn->params.peer->srx.transport.sin6.sin6_addr.in6_u.u6_addr8,
5628c3e34a4SDavid Howells 		       sizeof(call->peer_ip.ipv6_addr));
5638c3e34a4SDavid Howells 		break;
5648c3e34a4SDavid Howells 	default:
5658c3e34a4SDavid Howells 		break;
5668c3e34a4SDavid Howells 	}
56719ffa01cSDavid Howells 	call->epoch = conn->proto.epoch;
56819ffa01cSDavid Howells 	call->service_id = conn->params.service_id;
569e8d70ce1SDavid Howells 	call->in_clientflag = RXRPC_CLIENT_INITIATED;
5708c3e34a4SDavid Howells 	/* Add the new call to the hashtable */
5718c3e34a4SDavid Howells 	rxrpc_call_hash_add(call);
5728c3e34a4SDavid Howells 
5738c3e34a4SDavid Howells 	_net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
5748c3e34a4SDavid Howells 
5758c3e34a4SDavid Howells 	call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
5768c3e34a4SDavid Howells 	add_timer(&call->lifetimer);
5778c3e34a4SDavid Howells 	_leave(" = %p {%d} [new]", call, call->debug_id);
5788c3e34a4SDavid Howells 	return call;
5798c3e34a4SDavid Howells 
5808c3e34a4SDavid Howells extant_call:
581a1399f8bSDavid Howells 	spin_unlock(&conn->channel_lock);
5828c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, candidate);
5838c3e34a4SDavid Howells 	_leave(" = %p {%d} [extant]", call, call ? call->debug_id : -1);
5848c3e34a4SDavid Howells 	return call;
5858c3e34a4SDavid Howells 
5868c3e34a4SDavid Howells aborted_call:
587a1399f8bSDavid Howells 	spin_unlock(&conn->channel_lock);
5888c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, candidate);
5898c3e34a4SDavid Howells 	_leave(" = -ECONNABORTED");
5908c3e34a4SDavid Howells 	return ERR_PTR(-ECONNABORTED);
5918c3e34a4SDavid Howells 
5928c3e34a4SDavid Howells old_call:
593a1399f8bSDavid Howells 	spin_unlock(&conn->channel_lock);
5948c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, candidate);
5958c3e34a4SDavid Howells 	_leave(" = -ECONNRESET [old]");
5968c3e34a4SDavid Howells 	return ERR_PTR(-ECONNRESET);
5978c3e34a4SDavid Howells }
5988c3e34a4SDavid Howells 
5998c3e34a4SDavid Howells /*
6008c3e34a4SDavid Howells  * detach a call from a socket and set up for release
6018c3e34a4SDavid Howells  */
6028c3e34a4SDavid Howells void rxrpc_release_call(struct rxrpc_call *call)
6038c3e34a4SDavid Howells {
6048c3e34a4SDavid Howells 	struct rxrpc_connection *conn = call->conn;
6058c3e34a4SDavid Howells 	struct rxrpc_sock *rx = call->socket;
6068c3e34a4SDavid Howells 
6078c3e34a4SDavid Howells 	_enter("{%d,%d,%d,%d}",
6088c3e34a4SDavid Howells 	       call->debug_id, atomic_read(&call->usage),
6098c3e34a4SDavid Howells 	       atomic_read(&call->ackr_not_idle),
6108c3e34a4SDavid Howells 	       call->rx_first_oos);
6118c3e34a4SDavid Howells 
6128c3e34a4SDavid Howells 	spin_lock_bh(&call->lock);
6138c3e34a4SDavid Howells 	if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
6148c3e34a4SDavid Howells 		BUG();
6158c3e34a4SDavid Howells 	spin_unlock_bh(&call->lock);
6168c3e34a4SDavid Howells 
6178c3e34a4SDavid Howells 	/* dissociate from the socket
6188c3e34a4SDavid Howells 	 * - the socket's ref on the call is passed to the death timer
6198c3e34a4SDavid Howells 	 */
6208c3e34a4SDavid Howells 	_debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
6218c3e34a4SDavid Howells 
622e653cfe4SDavid Howells 	spin_lock(&conn->params.peer->lock);
623e653cfe4SDavid Howells 	hlist_del_init(&call->error_link);
624e653cfe4SDavid Howells 	spin_unlock(&conn->params.peer->lock);
625e653cfe4SDavid Howells 
6268c3e34a4SDavid Howells 	write_lock_bh(&rx->call_lock);
6278c3e34a4SDavid Howells 	if (!list_empty(&call->accept_link)) {
6288c3e34a4SDavid Howells 		_debug("unlinking once-pending call %p { e=%lx f=%lx }",
6298c3e34a4SDavid Howells 		       call, call->events, call->flags);
6308c3e34a4SDavid Howells 		ASSERT(!test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
6318c3e34a4SDavid Howells 		list_del_init(&call->accept_link);
6328c3e34a4SDavid Howells 		sk_acceptq_removed(&rx->sk);
6338c3e34a4SDavid Howells 	} else if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
6348c3e34a4SDavid Howells 		rb_erase(&call->sock_node, &rx->calls);
6358c3e34a4SDavid Howells 		memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
6368c3e34a4SDavid Howells 		clear_bit(RXRPC_CALL_HAS_USERID, &call->flags);
6378c3e34a4SDavid Howells 	}
6388c3e34a4SDavid Howells 	write_unlock_bh(&rx->call_lock);
6398c3e34a4SDavid Howells 
6408c3e34a4SDavid Howells 	/* free up the channel for reuse */
641a1399f8bSDavid Howells 	write_lock_bh(&call->state_lock);
6428c3e34a4SDavid Howells 
6438c3e34a4SDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE &&
6448c3e34a4SDavid Howells 	    call->state != RXRPC_CALL_CLIENT_FINAL_ACK) {
6458c3e34a4SDavid Howells 		_debug("+++ ABORTING STATE %d +++\n", call->state);
6468c3e34a4SDavid Howells 		call->state = RXRPC_CALL_LOCALLY_ABORTED;
6478c3e34a4SDavid Howells 		call->local_abort = RX_CALL_DEAD;
6488c3e34a4SDavid Howells 	}
649a1399f8bSDavid Howells 	write_unlock_bh(&call->state_lock);
6508c3e34a4SDavid Howells 
651e653cfe4SDavid Howells 	rxrpc_disconnect_call(call);
652e653cfe4SDavid Howells 
6538c3e34a4SDavid Howells 	/* clean up the Rx queue */
6548c3e34a4SDavid Howells 	if (!skb_queue_empty(&call->rx_queue) ||
6558c3e34a4SDavid Howells 	    !skb_queue_empty(&call->rx_oos_queue)) {
6568c3e34a4SDavid Howells 		struct rxrpc_skb_priv *sp;
6578c3e34a4SDavid Howells 		struct sk_buff *skb;
6588c3e34a4SDavid Howells 
6598c3e34a4SDavid Howells 		_debug("purge Rx queues");
6608c3e34a4SDavid Howells 
6618c3e34a4SDavid Howells 		spin_lock_bh(&call->lock);
6628c3e34a4SDavid Howells 		while ((skb = skb_dequeue(&call->rx_queue)) ||
6638c3e34a4SDavid Howells 		       (skb = skb_dequeue(&call->rx_oos_queue))) {
6648c3e34a4SDavid Howells 			sp = rxrpc_skb(skb);
6658c3e34a4SDavid Howells 			if (sp->call) {
6668c3e34a4SDavid Howells 				ASSERTCMP(sp->call, ==, call);
6678c3e34a4SDavid Howells 				rxrpc_put_call(call);
6688c3e34a4SDavid Howells 				sp->call = NULL;
6698c3e34a4SDavid Howells 			}
6708c3e34a4SDavid Howells 			skb->destructor = NULL;
6718c3e34a4SDavid Howells 			spin_unlock_bh(&call->lock);
6728c3e34a4SDavid Howells 
6738c3e34a4SDavid Howells 			_debug("- zap %s %%%u #%u",
6748c3e34a4SDavid Howells 			       rxrpc_pkts[sp->hdr.type],
6758c3e34a4SDavid Howells 			       sp->hdr.serial, sp->hdr.seq);
6768c3e34a4SDavid Howells 			rxrpc_free_skb(skb);
6778c3e34a4SDavid Howells 			spin_lock_bh(&call->lock);
6788c3e34a4SDavid Howells 		}
6798c3e34a4SDavid Howells 		spin_unlock_bh(&call->lock);
6808c3e34a4SDavid Howells 
6818c3e34a4SDavid Howells 		ASSERTCMP(call->state, !=, RXRPC_CALL_COMPLETE);
6828c3e34a4SDavid Howells 	}
6838c3e34a4SDavid Howells 
6848c3e34a4SDavid Howells 	del_timer_sync(&call->resend_timer);
6858c3e34a4SDavid Howells 	del_timer_sync(&call->ack_timer);
6868c3e34a4SDavid Howells 	del_timer_sync(&call->lifetimer);
6878c3e34a4SDavid Howells 	call->deadspan.expires = jiffies + rxrpc_dead_call_expiry;
6888c3e34a4SDavid Howells 	add_timer(&call->deadspan);
6898c3e34a4SDavid Howells 
6908c3e34a4SDavid Howells 	_leave("");
6918c3e34a4SDavid Howells }
6928c3e34a4SDavid Howells 
6938c3e34a4SDavid Howells /*
6948c3e34a4SDavid Howells  * handle a dead call being ready for reaping
6958c3e34a4SDavid Howells  */
6968c3e34a4SDavid Howells static void rxrpc_dead_call_expired(unsigned long _call)
6978c3e34a4SDavid Howells {
6988c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
6998c3e34a4SDavid Howells 
7008c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
7018c3e34a4SDavid Howells 
7028c3e34a4SDavid Howells 	write_lock_bh(&call->state_lock);
7038c3e34a4SDavid Howells 	call->state = RXRPC_CALL_DEAD;
7048c3e34a4SDavid Howells 	write_unlock_bh(&call->state_lock);
7058c3e34a4SDavid Howells 	rxrpc_put_call(call);
7068c3e34a4SDavid Howells }
7078c3e34a4SDavid Howells 
7088c3e34a4SDavid Howells /*
7098c3e34a4SDavid Howells  * mark a call as to be released, aborting it if it's still in progress
7108c3e34a4SDavid Howells  * - called with softirqs disabled
7118c3e34a4SDavid Howells  */
7128c3e34a4SDavid Howells static void rxrpc_mark_call_released(struct rxrpc_call *call)
7138c3e34a4SDavid Howells {
7148c3e34a4SDavid Howells 	bool sched;
7158c3e34a4SDavid Howells 
7168c3e34a4SDavid Howells 	write_lock(&call->state_lock);
7178c3e34a4SDavid Howells 	if (call->state < RXRPC_CALL_DEAD) {
7188c3e34a4SDavid Howells 		sched = false;
7198c3e34a4SDavid Howells 		if (call->state < RXRPC_CALL_COMPLETE) {
7208c3e34a4SDavid Howells 			_debug("abort call %p", call);
7218c3e34a4SDavid Howells 			call->state = RXRPC_CALL_LOCALLY_ABORTED;
7228c3e34a4SDavid Howells 			call->local_abort = RX_CALL_DEAD;
7238c3e34a4SDavid Howells 			if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events))
7248c3e34a4SDavid Howells 				sched = true;
7258c3e34a4SDavid Howells 		}
7268c3e34a4SDavid Howells 		if (!test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
7278c3e34a4SDavid Howells 			sched = true;
7288c3e34a4SDavid Howells 		if (sched)
7298c3e34a4SDavid Howells 			rxrpc_queue_call(call);
7308c3e34a4SDavid Howells 	}
7318c3e34a4SDavid Howells 	write_unlock(&call->state_lock);
7328c3e34a4SDavid Howells }
7338c3e34a4SDavid Howells 
7348c3e34a4SDavid Howells /*
7358c3e34a4SDavid Howells  * release all the calls associated with a socket
7368c3e34a4SDavid Howells  */
7378c3e34a4SDavid Howells void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
7388c3e34a4SDavid Howells {
7398c3e34a4SDavid Howells 	struct rxrpc_call *call;
7408c3e34a4SDavid Howells 	struct rb_node *p;
7418c3e34a4SDavid Howells 
7428c3e34a4SDavid Howells 	_enter("%p", rx);
7438c3e34a4SDavid Howells 
7448c3e34a4SDavid Howells 	read_lock_bh(&rx->call_lock);
7458c3e34a4SDavid Howells 
7468c3e34a4SDavid Howells 	/* mark all the calls as no longer wanting incoming packets */
7478c3e34a4SDavid Howells 	for (p = rb_first(&rx->calls); p; p = rb_next(p)) {
7488c3e34a4SDavid Howells 		call = rb_entry(p, struct rxrpc_call, sock_node);
7498c3e34a4SDavid Howells 		rxrpc_mark_call_released(call);
7508c3e34a4SDavid Howells 	}
7518c3e34a4SDavid Howells 
7528c3e34a4SDavid Howells 	/* kill the not-yet-accepted incoming calls */
7538c3e34a4SDavid Howells 	list_for_each_entry(call, &rx->secureq, accept_link) {
7548c3e34a4SDavid Howells 		rxrpc_mark_call_released(call);
7558c3e34a4SDavid Howells 	}
7568c3e34a4SDavid Howells 
7578c3e34a4SDavid Howells 	list_for_each_entry(call, &rx->acceptq, accept_link) {
7588c3e34a4SDavid Howells 		rxrpc_mark_call_released(call);
7598c3e34a4SDavid Howells 	}
7608c3e34a4SDavid Howells 
7618c3e34a4SDavid Howells 	read_unlock_bh(&rx->call_lock);
7628c3e34a4SDavid Howells 	_leave("");
7638c3e34a4SDavid Howells }
7648c3e34a4SDavid Howells 
7658c3e34a4SDavid Howells /*
7668c3e34a4SDavid Howells  * release a call
7678c3e34a4SDavid Howells  */
7688c3e34a4SDavid Howells void __rxrpc_put_call(struct rxrpc_call *call)
7698c3e34a4SDavid Howells {
7708c3e34a4SDavid Howells 	ASSERT(call != NULL);
7718c3e34a4SDavid Howells 
7728c3e34a4SDavid Howells 	_enter("%p{u=%d}", call, atomic_read(&call->usage));
7738c3e34a4SDavid Howells 
7748c3e34a4SDavid Howells 	ASSERTCMP(atomic_read(&call->usage), >, 0);
7758c3e34a4SDavid Howells 
7768c3e34a4SDavid Howells 	if (atomic_dec_and_test(&call->usage)) {
7778c3e34a4SDavid Howells 		_debug("call %d dead", call->debug_id);
7788c3e34a4SDavid Howells 		ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
7798c3e34a4SDavid Howells 		rxrpc_queue_work(&call->destroyer);
7808c3e34a4SDavid Howells 	}
7818c3e34a4SDavid Howells 	_leave("");
7828c3e34a4SDavid Howells }
7838c3e34a4SDavid Howells 
7848c3e34a4SDavid Howells /*
785dee46364SDavid Howells  * Final call destruction under RCU.
786dee46364SDavid Howells  */
787dee46364SDavid Howells static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
788dee46364SDavid Howells {
789dee46364SDavid Howells 	struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
790dee46364SDavid Howells 
791dee46364SDavid Howells 	rxrpc_purge_queue(&call->rx_queue);
792dee46364SDavid Howells 	kmem_cache_free(rxrpc_call_jar, call);
793dee46364SDavid Howells }
794dee46364SDavid Howells 
795dee46364SDavid Howells /*
7968c3e34a4SDavid Howells  * clean up a call
7978c3e34a4SDavid Howells  */
7988c3e34a4SDavid Howells static void rxrpc_cleanup_call(struct rxrpc_call *call)
7998c3e34a4SDavid Howells {
8008c3e34a4SDavid Howells 	_net("DESTROY CALL %d", call->debug_id);
8018c3e34a4SDavid Howells 
8028c3e34a4SDavid Howells 	ASSERT(call->socket);
8038c3e34a4SDavid Howells 
8048c3e34a4SDavid Howells 	memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
8058c3e34a4SDavid Howells 
8068c3e34a4SDavid Howells 	del_timer_sync(&call->lifetimer);
8078c3e34a4SDavid Howells 	del_timer_sync(&call->deadspan);
8088c3e34a4SDavid Howells 	del_timer_sync(&call->ack_timer);
8098c3e34a4SDavid Howells 	del_timer_sync(&call->resend_timer);
8108c3e34a4SDavid Howells 
8118c3e34a4SDavid Howells 	ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
8128c3e34a4SDavid Howells 	ASSERTCMP(call->events, ==, 0);
8138c3e34a4SDavid Howells 	if (work_pending(&call->processor)) {
8148c3e34a4SDavid Howells 		_debug("defer destroy");
8158c3e34a4SDavid Howells 		rxrpc_queue_work(&call->destroyer);
8168c3e34a4SDavid Howells 		return;
8178c3e34a4SDavid Howells 	}
8188c3e34a4SDavid Howells 
819e653cfe4SDavid Howells 	ASSERTCMP(call->conn, ==, NULL);
8208c3e34a4SDavid Howells 
8218c3e34a4SDavid Howells 	/* Remove the call from the hash */
8228c3e34a4SDavid Howells 	rxrpc_call_hash_del(call);
8238c3e34a4SDavid Howells 
8248c3e34a4SDavid Howells 	if (call->acks_window) {
8258c3e34a4SDavid Howells 		_debug("kill Tx window %d",
8268c3e34a4SDavid Howells 		       CIRC_CNT(call->acks_head, call->acks_tail,
8278c3e34a4SDavid Howells 				call->acks_winsz));
8288c3e34a4SDavid Howells 		smp_mb();
8298c3e34a4SDavid Howells 		while (CIRC_CNT(call->acks_head, call->acks_tail,
8308c3e34a4SDavid Howells 				call->acks_winsz) > 0) {
8318c3e34a4SDavid Howells 			struct rxrpc_skb_priv *sp;
8328c3e34a4SDavid Howells 			unsigned long _skb;
8338c3e34a4SDavid Howells 
8348c3e34a4SDavid Howells 			_skb = call->acks_window[call->acks_tail] & ~1;
8358c3e34a4SDavid Howells 			sp = rxrpc_skb((struct sk_buff *)_skb);
8368c3e34a4SDavid Howells 			_debug("+++ clear Tx %u", sp->hdr.seq);
8378c3e34a4SDavid Howells 			rxrpc_free_skb((struct sk_buff *)_skb);
8388c3e34a4SDavid Howells 			call->acks_tail =
8398c3e34a4SDavid Howells 				(call->acks_tail + 1) & (call->acks_winsz - 1);
8408c3e34a4SDavid Howells 		}
8418c3e34a4SDavid Howells 
8428c3e34a4SDavid Howells 		kfree(call->acks_window);
8438c3e34a4SDavid Howells 	}
8448c3e34a4SDavid Howells 
8458c3e34a4SDavid Howells 	rxrpc_free_skb(call->tx_pending);
8468c3e34a4SDavid Howells 
8478c3e34a4SDavid Howells 	rxrpc_purge_queue(&call->rx_queue);
8488c3e34a4SDavid Howells 	ASSERT(skb_queue_empty(&call->rx_oos_queue));
8498c3e34a4SDavid Howells 	sock_put(&call->socket->sk);
850dee46364SDavid Howells 	call_rcu(&call->rcu, rxrpc_rcu_destroy_call);
8518c3e34a4SDavid Howells }
8528c3e34a4SDavid Howells 
8538c3e34a4SDavid Howells /*
8548c3e34a4SDavid Howells  * destroy a call
8558c3e34a4SDavid Howells  */
8568c3e34a4SDavid Howells static void rxrpc_destroy_call(struct work_struct *work)
8578c3e34a4SDavid Howells {
8588c3e34a4SDavid Howells 	struct rxrpc_call *call =
8598c3e34a4SDavid Howells 		container_of(work, struct rxrpc_call, destroyer);
8608c3e34a4SDavid Howells 
8618c3e34a4SDavid Howells 	_enter("%p{%d,%d,%p}",
8628c3e34a4SDavid Howells 	       call, atomic_read(&call->usage), call->channel, call->conn);
8638c3e34a4SDavid Howells 
8648c3e34a4SDavid Howells 	ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
8658c3e34a4SDavid Howells 
8668c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
8678c3e34a4SDavid Howells 	list_del_init(&call->link);
8688c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
8698c3e34a4SDavid Howells 
8708c3e34a4SDavid Howells 	rxrpc_cleanup_call(call);
8718c3e34a4SDavid Howells 	_leave("");
8728c3e34a4SDavid Howells }
8738c3e34a4SDavid Howells 
8748c3e34a4SDavid Howells /*
8758c3e34a4SDavid Howells  * preemptively destroy all the call records from a transport endpoint rather
8768c3e34a4SDavid Howells  * than waiting for them to time out
8778c3e34a4SDavid Howells  */
8788c3e34a4SDavid Howells void __exit rxrpc_destroy_all_calls(void)
8798c3e34a4SDavid Howells {
8808c3e34a4SDavid Howells 	struct rxrpc_call *call;
8818c3e34a4SDavid Howells 
8828c3e34a4SDavid Howells 	_enter("");
8838c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
8848c3e34a4SDavid Howells 
8858c3e34a4SDavid Howells 	while (!list_empty(&rxrpc_calls)) {
8868c3e34a4SDavid Howells 		call = list_entry(rxrpc_calls.next, struct rxrpc_call, link);
8878c3e34a4SDavid Howells 		_debug("Zapping call %p", call);
8888c3e34a4SDavid Howells 
8898c3e34a4SDavid Howells 		list_del_init(&call->link);
8908c3e34a4SDavid Howells 
8918c3e34a4SDavid Howells 		switch (atomic_read(&call->usage)) {
8928c3e34a4SDavid Howells 		case 0:
8938c3e34a4SDavid Howells 			ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
8948c3e34a4SDavid Howells 			break;
8958c3e34a4SDavid Howells 		case 1:
8968c3e34a4SDavid Howells 			if (del_timer_sync(&call->deadspan) != 0 &&
8978c3e34a4SDavid Howells 			    call->state != RXRPC_CALL_DEAD)
8988c3e34a4SDavid Howells 				rxrpc_dead_call_expired((unsigned long) call);
8998c3e34a4SDavid Howells 			if (call->state != RXRPC_CALL_DEAD)
9008c3e34a4SDavid Howells 				break;
9018c3e34a4SDavid Howells 		default:
9028c3e34a4SDavid Howells 			pr_err("Call %p still in use (%d,%d,%s,%lx,%lx)!\n",
9038c3e34a4SDavid Howells 			       call, atomic_read(&call->usage),
9048c3e34a4SDavid Howells 			       atomic_read(&call->ackr_not_idle),
9058c3e34a4SDavid Howells 			       rxrpc_call_states[call->state],
9068c3e34a4SDavid Howells 			       call->flags, call->events);
9078c3e34a4SDavid Howells 			if (!skb_queue_empty(&call->rx_queue))
9088c3e34a4SDavid Howells 				pr_err("Rx queue occupied\n");
9098c3e34a4SDavid Howells 			if (!skb_queue_empty(&call->rx_oos_queue))
9108c3e34a4SDavid Howells 				pr_err("OOS queue occupied\n");
9118c3e34a4SDavid Howells 			break;
9128c3e34a4SDavid Howells 		}
9138c3e34a4SDavid Howells 
9148c3e34a4SDavid Howells 		write_unlock_bh(&rxrpc_call_lock);
9158c3e34a4SDavid Howells 		cond_resched();
9168c3e34a4SDavid Howells 		write_lock_bh(&rxrpc_call_lock);
9178c3e34a4SDavid Howells 	}
9188c3e34a4SDavid Howells 
9198c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
9208c3e34a4SDavid Howells 	_leave("");
9218c3e34a4SDavid Howells }
9228c3e34a4SDavid Howells 
9238c3e34a4SDavid Howells /*
9248c3e34a4SDavid Howells  * handle call lifetime being exceeded
9258c3e34a4SDavid Howells  */
9268c3e34a4SDavid Howells static void rxrpc_call_life_expired(unsigned long _call)
9278c3e34a4SDavid Howells {
9288c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
9298c3e34a4SDavid Howells 
9308c3e34a4SDavid Howells 	if (call->state >= RXRPC_CALL_COMPLETE)
9318c3e34a4SDavid Howells 		return;
9328c3e34a4SDavid Howells 
9338c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
9348c3e34a4SDavid Howells 	read_lock_bh(&call->state_lock);
9358c3e34a4SDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE) {
9368c3e34a4SDavid Howells 		set_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events);
9378c3e34a4SDavid Howells 		rxrpc_queue_call(call);
9388c3e34a4SDavid Howells 	}
9398c3e34a4SDavid Howells 	read_unlock_bh(&call->state_lock);
9408c3e34a4SDavid Howells }
9418c3e34a4SDavid Howells 
9428c3e34a4SDavid Howells /*
9438c3e34a4SDavid Howells  * handle resend timer expiry
9448c3e34a4SDavid Howells  * - may not take call->state_lock as this can deadlock against del_timer_sync()
9458c3e34a4SDavid Howells  */
9468c3e34a4SDavid Howells static void rxrpc_resend_time_expired(unsigned long _call)
9478c3e34a4SDavid Howells {
9488c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
9498c3e34a4SDavid Howells 
9508c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
9518c3e34a4SDavid Howells 
9528c3e34a4SDavid Howells 	if (call->state >= RXRPC_CALL_COMPLETE)
9538c3e34a4SDavid Howells 		return;
9548c3e34a4SDavid Howells 
9558c3e34a4SDavid Howells 	clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
9568c3e34a4SDavid Howells 	if (!test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
9578c3e34a4SDavid Howells 		rxrpc_queue_call(call);
9588c3e34a4SDavid Howells }
9598c3e34a4SDavid Howells 
9608c3e34a4SDavid Howells /*
9618c3e34a4SDavid Howells  * handle ACK timer expiry
9628c3e34a4SDavid Howells  */
9638c3e34a4SDavid Howells static void rxrpc_ack_time_expired(unsigned long _call)
9648c3e34a4SDavid Howells {
9658c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
9668c3e34a4SDavid Howells 
9678c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
9688c3e34a4SDavid Howells 
9698c3e34a4SDavid Howells 	if (call->state >= RXRPC_CALL_COMPLETE)
9708c3e34a4SDavid Howells 		return;
9718c3e34a4SDavid Howells 
9728c3e34a4SDavid Howells 	read_lock_bh(&call->state_lock);
9738c3e34a4SDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE &&
9748c3e34a4SDavid Howells 	    !test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events))
9758c3e34a4SDavid Howells 		rxrpc_queue_call(call);
9768c3e34a4SDavid Howells 	read_unlock_bh(&call->state_lock);
9778c3e34a4SDavid Howells }
978