xref: /openbmc/linux/net/rxrpc/call_object.c (revision dee46364)
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;
4598c3e34a4SDavid Howells 	struct rb_node **p, *parent;
4608c3e34a4SDavid Howells 	u32 call_id;
4618c3e34a4SDavid Howells 
4628c3e34a4SDavid Howells 	_enter(",%d", conn->debug_id);
4638c3e34a4SDavid Howells 
4648c3e34a4SDavid Howells 	ASSERT(rx != NULL);
4658c3e34a4SDavid Howells 
4668c3e34a4SDavid Howells 	candidate = rxrpc_alloc_call(GFP_NOIO);
4678c3e34a4SDavid Howells 	if (!candidate)
4688c3e34a4SDavid Howells 		return ERR_PTR(-EBUSY);
4698c3e34a4SDavid Howells 
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;
47442886ffeSDavid Howells 	candidate->channel	= sp->hdr.cid & RXRPC_CHANNELMASK;
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 
4808c3e34a4SDavid Howells 	write_lock_bh(&conn->lock);
4818c3e34a4SDavid Howells 
4828c3e34a4SDavid Howells 	/* set the channel for this call */
483dee46364SDavid Howells 	call = rcu_dereference_protected(conn->channels[candidate->channel],
484dee46364SDavid Howells 					 lockdep_is_held(&conn->lock));
4858c3e34a4SDavid Howells 	_debug("channel[%u] is %p", candidate->channel, call);
48642886ffeSDavid Howells 	if (call && call->call_id == sp->hdr.callNumber) {
4878c3e34a4SDavid Howells 		/* already set; must've been a duplicate packet */
4888c3e34a4SDavid Howells 		_debug("extant call [%d]", call->state);
4898c3e34a4SDavid Howells 		ASSERTCMP(call->conn, ==, conn);
4908c3e34a4SDavid Howells 
4918c3e34a4SDavid Howells 		read_lock(&call->state_lock);
4928c3e34a4SDavid Howells 		switch (call->state) {
4938c3e34a4SDavid Howells 		case RXRPC_CALL_LOCALLY_ABORTED:
4948c3e34a4SDavid Howells 			if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events))
4958c3e34a4SDavid Howells 				rxrpc_queue_call(call);
4968c3e34a4SDavid Howells 		case RXRPC_CALL_REMOTELY_ABORTED:
4978c3e34a4SDavid Howells 			read_unlock(&call->state_lock);
4988c3e34a4SDavid Howells 			goto aborted_call;
4998c3e34a4SDavid Howells 		default:
5008c3e34a4SDavid Howells 			rxrpc_get_call(call);
5018c3e34a4SDavid Howells 			read_unlock(&call->state_lock);
5028c3e34a4SDavid Howells 			goto extant_call;
5038c3e34a4SDavid Howells 		}
5048c3e34a4SDavid Howells 	}
5058c3e34a4SDavid Howells 
5068c3e34a4SDavid Howells 	if (call) {
5078c3e34a4SDavid Howells 		/* it seems the channel is still in use from the previous call
5088c3e34a4SDavid Howells 		 * - ditch the old binding if its call is now complete */
5098c3e34a4SDavid Howells 		_debug("CALL: %u { %s }",
5108c3e34a4SDavid Howells 		       call->debug_id, rxrpc_call_states[call->state]);
5118c3e34a4SDavid Howells 
5128c3e34a4SDavid Howells 		if (call->state >= RXRPC_CALL_COMPLETE) {
5138c3e34a4SDavid Howells 			conn->channels[call->channel] = NULL;
5148c3e34a4SDavid Howells 		} else {
5158c3e34a4SDavid Howells 			write_unlock_bh(&conn->lock);
5168c3e34a4SDavid Howells 			kmem_cache_free(rxrpc_call_jar, candidate);
5178c3e34a4SDavid Howells 			_leave(" = -EBUSY");
5188c3e34a4SDavid Howells 			return ERR_PTR(-EBUSY);
5198c3e34a4SDavid Howells 		}
5208c3e34a4SDavid Howells 	}
5218c3e34a4SDavid Howells 
5228c3e34a4SDavid Howells 	/* check the call number isn't duplicate */
5238c3e34a4SDavid Howells 	_debug("check dup");
52442886ffeSDavid Howells 	call_id = sp->hdr.callNumber;
5258c3e34a4SDavid Howells 	p = &conn->calls.rb_node;
5268c3e34a4SDavid Howells 	parent = NULL;
5278c3e34a4SDavid Howells 	while (*p) {
5288c3e34a4SDavid Howells 		parent = *p;
5298c3e34a4SDavid Howells 		call = rb_entry(parent, struct rxrpc_call, conn_node);
5308c3e34a4SDavid Howells 
5318c3e34a4SDavid Howells 		/* The tree is sorted in order of the __be32 value without
5328c3e34a4SDavid Howells 		 * turning it into host order.
5338c3e34a4SDavid Howells 		 */
5348c3e34a4SDavid Howells 		if (call_id < call->call_id)
5358c3e34a4SDavid Howells 			p = &(*p)->rb_left;
5368c3e34a4SDavid Howells 		else if (call_id > call->call_id)
5378c3e34a4SDavid Howells 			p = &(*p)->rb_right;
5388c3e34a4SDavid Howells 		else
5398c3e34a4SDavid Howells 			goto old_call;
5408c3e34a4SDavid Howells 	}
5418c3e34a4SDavid Howells 
5428c3e34a4SDavid Howells 	/* make the call available */
5438c3e34a4SDavid Howells 	_debug("new call");
5448c3e34a4SDavid Howells 	call = candidate;
5458c3e34a4SDavid Howells 	candidate = NULL;
5468c3e34a4SDavid Howells 	rb_link_node(&call->conn_node, parent, p);
5478c3e34a4SDavid Howells 	rb_insert_color(&call->conn_node, &conn->calls);
548dee46364SDavid Howells 	rcu_assign_pointer(conn->channels[call->channel], call);
5498c3e34a4SDavid Howells 	sock_hold(&rx->sk);
5505627cc8bSDavid Howells 	rxrpc_get_connection(conn);
5518c3e34a4SDavid Howells 	write_unlock_bh(&conn->lock);
5528c3e34a4SDavid Howells 
55385f32278SDavid Howells 	spin_lock(&conn->params.peer->lock);
55485f32278SDavid Howells 	hlist_add_head(&call->error_link, &conn->params.peer->error_targets);
55585f32278SDavid Howells 	spin_unlock(&conn->params.peer->lock);
5568c3e34a4SDavid Howells 
5578c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
5588c3e34a4SDavid Howells 	list_add_tail(&call->link, &rxrpc_calls);
5598c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
5608c3e34a4SDavid Howells 
5618c3e34a4SDavid Howells 	/* Record copies of information for hashtable lookup */
56219ffa01cSDavid Howells 	call->family = rx->family;
56385f32278SDavid Howells 	call->local = conn->params.local;
56419ffa01cSDavid Howells 	switch (call->family) {
5658c3e34a4SDavid Howells 	case AF_INET:
5668c3e34a4SDavid Howells 		call->peer_ip.ipv4_addr =
56785f32278SDavid Howells 			conn->params.peer->srx.transport.sin.sin_addr.s_addr;
5688c3e34a4SDavid Howells 		break;
5698c3e34a4SDavid Howells 	case AF_INET6:
5708c3e34a4SDavid Howells 		memcpy(call->peer_ip.ipv6_addr,
57185f32278SDavid Howells 		       conn->params.peer->srx.transport.sin6.sin6_addr.in6_u.u6_addr8,
5728c3e34a4SDavid Howells 		       sizeof(call->peer_ip.ipv6_addr));
5738c3e34a4SDavid Howells 		break;
5748c3e34a4SDavid Howells 	default:
5758c3e34a4SDavid Howells 		break;
5768c3e34a4SDavid Howells 	}
57719ffa01cSDavid Howells 	call->epoch = conn->proto.epoch;
57819ffa01cSDavid Howells 	call->service_id = conn->params.service_id;
57919ffa01cSDavid Howells 	call->in_clientflag = conn->proto.in_clientflag;
5808c3e34a4SDavid Howells 	/* Add the new call to the hashtable */
5818c3e34a4SDavid Howells 	rxrpc_call_hash_add(call);
5828c3e34a4SDavid Howells 
5838c3e34a4SDavid Howells 	_net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
5848c3e34a4SDavid Howells 
5858c3e34a4SDavid Howells 	call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
5868c3e34a4SDavid Howells 	add_timer(&call->lifetimer);
5878c3e34a4SDavid Howells 	_leave(" = %p {%d} [new]", call, call->debug_id);
5888c3e34a4SDavid Howells 	return call;
5898c3e34a4SDavid Howells 
5908c3e34a4SDavid Howells extant_call:
5918c3e34a4SDavid Howells 	write_unlock_bh(&conn->lock);
5928c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, candidate);
5938c3e34a4SDavid Howells 	_leave(" = %p {%d} [extant]", call, call ? call->debug_id : -1);
5948c3e34a4SDavid Howells 	return call;
5958c3e34a4SDavid Howells 
5968c3e34a4SDavid Howells aborted_call:
5978c3e34a4SDavid Howells 	write_unlock_bh(&conn->lock);
5988c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, candidate);
5998c3e34a4SDavid Howells 	_leave(" = -ECONNABORTED");
6008c3e34a4SDavid Howells 	return ERR_PTR(-ECONNABORTED);
6018c3e34a4SDavid Howells 
6028c3e34a4SDavid Howells old_call:
6038c3e34a4SDavid Howells 	write_unlock_bh(&conn->lock);
6048c3e34a4SDavid Howells 	kmem_cache_free(rxrpc_call_jar, candidate);
6058c3e34a4SDavid Howells 	_leave(" = -ECONNRESET [old]");
6068c3e34a4SDavid Howells 	return ERR_PTR(-ECONNRESET);
6078c3e34a4SDavid Howells }
6088c3e34a4SDavid Howells 
6098c3e34a4SDavid Howells /*
6108c3e34a4SDavid Howells  * detach a call from a socket and set up for release
6118c3e34a4SDavid Howells  */
6128c3e34a4SDavid Howells void rxrpc_release_call(struct rxrpc_call *call)
6138c3e34a4SDavid Howells {
6148c3e34a4SDavid Howells 	struct rxrpc_connection *conn = call->conn;
6158c3e34a4SDavid Howells 	struct rxrpc_sock *rx = call->socket;
6168c3e34a4SDavid Howells 
6178c3e34a4SDavid Howells 	_enter("{%d,%d,%d,%d}",
6188c3e34a4SDavid Howells 	       call->debug_id, atomic_read(&call->usage),
6198c3e34a4SDavid Howells 	       atomic_read(&call->ackr_not_idle),
6208c3e34a4SDavid Howells 	       call->rx_first_oos);
6218c3e34a4SDavid Howells 
6228c3e34a4SDavid Howells 	spin_lock_bh(&call->lock);
6238c3e34a4SDavid Howells 	if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
6248c3e34a4SDavid Howells 		BUG();
6258c3e34a4SDavid Howells 	spin_unlock_bh(&call->lock);
6268c3e34a4SDavid Howells 
6278c3e34a4SDavid Howells 	/* dissociate from the socket
6288c3e34a4SDavid Howells 	 * - the socket's ref on the call is passed to the death timer
6298c3e34a4SDavid Howells 	 */
6308c3e34a4SDavid Howells 	_debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
6318c3e34a4SDavid Howells 
632e653cfe4SDavid Howells 	spin_lock(&conn->params.peer->lock);
633e653cfe4SDavid Howells 	hlist_del_init(&call->error_link);
634e653cfe4SDavid Howells 	spin_unlock(&conn->params.peer->lock);
635e653cfe4SDavid Howells 
6368c3e34a4SDavid Howells 	write_lock_bh(&rx->call_lock);
6378c3e34a4SDavid Howells 	if (!list_empty(&call->accept_link)) {
6388c3e34a4SDavid Howells 		_debug("unlinking once-pending call %p { e=%lx f=%lx }",
6398c3e34a4SDavid Howells 		       call, call->events, call->flags);
6408c3e34a4SDavid Howells 		ASSERT(!test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
6418c3e34a4SDavid Howells 		list_del_init(&call->accept_link);
6428c3e34a4SDavid Howells 		sk_acceptq_removed(&rx->sk);
6438c3e34a4SDavid Howells 	} else if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
6448c3e34a4SDavid Howells 		rb_erase(&call->sock_node, &rx->calls);
6458c3e34a4SDavid Howells 		memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
6468c3e34a4SDavid Howells 		clear_bit(RXRPC_CALL_HAS_USERID, &call->flags);
6478c3e34a4SDavid Howells 	}
6488c3e34a4SDavid Howells 	write_unlock_bh(&rx->call_lock);
6498c3e34a4SDavid Howells 
6508c3e34a4SDavid Howells 	/* free up the channel for reuse */
6518c3e34a4SDavid Howells 	write_lock_bh(&conn->lock);
6528c3e34a4SDavid Howells 	write_lock(&call->state_lock);
6538c3e34a4SDavid Howells 
6548c3e34a4SDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE &&
6558c3e34a4SDavid Howells 	    call->state != RXRPC_CALL_CLIENT_FINAL_ACK) {
6568c3e34a4SDavid Howells 		_debug("+++ ABORTING STATE %d +++\n", call->state);
6578c3e34a4SDavid Howells 		call->state = RXRPC_CALL_LOCALLY_ABORTED;
6588c3e34a4SDavid Howells 		call->local_abort = RX_CALL_DEAD;
6598c3e34a4SDavid Howells 	}
6608c3e34a4SDavid Howells 	write_unlock(&call->state_lock);
661e653cfe4SDavid Howells 
662e653cfe4SDavid Howells 	rb_erase(&call->conn_node, &conn->calls);
6638c3e34a4SDavid Howells 	write_unlock_bh(&conn->lock);
6648c3e34a4SDavid Howells 
665e653cfe4SDavid Howells 	rxrpc_disconnect_call(call);
666e653cfe4SDavid 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 /*
799dee46364SDavid Howells  * Final call destruction under RCU.
800dee46364SDavid Howells  */
801dee46364SDavid Howells static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
802dee46364SDavid Howells {
803dee46364SDavid Howells 	struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
804dee46364SDavid Howells 
805dee46364SDavid Howells 	rxrpc_purge_queue(&call->rx_queue);
806dee46364SDavid Howells 	kmem_cache_free(rxrpc_call_jar, call);
807dee46364SDavid Howells }
808dee46364SDavid Howells 
809dee46364SDavid Howells /*
8108c3e34a4SDavid Howells  * clean up a call
8118c3e34a4SDavid Howells  */
8128c3e34a4SDavid Howells static void rxrpc_cleanup_call(struct rxrpc_call *call)
8138c3e34a4SDavid Howells {
8148c3e34a4SDavid Howells 	_net("DESTROY CALL %d", call->debug_id);
8158c3e34a4SDavid Howells 
8168c3e34a4SDavid Howells 	ASSERT(call->socket);
8178c3e34a4SDavid Howells 
8188c3e34a4SDavid Howells 	memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
8198c3e34a4SDavid Howells 
8208c3e34a4SDavid Howells 	del_timer_sync(&call->lifetimer);
8218c3e34a4SDavid Howells 	del_timer_sync(&call->deadspan);
8228c3e34a4SDavid Howells 	del_timer_sync(&call->ack_timer);
8238c3e34a4SDavid Howells 	del_timer_sync(&call->resend_timer);
8248c3e34a4SDavid Howells 
8258c3e34a4SDavid Howells 	ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
8268c3e34a4SDavid Howells 	ASSERTCMP(call->events, ==, 0);
8278c3e34a4SDavid Howells 	if (work_pending(&call->processor)) {
8288c3e34a4SDavid Howells 		_debug("defer destroy");
8298c3e34a4SDavid Howells 		rxrpc_queue_work(&call->destroyer);
8308c3e34a4SDavid Howells 		return;
8318c3e34a4SDavid Howells 	}
8328c3e34a4SDavid Howells 
833e653cfe4SDavid Howells 	ASSERTCMP(call->conn, ==, NULL);
8348c3e34a4SDavid Howells 
8358c3e34a4SDavid Howells 	/* Remove the call from the hash */
8368c3e34a4SDavid Howells 	rxrpc_call_hash_del(call);
8378c3e34a4SDavid Howells 
8388c3e34a4SDavid Howells 	if (call->acks_window) {
8398c3e34a4SDavid Howells 		_debug("kill Tx window %d",
8408c3e34a4SDavid Howells 		       CIRC_CNT(call->acks_head, call->acks_tail,
8418c3e34a4SDavid Howells 				call->acks_winsz));
8428c3e34a4SDavid Howells 		smp_mb();
8438c3e34a4SDavid Howells 		while (CIRC_CNT(call->acks_head, call->acks_tail,
8448c3e34a4SDavid Howells 				call->acks_winsz) > 0) {
8458c3e34a4SDavid Howells 			struct rxrpc_skb_priv *sp;
8468c3e34a4SDavid Howells 			unsigned long _skb;
8478c3e34a4SDavid Howells 
8488c3e34a4SDavid Howells 			_skb = call->acks_window[call->acks_tail] & ~1;
8498c3e34a4SDavid Howells 			sp = rxrpc_skb((struct sk_buff *)_skb);
8508c3e34a4SDavid Howells 			_debug("+++ clear Tx %u", sp->hdr.seq);
8518c3e34a4SDavid Howells 			rxrpc_free_skb((struct sk_buff *)_skb);
8528c3e34a4SDavid Howells 			call->acks_tail =
8538c3e34a4SDavid Howells 				(call->acks_tail + 1) & (call->acks_winsz - 1);
8548c3e34a4SDavid Howells 		}
8558c3e34a4SDavid Howells 
8568c3e34a4SDavid Howells 		kfree(call->acks_window);
8578c3e34a4SDavid Howells 	}
8588c3e34a4SDavid Howells 
8598c3e34a4SDavid Howells 	rxrpc_free_skb(call->tx_pending);
8608c3e34a4SDavid Howells 
8618c3e34a4SDavid Howells 	rxrpc_purge_queue(&call->rx_queue);
8628c3e34a4SDavid Howells 	ASSERT(skb_queue_empty(&call->rx_oos_queue));
8638c3e34a4SDavid Howells 	sock_put(&call->socket->sk);
864dee46364SDavid Howells 	call_rcu(&call->rcu, rxrpc_rcu_destroy_call);
8658c3e34a4SDavid Howells }
8668c3e34a4SDavid Howells 
8678c3e34a4SDavid Howells /*
8688c3e34a4SDavid Howells  * destroy a call
8698c3e34a4SDavid Howells  */
8708c3e34a4SDavid Howells static void rxrpc_destroy_call(struct work_struct *work)
8718c3e34a4SDavid Howells {
8728c3e34a4SDavid Howells 	struct rxrpc_call *call =
8738c3e34a4SDavid Howells 		container_of(work, struct rxrpc_call, destroyer);
8748c3e34a4SDavid Howells 
8758c3e34a4SDavid Howells 	_enter("%p{%d,%d,%p}",
8768c3e34a4SDavid Howells 	       call, atomic_read(&call->usage), call->channel, call->conn);
8778c3e34a4SDavid Howells 
8788c3e34a4SDavid Howells 	ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
8798c3e34a4SDavid Howells 
8808c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
8818c3e34a4SDavid Howells 	list_del_init(&call->link);
8828c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
8838c3e34a4SDavid Howells 
8848c3e34a4SDavid Howells 	rxrpc_cleanup_call(call);
8858c3e34a4SDavid Howells 	_leave("");
8868c3e34a4SDavid Howells }
8878c3e34a4SDavid Howells 
8888c3e34a4SDavid Howells /*
8898c3e34a4SDavid Howells  * preemptively destroy all the call records from a transport endpoint rather
8908c3e34a4SDavid Howells  * than waiting for them to time out
8918c3e34a4SDavid Howells  */
8928c3e34a4SDavid Howells void __exit rxrpc_destroy_all_calls(void)
8938c3e34a4SDavid Howells {
8948c3e34a4SDavid Howells 	struct rxrpc_call *call;
8958c3e34a4SDavid Howells 
8968c3e34a4SDavid Howells 	_enter("");
8978c3e34a4SDavid Howells 	write_lock_bh(&rxrpc_call_lock);
8988c3e34a4SDavid Howells 
8998c3e34a4SDavid Howells 	while (!list_empty(&rxrpc_calls)) {
9008c3e34a4SDavid Howells 		call = list_entry(rxrpc_calls.next, struct rxrpc_call, link);
9018c3e34a4SDavid Howells 		_debug("Zapping call %p", call);
9028c3e34a4SDavid Howells 
9038c3e34a4SDavid Howells 		list_del_init(&call->link);
9048c3e34a4SDavid Howells 
9058c3e34a4SDavid Howells 		switch (atomic_read(&call->usage)) {
9068c3e34a4SDavid Howells 		case 0:
9078c3e34a4SDavid Howells 			ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
9088c3e34a4SDavid Howells 			break;
9098c3e34a4SDavid Howells 		case 1:
9108c3e34a4SDavid Howells 			if (del_timer_sync(&call->deadspan) != 0 &&
9118c3e34a4SDavid Howells 			    call->state != RXRPC_CALL_DEAD)
9128c3e34a4SDavid Howells 				rxrpc_dead_call_expired((unsigned long) call);
9138c3e34a4SDavid Howells 			if (call->state != RXRPC_CALL_DEAD)
9148c3e34a4SDavid Howells 				break;
9158c3e34a4SDavid Howells 		default:
9168c3e34a4SDavid Howells 			pr_err("Call %p still in use (%d,%d,%s,%lx,%lx)!\n",
9178c3e34a4SDavid Howells 			       call, atomic_read(&call->usage),
9188c3e34a4SDavid Howells 			       atomic_read(&call->ackr_not_idle),
9198c3e34a4SDavid Howells 			       rxrpc_call_states[call->state],
9208c3e34a4SDavid Howells 			       call->flags, call->events);
9218c3e34a4SDavid Howells 			if (!skb_queue_empty(&call->rx_queue))
9228c3e34a4SDavid Howells 				pr_err("Rx queue occupied\n");
9238c3e34a4SDavid Howells 			if (!skb_queue_empty(&call->rx_oos_queue))
9248c3e34a4SDavid Howells 				pr_err("OOS queue occupied\n");
9258c3e34a4SDavid Howells 			break;
9268c3e34a4SDavid Howells 		}
9278c3e34a4SDavid Howells 
9288c3e34a4SDavid Howells 		write_unlock_bh(&rxrpc_call_lock);
9298c3e34a4SDavid Howells 		cond_resched();
9308c3e34a4SDavid Howells 		write_lock_bh(&rxrpc_call_lock);
9318c3e34a4SDavid Howells 	}
9328c3e34a4SDavid Howells 
9338c3e34a4SDavid Howells 	write_unlock_bh(&rxrpc_call_lock);
9348c3e34a4SDavid Howells 	_leave("");
9358c3e34a4SDavid Howells }
9368c3e34a4SDavid Howells 
9378c3e34a4SDavid Howells /*
9388c3e34a4SDavid Howells  * handle call lifetime being exceeded
9398c3e34a4SDavid Howells  */
9408c3e34a4SDavid Howells static void rxrpc_call_life_expired(unsigned long _call)
9418c3e34a4SDavid Howells {
9428c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
9438c3e34a4SDavid Howells 
9448c3e34a4SDavid Howells 	if (call->state >= RXRPC_CALL_COMPLETE)
9458c3e34a4SDavid Howells 		return;
9468c3e34a4SDavid Howells 
9478c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
9488c3e34a4SDavid Howells 	read_lock_bh(&call->state_lock);
9498c3e34a4SDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE) {
9508c3e34a4SDavid Howells 		set_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events);
9518c3e34a4SDavid Howells 		rxrpc_queue_call(call);
9528c3e34a4SDavid Howells 	}
9538c3e34a4SDavid Howells 	read_unlock_bh(&call->state_lock);
9548c3e34a4SDavid Howells }
9558c3e34a4SDavid Howells 
9568c3e34a4SDavid Howells /*
9578c3e34a4SDavid Howells  * handle resend timer expiry
9588c3e34a4SDavid Howells  * - may not take call->state_lock as this can deadlock against del_timer_sync()
9598c3e34a4SDavid Howells  */
9608c3e34a4SDavid Howells static void rxrpc_resend_time_expired(unsigned long _call)
9618c3e34a4SDavid Howells {
9628c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
9638c3e34a4SDavid Howells 
9648c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
9658c3e34a4SDavid Howells 
9668c3e34a4SDavid Howells 	if (call->state >= RXRPC_CALL_COMPLETE)
9678c3e34a4SDavid Howells 		return;
9688c3e34a4SDavid Howells 
9698c3e34a4SDavid Howells 	clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
9708c3e34a4SDavid Howells 	if (!test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
9718c3e34a4SDavid Howells 		rxrpc_queue_call(call);
9728c3e34a4SDavid Howells }
9738c3e34a4SDavid Howells 
9748c3e34a4SDavid Howells /*
9758c3e34a4SDavid Howells  * handle ACK timer expiry
9768c3e34a4SDavid Howells  */
9778c3e34a4SDavid Howells static void rxrpc_ack_time_expired(unsigned long _call)
9788c3e34a4SDavid Howells {
9798c3e34a4SDavid Howells 	struct rxrpc_call *call = (struct rxrpc_call *) _call;
9808c3e34a4SDavid Howells 
9818c3e34a4SDavid Howells 	_enter("{%d}", call->debug_id);
9828c3e34a4SDavid Howells 
9838c3e34a4SDavid Howells 	if (call->state >= RXRPC_CALL_COMPLETE)
9848c3e34a4SDavid Howells 		return;
9858c3e34a4SDavid Howells 
9868c3e34a4SDavid Howells 	read_lock_bh(&call->state_lock);
9878c3e34a4SDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE &&
9888c3e34a4SDavid Howells 	    !test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events))
9898c3e34a4SDavid Howells 		rxrpc_queue_call(call);
9908c3e34a4SDavid Howells 	read_unlock_bh(&call->state_lock);
9918c3e34a4SDavid Howells }
992