xref: /openbmc/linux/net/rxrpc/output.c (revision 2874c5fd)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
28c3e34a4SDavid Howells /* RxRPC packet transmission
38c3e34a4SDavid Howells  *
48c3e34a4SDavid Howells  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
58c3e34a4SDavid Howells  * Written by David Howells (dhowells@redhat.com)
68c3e34a4SDavid Howells  */
78c3e34a4SDavid Howells 
88c3e34a4SDavid Howells #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
98c3e34a4SDavid Howells 
108c3e34a4SDavid Howells #include <linux/net.h>
118c3e34a4SDavid Howells #include <linux/gfp.h>
128c3e34a4SDavid Howells #include <linux/skbuff.h>
138c3e34a4SDavid Howells #include <linux/export.h>
148c3e34a4SDavid Howells #include <net/sock.h>
158c3e34a4SDavid Howells #include <net/af_rxrpc.h>
168c3e34a4SDavid Howells #include "ar-internal.h"
178c3e34a4SDavid Howells 
1826cb02aaSDavid Howells struct rxrpc_ack_buffer {
198d94aa38SDavid Howells 	struct rxrpc_wire_header whdr;
208d94aa38SDavid Howells 	struct rxrpc_ackpacket ack;
218d94aa38SDavid Howells 	u8 acks[255];
228d94aa38SDavid Howells 	u8 pad[3];
238d94aa38SDavid Howells 	struct rxrpc_ackinfo ackinfo;
248d94aa38SDavid Howells };
258d94aa38SDavid Howells 
2626cb02aaSDavid Howells struct rxrpc_abort_buffer {
2726cb02aaSDavid Howells 	struct rxrpc_wire_header whdr;
2826cb02aaSDavid Howells 	__be32 abort_code;
2926cb02aaSDavid Howells };
3026cb02aaSDavid Howells 
31ace45becSDavid Howells static const char rxrpc_keepalive_string[] = "";
32ace45becSDavid Howells 
338d94aa38SDavid Howells /*
34c7e86acfSDavid Howells  * Increase Tx backoff on transmission failure and clear it on success.
35c7e86acfSDavid Howells  */
36c7e86acfSDavid Howells static void rxrpc_tx_backoff(struct rxrpc_call *call, int ret)
37c7e86acfSDavid Howells {
38c7e86acfSDavid Howells 	if (ret < 0) {
39c7e86acfSDavid Howells 		u16 tx_backoff = READ_ONCE(call->tx_backoff);
40c7e86acfSDavid Howells 
41c7e86acfSDavid Howells 		if (tx_backoff < HZ)
42c7e86acfSDavid Howells 			WRITE_ONCE(call->tx_backoff, tx_backoff + 1);
43c7e86acfSDavid Howells 	} else {
44c7e86acfSDavid Howells 		WRITE_ONCE(call->tx_backoff, 0);
45c7e86acfSDavid Howells 	}
46c7e86acfSDavid Howells }
47c7e86acfSDavid Howells 
48c7e86acfSDavid Howells /*
49415f44e4SDavid Howells  * Arrange for a keepalive ping a certain time after we last transmitted.  This
50415f44e4SDavid Howells  * lets the far side know we're still interested in this call and helps keep
51415f44e4SDavid Howells  * the route through any intervening firewall open.
52415f44e4SDavid Howells  *
53415f44e4SDavid Howells  * Receiving a response to the ping will prevent the ->expect_rx_by timer from
54415f44e4SDavid Howells  * expiring.
55415f44e4SDavid Howells  */
56415f44e4SDavid Howells static void rxrpc_set_keepalive(struct rxrpc_call *call)
57415f44e4SDavid Howells {
58415f44e4SDavid Howells 	unsigned long now = jiffies, keepalive_at = call->next_rx_timo / 6;
59415f44e4SDavid Howells 
60415f44e4SDavid Howells 	keepalive_at += now;
61415f44e4SDavid Howells 	WRITE_ONCE(call->keepalive_at, keepalive_at);
62415f44e4SDavid Howells 	rxrpc_reduce_call_timer(call, keepalive_at, now,
63415f44e4SDavid Howells 				rxrpc_timer_set_for_keepalive);
64415f44e4SDavid Howells }
65415f44e4SDavid Howells 
66415f44e4SDavid Howells /*
678d94aa38SDavid Howells  * Fill out an ACK packet.
688d94aa38SDavid Howells  */
691457cc4cSDavid Howells static size_t rxrpc_fill_out_ack(struct rxrpc_connection *conn,
701457cc4cSDavid Howells 				 struct rxrpc_call *call,
7126cb02aaSDavid Howells 				 struct rxrpc_ack_buffer *pkt,
72805b21b9SDavid Howells 				 rxrpc_seq_t *_hard_ack,
73a5af7e1fSDavid Howells 				 rxrpc_seq_t *_top,
74a5af7e1fSDavid Howells 				 u8 reason)
758d94aa38SDavid Howells {
76f3639df2SDavid Howells 	rxrpc_serial_t serial;
77248f219cSDavid Howells 	rxrpc_seq_t hard_ack, top, seq;
78248f219cSDavid Howells 	int ix;
798d94aa38SDavid Howells 	u32 mtu, jmax;
808d94aa38SDavid Howells 	u8 *ackp = pkt->acks;
818d94aa38SDavid Howells 
82248f219cSDavid Howells 	/* Barrier against rxrpc_input_data(). */
83f3639df2SDavid Howells 	serial = call->ackr_serial;
84248f219cSDavid Howells 	hard_ack = READ_ONCE(call->rx_hard_ack);
85248f219cSDavid Howells 	top = smp_load_acquire(&call->rx_top);
86805b21b9SDavid Howells 	*_hard_ack = hard_ack;
87805b21b9SDavid Howells 	*_top = top;
88248f219cSDavid Howells 
898d94aa38SDavid Howells 	pkt->ack.bufferSpace	= htons(8);
90248f219cSDavid Howells 	pkt->ack.maxSkew	= htons(call->ackr_skew);
91248f219cSDavid Howells 	pkt->ack.firstPacket	= htonl(hard_ack + 1);
928d94aa38SDavid Howells 	pkt->ack.previousPacket	= htonl(call->ackr_prev_seq);
93f3639df2SDavid Howells 	pkt->ack.serial		= htonl(serial);
94a5af7e1fSDavid Howells 	pkt->ack.reason		= reason;
95248f219cSDavid Howells 	pkt->ack.nAcks		= top - hard_ack;
968d94aa38SDavid Howells 
97a5af7e1fSDavid Howells 	if (reason == RXRPC_ACK_PING)
988e83134dSDavid Howells 		pkt->whdr.flags |= RXRPC_REQUEST_ACK;
998e83134dSDavid Howells 
100248f219cSDavid Howells 	if (after(top, hard_ack)) {
101248f219cSDavid Howells 		seq = hard_ack + 1;
102248f219cSDavid Howells 		do {
103248f219cSDavid Howells 			ix = seq & RXRPC_RXTX_BUFF_MASK;
104248f219cSDavid Howells 			if (call->rxtx_buffer[ix])
105248f219cSDavid Howells 				*ackp++ = RXRPC_ACK_TYPE_ACK;
106248f219cSDavid Howells 			else
107248f219cSDavid Howells 				*ackp++ = RXRPC_ACK_TYPE_NACK;
108248f219cSDavid Howells 			seq++;
109248f219cSDavid Howells 		} while (before_eq(seq, top));
110248f219cSDavid Howells 	}
111248f219cSDavid Howells 
1121457cc4cSDavid Howells 	mtu = conn->params.peer->if_mtu;
1131457cc4cSDavid Howells 	mtu -= conn->params.peer->hdrsize;
11475e42126SDavid Howells 	jmax = (call->nr_jumbo_bad > 3) ? 1 : rxrpc_rx_jumbo_max;
1158d94aa38SDavid Howells 	pkt->ackinfo.rxMTU	= htonl(rxrpc_rx_mtu);
1168d94aa38SDavid Howells 	pkt->ackinfo.maxMTU	= htonl(mtu);
11775e42126SDavid Howells 	pkt->ackinfo.rwind	= htonl(call->rx_winsize);
1188d94aa38SDavid Howells 	pkt->ackinfo.jumbo_max	= htonl(jmax);
1198d94aa38SDavid Howells 
1208d94aa38SDavid Howells 	*ackp++ = 0;
1218d94aa38SDavid Howells 	*ackp++ = 0;
1228d94aa38SDavid Howells 	*ackp++ = 0;
123248f219cSDavid Howells 	return top - hard_ack + 3;
1248d94aa38SDavid Howells }
1258d94aa38SDavid Howells 
1268d94aa38SDavid Howells /*
12726cb02aaSDavid Howells  * Send an ACK call packet.
1288d94aa38SDavid Howells  */
129bd1fdf8cSDavid Howells int rxrpc_send_ack_packet(struct rxrpc_call *call, bool ping,
130bd1fdf8cSDavid Howells 			  rxrpc_serial_t *_serial)
1318d94aa38SDavid Howells {
1328d94aa38SDavid Howells 	struct rxrpc_connection *conn = NULL;
13326cb02aaSDavid Howells 	struct rxrpc_ack_buffer *pkt;
1348d94aa38SDavid Howells 	struct msghdr msg;
1358d94aa38SDavid Howells 	struct kvec iov[2];
1368d94aa38SDavid Howells 	rxrpc_serial_t serial;
137805b21b9SDavid Howells 	rxrpc_seq_t hard_ack, top;
1388d94aa38SDavid Howells 	size_t len, n;
13926cb02aaSDavid Howells 	int ret;
140a5af7e1fSDavid Howells 	u8 reason;
1418d94aa38SDavid Howells 
1428d94aa38SDavid Howells 	spin_lock_bh(&call->lock);
1438d94aa38SDavid Howells 	if (call->conn)
1448d94aa38SDavid Howells 		conn = rxrpc_get_connection_maybe(call->conn);
1458d94aa38SDavid Howells 	spin_unlock_bh(&call->lock);
1468d94aa38SDavid Howells 	if (!conn)
1478d94aa38SDavid Howells 		return -ECONNRESET;
1488d94aa38SDavid Howells 
1498d94aa38SDavid Howells 	pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
1508d94aa38SDavid Howells 	if (!pkt) {
1518d94aa38SDavid Howells 		rxrpc_put_connection(conn);
1528d94aa38SDavid Howells 		return -ENOMEM;
1538d94aa38SDavid Howells 	}
1548d94aa38SDavid Howells 
1558d94aa38SDavid Howells 	msg.msg_name	= &call->peer->srx.transport;
1568d94aa38SDavid Howells 	msg.msg_namelen	= call->peer->srx.transport_len;
1578d94aa38SDavid Howells 	msg.msg_control	= NULL;
1588d94aa38SDavid Howells 	msg.msg_controllen = 0;
1598d94aa38SDavid Howells 	msg.msg_flags	= 0;
1608d94aa38SDavid Howells 
1618d94aa38SDavid Howells 	pkt->whdr.epoch		= htonl(conn->proto.epoch);
1628d94aa38SDavid Howells 	pkt->whdr.cid		= htonl(call->cid);
1638d94aa38SDavid Howells 	pkt->whdr.callNumber	= htonl(call->call_id);
1648d94aa38SDavid Howells 	pkt->whdr.seq		= 0;
16526cb02aaSDavid Howells 	pkt->whdr.type		= RXRPC_PACKET_TYPE_ACK;
16626cb02aaSDavid Howells 	pkt->whdr.flags		= RXRPC_SLOW_START_OK | conn->out_clientflag;
1678d94aa38SDavid Howells 	pkt->whdr.userStatus	= 0;
1688d94aa38SDavid Howells 	pkt->whdr.securityIndex	= call->security_ix;
1698d94aa38SDavid Howells 	pkt->whdr._rsvd		= 0;
1708d94aa38SDavid Howells 	pkt->whdr.serviceId	= htons(call->service_id);
1718d94aa38SDavid Howells 
1728d94aa38SDavid Howells 	spin_lock_bh(&call->lock);
173a5af7e1fSDavid Howells 	if (ping) {
174a5af7e1fSDavid Howells 		reason = RXRPC_ACK_PING;
175a5af7e1fSDavid Howells 	} else {
176a5af7e1fSDavid Howells 		reason = call->ackr_reason;
17727d0fc43SDavid Howells 		if (!call->ackr_reason) {
17827d0fc43SDavid Howells 			spin_unlock_bh(&call->lock);
17927d0fc43SDavid Howells 			ret = 0;
18027d0fc43SDavid Howells 			goto out;
18127d0fc43SDavid Howells 		}
1828d94aa38SDavid Howells 		call->ackr_reason = 0;
183a5af7e1fSDavid Howells 	}
1841457cc4cSDavid Howells 	n = rxrpc_fill_out_ack(conn, call, pkt, &hard_ack, &top, reason);
1858d94aa38SDavid Howells 
1868d94aa38SDavid Howells 	spin_unlock_bh(&call->lock);
1878d94aa38SDavid Howells 
18826cb02aaSDavid Howells 	iov[0].iov_base	= pkt;
18926cb02aaSDavid Howells 	iov[0].iov_len	= sizeof(pkt->whdr) + sizeof(pkt->ack) + n;
1908d94aa38SDavid Howells 	iov[1].iov_base = &pkt->ackinfo;
1918d94aa38SDavid Howells 	iov[1].iov_len	= sizeof(pkt->ackinfo);
19226cb02aaSDavid Howells 	len = iov[0].iov_len + iov[1].iov_len;
1938d94aa38SDavid Howells 
194b86e218eSDavid Howells 	serial = atomic_inc_return(&conn->serial);
195b86e218eSDavid Howells 	pkt->whdr.serial = htonl(serial);
1964764c0daSDavid Howells 	trace_rxrpc_tx_ack(call->debug_id, serial,
197b86e218eSDavid Howells 			   ntohl(pkt->ack.firstPacket),
198b86e218eSDavid Howells 			   ntohl(pkt->ack.serial),
199b86e218eSDavid Howells 			   pkt->ack.reason, pkt->ack.nAcks);
200bd1fdf8cSDavid Howells 	if (_serial)
201bd1fdf8cSDavid Howells 		*_serial = serial;
202b86e218eSDavid Howells 
2038e83134dSDavid Howells 	if (ping) {
204a5af7e1fSDavid Howells 		call->ping_serial = serial;
2058e83134dSDavid Howells 		smp_wmb();
2068e83134dSDavid Howells 		/* We need to stick a time in before we send the packet in case
2078e83134dSDavid Howells 		 * the reply gets back before kernel_sendmsg() completes - but
2088e83134dSDavid Howells 		 * asking UDP to send the packet can take a relatively long
209b604dd98SDavid Howells 		 * time.
2108e83134dSDavid Howells 		 */
211a5af7e1fSDavid Howells 		call->ping_time = ktime_get_real();
2128e83134dSDavid Howells 		set_bit(RXRPC_CALL_PINGING, &call->flags);
2138e83134dSDavid Howells 		trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_ping, serial);
2148e83134dSDavid Howells 	}
21526cb02aaSDavid Howells 
21626cb02aaSDavid Howells 	ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
217330bdcfaSDavid Howells 	conn->params.peer->last_tx_at = ktime_get_seconds();
2186b47fe1dSDavid Howells 	if (ret < 0)
2196b47fe1dSDavid Howells 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
2204764c0daSDavid Howells 				    rxrpc_tx_point_call_ack);
2214764c0daSDavid Howells 	else
2224764c0daSDavid Howells 		trace_rxrpc_tx_packet(call->debug_id, &pkt->whdr,
2234764c0daSDavid Howells 				      rxrpc_tx_point_call_ack);
224c7e86acfSDavid Howells 	rxrpc_tx_backoff(call, ret);
2258d94aa38SDavid Howells 
22626cb02aaSDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE) {
227805b21b9SDavid Howells 		if (ret < 0) {
228a5af7e1fSDavid Howells 			if (ping)
2298e83134dSDavid Howells 				clear_bit(RXRPC_CALL_PINGING, &call->flags);
230248f219cSDavid Howells 			rxrpc_propose_ACK(call, pkt->ack.reason,
231248f219cSDavid Howells 					  ntohs(pkt->ack.maxSkew),
232248f219cSDavid Howells 					  ntohl(pkt->ack.serial),
233c7e86acfSDavid Howells 					  false, true,
2349c7ad434SDavid Howells 					  rxrpc_propose_ack_retry_tx);
235805b21b9SDavid Howells 		} else {
236805b21b9SDavid Howells 			spin_lock_bh(&call->lock);
237805b21b9SDavid Howells 			if (after(hard_ack, call->ackr_consumed))
238805b21b9SDavid Howells 				call->ackr_consumed = hard_ack;
239805b21b9SDavid Howells 			if (after(top, call->ackr_seen))
240805b21b9SDavid Howells 				call->ackr_seen = top;
241805b21b9SDavid Howells 			spin_unlock_bh(&call->lock);
242248f219cSDavid Howells 		}
243415f44e4SDavid Howells 
244415f44e4SDavid Howells 		rxrpc_set_keepalive(call);
245248f219cSDavid Howells 	}
246248f219cSDavid Howells 
2478d94aa38SDavid Howells out:
2488d94aa38SDavid Howells 	rxrpc_put_connection(conn);
2498d94aa38SDavid Howells 	kfree(pkt);
2508d94aa38SDavid Howells 	return ret;
2518d94aa38SDavid Howells }
2528d94aa38SDavid Howells 
2538c3e34a4SDavid Howells /*
25426cb02aaSDavid Howells  * Send an ABORT call packet.
25526cb02aaSDavid Howells  */
25626cb02aaSDavid Howells int rxrpc_send_abort_packet(struct rxrpc_call *call)
25726cb02aaSDavid Howells {
25826cb02aaSDavid Howells 	struct rxrpc_connection *conn = NULL;
25926cb02aaSDavid Howells 	struct rxrpc_abort_buffer pkt;
26026cb02aaSDavid Howells 	struct msghdr msg;
26126cb02aaSDavid Howells 	struct kvec iov[1];
26226cb02aaSDavid Howells 	rxrpc_serial_t serial;
26326cb02aaSDavid Howells 	int ret;
26426cb02aaSDavid Howells 
265dcbefc30SDavid Howells 	/* Don't bother sending aborts for a client call once the server has
266dcbefc30SDavid Howells 	 * hard-ACK'd all of its request data.  After that point, we're not
267dcbefc30SDavid Howells 	 * going to stop the operation proceeding, and whilst we might limit
268dcbefc30SDavid Howells 	 * the reply, it's not worth it if we can send a new call on the same
269dcbefc30SDavid Howells 	 * channel instead, thereby closing off this call.
270dcbefc30SDavid Howells 	 */
271dcbefc30SDavid Howells 	if (rxrpc_is_client_call(call) &&
272dcbefc30SDavid Howells 	    test_bit(RXRPC_CALL_TX_LAST, &call->flags))
273dcbefc30SDavid Howells 		return 0;
274dcbefc30SDavid Howells 
27526cb02aaSDavid Howells 	spin_lock_bh(&call->lock);
27626cb02aaSDavid Howells 	if (call->conn)
27726cb02aaSDavid Howells 		conn = rxrpc_get_connection_maybe(call->conn);
27826cb02aaSDavid Howells 	spin_unlock_bh(&call->lock);
27926cb02aaSDavid Howells 	if (!conn)
28026cb02aaSDavid Howells 		return -ECONNRESET;
28126cb02aaSDavid Howells 
28226cb02aaSDavid Howells 	msg.msg_name	= &call->peer->srx.transport;
28326cb02aaSDavid Howells 	msg.msg_namelen	= call->peer->srx.transport_len;
28426cb02aaSDavid Howells 	msg.msg_control	= NULL;
28526cb02aaSDavid Howells 	msg.msg_controllen = 0;
28626cb02aaSDavid Howells 	msg.msg_flags	= 0;
28726cb02aaSDavid Howells 
28826cb02aaSDavid Howells 	pkt.whdr.epoch		= htonl(conn->proto.epoch);
28926cb02aaSDavid Howells 	pkt.whdr.cid		= htonl(call->cid);
29026cb02aaSDavid Howells 	pkt.whdr.callNumber	= htonl(call->call_id);
29126cb02aaSDavid Howells 	pkt.whdr.seq		= 0;
29226cb02aaSDavid Howells 	pkt.whdr.type		= RXRPC_PACKET_TYPE_ABORT;
29326cb02aaSDavid Howells 	pkt.whdr.flags		= conn->out_clientflag;
29426cb02aaSDavid Howells 	pkt.whdr.userStatus	= 0;
29526cb02aaSDavid Howells 	pkt.whdr.securityIndex	= call->security_ix;
29626cb02aaSDavid Howells 	pkt.whdr._rsvd		= 0;
29726cb02aaSDavid Howells 	pkt.whdr.serviceId	= htons(call->service_id);
29826cb02aaSDavid Howells 	pkt.abort_code		= htonl(call->abort_code);
29926cb02aaSDavid Howells 
30026cb02aaSDavid Howells 	iov[0].iov_base	= &pkt;
30126cb02aaSDavid Howells 	iov[0].iov_len	= sizeof(pkt);
30226cb02aaSDavid Howells 
30326cb02aaSDavid Howells 	serial = atomic_inc_return(&conn->serial);
30426cb02aaSDavid Howells 	pkt.whdr.serial = htonl(serial);
30526cb02aaSDavid Howells 
30626cb02aaSDavid Howells 	ret = kernel_sendmsg(conn->params.local->socket,
30726cb02aaSDavid Howells 			     &msg, iov, 1, sizeof(pkt));
308330bdcfaSDavid Howells 	conn->params.peer->last_tx_at = ktime_get_seconds();
3096b47fe1dSDavid Howells 	if (ret < 0)
3106b47fe1dSDavid Howells 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
3114764c0daSDavid Howells 				    rxrpc_tx_point_call_abort);
3124764c0daSDavid Howells 	else
3134764c0daSDavid Howells 		trace_rxrpc_tx_packet(call->debug_id, &pkt.whdr,
3144764c0daSDavid Howells 				      rxrpc_tx_point_call_abort);
315c7e86acfSDavid Howells 	rxrpc_tx_backoff(call, ret);
31626cb02aaSDavid Howells 
31726cb02aaSDavid Howells 	rxrpc_put_connection(conn);
31826cb02aaSDavid Howells 	return ret;
31926cb02aaSDavid Howells }
32026cb02aaSDavid Howells 
32126cb02aaSDavid Howells /*
3228c3e34a4SDavid Howells  * send a packet through the transport endpoint
3238c3e34a4SDavid Howells  */
324a1767077SDavid Howells int rxrpc_send_data_packet(struct rxrpc_call *call, struct sk_buff *skb,
325a1767077SDavid Howells 			   bool retrans)
3268c3e34a4SDavid Howells {
3275a924b89SDavid Howells 	struct rxrpc_connection *conn = call->conn;
3285a924b89SDavid Howells 	struct rxrpc_wire_header whdr;
3295a924b89SDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
3308c3e34a4SDavid Howells 	struct msghdr msg;
3315a924b89SDavid Howells 	struct kvec iov[2];
3325a924b89SDavid Howells 	rxrpc_serial_t serial;
3335a924b89SDavid Howells 	size_t len;
3348c3e34a4SDavid Howells 	int ret, opt;
3358c3e34a4SDavid Howells 
3368c3e34a4SDavid Howells 	_enter(",{%d}", skb->len);
3378c3e34a4SDavid Howells 
3385a924b89SDavid Howells 	/* Each transmission of a Tx packet needs a new serial number */
3395a924b89SDavid Howells 	serial = atomic_inc_return(&conn->serial);
3408c3e34a4SDavid Howells 
3415a924b89SDavid Howells 	whdr.epoch	= htonl(conn->proto.epoch);
3425a924b89SDavid Howells 	whdr.cid	= htonl(call->cid);
3435a924b89SDavid Howells 	whdr.callNumber	= htonl(call->call_id);
3445a924b89SDavid Howells 	whdr.seq	= htonl(sp->hdr.seq);
3455a924b89SDavid Howells 	whdr.serial	= htonl(serial);
3465a924b89SDavid Howells 	whdr.type	= RXRPC_PACKET_TYPE_DATA;
3475a924b89SDavid Howells 	whdr.flags	= sp->hdr.flags;
3485a924b89SDavid Howells 	whdr.userStatus	= 0;
3495a924b89SDavid Howells 	whdr.securityIndex = call->security_ix;
3505a924b89SDavid Howells 	whdr._rsvd	= htons(sp->hdr._rsvd);
3515a924b89SDavid Howells 	whdr.serviceId	= htons(call->service_id);
3525a924b89SDavid Howells 
3534e255721SDavid Howells 	if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) &&
3544e255721SDavid Howells 	    sp->hdr.seq == 1)
3554e255721SDavid Howells 		whdr.userStatus	= RXRPC_USERSTATUS_SERVICE_UPGRADE;
3564e255721SDavid Howells 
3575a924b89SDavid Howells 	iov[0].iov_base = &whdr;
3585a924b89SDavid Howells 	iov[0].iov_len = sizeof(whdr);
3595a924b89SDavid Howells 	iov[1].iov_base = skb->head;
3605a924b89SDavid Howells 	iov[1].iov_len = skb->len;
3615a924b89SDavid Howells 	len = iov[0].iov_len + iov[1].iov_len;
3625a924b89SDavid Howells 
3635a924b89SDavid Howells 	msg.msg_name = &call->peer->srx.transport;
3645a924b89SDavid Howells 	msg.msg_namelen = call->peer->srx.transport_len;
3658c3e34a4SDavid Howells 	msg.msg_control = NULL;
3668c3e34a4SDavid Howells 	msg.msg_controllen = 0;
3678c3e34a4SDavid Howells 	msg.msg_flags = 0;
3688c3e34a4SDavid Howells 
36957494343SDavid Howells 	/* If our RTT cache needs working on, request an ACK.  Also request
37057494343SDavid Howells 	 * ACKs if a DATA packet appears to have been lost.
371b604dd98SDavid Howells 	 *
372b604dd98SDavid Howells 	 * However, we mustn't request an ACK on the last reply packet of a
373b604dd98SDavid Howells 	 * service call, lest OpenAFS incorrectly send us an ACK with some
374b604dd98SDavid Howells 	 * soft-ACKs in it and then never follow up with a proper hard ACK.
37557494343SDavid Howells 	 */
376b604dd98SDavid Howells 	if ((!(sp->hdr.flags & RXRPC_LAST_PACKET) ||
377b604dd98SDavid Howells 	     rxrpc_to_server(sp)
378b604dd98SDavid Howells 	     ) &&
379bd1fdf8cSDavid Howells 	    (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events) ||
380bd1fdf8cSDavid Howells 	     retrans ||
381b112a670SDavid Howells 	     call->cong_mode == RXRPC_CALL_SLOW_START ||
38257494343SDavid Howells 	     (call->peer->rtt_usage < 3 && sp->hdr.seq & 1) ||
3830d4b103cSDavid Howells 	     ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
384bf7d620aSDavid Howells 			  ktime_get_real())))
3850d4b103cSDavid Howells 		whdr.flags |= RXRPC_REQUEST_ACK;
3860d4b103cSDavid Howells 
3878a681c36SDavid Howells 	if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
3888a681c36SDavid Howells 		static int lose;
3898a681c36SDavid Howells 		if ((lose++ & 7) == 7) {
390a1767077SDavid Howells 			ret = 0;
391526949e8SArnd Bergmann 			trace_rxrpc_tx_data(call, sp->hdr.seq, serial,
392526949e8SArnd Bergmann 					    whdr.flags, retrans, true);
393526949e8SArnd Bergmann 			goto done;
3948a681c36SDavid Howells 		}
3958a681c36SDavid Howells 	}
3968a681c36SDavid Howells 
397526949e8SArnd Bergmann 	trace_rxrpc_tx_data(call, sp->hdr.seq, serial, whdr.flags, retrans,
398526949e8SArnd Bergmann 			    false);
3995a924b89SDavid Howells 
4008c3e34a4SDavid Howells 	/* send the packet with the don't fragment bit set if we currently
4018c3e34a4SDavid Howells 	 * think it's small enough */
4025a924b89SDavid Howells 	if (iov[1].iov_len >= call->peer->maxdata)
4035a924b89SDavid Howells 		goto send_fragmentable;
4045a924b89SDavid Howells 
405985a5c82SDavid Howells 	down_read(&conn->params.local->defrag_sem);
406b604dd98SDavid Howells 
407b604dd98SDavid Howells 	sp->hdr.serial = serial;
408b604dd98SDavid Howells 	smp_wmb(); /* Set serial before timestamp */
409b604dd98SDavid Howells 	skb->tstamp = ktime_get_real();
410b604dd98SDavid Howells 
4118c3e34a4SDavid Howells 	/* send the packet by UDP
4128c3e34a4SDavid Howells 	 * - returns -EMSGSIZE if UDP would have to fragment the packet
4138c3e34a4SDavid Howells 	 *   to go out of the interface
4148c3e34a4SDavid Howells 	 *   - in which case, we'll have processed the ICMP error
4158c3e34a4SDavid Howells 	 *     message and update the peer record
4168c3e34a4SDavid Howells 	 */
4175a924b89SDavid Howells 	ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
418330bdcfaSDavid Howells 	conn->params.peer->last_tx_at = ktime_get_seconds();
4198c3e34a4SDavid Howells 
420985a5c82SDavid Howells 	up_read(&conn->params.local->defrag_sem);
4216b47fe1dSDavid Howells 	if (ret < 0)
4226b47fe1dSDavid Howells 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
4234764c0daSDavid Howells 				    rxrpc_tx_point_call_data_nofrag);
4244764c0daSDavid Howells 	else
4254764c0daSDavid Howells 		trace_rxrpc_tx_packet(call->debug_id, &whdr,
4264764c0daSDavid Howells 				      rxrpc_tx_point_call_data_nofrag);
427c7e86acfSDavid Howells 	rxrpc_tx_backoff(call, ret);
4288c3e34a4SDavid Howells 	if (ret == -EMSGSIZE)
4298c3e34a4SDavid Howells 		goto send_fragmentable;
4308c3e34a4SDavid Howells 
4315a924b89SDavid Howells done:
43250235c4bSDavid Howells 	if (ret >= 0) {
4330d4b103cSDavid Howells 		if (whdr.flags & RXRPC_REQUEST_ACK) {
434b604dd98SDavid Howells 			call->peer->rtt_last_req = skb->tstamp;
43550235c4bSDavid Howells 			trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_data, serial);
436bd1fdf8cSDavid Howells 			if (call->peer->rtt_usage > 1) {
437bd1fdf8cSDavid Howells 				unsigned long nowj = jiffies, ack_lost_at;
438bd1fdf8cSDavid Howells 
439bd1fdf8cSDavid Howells 				ack_lost_at = nsecs_to_jiffies(2 * call->peer->rtt);
440bd1fdf8cSDavid Howells 				if (ack_lost_at < 1)
441bd1fdf8cSDavid Howells 					ack_lost_at = 1;
442bd1fdf8cSDavid Howells 
443bd1fdf8cSDavid Howells 				ack_lost_at += nowj;
444bd1fdf8cSDavid Howells 				WRITE_ONCE(call->ack_lost_at, ack_lost_at);
445bd1fdf8cSDavid Howells 				rxrpc_reduce_call_timer(call, ack_lost_at, nowj,
446bd1fdf8cSDavid Howells 							rxrpc_timer_set_for_lost_ack);
447bd1fdf8cSDavid Howells 			}
4488c3e34a4SDavid Howells 		}
449c54e43d7SDavid Howells 
450c54e43d7SDavid Howells 		if (sp->hdr.seq == 1 &&
451c54e43d7SDavid Howells 		    !test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER,
452c54e43d7SDavid Howells 				      &call->flags)) {
453c54e43d7SDavid Howells 			unsigned long nowj = jiffies, expect_rx_by;
454c54e43d7SDavid Howells 
455c54e43d7SDavid Howells 			expect_rx_by = nowj + call->next_rx_timo;
456c54e43d7SDavid Howells 			WRITE_ONCE(call->expect_rx_by, expect_rx_by);
457c54e43d7SDavid Howells 			rxrpc_reduce_call_timer(call, expect_rx_by, nowj,
458c54e43d7SDavid Howells 						rxrpc_timer_set_for_normal);
459c54e43d7SDavid Howells 		}
460415f44e4SDavid Howells 
461415f44e4SDavid Howells 		rxrpc_set_keepalive(call);
462c7e86acfSDavid Howells 	} else {
463c7e86acfSDavid Howells 		/* Cancel the call if the initial transmission fails,
464c7e86acfSDavid Howells 		 * particularly if that's due to network routing issues that
465c7e86acfSDavid Howells 		 * aren't going away anytime soon.  The layer above can arrange
466c7e86acfSDavid Howells 		 * the retransmission.
467c7e86acfSDavid Howells 		 */
468c7e86acfSDavid Howells 		if (!test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER, &call->flags))
469c7e86acfSDavid Howells 			rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
470c7e86acfSDavid Howells 						  RX_USER_ABORT, ret);
471c7e86acfSDavid Howells 	}
472415f44e4SDavid Howells 
4735a924b89SDavid Howells 	_leave(" = %d [%u]", ret, call->peer->maxdata);
4745a924b89SDavid Howells 	return ret;
4758c3e34a4SDavid Howells 
4768c3e34a4SDavid Howells send_fragmentable:
4778c3e34a4SDavid Howells 	/* attempt to send this message with fragmentation enabled */
4788c3e34a4SDavid Howells 	_debug("send fragment");
4798c3e34a4SDavid Howells 
480985a5c82SDavid Howells 	down_write(&conn->params.local->defrag_sem);
481985a5c82SDavid Howells 
482b604dd98SDavid Howells 	sp->hdr.serial = serial;
483b604dd98SDavid Howells 	smp_wmb(); /* Set serial before timestamp */
484b604dd98SDavid Howells 	skb->tstamp = ktime_get_real();
485b604dd98SDavid Howells 
486985a5c82SDavid Howells 	switch (conn->params.local->srx.transport.family) {
487985a5c82SDavid Howells 	case AF_INET:
4888c3e34a4SDavid Howells 		opt = IP_PMTUDISC_DONT;
489985a5c82SDavid Howells 		ret = kernel_setsockopt(conn->params.local->socket,
490985a5c82SDavid Howells 					SOL_IP, IP_MTU_DISCOVER,
4918c3e34a4SDavid Howells 					(char *)&opt, sizeof(opt));
4928c3e34a4SDavid Howells 		if (ret == 0) {
4935a924b89SDavid Howells 			ret = kernel_sendmsg(conn->params.local->socket, &msg,
4945a924b89SDavid Howells 					     iov, 2, len);
495330bdcfaSDavid Howells 			conn->params.peer->last_tx_at = ktime_get_seconds();
4968c3e34a4SDavid Howells 
4978c3e34a4SDavid Howells 			opt = IP_PMTUDISC_DO;
498985a5c82SDavid Howells 			kernel_setsockopt(conn->params.local->socket, SOL_IP,
499985a5c82SDavid Howells 					  IP_MTU_DISCOVER,
500985a5c82SDavid Howells 					  (char *)&opt, sizeof(opt));
501985a5c82SDavid Howells 		}
502985a5c82SDavid Howells 		break;
50375b54cb5SDavid Howells 
504d1912747SDavid Howells #ifdef CONFIG_AF_RXRPC_IPV6
50575b54cb5SDavid Howells 	case AF_INET6:
50675b54cb5SDavid Howells 		opt = IPV6_PMTUDISC_DONT;
50775b54cb5SDavid Howells 		ret = kernel_setsockopt(conn->params.local->socket,
50875b54cb5SDavid Howells 					SOL_IPV6, IPV6_MTU_DISCOVER,
50975b54cb5SDavid Howells 					(char *)&opt, sizeof(opt));
51075b54cb5SDavid Howells 		if (ret == 0) {
51175b54cb5SDavid Howells 			ret = kernel_sendmsg(conn->params.local->socket, &msg,
51293c62c45SDavid Howells 					     iov, 2, len);
513330bdcfaSDavid Howells 			conn->params.peer->last_tx_at = ktime_get_seconds();
51475b54cb5SDavid Howells 
51575b54cb5SDavid Howells 			opt = IPV6_PMTUDISC_DO;
51675b54cb5SDavid Howells 			kernel_setsockopt(conn->params.local->socket,
51775b54cb5SDavid Howells 					  SOL_IPV6, IPV6_MTU_DISCOVER,
51875b54cb5SDavid Howells 					  (char *)&opt, sizeof(opt));
51975b54cb5SDavid Howells 		}
52075b54cb5SDavid Howells 		break;
521d1912747SDavid Howells #endif
5228c3e34a4SDavid Howells 	}
5238c3e34a4SDavid Howells 
5246b47fe1dSDavid Howells 	if (ret < 0)
5256b47fe1dSDavid Howells 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
5264764c0daSDavid Howells 				    rxrpc_tx_point_call_data_frag);
5274764c0daSDavid Howells 	else
5284764c0daSDavid Howells 		trace_rxrpc_tx_packet(call->debug_id, &whdr,
5294764c0daSDavid Howells 				      rxrpc_tx_point_call_data_frag);
530c7e86acfSDavid Howells 	rxrpc_tx_backoff(call, ret);
5316b47fe1dSDavid Howells 
532985a5c82SDavid Howells 	up_write(&conn->params.local->defrag_sem);
5335a924b89SDavid Howells 	goto done;
5348c3e34a4SDavid Howells }
535248f219cSDavid Howells 
536248f219cSDavid Howells /*
537248f219cSDavid Howells  * reject packets through the local endpoint
538248f219cSDavid Howells  */
539248f219cSDavid Howells void rxrpc_reject_packets(struct rxrpc_local *local)
540248f219cSDavid Howells {
5411c2bc7b9SDavid Howells 	struct sockaddr_rxrpc srx;
542248f219cSDavid Howells 	struct rxrpc_skb_priv *sp;
543248f219cSDavid Howells 	struct rxrpc_wire_header whdr;
544248f219cSDavid Howells 	struct sk_buff *skb;
545248f219cSDavid Howells 	struct msghdr msg;
546248f219cSDavid Howells 	struct kvec iov[2];
547248f219cSDavid Howells 	size_t size;
548248f219cSDavid Howells 	__be32 code;
549ece64fecSDavid Howells 	int ret, ioc;
550248f219cSDavid Howells 
551248f219cSDavid Howells 	_enter("%d", local->debug_id);
552248f219cSDavid Howells 
553248f219cSDavid Howells 	iov[0].iov_base = &whdr;
554248f219cSDavid Howells 	iov[0].iov_len = sizeof(whdr);
555248f219cSDavid Howells 	iov[1].iov_base = &code;
556248f219cSDavid Howells 	iov[1].iov_len = sizeof(code);
557248f219cSDavid Howells 
5581c2bc7b9SDavid Howells 	msg.msg_name = &srx.transport;
559248f219cSDavid Howells 	msg.msg_control = NULL;
560248f219cSDavid Howells 	msg.msg_controllen = 0;
561248f219cSDavid Howells 	msg.msg_flags = 0;
562248f219cSDavid Howells 
563248f219cSDavid Howells 	memset(&whdr, 0, sizeof(whdr));
564248f219cSDavid Howells 
565248f219cSDavid Howells 	while ((skb = skb_dequeue(&local->reject_queue))) {
56671f3ca40SDavid Howells 		rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
567248f219cSDavid Howells 		sp = rxrpc_skb(skb);
5681c2bc7b9SDavid Howells 
569ece64fecSDavid Howells 		switch (skb->mark) {
570ece64fecSDavid Howells 		case RXRPC_SKB_MARK_REJECT_BUSY:
571ece64fecSDavid Howells 			whdr.type = RXRPC_PACKET_TYPE_BUSY;
572ece64fecSDavid Howells 			size = sizeof(whdr);
573ece64fecSDavid Howells 			ioc = 1;
574ece64fecSDavid Howells 			break;
575ece64fecSDavid Howells 		case RXRPC_SKB_MARK_REJECT_ABORT:
576ece64fecSDavid Howells 			whdr.type = RXRPC_PACKET_TYPE_ABORT;
577ece64fecSDavid Howells 			code = htonl(skb->priority);
578ece64fecSDavid Howells 			size = sizeof(whdr) + sizeof(code);
579ece64fecSDavid Howells 			ioc = 2;
580ece64fecSDavid Howells 			break;
581ece64fecSDavid Howells 		default:
582ece64fecSDavid Howells 			rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
583ece64fecSDavid Howells 			continue;
584ece64fecSDavid Howells 		}
585ece64fecSDavid Howells 
5865a790b73SDavid Howells 		if (rxrpc_extract_addr_from_skb(&srx, skb) == 0) {
5871c2bc7b9SDavid Howells 			msg.msg_namelen = srx.transport_len;
5881c2bc7b9SDavid Howells 
589248f219cSDavid Howells 			whdr.epoch	= htonl(sp->hdr.epoch);
590248f219cSDavid Howells 			whdr.cid	= htonl(sp->hdr.cid);
591248f219cSDavid Howells 			whdr.callNumber	= htonl(sp->hdr.callNumber);
592248f219cSDavid Howells 			whdr.serviceId	= htons(sp->hdr.serviceId);
593248f219cSDavid Howells 			whdr.flags	= sp->hdr.flags;
594248f219cSDavid Howells 			whdr.flags	^= RXRPC_CLIENT_INITIATED;
595248f219cSDavid Howells 			whdr.flags	&= RXRPC_CLIENT_INITIATED;
596248f219cSDavid Howells 
597d6672a5aSYueHaibing 			ret = kernel_sendmsg(local->socket, &msg,
598d6672a5aSYueHaibing 					     iov, ioc, size);
5996b47fe1dSDavid Howells 			if (ret < 0)
6006b47fe1dSDavid Howells 				trace_rxrpc_tx_fail(local->debug_id, 0, ret,
6014764c0daSDavid Howells 						    rxrpc_tx_point_reject);
6024764c0daSDavid Howells 			else
6034764c0daSDavid Howells 				trace_rxrpc_tx_packet(local->debug_id, &whdr,
6044764c0daSDavid Howells 						      rxrpc_tx_point_reject);
605248f219cSDavid Howells 		}
606248f219cSDavid Howells 
60771f3ca40SDavid Howells 		rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
608248f219cSDavid Howells 	}
609248f219cSDavid Howells 
610248f219cSDavid Howells 	_leave("");
611248f219cSDavid Howells }
612ace45becSDavid Howells 
613ace45becSDavid Howells /*
614ace45becSDavid Howells  * Send a VERSION reply to a peer as a keepalive.
615ace45becSDavid Howells  */
616ace45becSDavid Howells void rxrpc_send_keepalive(struct rxrpc_peer *peer)
617ace45becSDavid Howells {
618ace45becSDavid Howells 	struct rxrpc_wire_header whdr;
619ace45becSDavid Howells 	struct msghdr msg;
620ace45becSDavid Howells 	struct kvec iov[2];
621ace45becSDavid Howells 	size_t len;
622ace45becSDavid Howells 	int ret;
623ace45becSDavid Howells 
624ace45becSDavid Howells 	_enter("");
625ace45becSDavid Howells 
626ace45becSDavid Howells 	msg.msg_name	= &peer->srx.transport;
627ace45becSDavid Howells 	msg.msg_namelen	= peer->srx.transport_len;
628ace45becSDavid Howells 	msg.msg_control	= NULL;
629ace45becSDavid Howells 	msg.msg_controllen = 0;
630ace45becSDavid Howells 	msg.msg_flags	= 0;
631ace45becSDavid Howells 
632ace45becSDavid Howells 	whdr.epoch	= htonl(peer->local->rxnet->epoch);
633ace45becSDavid Howells 	whdr.cid	= 0;
634ace45becSDavid Howells 	whdr.callNumber	= 0;
635ace45becSDavid Howells 	whdr.seq	= 0;
636ace45becSDavid Howells 	whdr.serial	= 0;
637ace45becSDavid Howells 	whdr.type	= RXRPC_PACKET_TYPE_VERSION; /* Not client-initiated */
638ace45becSDavid Howells 	whdr.flags	= RXRPC_LAST_PACKET;
639ace45becSDavid Howells 	whdr.userStatus	= 0;
640ace45becSDavid Howells 	whdr.securityIndex = 0;
641ace45becSDavid Howells 	whdr._rsvd	= 0;
642ace45becSDavid Howells 	whdr.serviceId	= 0;
643ace45becSDavid Howells 
644ace45becSDavid Howells 	iov[0].iov_base	= &whdr;
645ace45becSDavid Howells 	iov[0].iov_len	= sizeof(whdr);
646ace45becSDavid Howells 	iov[1].iov_base	= (char *)rxrpc_keepalive_string;
647ace45becSDavid Howells 	iov[1].iov_len	= sizeof(rxrpc_keepalive_string);
648ace45becSDavid Howells 
649ace45becSDavid Howells 	len = iov[0].iov_len + iov[1].iov_len;
650ace45becSDavid Howells 
651ace45becSDavid Howells 	_proto("Tx VERSION (keepalive)");
652ace45becSDavid Howells 
653ace45becSDavid Howells 	ret = kernel_sendmsg(peer->local->socket, &msg, iov, 2, len);
654ace45becSDavid Howells 	if (ret < 0)
6556b47fe1dSDavid Howells 		trace_rxrpc_tx_fail(peer->debug_id, 0, ret,
6564764c0daSDavid Howells 				    rxrpc_tx_point_version_keepalive);
6574764c0daSDavid Howells 	else
6584764c0daSDavid Howells 		trace_rxrpc_tx_packet(peer->debug_id, &whdr,
6594764c0daSDavid Howells 				      rxrpc_tx_point_version_keepalive);
660ace45becSDavid Howells 
661330bdcfaSDavid Howells 	peer->last_tx_at = ktime_get_seconds();
662ace45becSDavid Howells 	_leave("");
663ace45becSDavid Howells }
664