xref: /openbmc/linux/net/rxrpc/conn_event.c (revision 39ce6755)
18c3e34a4SDavid Howells /* connection-level event 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/module.h>
158c3e34a4SDavid Howells #include <linux/net.h>
168c3e34a4SDavid Howells #include <linux/skbuff.h>
178c3e34a4SDavid Howells #include <linux/errqueue.h>
188c3e34a4SDavid Howells #include <net/sock.h>
198c3e34a4SDavid Howells #include <net/af_rxrpc.h>
208c3e34a4SDavid Howells #include <net/ip.h>
218c3e34a4SDavid Howells #include "ar-internal.h"
228c3e34a4SDavid Howells 
238c3e34a4SDavid Howells /*
2418bfeba5SDavid Howells  * Retransmit terminal ACK or ABORT of the previous call.
2518bfeba5SDavid Howells  */
26f5c17aaeSDavid Howells static void rxrpc_conn_retransmit_call(struct rxrpc_connection *conn,
273136ef49SDavid Howells 				       struct sk_buff *skb,
283136ef49SDavid Howells 				       unsigned int channel)
2918bfeba5SDavid Howells {
303136ef49SDavid Howells 	struct rxrpc_skb_priv *sp = skb ? rxrpc_skb(skb) : NULL;
3118bfeba5SDavid Howells 	struct rxrpc_channel *chan;
3218bfeba5SDavid Howells 	struct msghdr msg;
335fc62f6aSDavid Howells 	struct kvec iov[3];
3418bfeba5SDavid Howells 	struct {
3518bfeba5SDavid Howells 		struct rxrpc_wire_header whdr;
3618bfeba5SDavid Howells 		union {
375fc62f6aSDavid Howells 			__be32 abort_code;
3818bfeba5SDavid Howells 			struct rxrpc_ackpacket ack;
3918bfeba5SDavid Howells 		};
4018bfeba5SDavid Howells 	} __attribute__((packed)) pkt;
415fc62f6aSDavid Howells 	struct rxrpc_ackinfo ack_info;
4218bfeba5SDavid Howells 	size_t len;
436b47fe1dSDavid Howells 	int ret, ioc;
445fc62f6aSDavid Howells 	u32 serial, mtu, call_id, padding;
4518bfeba5SDavid Howells 
4618bfeba5SDavid Howells 	_enter("%d", conn->debug_id);
4718bfeba5SDavid Howells 
483136ef49SDavid Howells 	chan = &conn->channels[channel];
4918bfeba5SDavid Howells 
5018bfeba5SDavid Howells 	/* If the last call got moved on whilst we were waiting to run, just
5118bfeba5SDavid Howells 	 * ignore this packet.
5218bfeba5SDavid Howells 	 */
5318bfeba5SDavid Howells 	call_id = READ_ONCE(chan->last_call);
5418bfeba5SDavid Howells 	/* Sync with __rxrpc_disconnect_call() */
5518bfeba5SDavid Howells 	smp_rmb();
563136ef49SDavid Howells 	if (skb && call_id != sp->hdr.callNumber)
5718bfeba5SDavid Howells 		return;
5818bfeba5SDavid Howells 
5918bfeba5SDavid Howells 	msg.msg_name	= &conn->params.peer->srx.transport;
6018bfeba5SDavid Howells 	msg.msg_namelen	= conn->params.peer->srx.transport_len;
6118bfeba5SDavid Howells 	msg.msg_control	= NULL;
6218bfeba5SDavid Howells 	msg.msg_controllen = 0;
6318bfeba5SDavid Howells 	msg.msg_flags	= 0;
6418bfeba5SDavid Howells 
655fc62f6aSDavid Howells 	iov[0].iov_base	= &pkt;
665fc62f6aSDavid Howells 	iov[0].iov_len	= sizeof(pkt.whdr);
675fc62f6aSDavid Howells 	iov[1].iov_base	= &padding;
685fc62f6aSDavid Howells 	iov[1].iov_len	= 3;
695fc62f6aSDavid Howells 	iov[2].iov_base	= &ack_info;
705fc62f6aSDavid Howells 	iov[2].iov_len	= sizeof(ack_info);
715fc62f6aSDavid Howells 
723136ef49SDavid Howells 	pkt.whdr.epoch		= htonl(conn->proto.epoch);
73fb1967a6SDavid Howells 	pkt.whdr.cid		= htonl(conn->proto.cid | channel);
743136ef49SDavid Howells 	pkt.whdr.callNumber	= htonl(call_id);
7518bfeba5SDavid Howells 	pkt.whdr.seq		= 0;
7618bfeba5SDavid Howells 	pkt.whdr.type		= chan->last_type;
7718bfeba5SDavid Howells 	pkt.whdr.flags		= conn->out_clientflag;
7818bfeba5SDavid Howells 	pkt.whdr.userStatus	= 0;
7918bfeba5SDavid Howells 	pkt.whdr.securityIndex	= conn->security_ix;
8018bfeba5SDavid Howells 	pkt.whdr._rsvd		= 0;
8168d6d1aeSDavid Howells 	pkt.whdr.serviceId	= htons(conn->service_id);
8218bfeba5SDavid Howells 
8318bfeba5SDavid Howells 	len = sizeof(pkt.whdr);
8418bfeba5SDavid Howells 	switch (chan->last_type) {
8518bfeba5SDavid Howells 	case RXRPC_PACKET_TYPE_ABORT:
865fc62f6aSDavid Howells 		pkt.abort_code	= htonl(chan->last_abort);
875fc62f6aSDavid Howells 		iov[0].iov_len += sizeof(pkt.abort_code);
885fc62f6aSDavid Howells 		len += sizeof(pkt.abort_code);
895fc62f6aSDavid Howells 		ioc = 1;
9018bfeba5SDavid Howells 		break;
9118bfeba5SDavid Howells 
9218bfeba5SDavid Howells 	case RXRPC_PACKET_TYPE_ACK:
9318bfeba5SDavid Howells 		mtu = conn->params.peer->if_mtu;
9418bfeba5SDavid Howells 		mtu -= conn->params.peer->hdrsize;
9518bfeba5SDavid Howells 		pkt.ack.bufferSpace	= 0;
963136ef49SDavid Howells 		pkt.ack.maxSkew		= htons(skb ? skb->priority : 0);
973136ef49SDavid Howells 		pkt.ack.firstPacket	= htonl(chan->last_seq + 1);
983136ef49SDavid Howells 		pkt.ack.previousPacket	= htonl(chan->last_seq);
993136ef49SDavid Howells 		pkt.ack.serial		= htonl(skb ? sp->hdr.serial : 0);
1003136ef49SDavid Howells 		pkt.ack.reason		= skb ? RXRPC_ACK_DUPLICATE : RXRPC_ACK_IDLE;
10118bfeba5SDavid Howells 		pkt.ack.nAcks		= 0;
1025fc62f6aSDavid Howells 		ack_info.rxMTU		= htonl(rxrpc_rx_mtu);
1035fc62f6aSDavid Howells 		ack_info.maxMTU		= htonl(mtu);
1045fc62f6aSDavid Howells 		ack_info.rwind		= htonl(rxrpc_rx_window_size);
1055fc62f6aSDavid Howells 		ack_info.jumbo_max	= htonl(rxrpc_rx_jumbo_max);
10657494343SDavid Howells 		pkt.whdr.flags		|= RXRPC_SLOW_START_OK;
1075fc62f6aSDavid Howells 		padding			= 0;
1085fc62f6aSDavid Howells 		iov[0].iov_len += sizeof(pkt.ack);
1095fc62f6aSDavid Howells 		len += sizeof(pkt.ack) + 3 + sizeof(ack_info);
1105fc62f6aSDavid Howells 		ioc = 3;
11118bfeba5SDavid Howells 		break;
1125fc62f6aSDavid Howells 
1135fc62f6aSDavid Howells 	default:
1145fc62f6aSDavid Howells 		return;
11518bfeba5SDavid Howells 	}
11618bfeba5SDavid Howells 
11718bfeba5SDavid Howells 	/* Resync with __rxrpc_disconnect_call() and check that the last call
11818bfeba5SDavid Howells 	 * didn't get advanced whilst we were filling out the packets.
11918bfeba5SDavid Howells 	 */
12018bfeba5SDavid Howells 	smp_rmb();
12118bfeba5SDavid Howells 	if (READ_ONCE(chan->last_call) != call_id)
12218bfeba5SDavid Howells 		return;
12318bfeba5SDavid Howells 
12418bfeba5SDavid Howells 	serial = atomic_inc_return(&conn->serial);
12518bfeba5SDavid Howells 	pkt.whdr.serial = htonl(serial);
12618bfeba5SDavid Howells 
12718bfeba5SDavid Howells 	switch (chan->last_type) {
12818bfeba5SDavid Howells 	case RXRPC_PACKET_TYPE_ABORT:
12964753092SDavid Howells 		_proto("Tx ABORT %%%u { %d } [re]", serial, conn->abort_code);
13018bfeba5SDavid Howells 		break;
13118bfeba5SDavid Howells 	case RXRPC_PACKET_TYPE_ACK:
1324764c0daSDavid Howells 		trace_rxrpc_tx_ack(chan->call_debug_id, serial,
133f3f8337cSDavid Howells 				   ntohl(pkt.ack.firstPacket),
134f3f8337cSDavid Howells 				   ntohl(pkt.ack.serial),
135f3f8337cSDavid Howells 				   pkt.ack.reason, 0);
13618bfeba5SDavid Howells 		_proto("Tx ACK %%%u [re]", serial);
13718bfeba5SDavid Howells 		break;
13818bfeba5SDavid Howells 	}
13918bfeba5SDavid Howells 
1406b47fe1dSDavid Howells 	ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, ioc, len);
141330bdcfaSDavid Howells 	conn->params.peer->last_tx_at = ktime_get_seconds();
1426b47fe1dSDavid Howells 	if (ret < 0)
1434764c0daSDavid Howells 		trace_rxrpc_tx_fail(chan->call_debug_id, serial, ret,
1444764c0daSDavid Howells 				    rxrpc_tx_point_call_final_resend);
1454764c0daSDavid Howells 	else
1464764c0daSDavid Howells 		trace_rxrpc_tx_packet(chan->call_debug_id, &pkt.whdr,
1474764c0daSDavid Howells 				      rxrpc_tx_point_call_final_resend);
1486b47fe1dSDavid Howells 
14918bfeba5SDavid Howells 	_leave("");
15018bfeba5SDavid Howells }
15118bfeba5SDavid Howells 
15218bfeba5SDavid Howells /*
1538c3e34a4SDavid Howells  * pass a connection-level abort onto all calls on that connection
1548c3e34a4SDavid Howells  */
155f5c17aaeSDavid Howells static void rxrpc_abort_calls(struct rxrpc_connection *conn,
15639ce6755SDavid Howells 			      enum rxrpc_call_completion compl,
15739ce6755SDavid Howells 			      rxrpc_serial_t serial)
1588c3e34a4SDavid Howells {
1598c3e34a4SDavid Howells 	struct rxrpc_call *call;
160248f219cSDavid Howells 	int i;
1618c3e34a4SDavid Howells 
16264753092SDavid Howells 	_enter("{%d},%x", conn->debug_id, conn->abort_code);
1638c3e34a4SDavid Howells 
164a1399f8bSDavid Howells 	spin_lock(&conn->channel_lock);
1658c3e34a4SDavid Howells 
166a1399f8bSDavid Howells 	for (i = 0; i < RXRPC_MAXCALLS; i++) {
167a1399f8bSDavid Howells 		call = rcu_dereference_protected(
168a1399f8bSDavid Howells 			conn->channels[i].call,
169a1399f8bSDavid Howells 			lockdep_is_held(&conn->channel_lock));
170ccbd3dbeSDavid Howells 		if (call) {
1715a42976dSDavid Howells 			if (compl == RXRPC_CALL_LOCALLY_ABORTED)
172a25e21f0SDavid Howells 				trace_rxrpc_abort(call->debug_id,
173a25e21f0SDavid Howells 						  "CON", call->cid,
1745a42976dSDavid Howells 						  call->call_id, 0,
17564753092SDavid Howells 						  conn->abort_code,
17664753092SDavid Howells 						  conn->error);
17739ce6755SDavid Howells 			else
17839ce6755SDavid Howells 				trace_rxrpc_rx_abort(call, serial,
17939ce6755SDavid Howells 						     conn->abort_code);
180248f219cSDavid Howells 			if (rxrpc_set_call_completion(call, compl,
18164753092SDavid Howells 						      conn->abort_code,
18264753092SDavid Howells 						      conn->error))
183248f219cSDavid Howells 				rxrpc_notify_socket(call);
1848c3e34a4SDavid Howells 		}
185ccbd3dbeSDavid Howells 	}
1868c3e34a4SDavid Howells 
187a1399f8bSDavid Howells 	spin_unlock(&conn->channel_lock);
1888c3e34a4SDavid Howells 	_leave("");
1898c3e34a4SDavid Howells }
1908c3e34a4SDavid Howells 
1918c3e34a4SDavid Howells /*
1928c3e34a4SDavid Howells  * generate a connection-level abort
1938c3e34a4SDavid Howells  */
1948c3e34a4SDavid Howells static int rxrpc_abort_connection(struct rxrpc_connection *conn,
1953a92789aSDavid Howells 				  int error, u32 abort_code)
1968c3e34a4SDavid Howells {
1978c3e34a4SDavid Howells 	struct rxrpc_wire_header whdr;
1988c3e34a4SDavid Howells 	struct msghdr msg;
1998c3e34a4SDavid Howells 	struct kvec iov[2];
2008c3e34a4SDavid Howells 	__be32 word;
2018c3e34a4SDavid Howells 	size_t len;
2028c3e34a4SDavid Howells 	u32 serial;
2038c3e34a4SDavid Howells 	int ret;
2048c3e34a4SDavid Howells 
2058c3e34a4SDavid Howells 	_enter("%d,,%u,%u", conn->debug_id, error, abort_code);
2068c3e34a4SDavid Howells 
2078c3e34a4SDavid Howells 	/* generate a connection-level abort */
2088c3e34a4SDavid Howells 	spin_lock_bh(&conn->state_lock);
209f5c17aaeSDavid Howells 	if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
2108c3e34a4SDavid Howells 		spin_unlock_bh(&conn->state_lock);
2118c3e34a4SDavid Howells 		_leave(" = 0 [already dead]");
2128c3e34a4SDavid Howells 		return 0;
2138c3e34a4SDavid Howells 	}
2148c3e34a4SDavid Howells 
21564753092SDavid Howells 	conn->error = error;
21664753092SDavid Howells 	conn->abort_code = abort_code;
217f5c17aaeSDavid Howells 	conn->state = RXRPC_CONN_LOCALLY_ABORTED;
218f5c17aaeSDavid Howells 	spin_unlock_bh(&conn->state_lock);
219f5c17aaeSDavid Howells 
22085f32278SDavid Howells 	msg.msg_name	= &conn->params.peer->srx.transport;
22185f32278SDavid Howells 	msg.msg_namelen	= conn->params.peer->srx.transport_len;
2228c3e34a4SDavid Howells 	msg.msg_control	= NULL;
2238c3e34a4SDavid Howells 	msg.msg_controllen = 0;
2248c3e34a4SDavid Howells 	msg.msg_flags	= 0;
2258c3e34a4SDavid Howells 
22619ffa01cSDavid Howells 	whdr.epoch	= htonl(conn->proto.epoch);
22719ffa01cSDavid Howells 	whdr.cid	= htonl(conn->proto.cid);
2288c3e34a4SDavid Howells 	whdr.callNumber	= 0;
2298c3e34a4SDavid Howells 	whdr.seq	= 0;
2308c3e34a4SDavid Howells 	whdr.type	= RXRPC_PACKET_TYPE_ABORT;
2318c3e34a4SDavid Howells 	whdr.flags	= conn->out_clientflag;
2328c3e34a4SDavid Howells 	whdr.userStatus	= 0;
2338c3e34a4SDavid Howells 	whdr.securityIndex = conn->security_ix;
2348c3e34a4SDavid Howells 	whdr._rsvd	= 0;
23568d6d1aeSDavid Howells 	whdr.serviceId	= htons(conn->service_id);
2368c3e34a4SDavid Howells 
23764753092SDavid Howells 	word		= htonl(conn->abort_code);
2388c3e34a4SDavid Howells 
2398c3e34a4SDavid Howells 	iov[0].iov_base	= &whdr;
2408c3e34a4SDavid Howells 	iov[0].iov_len	= sizeof(whdr);
2418c3e34a4SDavid Howells 	iov[1].iov_base	= &word;
2428c3e34a4SDavid Howells 	iov[1].iov_len	= sizeof(word);
2438c3e34a4SDavid Howells 
2448c3e34a4SDavid Howells 	len = iov[0].iov_len + iov[1].iov_len;
2458c3e34a4SDavid Howells 
2468c3e34a4SDavid Howells 	serial = atomic_inc_return(&conn->serial);
24739ce6755SDavid Howells 	rxrpc_abort_calls(conn, RXRPC_CALL_LOCALLY_ABORTED, serial);
2488c3e34a4SDavid Howells 	whdr.serial = htonl(serial);
24964753092SDavid Howells 	_proto("Tx CONN ABORT %%%u { %d }", serial, conn->abort_code);
2508c3e34a4SDavid Howells 
25185f32278SDavid Howells 	ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
2528c3e34a4SDavid Howells 	if (ret < 0) {
2536b47fe1dSDavid Howells 		trace_rxrpc_tx_fail(conn->debug_id, serial, ret,
2544764c0daSDavid Howells 				    rxrpc_tx_point_conn_abort);
2558c3e34a4SDavid Howells 		_debug("sendmsg failed: %d", ret);
2568c3e34a4SDavid Howells 		return -EAGAIN;
2578c3e34a4SDavid Howells 	}
2588c3e34a4SDavid Howells 
2594764c0daSDavid Howells 	trace_rxrpc_tx_packet(conn->debug_id, &whdr, rxrpc_tx_point_conn_abort);
2604764c0daSDavid Howells 
261330bdcfaSDavid Howells 	conn->params.peer->last_tx_at = ktime_get_seconds();
262ace45becSDavid Howells 
2638c3e34a4SDavid Howells 	_leave(" = 0");
2648c3e34a4SDavid Howells 	return 0;
2658c3e34a4SDavid Howells }
2668c3e34a4SDavid Howells 
2678c3e34a4SDavid Howells /*
2688c3e34a4SDavid Howells  * mark a call as being on a now-secured channel
269248f219cSDavid Howells  * - must be called with BH's disabled.
2708c3e34a4SDavid Howells  */
2718c3e34a4SDavid Howells static void rxrpc_call_is_secure(struct rxrpc_call *call)
2728c3e34a4SDavid Howells {
2738c3e34a4SDavid Howells 	_enter("%p", call);
2748c3e34a4SDavid Howells 	if (call) {
275248f219cSDavid Howells 		write_lock_bh(&call->state_lock);
276248f219cSDavid Howells 		if (call->state == RXRPC_CALL_SERVER_SECURING) {
277248f219cSDavid Howells 			call->state = RXRPC_CALL_SERVER_ACCEPTING;
278248f219cSDavid Howells 			rxrpc_notify_socket(call);
279248f219cSDavid Howells 		}
280248f219cSDavid Howells 		write_unlock_bh(&call->state_lock);
2818c3e34a4SDavid Howells 	}
2828c3e34a4SDavid Howells }
2838c3e34a4SDavid Howells 
2848c3e34a4SDavid Howells /*
2858c3e34a4SDavid Howells  * connection-level Rx packet processor
2868c3e34a4SDavid Howells  */
2878c3e34a4SDavid Howells static int rxrpc_process_event(struct rxrpc_connection *conn,
2888c3e34a4SDavid Howells 			       struct sk_buff *skb,
2898c3e34a4SDavid Howells 			       u32 *_abort_code)
2908c3e34a4SDavid Howells {
2918c3e34a4SDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
2928c3e34a4SDavid Howells 	__be32 wtmp;
2938c3e34a4SDavid Howells 	u32 abort_code;
2948c3e34a4SDavid Howells 	int loop, ret;
2958c3e34a4SDavid Howells 
2968c3e34a4SDavid Howells 	if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
297248f219cSDavid Howells 		_leave(" = -ECONNABORTED [%u]", conn->state);
2988c3e34a4SDavid Howells 		return -ECONNABORTED;
2998c3e34a4SDavid Howells 	}
3008c3e34a4SDavid Howells 
3018c3e34a4SDavid Howells 	_enter("{%d},{%u,%%%u},", conn->debug_id, sp->hdr.type, sp->hdr.serial);
3028c3e34a4SDavid Howells 
3038c3e34a4SDavid Howells 	switch (sp->hdr.type) {
30418bfeba5SDavid Howells 	case RXRPC_PACKET_TYPE_DATA:
30518bfeba5SDavid Howells 	case RXRPC_PACKET_TYPE_ACK:
3063136ef49SDavid Howells 		rxrpc_conn_retransmit_call(conn, skb,
3073136ef49SDavid Howells 					   sp->hdr.cid & RXRPC_CHANNELMASK);
30818bfeba5SDavid Howells 		return 0;
30918bfeba5SDavid Howells 
3104d4a6ac7SDavid Howells 	case RXRPC_PACKET_TYPE_BUSY:
3114d4a6ac7SDavid Howells 		/* Just ignore BUSY packets for now. */
3124d4a6ac7SDavid Howells 		return 0;
3134d4a6ac7SDavid Howells 
3148c3e34a4SDavid Howells 	case RXRPC_PACKET_TYPE_ABORT:
315775e5b71SDavid Howells 		if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
316fb46f6eeSDavid Howells 				  &wtmp, sizeof(wtmp)) < 0) {
317fb46f6eeSDavid Howells 			trace_rxrpc_rx_eproto(NULL, sp->hdr.serial,
318fb46f6eeSDavid Howells 					      tracepoint_string("bad_abort"));
3198c3e34a4SDavid Howells 			return -EPROTO;
320fb46f6eeSDavid Howells 		}
3218c3e34a4SDavid Howells 		abort_code = ntohl(wtmp);
3228c3e34a4SDavid Howells 		_proto("Rx ABORT %%%u { ac=%d }", sp->hdr.serial, abort_code);
3238c3e34a4SDavid Howells 
32464753092SDavid Howells 		conn->error = -ECONNABORTED;
32564753092SDavid Howells 		conn->abort_code = abort_code;
3268c3e34a4SDavid Howells 		conn->state = RXRPC_CONN_REMOTELY_ABORTED;
32739ce6755SDavid Howells 		rxrpc_abort_calls(conn, RXRPC_CALL_REMOTELY_ABORTED, sp->hdr.serial);
3288c3e34a4SDavid Howells 		return -ECONNABORTED;
3298c3e34a4SDavid Howells 
3308c3e34a4SDavid Howells 	case RXRPC_PACKET_TYPE_CHALLENGE:
3318c3e34a4SDavid Howells 		return conn->security->respond_to_challenge(conn, skb,
3328c3e34a4SDavid Howells 							    _abort_code);
3338c3e34a4SDavid Howells 
3348c3e34a4SDavid Howells 	case RXRPC_PACKET_TYPE_RESPONSE:
3358c3e34a4SDavid Howells 		ret = conn->security->verify_response(conn, skb, _abort_code);
3368c3e34a4SDavid Howells 		if (ret < 0)
3378c3e34a4SDavid Howells 			return ret;
3388c3e34a4SDavid Howells 
3398c3e34a4SDavid Howells 		ret = conn->security->init_connection_security(conn);
3408c3e34a4SDavid Howells 		if (ret < 0)
3418c3e34a4SDavid Howells 			return ret;
3428c3e34a4SDavid Howells 
343a263629dSHerbert Xu 		ret = conn->security->prime_packet_security(conn);
344a263629dSHerbert Xu 		if (ret < 0)
345a263629dSHerbert Xu 			return ret;
346a263629dSHerbert Xu 
347a1399f8bSDavid Howells 		spin_lock(&conn->channel_lock);
3488c3e34a4SDavid Howells 		spin_lock(&conn->state_lock);
3498c3e34a4SDavid Howells 
350bba304dbSDavid Howells 		if (conn->state == RXRPC_CONN_SERVICE_CHALLENGING) {
351bba304dbSDavid Howells 			conn->state = RXRPC_CONN_SERVICE;
352248f219cSDavid Howells 			spin_unlock(&conn->state_lock);
3538c3e34a4SDavid Howells 			for (loop = 0; loop < RXRPC_MAXCALLS; loop++)
354dee46364SDavid Howells 				rxrpc_call_is_secure(
355dee46364SDavid Howells 					rcu_dereference_protected(
356a1399f8bSDavid Howells 						conn->channels[loop].call,
357a1399f8bSDavid Howells 						lockdep_is_held(&conn->channel_lock)));
358248f219cSDavid Howells 		} else {
359248f219cSDavid Howells 			spin_unlock(&conn->state_lock);
3608c3e34a4SDavid Howells 		}
3618c3e34a4SDavid Howells 
362a1399f8bSDavid Howells 		spin_unlock(&conn->channel_lock);
3638c3e34a4SDavid Howells 		return 0;
3648c3e34a4SDavid Howells 
3658c3e34a4SDavid Howells 	default:
366fb46f6eeSDavid Howells 		trace_rxrpc_rx_eproto(NULL, sp->hdr.serial,
367fb46f6eeSDavid Howells 				      tracepoint_string("bad_conn_pkt"));
3688c3e34a4SDavid Howells 		return -EPROTO;
3698c3e34a4SDavid Howells 	}
3708c3e34a4SDavid Howells }
3718c3e34a4SDavid Howells 
3728c3e34a4SDavid Howells /*
3738c3e34a4SDavid Howells  * set up security and issue a challenge
3748c3e34a4SDavid Howells  */
3758c3e34a4SDavid Howells static void rxrpc_secure_connection(struct rxrpc_connection *conn)
3768c3e34a4SDavid Howells {
3778c3e34a4SDavid Howells 	u32 abort_code;
3788c3e34a4SDavid Howells 	int ret;
3798c3e34a4SDavid Howells 
3808c3e34a4SDavid Howells 	_enter("{%d}", conn->debug_id);
3818c3e34a4SDavid Howells 
3828c3e34a4SDavid Howells 	ASSERT(conn->security_ix != 0);
3838c3e34a4SDavid Howells 
38419ffa01cSDavid Howells 	if (!conn->params.key) {
3858c3e34a4SDavid Howells 		_debug("set up security");
3868c3e34a4SDavid Howells 		ret = rxrpc_init_server_conn_security(conn);
3878c3e34a4SDavid Howells 		switch (ret) {
3888c3e34a4SDavid Howells 		case 0:
3898c3e34a4SDavid Howells 			break;
3908c3e34a4SDavid Howells 		case -ENOENT:
3918c3e34a4SDavid Howells 			abort_code = RX_CALL_DEAD;
3928c3e34a4SDavid Howells 			goto abort;
3938c3e34a4SDavid Howells 		default:
3948c3e34a4SDavid Howells 			abort_code = RXKADNOAUTH;
3958c3e34a4SDavid Howells 			goto abort;
3968c3e34a4SDavid Howells 		}
3978c3e34a4SDavid Howells 	}
3988c3e34a4SDavid Howells 
3998c3e34a4SDavid Howells 	if (conn->security->issue_challenge(conn) < 0) {
4008c3e34a4SDavid Howells 		abort_code = RX_CALL_DEAD;
4018c3e34a4SDavid Howells 		ret = -ENOMEM;
4028c3e34a4SDavid Howells 		goto abort;
4038c3e34a4SDavid Howells 	}
4048c3e34a4SDavid Howells 
4058c3e34a4SDavid Howells 	_leave("");
4068c3e34a4SDavid Howells 	return;
4078c3e34a4SDavid Howells 
4088c3e34a4SDavid Howells abort:
4098c3e34a4SDavid Howells 	_debug("abort %d, %d", ret, abort_code);
4103a92789aSDavid Howells 	rxrpc_abort_connection(conn, ret, abort_code);
4118c3e34a4SDavid Howells 	_leave(" [aborted]");
4128c3e34a4SDavid Howells }
4138c3e34a4SDavid Howells 
4148c3e34a4SDavid Howells /*
4153136ef49SDavid Howells  * Process delayed final ACKs that we haven't subsumed into a subsequent call.
4163136ef49SDavid Howells  */
4173136ef49SDavid Howells static void rxrpc_process_delayed_final_acks(struct rxrpc_connection *conn)
4183136ef49SDavid Howells {
4193136ef49SDavid Howells 	unsigned long j = jiffies, next_j;
4203136ef49SDavid Howells 	unsigned int channel;
4213136ef49SDavid Howells 	bool set;
4223136ef49SDavid Howells 
4233136ef49SDavid Howells again:
4243136ef49SDavid Howells 	next_j = j + LONG_MAX;
4253136ef49SDavid Howells 	set = false;
4263136ef49SDavid Howells 	for (channel = 0; channel < RXRPC_MAXCALLS; channel++) {
4273136ef49SDavid Howells 		struct rxrpc_channel *chan = &conn->channels[channel];
4283136ef49SDavid Howells 		unsigned long ack_at;
4293136ef49SDavid Howells 
4303136ef49SDavid Howells 		if (!test_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags))
4313136ef49SDavid Howells 			continue;
4323136ef49SDavid Howells 
4333136ef49SDavid Howells 		smp_rmb(); /* vs rxrpc_disconnect_client_call */
4343136ef49SDavid Howells 		ack_at = READ_ONCE(chan->final_ack_at);
4353136ef49SDavid Howells 
4363136ef49SDavid Howells 		if (time_before(j, ack_at)) {
4373136ef49SDavid Howells 			if (time_before(ack_at, next_j)) {
4383136ef49SDavid Howells 				next_j = ack_at;
4393136ef49SDavid Howells 				set = true;
4403136ef49SDavid Howells 			}
4413136ef49SDavid Howells 			continue;
4423136ef49SDavid Howells 		}
4433136ef49SDavid Howells 
4443136ef49SDavid Howells 		if (test_and_clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel,
4453136ef49SDavid Howells 				       &conn->flags))
4463136ef49SDavid Howells 			rxrpc_conn_retransmit_call(conn, NULL, channel);
4473136ef49SDavid Howells 	}
4483136ef49SDavid Howells 
4493136ef49SDavid Howells 	j = jiffies;
4503136ef49SDavid Howells 	if (time_before_eq(next_j, j))
4513136ef49SDavid Howells 		goto again;
4523136ef49SDavid Howells 	if (set)
4533136ef49SDavid Howells 		rxrpc_reduce_conn_timer(conn, next_j);
4543136ef49SDavid Howells }
4553136ef49SDavid Howells 
4563136ef49SDavid Howells /*
4578c3e34a4SDavid Howells  * connection-level event processor
4588c3e34a4SDavid Howells  */
4598c3e34a4SDavid Howells void rxrpc_process_connection(struct work_struct *work)
4608c3e34a4SDavid Howells {
4618c3e34a4SDavid Howells 	struct rxrpc_connection *conn =
4628c3e34a4SDavid Howells 		container_of(work, struct rxrpc_connection, processor);
4638c3e34a4SDavid Howells 	struct sk_buff *skb;
4648c3e34a4SDavid Howells 	u32 abort_code = RX_PROTOCOL_ERROR;
4658c3e34a4SDavid Howells 	int ret;
4668c3e34a4SDavid Howells 
467363deeabSDavid Howells 	rxrpc_see_connection(conn);
4688c3e34a4SDavid Howells 
4692c4579e4SDavid Howells 	if (test_and_clear_bit(RXRPC_CONN_EV_CHALLENGE, &conn->events))
4708c3e34a4SDavid Howells 		rxrpc_secure_connection(conn);
4718c3e34a4SDavid Howells 
4723136ef49SDavid Howells 	/* Process delayed ACKs whose time has come. */
4733136ef49SDavid Howells 	if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK)
4743136ef49SDavid Howells 		rxrpc_process_delayed_final_acks(conn);
4753136ef49SDavid Howells 
4768c3e34a4SDavid Howells 	/* go through the conn-level event packets, releasing the ref on this
4778c3e34a4SDavid Howells 	 * connection that each one has when we've finished with it */
4788c3e34a4SDavid Howells 	while ((skb = skb_dequeue(&conn->rx_queue))) {
47971f3ca40SDavid Howells 		rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
4808c3e34a4SDavid Howells 		ret = rxrpc_process_event(conn, skb, &abort_code);
4818c3e34a4SDavid Howells 		switch (ret) {
4828c3e34a4SDavid Howells 		case -EPROTO:
4838c3e34a4SDavid Howells 		case -EKEYEXPIRED:
4848c3e34a4SDavid Howells 		case -EKEYREJECTED:
4858c3e34a4SDavid Howells 			goto protocol_error;
4868c2f826dSDavid Howells 		case -ENOMEM:
4878c3e34a4SDavid Howells 		case -EAGAIN:
4888c3e34a4SDavid Howells 			goto requeue_and_leave;
4898c3e34a4SDavid Howells 		case -ECONNABORTED:
4908c3e34a4SDavid Howells 		default:
49171f3ca40SDavid Howells 			rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
4928c3e34a4SDavid Howells 			break;
4938c3e34a4SDavid Howells 		}
4948c3e34a4SDavid Howells 	}
4958c3e34a4SDavid Howells 
4968c3e34a4SDavid Howells out:
4978c3e34a4SDavid Howells 	rxrpc_put_connection(conn);
4988c3e34a4SDavid Howells 	_leave("");
4998c3e34a4SDavid Howells 	return;
5008c3e34a4SDavid Howells 
5018c3e34a4SDavid Howells requeue_and_leave:
5028c3e34a4SDavid Howells 	skb_queue_head(&conn->rx_queue, skb);
5038c3e34a4SDavid Howells 	goto out;
5048c3e34a4SDavid Howells 
5058c3e34a4SDavid Howells protocol_error:
5063a92789aSDavid Howells 	if (rxrpc_abort_connection(conn, ret, abort_code) < 0)
5078c3e34a4SDavid Howells 		goto requeue_and_leave;
50871f3ca40SDavid Howells 	rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
5098c3e34a4SDavid Howells 	goto out;
5108c3e34a4SDavid Howells }
511