xref: /openbmc/linux/net/rxrpc/output.c (revision ece64fec)
18c3e34a4SDavid Howells /* RxRPC packet transmission
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/net.h>
158c3e34a4SDavid Howells #include <linux/gfp.h>
168c3e34a4SDavid Howells #include <linux/skbuff.h>
178c3e34a4SDavid Howells #include <linux/export.h>
188c3e34a4SDavid Howells #include <net/sock.h>
198c3e34a4SDavid Howells #include <net/af_rxrpc.h>
208c3e34a4SDavid Howells #include "ar-internal.h"
218c3e34a4SDavid Howells 
2226cb02aaSDavid Howells struct rxrpc_ack_buffer {
238d94aa38SDavid Howells 	struct rxrpc_wire_header whdr;
248d94aa38SDavid Howells 	struct rxrpc_ackpacket ack;
258d94aa38SDavid Howells 	u8 acks[255];
268d94aa38SDavid Howells 	u8 pad[3];
278d94aa38SDavid Howells 	struct rxrpc_ackinfo ackinfo;
288d94aa38SDavid Howells };
298d94aa38SDavid Howells 
3026cb02aaSDavid Howells struct rxrpc_abort_buffer {
3126cb02aaSDavid Howells 	struct rxrpc_wire_header whdr;
3226cb02aaSDavid Howells 	__be32 abort_code;
3326cb02aaSDavid Howells };
3426cb02aaSDavid Howells 
35ace45becSDavid Howells static const char rxrpc_keepalive_string[] = "";
36ace45becSDavid Howells 
378d94aa38SDavid Howells /*
38415f44e4SDavid Howells  * Arrange for a keepalive ping a certain time after we last transmitted.  This
39415f44e4SDavid Howells  * lets the far side know we're still interested in this call and helps keep
40415f44e4SDavid Howells  * the route through any intervening firewall open.
41415f44e4SDavid Howells  *
42415f44e4SDavid Howells  * Receiving a response to the ping will prevent the ->expect_rx_by timer from
43415f44e4SDavid Howells  * expiring.
44415f44e4SDavid Howells  */
45415f44e4SDavid Howells static void rxrpc_set_keepalive(struct rxrpc_call *call)
46415f44e4SDavid Howells {
47415f44e4SDavid Howells 	unsigned long now = jiffies, keepalive_at = call->next_rx_timo / 6;
48415f44e4SDavid Howells 
49415f44e4SDavid Howells 	keepalive_at += now;
50415f44e4SDavid Howells 	WRITE_ONCE(call->keepalive_at, keepalive_at);
51415f44e4SDavid Howells 	rxrpc_reduce_call_timer(call, keepalive_at, now,
52415f44e4SDavid Howells 				rxrpc_timer_set_for_keepalive);
53415f44e4SDavid Howells }
54415f44e4SDavid Howells 
55415f44e4SDavid Howells /*
568d94aa38SDavid Howells  * Fill out an ACK packet.
578d94aa38SDavid Howells  */
581457cc4cSDavid Howells static size_t rxrpc_fill_out_ack(struct rxrpc_connection *conn,
591457cc4cSDavid Howells 				 struct rxrpc_call *call,
6026cb02aaSDavid Howells 				 struct rxrpc_ack_buffer *pkt,
61805b21b9SDavid Howells 				 rxrpc_seq_t *_hard_ack,
62a5af7e1fSDavid Howells 				 rxrpc_seq_t *_top,
63a5af7e1fSDavid Howells 				 u8 reason)
648d94aa38SDavid Howells {
65f3639df2SDavid Howells 	rxrpc_serial_t serial;
66248f219cSDavid Howells 	rxrpc_seq_t hard_ack, top, seq;
67248f219cSDavid Howells 	int ix;
688d94aa38SDavid Howells 	u32 mtu, jmax;
698d94aa38SDavid Howells 	u8 *ackp = pkt->acks;
708d94aa38SDavid Howells 
71248f219cSDavid Howells 	/* Barrier against rxrpc_input_data(). */
72f3639df2SDavid Howells 	serial = call->ackr_serial;
73248f219cSDavid Howells 	hard_ack = READ_ONCE(call->rx_hard_ack);
74248f219cSDavid Howells 	top = smp_load_acquire(&call->rx_top);
75805b21b9SDavid Howells 	*_hard_ack = hard_ack;
76805b21b9SDavid Howells 	*_top = top;
77248f219cSDavid Howells 
788d94aa38SDavid Howells 	pkt->ack.bufferSpace	= htons(8);
79248f219cSDavid Howells 	pkt->ack.maxSkew	= htons(call->ackr_skew);
80248f219cSDavid Howells 	pkt->ack.firstPacket	= htonl(hard_ack + 1);
818d94aa38SDavid Howells 	pkt->ack.previousPacket	= htonl(call->ackr_prev_seq);
82f3639df2SDavid Howells 	pkt->ack.serial		= htonl(serial);
83a5af7e1fSDavid Howells 	pkt->ack.reason		= reason;
84248f219cSDavid Howells 	pkt->ack.nAcks		= top - hard_ack;
858d94aa38SDavid Howells 
86a5af7e1fSDavid Howells 	if (reason == RXRPC_ACK_PING)
878e83134dSDavid Howells 		pkt->whdr.flags |= RXRPC_REQUEST_ACK;
888e83134dSDavid Howells 
89248f219cSDavid Howells 	if (after(top, hard_ack)) {
90248f219cSDavid Howells 		seq = hard_ack + 1;
91248f219cSDavid Howells 		do {
92248f219cSDavid Howells 			ix = seq & RXRPC_RXTX_BUFF_MASK;
93248f219cSDavid Howells 			if (call->rxtx_buffer[ix])
94248f219cSDavid Howells 				*ackp++ = RXRPC_ACK_TYPE_ACK;
95248f219cSDavid Howells 			else
96248f219cSDavid Howells 				*ackp++ = RXRPC_ACK_TYPE_NACK;
97248f219cSDavid Howells 			seq++;
98248f219cSDavid Howells 		} while (before_eq(seq, top));
99248f219cSDavid Howells 	}
100248f219cSDavid Howells 
1011457cc4cSDavid Howells 	mtu = conn->params.peer->if_mtu;
1021457cc4cSDavid Howells 	mtu -= conn->params.peer->hdrsize;
10375e42126SDavid Howells 	jmax = (call->nr_jumbo_bad > 3) ? 1 : rxrpc_rx_jumbo_max;
1048d94aa38SDavid Howells 	pkt->ackinfo.rxMTU	= htonl(rxrpc_rx_mtu);
1058d94aa38SDavid Howells 	pkt->ackinfo.maxMTU	= htonl(mtu);
10675e42126SDavid Howells 	pkt->ackinfo.rwind	= htonl(call->rx_winsize);
1078d94aa38SDavid Howells 	pkt->ackinfo.jumbo_max	= htonl(jmax);
1088d94aa38SDavid Howells 
1098d94aa38SDavid Howells 	*ackp++ = 0;
1108d94aa38SDavid Howells 	*ackp++ = 0;
1118d94aa38SDavid Howells 	*ackp++ = 0;
112248f219cSDavid Howells 	return top - hard_ack + 3;
1138d94aa38SDavid Howells }
1148d94aa38SDavid Howells 
1158d94aa38SDavid Howells /*
11626cb02aaSDavid Howells  * Send an ACK call packet.
1178d94aa38SDavid Howells  */
118bd1fdf8cSDavid Howells int rxrpc_send_ack_packet(struct rxrpc_call *call, bool ping,
119bd1fdf8cSDavid Howells 			  rxrpc_serial_t *_serial)
1208d94aa38SDavid Howells {
1218d94aa38SDavid Howells 	struct rxrpc_connection *conn = NULL;
12226cb02aaSDavid Howells 	struct rxrpc_ack_buffer *pkt;
1238d94aa38SDavid Howells 	struct msghdr msg;
1248d94aa38SDavid Howells 	struct kvec iov[2];
1258d94aa38SDavid Howells 	rxrpc_serial_t serial;
126805b21b9SDavid Howells 	rxrpc_seq_t hard_ack, top;
1278d94aa38SDavid Howells 	size_t len, n;
12826cb02aaSDavid Howells 	int ret;
129a5af7e1fSDavid Howells 	u8 reason;
1308d94aa38SDavid Howells 
1318d94aa38SDavid Howells 	spin_lock_bh(&call->lock);
1328d94aa38SDavid Howells 	if (call->conn)
1338d94aa38SDavid Howells 		conn = rxrpc_get_connection_maybe(call->conn);
1348d94aa38SDavid Howells 	spin_unlock_bh(&call->lock);
1358d94aa38SDavid Howells 	if (!conn)
1368d94aa38SDavid Howells 		return -ECONNRESET;
1378d94aa38SDavid Howells 
1388d94aa38SDavid Howells 	pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
1398d94aa38SDavid Howells 	if (!pkt) {
1408d94aa38SDavid Howells 		rxrpc_put_connection(conn);
1418d94aa38SDavid Howells 		return -ENOMEM;
1428d94aa38SDavid Howells 	}
1438d94aa38SDavid Howells 
1448d94aa38SDavid Howells 	msg.msg_name	= &call->peer->srx.transport;
1458d94aa38SDavid Howells 	msg.msg_namelen	= call->peer->srx.transport_len;
1468d94aa38SDavid Howells 	msg.msg_control	= NULL;
1478d94aa38SDavid Howells 	msg.msg_controllen = 0;
1488d94aa38SDavid Howells 	msg.msg_flags	= 0;
1498d94aa38SDavid Howells 
1508d94aa38SDavid Howells 	pkt->whdr.epoch		= htonl(conn->proto.epoch);
1518d94aa38SDavid Howells 	pkt->whdr.cid		= htonl(call->cid);
1528d94aa38SDavid Howells 	pkt->whdr.callNumber	= htonl(call->call_id);
1538d94aa38SDavid Howells 	pkt->whdr.seq		= 0;
15426cb02aaSDavid Howells 	pkt->whdr.type		= RXRPC_PACKET_TYPE_ACK;
15526cb02aaSDavid Howells 	pkt->whdr.flags		= RXRPC_SLOW_START_OK | conn->out_clientflag;
1568d94aa38SDavid Howells 	pkt->whdr.userStatus	= 0;
1578d94aa38SDavid Howells 	pkt->whdr.securityIndex	= call->security_ix;
1588d94aa38SDavid Howells 	pkt->whdr._rsvd		= 0;
1598d94aa38SDavid Howells 	pkt->whdr.serviceId	= htons(call->service_id);
1608d94aa38SDavid Howells 
1618d94aa38SDavid Howells 	spin_lock_bh(&call->lock);
162a5af7e1fSDavid Howells 	if (ping) {
163a5af7e1fSDavid Howells 		reason = RXRPC_ACK_PING;
164a5af7e1fSDavid Howells 	} else {
165a5af7e1fSDavid Howells 		reason = call->ackr_reason;
16627d0fc43SDavid Howells 		if (!call->ackr_reason) {
16727d0fc43SDavid Howells 			spin_unlock_bh(&call->lock);
16827d0fc43SDavid Howells 			ret = 0;
16927d0fc43SDavid Howells 			goto out;
17027d0fc43SDavid Howells 		}
1718d94aa38SDavid Howells 		call->ackr_reason = 0;
172a5af7e1fSDavid Howells 	}
1731457cc4cSDavid Howells 	n = rxrpc_fill_out_ack(conn, call, pkt, &hard_ack, &top, reason);
1748d94aa38SDavid Howells 
1758d94aa38SDavid Howells 	spin_unlock_bh(&call->lock);
1768d94aa38SDavid Howells 
17726cb02aaSDavid Howells 	iov[0].iov_base	= pkt;
17826cb02aaSDavid Howells 	iov[0].iov_len	= sizeof(pkt->whdr) + sizeof(pkt->ack) + n;
1798d94aa38SDavid Howells 	iov[1].iov_base = &pkt->ackinfo;
1808d94aa38SDavid Howells 	iov[1].iov_len	= sizeof(pkt->ackinfo);
18126cb02aaSDavid Howells 	len = iov[0].iov_len + iov[1].iov_len;
1828d94aa38SDavid Howells 
183b86e218eSDavid Howells 	serial = atomic_inc_return(&conn->serial);
184b86e218eSDavid Howells 	pkt->whdr.serial = htonl(serial);
1854764c0daSDavid Howells 	trace_rxrpc_tx_ack(call->debug_id, serial,
186b86e218eSDavid Howells 			   ntohl(pkt->ack.firstPacket),
187b86e218eSDavid Howells 			   ntohl(pkt->ack.serial),
188b86e218eSDavid Howells 			   pkt->ack.reason, pkt->ack.nAcks);
189bd1fdf8cSDavid Howells 	if (_serial)
190bd1fdf8cSDavid Howells 		*_serial = serial;
191b86e218eSDavid Howells 
1928e83134dSDavid Howells 	if (ping) {
193a5af7e1fSDavid Howells 		call->ping_serial = serial;
1948e83134dSDavid Howells 		smp_wmb();
1958e83134dSDavid Howells 		/* We need to stick a time in before we send the packet in case
1968e83134dSDavid Howells 		 * the reply gets back before kernel_sendmsg() completes - but
1978e83134dSDavid Howells 		 * asking UDP to send the packet can take a relatively long
198b604dd98SDavid Howells 		 * time.
1998e83134dSDavid Howells 		 */
200a5af7e1fSDavid Howells 		call->ping_time = ktime_get_real();
2018e83134dSDavid Howells 		set_bit(RXRPC_CALL_PINGING, &call->flags);
2028e83134dSDavid Howells 		trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_ping, serial);
2038e83134dSDavid Howells 	}
20426cb02aaSDavid Howells 
20526cb02aaSDavid Howells 	ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
206330bdcfaSDavid Howells 	conn->params.peer->last_tx_at = ktime_get_seconds();
2076b47fe1dSDavid Howells 	if (ret < 0)
2086b47fe1dSDavid Howells 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
2094764c0daSDavid Howells 				    rxrpc_tx_point_call_ack);
2104764c0daSDavid Howells 	else
2114764c0daSDavid Howells 		trace_rxrpc_tx_packet(call->debug_id, &pkt->whdr,
2124764c0daSDavid Howells 				      rxrpc_tx_point_call_ack);
2138d94aa38SDavid Howells 
21426cb02aaSDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE) {
215805b21b9SDavid Howells 		if (ret < 0) {
216a5af7e1fSDavid Howells 			if (ping)
2178e83134dSDavid Howells 				clear_bit(RXRPC_CALL_PINGING, &call->flags);
218248f219cSDavid Howells 			rxrpc_propose_ACK(call, pkt->ack.reason,
219248f219cSDavid Howells 					  ntohs(pkt->ack.maxSkew),
220248f219cSDavid Howells 					  ntohl(pkt->ack.serial),
2219c7ad434SDavid Howells 					  true, true,
2229c7ad434SDavid Howells 					  rxrpc_propose_ack_retry_tx);
223805b21b9SDavid Howells 		} else {
224805b21b9SDavid Howells 			spin_lock_bh(&call->lock);
225805b21b9SDavid Howells 			if (after(hard_ack, call->ackr_consumed))
226805b21b9SDavid Howells 				call->ackr_consumed = hard_ack;
227805b21b9SDavid Howells 			if (after(top, call->ackr_seen))
228805b21b9SDavid Howells 				call->ackr_seen = top;
229805b21b9SDavid Howells 			spin_unlock_bh(&call->lock);
230248f219cSDavid Howells 		}
231415f44e4SDavid Howells 
232415f44e4SDavid Howells 		rxrpc_set_keepalive(call);
233248f219cSDavid Howells 	}
234248f219cSDavid Howells 
2358d94aa38SDavid Howells out:
2368d94aa38SDavid Howells 	rxrpc_put_connection(conn);
2378d94aa38SDavid Howells 	kfree(pkt);
2388d94aa38SDavid Howells 	return ret;
2398d94aa38SDavid Howells }
2408d94aa38SDavid Howells 
2418c3e34a4SDavid Howells /*
24226cb02aaSDavid Howells  * Send an ABORT call packet.
24326cb02aaSDavid Howells  */
24426cb02aaSDavid Howells int rxrpc_send_abort_packet(struct rxrpc_call *call)
24526cb02aaSDavid Howells {
24626cb02aaSDavid Howells 	struct rxrpc_connection *conn = NULL;
24726cb02aaSDavid Howells 	struct rxrpc_abort_buffer pkt;
24826cb02aaSDavid Howells 	struct msghdr msg;
24926cb02aaSDavid Howells 	struct kvec iov[1];
25026cb02aaSDavid Howells 	rxrpc_serial_t serial;
25126cb02aaSDavid Howells 	int ret;
25226cb02aaSDavid Howells 
253dcbefc30SDavid Howells 	/* Don't bother sending aborts for a client call once the server has
254dcbefc30SDavid Howells 	 * hard-ACK'd all of its request data.  After that point, we're not
255dcbefc30SDavid Howells 	 * going to stop the operation proceeding, and whilst we might limit
256dcbefc30SDavid Howells 	 * the reply, it's not worth it if we can send a new call on the same
257dcbefc30SDavid Howells 	 * channel instead, thereby closing off this call.
258dcbefc30SDavid Howells 	 */
259dcbefc30SDavid Howells 	if (rxrpc_is_client_call(call) &&
260dcbefc30SDavid Howells 	    test_bit(RXRPC_CALL_TX_LAST, &call->flags))
261dcbefc30SDavid Howells 		return 0;
262dcbefc30SDavid Howells 
26326cb02aaSDavid Howells 	spin_lock_bh(&call->lock);
26426cb02aaSDavid Howells 	if (call->conn)
26526cb02aaSDavid Howells 		conn = rxrpc_get_connection_maybe(call->conn);
26626cb02aaSDavid Howells 	spin_unlock_bh(&call->lock);
26726cb02aaSDavid Howells 	if (!conn)
26826cb02aaSDavid Howells 		return -ECONNRESET;
26926cb02aaSDavid Howells 
27026cb02aaSDavid Howells 	msg.msg_name	= &call->peer->srx.transport;
27126cb02aaSDavid Howells 	msg.msg_namelen	= call->peer->srx.transport_len;
27226cb02aaSDavid Howells 	msg.msg_control	= NULL;
27326cb02aaSDavid Howells 	msg.msg_controllen = 0;
27426cb02aaSDavid Howells 	msg.msg_flags	= 0;
27526cb02aaSDavid Howells 
27626cb02aaSDavid Howells 	pkt.whdr.epoch		= htonl(conn->proto.epoch);
27726cb02aaSDavid Howells 	pkt.whdr.cid		= htonl(call->cid);
27826cb02aaSDavid Howells 	pkt.whdr.callNumber	= htonl(call->call_id);
27926cb02aaSDavid Howells 	pkt.whdr.seq		= 0;
28026cb02aaSDavid Howells 	pkt.whdr.type		= RXRPC_PACKET_TYPE_ABORT;
28126cb02aaSDavid Howells 	pkt.whdr.flags		= conn->out_clientflag;
28226cb02aaSDavid Howells 	pkt.whdr.userStatus	= 0;
28326cb02aaSDavid Howells 	pkt.whdr.securityIndex	= call->security_ix;
28426cb02aaSDavid Howells 	pkt.whdr._rsvd		= 0;
28526cb02aaSDavid Howells 	pkt.whdr.serviceId	= htons(call->service_id);
28626cb02aaSDavid Howells 	pkt.abort_code		= htonl(call->abort_code);
28726cb02aaSDavid Howells 
28826cb02aaSDavid Howells 	iov[0].iov_base	= &pkt;
28926cb02aaSDavid Howells 	iov[0].iov_len	= sizeof(pkt);
29026cb02aaSDavid Howells 
29126cb02aaSDavid Howells 	serial = atomic_inc_return(&conn->serial);
29226cb02aaSDavid Howells 	pkt.whdr.serial = htonl(serial);
29326cb02aaSDavid Howells 
29426cb02aaSDavid Howells 	ret = kernel_sendmsg(conn->params.local->socket,
29526cb02aaSDavid Howells 			     &msg, iov, 1, sizeof(pkt));
296330bdcfaSDavid Howells 	conn->params.peer->last_tx_at = ktime_get_seconds();
2976b47fe1dSDavid Howells 	if (ret < 0)
2986b47fe1dSDavid Howells 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
2994764c0daSDavid Howells 				    rxrpc_tx_point_call_abort);
3004764c0daSDavid Howells 	else
3014764c0daSDavid Howells 		trace_rxrpc_tx_packet(call->debug_id, &pkt.whdr,
3024764c0daSDavid Howells 				      rxrpc_tx_point_call_abort);
3036b47fe1dSDavid Howells 
30426cb02aaSDavid Howells 
30526cb02aaSDavid Howells 	rxrpc_put_connection(conn);
30626cb02aaSDavid Howells 	return ret;
30726cb02aaSDavid Howells }
30826cb02aaSDavid Howells 
30926cb02aaSDavid Howells /*
3108c3e34a4SDavid Howells  * send a packet through the transport endpoint
3118c3e34a4SDavid Howells  */
312a1767077SDavid Howells int rxrpc_send_data_packet(struct rxrpc_call *call, struct sk_buff *skb,
313a1767077SDavid Howells 			   bool retrans)
3148c3e34a4SDavid Howells {
3155a924b89SDavid Howells 	struct rxrpc_connection *conn = call->conn;
3165a924b89SDavid Howells 	struct rxrpc_wire_header whdr;
3175a924b89SDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
3188c3e34a4SDavid Howells 	struct msghdr msg;
3195a924b89SDavid Howells 	struct kvec iov[2];
3205a924b89SDavid Howells 	rxrpc_serial_t serial;
3215a924b89SDavid Howells 	size_t len;
322a1767077SDavid Howells 	bool lost = false;
3238c3e34a4SDavid Howells 	int ret, opt;
3248c3e34a4SDavid Howells 
3258c3e34a4SDavid Howells 	_enter(",{%d}", skb->len);
3268c3e34a4SDavid Howells 
3275a924b89SDavid Howells 	/* Each transmission of a Tx packet needs a new serial number */
3285a924b89SDavid Howells 	serial = atomic_inc_return(&conn->serial);
3298c3e34a4SDavid Howells 
3305a924b89SDavid Howells 	whdr.epoch	= htonl(conn->proto.epoch);
3315a924b89SDavid Howells 	whdr.cid	= htonl(call->cid);
3325a924b89SDavid Howells 	whdr.callNumber	= htonl(call->call_id);
3335a924b89SDavid Howells 	whdr.seq	= htonl(sp->hdr.seq);
3345a924b89SDavid Howells 	whdr.serial	= htonl(serial);
3355a924b89SDavid Howells 	whdr.type	= RXRPC_PACKET_TYPE_DATA;
3365a924b89SDavid Howells 	whdr.flags	= sp->hdr.flags;
3375a924b89SDavid Howells 	whdr.userStatus	= 0;
3385a924b89SDavid Howells 	whdr.securityIndex = call->security_ix;
3395a924b89SDavid Howells 	whdr._rsvd	= htons(sp->hdr._rsvd);
3405a924b89SDavid Howells 	whdr.serviceId	= htons(call->service_id);
3415a924b89SDavid Howells 
3424e255721SDavid Howells 	if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) &&
3434e255721SDavid Howells 	    sp->hdr.seq == 1)
3444e255721SDavid Howells 		whdr.userStatus	= RXRPC_USERSTATUS_SERVICE_UPGRADE;
3454e255721SDavid Howells 
3465a924b89SDavid Howells 	iov[0].iov_base = &whdr;
3475a924b89SDavid Howells 	iov[0].iov_len = sizeof(whdr);
3485a924b89SDavid Howells 	iov[1].iov_base = skb->head;
3495a924b89SDavid Howells 	iov[1].iov_len = skb->len;
3505a924b89SDavid Howells 	len = iov[0].iov_len + iov[1].iov_len;
3515a924b89SDavid Howells 
3525a924b89SDavid Howells 	msg.msg_name = &call->peer->srx.transport;
3535a924b89SDavid Howells 	msg.msg_namelen = call->peer->srx.transport_len;
3548c3e34a4SDavid Howells 	msg.msg_control = NULL;
3558c3e34a4SDavid Howells 	msg.msg_controllen = 0;
3568c3e34a4SDavid Howells 	msg.msg_flags = 0;
3578c3e34a4SDavid Howells 
35857494343SDavid Howells 	/* If our RTT cache needs working on, request an ACK.  Also request
35957494343SDavid Howells 	 * ACKs if a DATA packet appears to have been lost.
360b604dd98SDavid Howells 	 *
361b604dd98SDavid Howells 	 * However, we mustn't request an ACK on the last reply packet of a
362b604dd98SDavid Howells 	 * service call, lest OpenAFS incorrectly send us an ACK with some
363b604dd98SDavid Howells 	 * soft-ACKs in it and then never follow up with a proper hard ACK.
36457494343SDavid Howells 	 */
365b604dd98SDavid Howells 	if ((!(sp->hdr.flags & RXRPC_LAST_PACKET) ||
366b604dd98SDavid Howells 	     rxrpc_to_server(sp)
367b604dd98SDavid Howells 	     ) &&
368bd1fdf8cSDavid Howells 	    (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events) ||
369bd1fdf8cSDavid Howells 	     retrans ||
370b112a670SDavid Howells 	     call->cong_mode == RXRPC_CALL_SLOW_START ||
37157494343SDavid Howells 	     (call->peer->rtt_usage < 3 && sp->hdr.seq & 1) ||
3720d4b103cSDavid Howells 	     ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
373bf7d620aSDavid Howells 			  ktime_get_real())))
3740d4b103cSDavid Howells 		whdr.flags |= RXRPC_REQUEST_ACK;
3750d4b103cSDavid Howells 
3768a681c36SDavid Howells 	if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
3778a681c36SDavid Howells 		static int lose;
3788a681c36SDavid Howells 		if ((lose++ & 7) == 7) {
379a1767077SDavid Howells 			ret = 0;
380a1767077SDavid Howells 			lost = true;
381a1767077SDavid Howells 			goto done;
3828a681c36SDavid Howells 		}
3838a681c36SDavid Howells 	}
3848a681c36SDavid Howells 
3855a924b89SDavid Howells 	_proto("Tx DATA %%%u { #%u }", serial, sp->hdr.seq);
3865a924b89SDavid Howells 
3878c3e34a4SDavid Howells 	/* send the packet with the don't fragment bit set if we currently
3888c3e34a4SDavid Howells 	 * think it's small enough */
3895a924b89SDavid Howells 	if (iov[1].iov_len >= call->peer->maxdata)
3905a924b89SDavid Howells 		goto send_fragmentable;
3915a924b89SDavid Howells 
392985a5c82SDavid Howells 	down_read(&conn->params.local->defrag_sem);
393b604dd98SDavid Howells 
394b604dd98SDavid Howells 	sp->hdr.serial = serial;
395b604dd98SDavid Howells 	smp_wmb(); /* Set serial before timestamp */
396b604dd98SDavid Howells 	skb->tstamp = ktime_get_real();
397b604dd98SDavid Howells 
3988c3e34a4SDavid Howells 	/* send the packet by UDP
3998c3e34a4SDavid Howells 	 * - returns -EMSGSIZE if UDP would have to fragment the packet
4008c3e34a4SDavid Howells 	 *   to go out of the interface
4018c3e34a4SDavid Howells 	 *   - in which case, we'll have processed the ICMP error
4028c3e34a4SDavid Howells 	 *     message and update the peer record
4038c3e34a4SDavid Howells 	 */
4045a924b89SDavid Howells 	ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
405330bdcfaSDavid Howells 	conn->params.peer->last_tx_at = ktime_get_seconds();
4068c3e34a4SDavid Howells 
407985a5c82SDavid Howells 	up_read(&conn->params.local->defrag_sem);
4086b47fe1dSDavid Howells 	if (ret < 0)
4096b47fe1dSDavid Howells 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
4104764c0daSDavid Howells 				    rxrpc_tx_point_call_data_nofrag);
4114764c0daSDavid Howells 	else
4124764c0daSDavid Howells 		trace_rxrpc_tx_packet(call->debug_id, &whdr,
4134764c0daSDavid Howells 				      rxrpc_tx_point_call_data_nofrag);
4148c3e34a4SDavid Howells 	if (ret == -EMSGSIZE)
4158c3e34a4SDavid Howells 		goto send_fragmentable;
4168c3e34a4SDavid Howells 
4175a924b89SDavid Howells done:
418a1767077SDavid Howells 	trace_rxrpc_tx_data(call, sp->hdr.seq, serial, whdr.flags,
419a1767077SDavid Howells 			    retrans, lost);
42050235c4bSDavid Howells 	if (ret >= 0) {
4210d4b103cSDavid Howells 		if (whdr.flags & RXRPC_REQUEST_ACK) {
422b604dd98SDavid Howells 			call->peer->rtt_last_req = skb->tstamp;
42350235c4bSDavid Howells 			trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_data, serial);
424bd1fdf8cSDavid Howells 			if (call->peer->rtt_usage > 1) {
425bd1fdf8cSDavid Howells 				unsigned long nowj = jiffies, ack_lost_at;
426bd1fdf8cSDavid Howells 
427bd1fdf8cSDavid Howells 				ack_lost_at = nsecs_to_jiffies(2 * call->peer->rtt);
428bd1fdf8cSDavid Howells 				if (ack_lost_at < 1)
429bd1fdf8cSDavid Howells 					ack_lost_at = 1;
430bd1fdf8cSDavid Howells 
431bd1fdf8cSDavid Howells 				ack_lost_at += nowj;
432bd1fdf8cSDavid Howells 				WRITE_ONCE(call->ack_lost_at, ack_lost_at);
433bd1fdf8cSDavid Howells 				rxrpc_reduce_call_timer(call, ack_lost_at, nowj,
434bd1fdf8cSDavid Howells 							rxrpc_timer_set_for_lost_ack);
435bd1fdf8cSDavid Howells 			}
4368c3e34a4SDavid Howells 		}
437c54e43d7SDavid Howells 
438c54e43d7SDavid Howells 		if (sp->hdr.seq == 1 &&
439c54e43d7SDavid Howells 		    !test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER,
440c54e43d7SDavid Howells 				      &call->flags)) {
441c54e43d7SDavid Howells 			unsigned long nowj = jiffies, expect_rx_by;
442c54e43d7SDavid Howells 
443c54e43d7SDavid Howells 			expect_rx_by = nowj + call->next_rx_timo;
444c54e43d7SDavid Howells 			WRITE_ONCE(call->expect_rx_by, expect_rx_by);
445c54e43d7SDavid Howells 			rxrpc_reduce_call_timer(call, expect_rx_by, nowj,
446c54e43d7SDavid Howells 						rxrpc_timer_set_for_normal);
447c54e43d7SDavid Howells 		}
4480d4b103cSDavid Howells 	}
449415f44e4SDavid Howells 
450415f44e4SDavid Howells 	rxrpc_set_keepalive(call);
451415f44e4SDavid Howells 
4525a924b89SDavid Howells 	_leave(" = %d [%u]", ret, call->peer->maxdata);
4535a924b89SDavid Howells 	return ret;
4548c3e34a4SDavid Howells 
4558c3e34a4SDavid Howells send_fragmentable:
4568c3e34a4SDavid Howells 	/* attempt to send this message with fragmentation enabled */
4578c3e34a4SDavid Howells 	_debug("send fragment");
4588c3e34a4SDavid Howells 
459985a5c82SDavid Howells 	down_write(&conn->params.local->defrag_sem);
460985a5c82SDavid Howells 
461b604dd98SDavid Howells 	sp->hdr.serial = serial;
462b604dd98SDavid Howells 	smp_wmb(); /* Set serial before timestamp */
463b604dd98SDavid Howells 	skb->tstamp = ktime_get_real();
464b604dd98SDavid Howells 
465985a5c82SDavid Howells 	switch (conn->params.local->srx.transport.family) {
466985a5c82SDavid Howells 	case AF_INET:
4678c3e34a4SDavid Howells 		opt = IP_PMTUDISC_DONT;
468985a5c82SDavid Howells 		ret = kernel_setsockopt(conn->params.local->socket,
469985a5c82SDavid Howells 					SOL_IP, IP_MTU_DISCOVER,
4708c3e34a4SDavid Howells 					(char *)&opt, sizeof(opt));
4718c3e34a4SDavid Howells 		if (ret == 0) {
4725a924b89SDavid Howells 			ret = kernel_sendmsg(conn->params.local->socket, &msg,
4735a924b89SDavid Howells 					     iov, 2, len);
474330bdcfaSDavid Howells 			conn->params.peer->last_tx_at = ktime_get_seconds();
4758c3e34a4SDavid Howells 
4768c3e34a4SDavid Howells 			opt = IP_PMTUDISC_DO;
477985a5c82SDavid Howells 			kernel_setsockopt(conn->params.local->socket, SOL_IP,
478985a5c82SDavid Howells 					  IP_MTU_DISCOVER,
479985a5c82SDavid Howells 					  (char *)&opt, sizeof(opt));
480985a5c82SDavid Howells 		}
481985a5c82SDavid Howells 		break;
48275b54cb5SDavid Howells 
483d1912747SDavid Howells #ifdef CONFIG_AF_RXRPC_IPV6
48475b54cb5SDavid Howells 	case AF_INET6:
48575b54cb5SDavid Howells 		opt = IPV6_PMTUDISC_DONT;
48675b54cb5SDavid Howells 		ret = kernel_setsockopt(conn->params.local->socket,
48775b54cb5SDavid Howells 					SOL_IPV6, IPV6_MTU_DISCOVER,
48875b54cb5SDavid Howells 					(char *)&opt, sizeof(opt));
48975b54cb5SDavid Howells 		if (ret == 0) {
49075b54cb5SDavid Howells 			ret = kernel_sendmsg(conn->params.local->socket, &msg,
49193c62c45SDavid Howells 					     iov, 2, len);
492330bdcfaSDavid Howells 			conn->params.peer->last_tx_at = ktime_get_seconds();
49375b54cb5SDavid Howells 
49475b54cb5SDavid Howells 			opt = IPV6_PMTUDISC_DO;
49575b54cb5SDavid Howells 			kernel_setsockopt(conn->params.local->socket,
49675b54cb5SDavid Howells 					  SOL_IPV6, IPV6_MTU_DISCOVER,
49775b54cb5SDavid Howells 					  (char *)&opt, sizeof(opt));
49875b54cb5SDavid Howells 		}
49975b54cb5SDavid Howells 		break;
500d1912747SDavid Howells #endif
5018c3e34a4SDavid Howells 	}
5028c3e34a4SDavid Howells 
5036b47fe1dSDavid Howells 	if (ret < 0)
5046b47fe1dSDavid Howells 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
5054764c0daSDavid Howells 				    rxrpc_tx_point_call_data_frag);
5064764c0daSDavid Howells 	else
5074764c0daSDavid Howells 		trace_rxrpc_tx_packet(call->debug_id, &whdr,
5084764c0daSDavid Howells 				      rxrpc_tx_point_call_data_frag);
5096b47fe1dSDavid Howells 
510985a5c82SDavid Howells 	up_write(&conn->params.local->defrag_sem);
5115a924b89SDavid Howells 	goto done;
5128c3e34a4SDavid Howells }
513248f219cSDavid Howells 
514248f219cSDavid Howells /*
515248f219cSDavid Howells  * reject packets through the local endpoint
516248f219cSDavid Howells  */
517248f219cSDavid Howells void rxrpc_reject_packets(struct rxrpc_local *local)
518248f219cSDavid Howells {
5191c2bc7b9SDavid Howells 	struct sockaddr_rxrpc srx;
520248f219cSDavid Howells 	struct rxrpc_skb_priv *sp;
521248f219cSDavid Howells 	struct rxrpc_wire_header whdr;
522248f219cSDavid Howells 	struct sk_buff *skb;
523248f219cSDavid Howells 	struct msghdr msg;
524248f219cSDavid Howells 	struct kvec iov[2];
525248f219cSDavid Howells 	size_t size;
526248f219cSDavid Howells 	__be32 code;
527ece64fecSDavid Howells 	int ret, ioc;
528248f219cSDavid Howells 
529248f219cSDavid Howells 	_enter("%d", local->debug_id);
530248f219cSDavid Howells 
531248f219cSDavid Howells 	iov[0].iov_base = &whdr;
532248f219cSDavid Howells 	iov[0].iov_len = sizeof(whdr);
533248f219cSDavid Howells 	iov[1].iov_base = &code;
534248f219cSDavid Howells 	iov[1].iov_len = sizeof(code);
535248f219cSDavid Howells 
5361c2bc7b9SDavid Howells 	msg.msg_name = &srx.transport;
537248f219cSDavid Howells 	msg.msg_control = NULL;
538248f219cSDavid Howells 	msg.msg_controllen = 0;
539248f219cSDavid Howells 	msg.msg_flags = 0;
540248f219cSDavid Howells 
541248f219cSDavid Howells 	memset(&whdr, 0, sizeof(whdr));
542248f219cSDavid Howells 
543248f219cSDavid Howells 	while ((skb = skb_dequeue(&local->reject_queue))) {
54471f3ca40SDavid Howells 		rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
545248f219cSDavid Howells 		sp = rxrpc_skb(skb);
5461c2bc7b9SDavid Howells 
547ece64fecSDavid Howells 		switch (skb->mark) {
548ece64fecSDavid Howells 		case RXRPC_SKB_MARK_REJECT_BUSY:
549ece64fecSDavid Howells 			whdr.type = RXRPC_PACKET_TYPE_BUSY;
550ece64fecSDavid Howells 			size = sizeof(whdr);
551ece64fecSDavid Howells 			ioc = 1;
552ece64fecSDavid Howells 			break;
553ece64fecSDavid Howells 		case RXRPC_SKB_MARK_REJECT_ABORT:
554ece64fecSDavid Howells 			whdr.type = RXRPC_PACKET_TYPE_ABORT;
555ece64fecSDavid Howells 			code = htonl(skb->priority);
556ece64fecSDavid Howells 			size = sizeof(whdr) + sizeof(code);
557ece64fecSDavid Howells 			ioc = 2;
558ece64fecSDavid Howells 			break;
559ece64fecSDavid Howells 		default:
560ece64fecSDavid Howells 			rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
561ece64fecSDavid Howells 			continue;
562ece64fecSDavid Howells 		}
563ece64fecSDavid Howells 
5647b674e39SDavid Howells 		if (rxrpc_extract_addr_from_skb(local, &srx, skb) == 0) {
5651c2bc7b9SDavid Howells 			msg.msg_namelen = srx.transport_len;
5661c2bc7b9SDavid Howells 
567248f219cSDavid Howells 			whdr.epoch	= htonl(sp->hdr.epoch);
568248f219cSDavid Howells 			whdr.cid	= htonl(sp->hdr.cid);
569248f219cSDavid Howells 			whdr.callNumber	= htonl(sp->hdr.callNumber);
570248f219cSDavid Howells 			whdr.serviceId	= htons(sp->hdr.serviceId);
571248f219cSDavid Howells 			whdr.flags	= sp->hdr.flags;
572248f219cSDavid Howells 			whdr.flags	^= RXRPC_CLIENT_INITIATED;
573248f219cSDavid Howells 			whdr.flags	&= RXRPC_CLIENT_INITIATED;
574248f219cSDavid Howells 
5756b47fe1dSDavid Howells 			ret = kernel_sendmsg(local->socket, &msg, iov, 2, size);
5766b47fe1dSDavid Howells 			if (ret < 0)
5776b47fe1dSDavid Howells 				trace_rxrpc_tx_fail(local->debug_id, 0, ret,
5784764c0daSDavid Howells 						    rxrpc_tx_point_reject);
5794764c0daSDavid Howells 			else
5804764c0daSDavid Howells 				trace_rxrpc_tx_packet(local->debug_id, &whdr,
5814764c0daSDavid Howells 						      rxrpc_tx_point_reject);
582248f219cSDavid Howells 		}
583248f219cSDavid Howells 
58471f3ca40SDavid Howells 		rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
585248f219cSDavid Howells 	}
586248f219cSDavid Howells 
587248f219cSDavid Howells 	_leave("");
588248f219cSDavid Howells }
589ace45becSDavid Howells 
590ace45becSDavid Howells /*
591ace45becSDavid Howells  * Send a VERSION reply to a peer as a keepalive.
592ace45becSDavid Howells  */
593ace45becSDavid Howells void rxrpc_send_keepalive(struct rxrpc_peer *peer)
594ace45becSDavid Howells {
595ace45becSDavid Howells 	struct rxrpc_wire_header whdr;
596ace45becSDavid Howells 	struct msghdr msg;
597ace45becSDavid Howells 	struct kvec iov[2];
598ace45becSDavid Howells 	size_t len;
599ace45becSDavid Howells 	int ret;
600ace45becSDavid Howells 
601ace45becSDavid Howells 	_enter("");
602ace45becSDavid Howells 
603ace45becSDavid Howells 	msg.msg_name	= &peer->srx.transport;
604ace45becSDavid Howells 	msg.msg_namelen	= peer->srx.transport_len;
605ace45becSDavid Howells 	msg.msg_control	= NULL;
606ace45becSDavid Howells 	msg.msg_controllen = 0;
607ace45becSDavid Howells 	msg.msg_flags	= 0;
608ace45becSDavid Howells 
609ace45becSDavid Howells 	whdr.epoch	= htonl(peer->local->rxnet->epoch);
610ace45becSDavid Howells 	whdr.cid	= 0;
611ace45becSDavid Howells 	whdr.callNumber	= 0;
612ace45becSDavid Howells 	whdr.seq	= 0;
613ace45becSDavid Howells 	whdr.serial	= 0;
614ace45becSDavid Howells 	whdr.type	= RXRPC_PACKET_TYPE_VERSION; /* Not client-initiated */
615ace45becSDavid Howells 	whdr.flags	= RXRPC_LAST_PACKET;
616ace45becSDavid Howells 	whdr.userStatus	= 0;
617ace45becSDavid Howells 	whdr.securityIndex = 0;
618ace45becSDavid Howells 	whdr._rsvd	= 0;
619ace45becSDavid Howells 	whdr.serviceId	= 0;
620ace45becSDavid Howells 
621ace45becSDavid Howells 	iov[0].iov_base	= &whdr;
622ace45becSDavid Howells 	iov[0].iov_len	= sizeof(whdr);
623ace45becSDavid Howells 	iov[1].iov_base	= (char *)rxrpc_keepalive_string;
624ace45becSDavid Howells 	iov[1].iov_len	= sizeof(rxrpc_keepalive_string);
625ace45becSDavid Howells 
626ace45becSDavid Howells 	len = iov[0].iov_len + iov[1].iov_len;
627ace45becSDavid Howells 
628ace45becSDavid Howells 	_proto("Tx VERSION (keepalive)");
629ace45becSDavid Howells 
630ace45becSDavid Howells 	ret = kernel_sendmsg(peer->local->socket, &msg, iov, 2, len);
631ace45becSDavid Howells 	if (ret < 0)
6326b47fe1dSDavid Howells 		trace_rxrpc_tx_fail(peer->debug_id, 0, ret,
6334764c0daSDavid Howells 				    rxrpc_tx_point_version_keepalive);
6344764c0daSDavid Howells 	else
6354764c0daSDavid Howells 		trace_rxrpc_tx_packet(peer->debug_id, &whdr,
6364764c0daSDavid Howells 				      rxrpc_tx_point_version_keepalive);
637ace45becSDavid Howells 
638330bdcfaSDavid Howells 	peer->last_tx_at = ktime_get_seconds();
639ace45becSDavid Howells 	_leave("");
640ace45becSDavid Howells }
641