xref: /openbmc/linux/net/rxrpc/output.c (revision 93c62c45)
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 
358d94aa38SDavid Howells /*
36415f44e4SDavid Howells  * Arrange for a keepalive ping a certain time after we last transmitted.  This
37415f44e4SDavid Howells  * lets the far side know we're still interested in this call and helps keep
38415f44e4SDavid Howells  * the route through any intervening firewall open.
39415f44e4SDavid Howells  *
40415f44e4SDavid Howells  * Receiving a response to the ping will prevent the ->expect_rx_by timer from
41415f44e4SDavid Howells  * expiring.
42415f44e4SDavid Howells  */
43415f44e4SDavid Howells static void rxrpc_set_keepalive(struct rxrpc_call *call)
44415f44e4SDavid Howells {
45415f44e4SDavid Howells 	unsigned long now = jiffies, keepalive_at = call->next_rx_timo / 6;
46415f44e4SDavid Howells 
47415f44e4SDavid Howells 	keepalive_at += now;
48415f44e4SDavid Howells 	WRITE_ONCE(call->keepalive_at, keepalive_at);
49415f44e4SDavid Howells 	rxrpc_reduce_call_timer(call, keepalive_at, now,
50415f44e4SDavid Howells 				rxrpc_timer_set_for_keepalive);
51415f44e4SDavid Howells }
52415f44e4SDavid Howells 
53415f44e4SDavid Howells /*
548d94aa38SDavid Howells  * Fill out an ACK packet.
558d94aa38SDavid Howells  */
561457cc4cSDavid Howells static size_t rxrpc_fill_out_ack(struct rxrpc_connection *conn,
571457cc4cSDavid Howells 				 struct rxrpc_call *call,
5826cb02aaSDavid Howells 				 struct rxrpc_ack_buffer *pkt,
59805b21b9SDavid Howells 				 rxrpc_seq_t *_hard_ack,
60a5af7e1fSDavid Howells 				 rxrpc_seq_t *_top,
61a5af7e1fSDavid Howells 				 u8 reason)
628d94aa38SDavid Howells {
63f3639df2SDavid Howells 	rxrpc_serial_t serial;
64248f219cSDavid Howells 	rxrpc_seq_t hard_ack, top, seq;
65248f219cSDavid Howells 	int ix;
668d94aa38SDavid Howells 	u32 mtu, jmax;
678d94aa38SDavid Howells 	u8 *ackp = pkt->acks;
688d94aa38SDavid Howells 
69248f219cSDavid Howells 	/* Barrier against rxrpc_input_data(). */
70f3639df2SDavid Howells 	serial = call->ackr_serial;
71248f219cSDavid Howells 	hard_ack = READ_ONCE(call->rx_hard_ack);
72248f219cSDavid Howells 	top = smp_load_acquire(&call->rx_top);
73805b21b9SDavid Howells 	*_hard_ack = hard_ack;
74805b21b9SDavid Howells 	*_top = top;
75248f219cSDavid Howells 
768d94aa38SDavid Howells 	pkt->ack.bufferSpace	= htons(8);
77248f219cSDavid Howells 	pkt->ack.maxSkew	= htons(call->ackr_skew);
78248f219cSDavid Howells 	pkt->ack.firstPacket	= htonl(hard_ack + 1);
798d94aa38SDavid Howells 	pkt->ack.previousPacket	= htonl(call->ackr_prev_seq);
80f3639df2SDavid Howells 	pkt->ack.serial		= htonl(serial);
81a5af7e1fSDavid Howells 	pkt->ack.reason		= reason;
82248f219cSDavid Howells 	pkt->ack.nAcks		= top - hard_ack;
838d94aa38SDavid Howells 
84a5af7e1fSDavid Howells 	if (reason == RXRPC_ACK_PING)
858e83134dSDavid Howells 		pkt->whdr.flags |= RXRPC_REQUEST_ACK;
868e83134dSDavid Howells 
87248f219cSDavid Howells 	if (after(top, hard_ack)) {
88248f219cSDavid Howells 		seq = hard_ack + 1;
89248f219cSDavid Howells 		do {
90248f219cSDavid Howells 			ix = seq & RXRPC_RXTX_BUFF_MASK;
91248f219cSDavid Howells 			if (call->rxtx_buffer[ix])
92248f219cSDavid Howells 				*ackp++ = RXRPC_ACK_TYPE_ACK;
93248f219cSDavid Howells 			else
94248f219cSDavid Howells 				*ackp++ = RXRPC_ACK_TYPE_NACK;
95248f219cSDavid Howells 			seq++;
96248f219cSDavid Howells 		} while (before_eq(seq, top));
97248f219cSDavid Howells 	}
98248f219cSDavid Howells 
991457cc4cSDavid Howells 	mtu = conn->params.peer->if_mtu;
1001457cc4cSDavid Howells 	mtu -= conn->params.peer->hdrsize;
10175e42126SDavid Howells 	jmax = (call->nr_jumbo_bad > 3) ? 1 : rxrpc_rx_jumbo_max;
1028d94aa38SDavid Howells 	pkt->ackinfo.rxMTU	= htonl(rxrpc_rx_mtu);
1038d94aa38SDavid Howells 	pkt->ackinfo.maxMTU	= htonl(mtu);
10475e42126SDavid Howells 	pkt->ackinfo.rwind	= htonl(call->rx_winsize);
1058d94aa38SDavid Howells 	pkt->ackinfo.jumbo_max	= htonl(jmax);
1068d94aa38SDavid Howells 
1078d94aa38SDavid Howells 	*ackp++ = 0;
1088d94aa38SDavid Howells 	*ackp++ = 0;
1098d94aa38SDavid Howells 	*ackp++ = 0;
110248f219cSDavid Howells 	return top - hard_ack + 3;
1118d94aa38SDavid Howells }
1128d94aa38SDavid Howells 
1138d94aa38SDavid Howells /*
11426cb02aaSDavid Howells  * Send an ACK call packet.
1158d94aa38SDavid Howells  */
116bd1fdf8cSDavid Howells int rxrpc_send_ack_packet(struct rxrpc_call *call, bool ping,
117bd1fdf8cSDavid Howells 			  rxrpc_serial_t *_serial)
1188d94aa38SDavid Howells {
1198d94aa38SDavid Howells 	struct rxrpc_connection *conn = NULL;
12026cb02aaSDavid Howells 	struct rxrpc_ack_buffer *pkt;
1218d94aa38SDavid Howells 	struct msghdr msg;
1228d94aa38SDavid Howells 	struct kvec iov[2];
1238d94aa38SDavid Howells 	rxrpc_serial_t serial;
124805b21b9SDavid Howells 	rxrpc_seq_t hard_ack, top;
1258d94aa38SDavid Howells 	size_t len, n;
12626cb02aaSDavid Howells 	int ret;
127a5af7e1fSDavid Howells 	u8 reason;
1288d94aa38SDavid Howells 
1298d94aa38SDavid Howells 	spin_lock_bh(&call->lock);
1308d94aa38SDavid Howells 	if (call->conn)
1318d94aa38SDavid Howells 		conn = rxrpc_get_connection_maybe(call->conn);
1328d94aa38SDavid Howells 	spin_unlock_bh(&call->lock);
1338d94aa38SDavid Howells 	if (!conn)
1348d94aa38SDavid Howells 		return -ECONNRESET;
1358d94aa38SDavid Howells 
1368d94aa38SDavid Howells 	pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
1378d94aa38SDavid Howells 	if (!pkt) {
1388d94aa38SDavid Howells 		rxrpc_put_connection(conn);
1398d94aa38SDavid Howells 		return -ENOMEM;
1408d94aa38SDavid Howells 	}
1418d94aa38SDavid Howells 
1428d94aa38SDavid Howells 	msg.msg_name	= &call->peer->srx.transport;
1438d94aa38SDavid Howells 	msg.msg_namelen	= call->peer->srx.transport_len;
1448d94aa38SDavid Howells 	msg.msg_control	= NULL;
1458d94aa38SDavid Howells 	msg.msg_controllen = 0;
1468d94aa38SDavid Howells 	msg.msg_flags	= 0;
1478d94aa38SDavid Howells 
1488d94aa38SDavid Howells 	pkt->whdr.epoch		= htonl(conn->proto.epoch);
1498d94aa38SDavid Howells 	pkt->whdr.cid		= htonl(call->cid);
1508d94aa38SDavid Howells 	pkt->whdr.callNumber	= htonl(call->call_id);
1518d94aa38SDavid Howells 	pkt->whdr.seq		= 0;
15226cb02aaSDavid Howells 	pkt->whdr.type		= RXRPC_PACKET_TYPE_ACK;
15326cb02aaSDavid Howells 	pkt->whdr.flags		= RXRPC_SLOW_START_OK | conn->out_clientflag;
1548d94aa38SDavid Howells 	pkt->whdr.userStatus	= 0;
1558d94aa38SDavid Howells 	pkt->whdr.securityIndex	= call->security_ix;
1568d94aa38SDavid Howells 	pkt->whdr._rsvd		= 0;
1578d94aa38SDavid Howells 	pkt->whdr.serviceId	= htons(call->service_id);
1588d94aa38SDavid Howells 
1598d94aa38SDavid Howells 	spin_lock_bh(&call->lock);
160a5af7e1fSDavid Howells 	if (ping) {
161a5af7e1fSDavid Howells 		reason = RXRPC_ACK_PING;
162a5af7e1fSDavid Howells 	} else {
163a5af7e1fSDavid Howells 		reason = call->ackr_reason;
16427d0fc43SDavid Howells 		if (!call->ackr_reason) {
16527d0fc43SDavid Howells 			spin_unlock_bh(&call->lock);
16627d0fc43SDavid Howells 			ret = 0;
16727d0fc43SDavid Howells 			goto out;
16827d0fc43SDavid Howells 		}
1698d94aa38SDavid Howells 		call->ackr_reason = 0;
170a5af7e1fSDavid Howells 	}
1711457cc4cSDavid Howells 	n = rxrpc_fill_out_ack(conn, call, pkt, &hard_ack, &top, reason);
1728d94aa38SDavid Howells 
1738d94aa38SDavid Howells 	spin_unlock_bh(&call->lock);
1748d94aa38SDavid Howells 
17526cb02aaSDavid Howells 	iov[0].iov_base	= pkt;
17626cb02aaSDavid Howells 	iov[0].iov_len	= sizeof(pkt->whdr) + sizeof(pkt->ack) + n;
1778d94aa38SDavid Howells 	iov[1].iov_base = &pkt->ackinfo;
1788d94aa38SDavid Howells 	iov[1].iov_len	= sizeof(pkt->ackinfo);
17926cb02aaSDavid Howells 	len = iov[0].iov_len + iov[1].iov_len;
1808d94aa38SDavid Howells 
181b86e218eSDavid Howells 	serial = atomic_inc_return(&conn->serial);
182b86e218eSDavid Howells 	pkt->whdr.serial = htonl(serial);
183be832aecSDavid Howells 	trace_rxrpc_tx_ack(call, serial,
184b86e218eSDavid Howells 			   ntohl(pkt->ack.firstPacket),
185b86e218eSDavid Howells 			   ntohl(pkt->ack.serial),
186b86e218eSDavid Howells 			   pkt->ack.reason, pkt->ack.nAcks);
187bd1fdf8cSDavid Howells 	if (_serial)
188bd1fdf8cSDavid Howells 		*_serial = serial;
189b86e218eSDavid Howells 
1908e83134dSDavid Howells 	if (ping) {
191a5af7e1fSDavid Howells 		call->ping_serial = serial;
1928e83134dSDavid Howells 		smp_wmb();
1938e83134dSDavid Howells 		/* We need to stick a time in before we send the packet in case
1948e83134dSDavid Howells 		 * the reply gets back before kernel_sendmsg() completes - but
1958e83134dSDavid Howells 		 * asking UDP to send the packet can take a relatively long
1968e83134dSDavid Howells 		 * time, so we update the time after, on the assumption that
1978e83134dSDavid Howells 		 * the packet transmission is more likely to happen towards the
1988e83134dSDavid Howells 		 * end of the kernel_sendmsg() call.
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);
2068e83134dSDavid Howells 	if (ping)
207a5af7e1fSDavid Howells 		call->ping_time = ktime_get_real();
2088d94aa38SDavid Howells 
20926cb02aaSDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE) {
210805b21b9SDavid Howells 		if (ret < 0) {
211a5af7e1fSDavid Howells 			if (ping)
2128e83134dSDavid Howells 				clear_bit(RXRPC_CALL_PINGING, &call->flags);
213248f219cSDavid Howells 			rxrpc_propose_ACK(call, pkt->ack.reason,
214248f219cSDavid Howells 					  ntohs(pkt->ack.maxSkew),
215248f219cSDavid Howells 					  ntohl(pkt->ack.serial),
2169c7ad434SDavid Howells 					  true, true,
2179c7ad434SDavid Howells 					  rxrpc_propose_ack_retry_tx);
218805b21b9SDavid Howells 		} else {
219805b21b9SDavid Howells 			spin_lock_bh(&call->lock);
220805b21b9SDavid Howells 			if (after(hard_ack, call->ackr_consumed))
221805b21b9SDavid Howells 				call->ackr_consumed = hard_ack;
222805b21b9SDavid Howells 			if (after(top, call->ackr_seen))
223805b21b9SDavid Howells 				call->ackr_seen = top;
224805b21b9SDavid Howells 			spin_unlock_bh(&call->lock);
225248f219cSDavid Howells 		}
226415f44e4SDavid Howells 
227415f44e4SDavid Howells 		rxrpc_set_keepalive(call);
228248f219cSDavid Howells 	}
229248f219cSDavid Howells 
2308d94aa38SDavid Howells out:
2318d94aa38SDavid Howells 	rxrpc_put_connection(conn);
2328d94aa38SDavid Howells 	kfree(pkt);
2338d94aa38SDavid Howells 	return ret;
2348d94aa38SDavid Howells }
2358d94aa38SDavid Howells 
2368c3e34a4SDavid Howells /*
23726cb02aaSDavid Howells  * Send an ABORT call packet.
23826cb02aaSDavid Howells  */
23926cb02aaSDavid Howells int rxrpc_send_abort_packet(struct rxrpc_call *call)
24026cb02aaSDavid Howells {
24126cb02aaSDavid Howells 	struct rxrpc_connection *conn = NULL;
24226cb02aaSDavid Howells 	struct rxrpc_abort_buffer pkt;
24326cb02aaSDavid Howells 	struct msghdr msg;
24426cb02aaSDavid Howells 	struct kvec iov[1];
24526cb02aaSDavid Howells 	rxrpc_serial_t serial;
24626cb02aaSDavid Howells 	int ret;
24726cb02aaSDavid Howells 
248dcbefc30SDavid Howells 	/* Don't bother sending aborts for a client call once the server has
249dcbefc30SDavid Howells 	 * hard-ACK'd all of its request data.  After that point, we're not
250dcbefc30SDavid Howells 	 * going to stop the operation proceeding, and whilst we might limit
251dcbefc30SDavid Howells 	 * the reply, it's not worth it if we can send a new call on the same
252dcbefc30SDavid Howells 	 * channel instead, thereby closing off this call.
253dcbefc30SDavid Howells 	 */
254dcbefc30SDavid Howells 	if (rxrpc_is_client_call(call) &&
255dcbefc30SDavid Howells 	    test_bit(RXRPC_CALL_TX_LAST, &call->flags))
256dcbefc30SDavid Howells 		return 0;
257dcbefc30SDavid Howells 
25826cb02aaSDavid Howells 	spin_lock_bh(&call->lock);
25926cb02aaSDavid Howells 	if (call->conn)
26026cb02aaSDavid Howells 		conn = rxrpc_get_connection_maybe(call->conn);
26126cb02aaSDavid Howells 	spin_unlock_bh(&call->lock);
26226cb02aaSDavid Howells 	if (!conn)
26326cb02aaSDavid Howells 		return -ECONNRESET;
26426cb02aaSDavid Howells 
26526cb02aaSDavid Howells 	msg.msg_name	= &call->peer->srx.transport;
26626cb02aaSDavid Howells 	msg.msg_namelen	= call->peer->srx.transport_len;
26726cb02aaSDavid Howells 	msg.msg_control	= NULL;
26826cb02aaSDavid Howells 	msg.msg_controllen = 0;
26926cb02aaSDavid Howells 	msg.msg_flags	= 0;
27026cb02aaSDavid Howells 
27126cb02aaSDavid Howells 	pkt.whdr.epoch		= htonl(conn->proto.epoch);
27226cb02aaSDavid Howells 	pkt.whdr.cid		= htonl(call->cid);
27326cb02aaSDavid Howells 	pkt.whdr.callNumber	= htonl(call->call_id);
27426cb02aaSDavid Howells 	pkt.whdr.seq		= 0;
27526cb02aaSDavid Howells 	pkt.whdr.type		= RXRPC_PACKET_TYPE_ABORT;
27626cb02aaSDavid Howells 	pkt.whdr.flags		= conn->out_clientflag;
27726cb02aaSDavid Howells 	pkt.whdr.userStatus	= 0;
27826cb02aaSDavid Howells 	pkt.whdr.securityIndex	= call->security_ix;
27926cb02aaSDavid Howells 	pkt.whdr._rsvd		= 0;
28026cb02aaSDavid Howells 	pkt.whdr.serviceId	= htons(call->service_id);
28126cb02aaSDavid Howells 	pkt.abort_code		= htonl(call->abort_code);
28226cb02aaSDavid Howells 
28326cb02aaSDavid Howells 	iov[0].iov_base	= &pkt;
28426cb02aaSDavid Howells 	iov[0].iov_len	= sizeof(pkt);
28526cb02aaSDavid Howells 
28626cb02aaSDavid Howells 	serial = atomic_inc_return(&conn->serial);
28726cb02aaSDavid Howells 	pkt.whdr.serial = htonl(serial);
28826cb02aaSDavid Howells 
28926cb02aaSDavid Howells 	ret = kernel_sendmsg(conn->params.local->socket,
29026cb02aaSDavid Howells 			     &msg, iov, 1, sizeof(pkt));
29126cb02aaSDavid Howells 
29226cb02aaSDavid Howells 	rxrpc_put_connection(conn);
29326cb02aaSDavid Howells 	return ret;
29426cb02aaSDavid Howells }
29526cb02aaSDavid Howells 
29626cb02aaSDavid Howells /*
2978c3e34a4SDavid Howells  * send a packet through the transport endpoint
2988c3e34a4SDavid Howells  */
299a1767077SDavid Howells int rxrpc_send_data_packet(struct rxrpc_call *call, struct sk_buff *skb,
300a1767077SDavid Howells 			   bool retrans)
3018c3e34a4SDavid Howells {
3025a924b89SDavid Howells 	struct rxrpc_connection *conn = call->conn;
3035a924b89SDavid Howells 	struct rxrpc_wire_header whdr;
3045a924b89SDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
3058c3e34a4SDavid Howells 	struct msghdr msg;
3065a924b89SDavid Howells 	struct kvec iov[2];
3075a924b89SDavid Howells 	rxrpc_serial_t serial;
3085a924b89SDavid Howells 	size_t len;
309a1767077SDavid Howells 	bool lost = false;
3108c3e34a4SDavid Howells 	int ret, opt;
3118c3e34a4SDavid Howells 
3128c3e34a4SDavid Howells 	_enter(",{%d}", skb->len);
3138c3e34a4SDavid Howells 
3145a924b89SDavid Howells 	/* Each transmission of a Tx packet needs a new serial number */
3155a924b89SDavid Howells 	serial = atomic_inc_return(&conn->serial);
3168c3e34a4SDavid Howells 
3175a924b89SDavid Howells 	whdr.epoch	= htonl(conn->proto.epoch);
3185a924b89SDavid Howells 	whdr.cid	= htonl(call->cid);
3195a924b89SDavid Howells 	whdr.callNumber	= htonl(call->call_id);
3205a924b89SDavid Howells 	whdr.seq	= htonl(sp->hdr.seq);
3215a924b89SDavid Howells 	whdr.serial	= htonl(serial);
3225a924b89SDavid Howells 	whdr.type	= RXRPC_PACKET_TYPE_DATA;
3235a924b89SDavid Howells 	whdr.flags	= sp->hdr.flags;
3245a924b89SDavid Howells 	whdr.userStatus	= 0;
3255a924b89SDavid Howells 	whdr.securityIndex = call->security_ix;
3265a924b89SDavid Howells 	whdr._rsvd	= htons(sp->hdr._rsvd);
3275a924b89SDavid Howells 	whdr.serviceId	= htons(call->service_id);
3285a924b89SDavid Howells 
3294e255721SDavid Howells 	if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) &&
3304e255721SDavid Howells 	    sp->hdr.seq == 1)
3314e255721SDavid Howells 		whdr.userStatus	= RXRPC_USERSTATUS_SERVICE_UPGRADE;
3324e255721SDavid Howells 
3335a924b89SDavid Howells 	iov[0].iov_base = &whdr;
3345a924b89SDavid Howells 	iov[0].iov_len = sizeof(whdr);
3355a924b89SDavid Howells 	iov[1].iov_base = skb->head;
3365a924b89SDavid Howells 	iov[1].iov_len = skb->len;
3375a924b89SDavid Howells 	len = iov[0].iov_len + iov[1].iov_len;
3385a924b89SDavid Howells 
3395a924b89SDavid Howells 	msg.msg_name = &call->peer->srx.transport;
3405a924b89SDavid Howells 	msg.msg_namelen = call->peer->srx.transport_len;
3418c3e34a4SDavid Howells 	msg.msg_control = NULL;
3428c3e34a4SDavid Howells 	msg.msg_controllen = 0;
3438c3e34a4SDavid Howells 	msg.msg_flags = 0;
3448c3e34a4SDavid Howells 
34557494343SDavid Howells 	/* If our RTT cache needs working on, request an ACK.  Also request
34657494343SDavid Howells 	 * ACKs if a DATA packet appears to have been lost.
34757494343SDavid Howells 	 */
348bf7d620aSDavid Howells 	if (!(sp->hdr.flags & RXRPC_LAST_PACKET) &&
349bd1fdf8cSDavid Howells 	    (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events) ||
350bd1fdf8cSDavid Howells 	     retrans ||
351b112a670SDavid Howells 	     call->cong_mode == RXRPC_CALL_SLOW_START ||
35257494343SDavid Howells 	     (call->peer->rtt_usage < 3 && sp->hdr.seq & 1) ||
3530d4b103cSDavid Howells 	     ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
354bf7d620aSDavid Howells 			  ktime_get_real())))
3550d4b103cSDavid Howells 		whdr.flags |= RXRPC_REQUEST_ACK;
3560d4b103cSDavid Howells 
3578a681c36SDavid Howells 	if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
3588a681c36SDavid Howells 		static int lose;
3598a681c36SDavid Howells 		if ((lose++ & 7) == 7) {
360a1767077SDavid Howells 			ret = 0;
361a1767077SDavid Howells 			lost = true;
362a1767077SDavid Howells 			goto done;
3638a681c36SDavid Howells 		}
3648a681c36SDavid Howells 	}
3658a681c36SDavid Howells 
3665a924b89SDavid Howells 	_proto("Tx DATA %%%u { #%u }", serial, sp->hdr.seq);
3675a924b89SDavid Howells 
3688c3e34a4SDavid Howells 	/* send the packet with the don't fragment bit set if we currently
3698c3e34a4SDavid Howells 	 * think it's small enough */
3705a924b89SDavid Howells 	if (iov[1].iov_len >= call->peer->maxdata)
3715a924b89SDavid Howells 		goto send_fragmentable;
3725a924b89SDavid Howells 
373985a5c82SDavid Howells 	down_read(&conn->params.local->defrag_sem);
3748c3e34a4SDavid Howells 	/* send the packet by UDP
3758c3e34a4SDavid Howells 	 * - returns -EMSGSIZE if UDP would have to fragment the packet
3768c3e34a4SDavid Howells 	 *   to go out of the interface
3778c3e34a4SDavid Howells 	 *   - in which case, we'll have processed the ICMP error
3788c3e34a4SDavid Howells 	 *     message and update the peer record
3798c3e34a4SDavid Howells 	 */
3805a924b89SDavid Howells 	ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
3818c3e34a4SDavid Howells 
382985a5c82SDavid Howells 	up_read(&conn->params.local->defrag_sem);
3838c3e34a4SDavid Howells 	if (ret == -EMSGSIZE)
3848c3e34a4SDavid Howells 		goto send_fragmentable;
3858c3e34a4SDavid Howells 
3865a924b89SDavid Howells done:
387a1767077SDavid Howells 	trace_rxrpc_tx_data(call, sp->hdr.seq, serial, whdr.flags,
388a1767077SDavid Howells 			    retrans, lost);
38950235c4bSDavid Howells 	if (ret >= 0) {
3900d4b103cSDavid Howells 		ktime_t now = ktime_get_real();
3910d4b103cSDavid Howells 		skb->tstamp = now;
39250235c4bSDavid Howells 		smp_wmb();
3935a924b89SDavid Howells 		sp->hdr.serial = serial;
3940d4b103cSDavid Howells 		if (whdr.flags & RXRPC_REQUEST_ACK) {
3950d4b103cSDavid Howells 			call->peer->rtt_last_req = now;
39650235c4bSDavid Howells 			trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_data, serial);
397bd1fdf8cSDavid Howells 			if (call->peer->rtt_usage > 1) {
398bd1fdf8cSDavid Howells 				unsigned long nowj = jiffies, ack_lost_at;
399bd1fdf8cSDavid Howells 
400bd1fdf8cSDavid Howells 				ack_lost_at = nsecs_to_jiffies(2 * call->peer->rtt);
401bd1fdf8cSDavid Howells 				if (ack_lost_at < 1)
402bd1fdf8cSDavid Howells 					ack_lost_at = 1;
403bd1fdf8cSDavid Howells 
404bd1fdf8cSDavid Howells 				ack_lost_at += nowj;
405bd1fdf8cSDavid Howells 				WRITE_ONCE(call->ack_lost_at, ack_lost_at);
406bd1fdf8cSDavid Howells 				rxrpc_reduce_call_timer(call, ack_lost_at, nowj,
407bd1fdf8cSDavid Howells 							rxrpc_timer_set_for_lost_ack);
408bd1fdf8cSDavid Howells 			}
4098c3e34a4SDavid Howells 		}
4100d4b103cSDavid Howells 	}
411415f44e4SDavid Howells 
412415f44e4SDavid Howells 	rxrpc_set_keepalive(call);
413415f44e4SDavid Howells 
4145a924b89SDavid Howells 	_leave(" = %d [%u]", ret, call->peer->maxdata);
4155a924b89SDavid Howells 	return ret;
4168c3e34a4SDavid Howells 
4178c3e34a4SDavid Howells send_fragmentable:
4188c3e34a4SDavid Howells 	/* attempt to send this message with fragmentation enabled */
4198c3e34a4SDavid Howells 	_debug("send fragment");
4208c3e34a4SDavid Howells 
421985a5c82SDavid Howells 	down_write(&conn->params.local->defrag_sem);
422985a5c82SDavid Howells 
423985a5c82SDavid Howells 	switch (conn->params.local->srx.transport.family) {
424985a5c82SDavid Howells 	case AF_INET:
4258c3e34a4SDavid Howells 		opt = IP_PMTUDISC_DONT;
426985a5c82SDavid Howells 		ret = kernel_setsockopt(conn->params.local->socket,
427985a5c82SDavid Howells 					SOL_IP, IP_MTU_DISCOVER,
4288c3e34a4SDavid Howells 					(char *)&opt, sizeof(opt));
4298c3e34a4SDavid Howells 		if (ret == 0) {
4305a924b89SDavid Howells 			ret = kernel_sendmsg(conn->params.local->socket, &msg,
4315a924b89SDavid Howells 					     iov, 2, len);
4328c3e34a4SDavid Howells 
4338c3e34a4SDavid Howells 			opt = IP_PMTUDISC_DO;
434985a5c82SDavid Howells 			kernel_setsockopt(conn->params.local->socket, SOL_IP,
435985a5c82SDavid Howells 					  IP_MTU_DISCOVER,
436985a5c82SDavid Howells 					  (char *)&opt, sizeof(opt));
437985a5c82SDavid Howells 		}
438985a5c82SDavid Howells 		break;
43975b54cb5SDavid Howells 
440d1912747SDavid Howells #ifdef CONFIG_AF_RXRPC_IPV6
44175b54cb5SDavid Howells 	case AF_INET6:
44275b54cb5SDavid Howells 		opt = IPV6_PMTUDISC_DONT;
44375b54cb5SDavid Howells 		ret = kernel_setsockopt(conn->params.local->socket,
44475b54cb5SDavid Howells 					SOL_IPV6, IPV6_MTU_DISCOVER,
44575b54cb5SDavid Howells 					(char *)&opt, sizeof(opt));
44675b54cb5SDavid Howells 		if (ret == 0) {
44775b54cb5SDavid Howells 			ret = kernel_sendmsg(conn->params.local->socket, &msg,
44893c62c45SDavid Howells 					     iov, 2, len);
44975b54cb5SDavid Howells 
45075b54cb5SDavid Howells 			opt = IPV6_PMTUDISC_DO;
45175b54cb5SDavid Howells 			kernel_setsockopt(conn->params.local->socket,
45275b54cb5SDavid Howells 					  SOL_IPV6, IPV6_MTU_DISCOVER,
45375b54cb5SDavid Howells 					  (char *)&opt, sizeof(opt));
45475b54cb5SDavid Howells 		}
45575b54cb5SDavid Howells 		break;
456d1912747SDavid Howells #endif
4578c3e34a4SDavid Howells 	}
4588c3e34a4SDavid Howells 
459985a5c82SDavid Howells 	up_write(&conn->params.local->defrag_sem);
4605a924b89SDavid Howells 	goto done;
4618c3e34a4SDavid Howells }
462248f219cSDavid Howells 
463248f219cSDavid Howells /*
464248f219cSDavid Howells  * reject packets through the local endpoint
465248f219cSDavid Howells  */
466248f219cSDavid Howells void rxrpc_reject_packets(struct rxrpc_local *local)
467248f219cSDavid Howells {
4681c2bc7b9SDavid Howells 	struct sockaddr_rxrpc srx;
469248f219cSDavid Howells 	struct rxrpc_skb_priv *sp;
470248f219cSDavid Howells 	struct rxrpc_wire_header whdr;
471248f219cSDavid Howells 	struct sk_buff *skb;
472248f219cSDavid Howells 	struct msghdr msg;
473248f219cSDavid Howells 	struct kvec iov[2];
474248f219cSDavid Howells 	size_t size;
475248f219cSDavid Howells 	__be32 code;
476248f219cSDavid Howells 
477248f219cSDavid Howells 	_enter("%d", local->debug_id);
478248f219cSDavid Howells 
479248f219cSDavid Howells 	iov[0].iov_base = &whdr;
480248f219cSDavid Howells 	iov[0].iov_len = sizeof(whdr);
481248f219cSDavid Howells 	iov[1].iov_base = &code;
482248f219cSDavid Howells 	iov[1].iov_len = sizeof(code);
483248f219cSDavid Howells 	size = sizeof(whdr) + sizeof(code);
484248f219cSDavid Howells 
4851c2bc7b9SDavid Howells 	msg.msg_name = &srx.transport;
486248f219cSDavid Howells 	msg.msg_control = NULL;
487248f219cSDavid Howells 	msg.msg_controllen = 0;
488248f219cSDavid Howells 	msg.msg_flags = 0;
489248f219cSDavid Howells 
490248f219cSDavid Howells 	memset(&whdr, 0, sizeof(whdr));
491248f219cSDavid Howells 	whdr.type = RXRPC_PACKET_TYPE_ABORT;
492248f219cSDavid Howells 
493248f219cSDavid Howells 	while ((skb = skb_dequeue(&local->reject_queue))) {
49471f3ca40SDavid Howells 		rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
495248f219cSDavid Howells 		sp = rxrpc_skb(skb);
4961c2bc7b9SDavid Howells 
4977b674e39SDavid Howells 		if (rxrpc_extract_addr_from_skb(local, &srx, skb) == 0) {
4981c2bc7b9SDavid Howells 			msg.msg_namelen = srx.transport_len;
4991c2bc7b9SDavid Howells 
500248f219cSDavid Howells 			code = htonl(skb->priority);
501248f219cSDavid Howells 
502248f219cSDavid Howells 			whdr.epoch	= htonl(sp->hdr.epoch);
503248f219cSDavid Howells 			whdr.cid	= htonl(sp->hdr.cid);
504248f219cSDavid Howells 			whdr.callNumber	= htonl(sp->hdr.callNumber);
505248f219cSDavid Howells 			whdr.serviceId	= htons(sp->hdr.serviceId);
506248f219cSDavid Howells 			whdr.flags	= sp->hdr.flags;
507248f219cSDavid Howells 			whdr.flags	^= RXRPC_CLIENT_INITIATED;
508248f219cSDavid Howells 			whdr.flags	&= RXRPC_CLIENT_INITIATED;
509248f219cSDavid Howells 
510248f219cSDavid Howells 			kernel_sendmsg(local->socket, &msg, iov, 2, size);
511248f219cSDavid Howells 		}
512248f219cSDavid Howells 
51371f3ca40SDavid Howells 		rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
514248f219cSDavid Howells 	}
515248f219cSDavid Howells 
516248f219cSDavid Howells 	_leave("");
517248f219cSDavid Howells }
518