xref: /openbmc/linux/net/rxrpc/output.c (revision ace45bec)
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;
127ace45becSDavid Howells 	ktime_t now;
1288d94aa38SDavid Howells 	size_t len, n;
12926cb02aaSDavid Howells 	int ret;
130a5af7e1fSDavid Howells 	u8 reason;
1318d94aa38SDavid Howells 
1328d94aa38SDavid Howells 	spin_lock_bh(&call->lock);
1338d94aa38SDavid Howells 	if (call->conn)
1348d94aa38SDavid Howells 		conn = rxrpc_get_connection_maybe(call->conn);
1358d94aa38SDavid Howells 	spin_unlock_bh(&call->lock);
1368d94aa38SDavid Howells 	if (!conn)
1378d94aa38SDavid Howells 		return -ECONNRESET;
1388d94aa38SDavid Howells 
1398d94aa38SDavid Howells 	pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
1408d94aa38SDavid Howells 	if (!pkt) {
1418d94aa38SDavid Howells 		rxrpc_put_connection(conn);
1428d94aa38SDavid Howells 		return -ENOMEM;
1438d94aa38SDavid Howells 	}
1448d94aa38SDavid Howells 
1458d94aa38SDavid Howells 	msg.msg_name	= &call->peer->srx.transport;
1468d94aa38SDavid Howells 	msg.msg_namelen	= call->peer->srx.transport_len;
1478d94aa38SDavid Howells 	msg.msg_control	= NULL;
1488d94aa38SDavid Howells 	msg.msg_controllen = 0;
1498d94aa38SDavid Howells 	msg.msg_flags	= 0;
1508d94aa38SDavid Howells 
1518d94aa38SDavid Howells 	pkt->whdr.epoch		= htonl(conn->proto.epoch);
1528d94aa38SDavid Howells 	pkt->whdr.cid		= htonl(call->cid);
1538d94aa38SDavid Howells 	pkt->whdr.callNumber	= htonl(call->call_id);
1548d94aa38SDavid Howells 	pkt->whdr.seq		= 0;
15526cb02aaSDavid Howells 	pkt->whdr.type		= RXRPC_PACKET_TYPE_ACK;
15626cb02aaSDavid Howells 	pkt->whdr.flags		= RXRPC_SLOW_START_OK | conn->out_clientflag;
1578d94aa38SDavid Howells 	pkt->whdr.userStatus	= 0;
1588d94aa38SDavid Howells 	pkt->whdr.securityIndex	= call->security_ix;
1598d94aa38SDavid Howells 	pkt->whdr._rsvd		= 0;
1608d94aa38SDavid Howells 	pkt->whdr.serviceId	= htons(call->service_id);
1618d94aa38SDavid Howells 
1628d94aa38SDavid Howells 	spin_lock_bh(&call->lock);
163a5af7e1fSDavid Howells 	if (ping) {
164a5af7e1fSDavid Howells 		reason = RXRPC_ACK_PING;
165a5af7e1fSDavid Howells 	} else {
166a5af7e1fSDavid Howells 		reason = call->ackr_reason;
16727d0fc43SDavid Howells 		if (!call->ackr_reason) {
16827d0fc43SDavid Howells 			spin_unlock_bh(&call->lock);
16927d0fc43SDavid Howells 			ret = 0;
17027d0fc43SDavid Howells 			goto out;
17127d0fc43SDavid Howells 		}
1728d94aa38SDavid Howells 		call->ackr_reason = 0;
173a5af7e1fSDavid Howells 	}
1741457cc4cSDavid Howells 	n = rxrpc_fill_out_ack(conn, call, pkt, &hard_ack, &top, reason);
1758d94aa38SDavid Howells 
1768d94aa38SDavid Howells 	spin_unlock_bh(&call->lock);
1778d94aa38SDavid Howells 
17826cb02aaSDavid Howells 	iov[0].iov_base	= pkt;
17926cb02aaSDavid Howells 	iov[0].iov_len	= sizeof(pkt->whdr) + sizeof(pkt->ack) + n;
1808d94aa38SDavid Howells 	iov[1].iov_base = &pkt->ackinfo;
1818d94aa38SDavid Howells 	iov[1].iov_len	= sizeof(pkt->ackinfo);
18226cb02aaSDavid Howells 	len = iov[0].iov_len + iov[1].iov_len;
1838d94aa38SDavid Howells 
184b86e218eSDavid Howells 	serial = atomic_inc_return(&conn->serial);
185b86e218eSDavid Howells 	pkt->whdr.serial = htonl(serial);
186be832aecSDavid Howells 	trace_rxrpc_tx_ack(call, serial,
187b86e218eSDavid Howells 			   ntohl(pkt->ack.firstPacket),
188b86e218eSDavid Howells 			   ntohl(pkt->ack.serial),
189b86e218eSDavid Howells 			   pkt->ack.reason, pkt->ack.nAcks);
190bd1fdf8cSDavid Howells 	if (_serial)
191bd1fdf8cSDavid Howells 		*_serial = serial;
192b86e218eSDavid Howells 
1938e83134dSDavid Howells 	if (ping) {
194a5af7e1fSDavid Howells 		call->ping_serial = serial;
1958e83134dSDavid Howells 		smp_wmb();
1968e83134dSDavid Howells 		/* We need to stick a time in before we send the packet in case
1978e83134dSDavid Howells 		 * the reply gets back before kernel_sendmsg() completes - but
1988e83134dSDavid Howells 		 * asking UDP to send the packet can take a relatively long
1998e83134dSDavid Howells 		 * time, so we update the time after, on the assumption that
2008e83134dSDavid Howells 		 * the packet transmission is more likely to happen towards the
2018e83134dSDavid Howells 		 * end of the kernel_sendmsg() call.
2028e83134dSDavid Howells 		 */
203a5af7e1fSDavid Howells 		call->ping_time = ktime_get_real();
2048e83134dSDavid Howells 		set_bit(RXRPC_CALL_PINGING, &call->flags);
2058e83134dSDavid Howells 		trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_ping, serial);
2068e83134dSDavid Howells 	}
20726cb02aaSDavid Howells 
20826cb02aaSDavid Howells 	ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
209ace45becSDavid Howells 	now = ktime_get_real();
2108e83134dSDavid Howells 	if (ping)
211ace45becSDavid Howells 		call->ping_time = now;
212ace45becSDavid Howells 	conn->params.peer->last_tx_at = ktime_get_real();
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));
296ace45becSDavid Howells 	conn->params.peer->last_tx_at = ktime_get_real();
29726cb02aaSDavid Howells 
29826cb02aaSDavid Howells 	rxrpc_put_connection(conn);
29926cb02aaSDavid Howells 	return ret;
30026cb02aaSDavid Howells }
30126cb02aaSDavid Howells 
30226cb02aaSDavid Howells /*
3038c3e34a4SDavid Howells  * send a packet through the transport endpoint
3048c3e34a4SDavid Howells  */
305a1767077SDavid Howells int rxrpc_send_data_packet(struct rxrpc_call *call, struct sk_buff *skb,
306a1767077SDavid Howells 			   bool retrans)
3078c3e34a4SDavid Howells {
3085a924b89SDavid Howells 	struct rxrpc_connection *conn = call->conn;
3095a924b89SDavid Howells 	struct rxrpc_wire_header whdr;
3105a924b89SDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
3118c3e34a4SDavid Howells 	struct msghdr msg;
3125a924b89SDavid Howells 	struct kvec iov[2];
3135a924b89SDavid Howells 	rxrpc_serial_t serial;
3145a924b89SDavid Howells 	size_t len;
315a1767077SDavid Howells 	bool lost = false;
3168c3e34a4SDavid Howells 	int ret, opt;
3178c3e34a4SDavid Howells 
3188c3e34a4SDavid Howells 	_enter(",{%d}", skb->len);
3198c3e34a4SDavid Howells 
3205a924b89SDavid Howells 	/* Each transmission of a Tx packet needs a new serial number */
3215a924b89SDavid Howells 	serial = atomic_inc_return(&conn->serial);
3228c3e34a4SDavid Howells 
3235a924b89SDavid Howells 	whdr.epoch	= htonl(conn->proto.epoch);
3245a924b89SDavid Howells 	whdr.cid	= htonl(call->cid);
3255a924b89SDavid Howells 	whdr.callNumber	= htonl(call->call_id);
3265a924b89SDavid Howells 	whdr.seq	= htonl(sp->hdr.seq);
3275a924b89SDavid Howells 	whdr.serial	= htonl(serial);
3285a924b89SDavid Howells 	whdr.type	= RXRPC_PACKET_TYPE_DATA;
3295a924b89SDavid Howells 	whdr.flags	= sp->hdr.flags;
3305a924b89SDavid Howells 	whdr.userStatus	= 0;
3315a924b89SDavid Howells 	whdr.securityIndex = call->security_ix;
3325a924b89SDavid Howells 	whdr._rsvd	= htons(sp->hdr._rsvd);
3335a924b89SDavid Howells 	whdr.serviceId	= htons(call->service_id);
3345a924b89SDavid Howells 
3354e255721SDavid Howells 	if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) &&
3364e255721SDavid Howells 	    sp->hdr.seq == 1)
3374e255721SDavid Howells 		whdr.userStatus	= RXRPC_USERSTATUS_SERVICE_UPGRADE;
3384e255721SDavid Howells 
3395a924b89SDavid Howells 	iov[0].iov_base = &whdr;
3405a924b89SDavid Howells 	iov[0].iov_len = sizeof(whdr);
3415a924b89SDavid Howells 	iov[1].iov_base = skb->head;
3425a924b89SDavid Howells 	iov[1].iov_len = skb->len;
3435a924b89SDavid Howells 	len = iov[0].iov_len + iov[1].iov_len;
3445a924b89SDavid Howells 
3455a924b89SDavid Howells 	msg.msg_name = &call->peer->srx.transport;
3465a924b89SDavid Howells 	msg.msg_namelen = call->peer->srx.transport_len;
3478c3e34a4SDavid Howells 	msg.msg_control = NULL;
3488c3e34a4SDavid Howells 	msg.msg_controllen = 0;
3498c3e34a4SDavid Howells 	msg.msg_flags = 0;
3508c3e34a4SDavid Howells 
35157494343SDavid Howells 	/* If our RTT cache needs working on, request an ACK.  Also request
35257494343SDavid Howells 	 * ACKs if a DATA packet appears to have been lost.
35357494343SDavid Howells 	 */
354bf7d620aSDavid Howells 	if (!(sp->hdr.flags & RXRPC_LAST_PACKET) &&
355bd1fdf8cSDavid Howells 	    (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events) ||
356bd1fdf8cSDavid Howells 	     retrans ||
357b112a670SDavid Howells 	     call->cong_mode == RXRPC_CALL_SLOW_START ||
35857494343SDavid Howells 	     (call->peer->rtt_usage < 3 && sp->hdr.seq & 1) ||
3590d4b103cSDavid Howells 	     ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
360bf7d620aSDavid Howells 			  ktime_get_real())))
3610d4b103cSDavid Howells 		whdr.flags |= RXRPC_REQUEST_ACK;
3620d4b103cSDavid Howells 
3638a681c36SDavid Howells 	if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
3648a681c36SDavid Howells 		static int lose;
3658a681c36SDavid Howells 		if ((lose++ & 7) == 7) {
366a1767077SDavid Howells 			ret = 0;
367a1767077SDavid Howells 			lost = true;
368a1767077SDavid Howells 			goto done;
3698a681c36SDavid Howells 		}
3708a681c36SDavid Howells 	}
3718a681c36SDavid Howells 
3725a924b89SDavid Howells 	_proto("Tx DATA %%%u { #%u }", serial, sp->hdr.seq);
3735a924b89SDavid Howells 
3748c3e34a4SDavid Howells 	/* send the packet with the don't fragment bit set if we currently
3758c3e34a4SDavid Howells 	 * think it's small enough */
3765a924b89SDavid Howells 	if (iov[1].iov_len >= call->peer->maxdata)
3775a924b89SDavid Howells 		goto send_fragmentable;
3785a924b89SDavid Howells 
379985a5c82SDavid Howells 	down_read(&conn->params.local->defrag_sem);
3808c3e34a4SDavid Howells 	/* send the packet by UDP
3818c3e34a4SDavid Howells 	 * - returns -EMSGSIZE if UDP would have to fragment the packet
3828c3e34a4SDavid Howells 	 *   to go out of the interface
3838c3e34a4SDavid Howells 	 *   - in which case, we'll have processed the ICMP error
3848c3e34a4SDavid Howells 	 *     message and update the peer record
3858c3e34a4SDavid Howells 	 */
3865a924b89SDavid Howells 	ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
387ace45becSDavid Howells 	conn->params.peer->last_tx_at = ktime_get_real();
3888c3e34a4SDavid Howells 
389985a5c82SDavid Howells 	up_read(&conn->params.local->defrag_sem);
3908c3e34a4SDavid Howells 	if (ret == -EMSGSIZE)
3918c3e34a4SDavid Howells 		goto send_fragmentable;
3928c3e34a4SDavid Howells 
3935a924b89SDavid Howells done:
394a1767077SDavid Howells 	trace_rxrpc_tx_data(call, sp->hdr.seq, serial, whdr.flags,
395a1767077SDavid Howells 			    retrans, lost);
39650235c4bSDavid Howells 	if (ret >= 0) {
3970d4b103cSDavid Howells 		ktime_t now = ktime_get_real();
3980d4b103cSDavid Howells 		skb->tstamp = now;
39950235c4bSDavid Howells 		smp_wmb();
4005a924b89SDavid Howells 		sp->hdr.serial = serial;
4010d4b103cSDavid Howells 		if (whdr.flags & RXRPC_REQUEST_ACK) {
4020d4b103cSDavid Howells 			call->peer->rtt_last_req = now;
40350235c4bSDavid Howells 			trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_data, serial);
404bd1fdf8cSDavid Howells 			if (call->peer->rtt_usage > 1) {
405bd1fdf8cSDavid Howells 				unsigned long nowj = jiffies, ack_lost_at;
406bd1fdf8cSDavid Howells 
407bd1fdf8cSDavid Howells 				ack_lost_at = nsecs_to_jiffies(2 * call->peer->rtt);
408bd1fdf8cSDavid Howells 				if (ack_lost_at < 1)
409bd1fdf8cSDavid Howells 					ack_lost_at = 1;
410bd1fdf8cSDavid Howells 
411bd1fdf8cSDavid Howells 				ack_lost_at += nowj;
412bd1fdf8cSDavid Howells 				WRITE_ONCE(call->ack_lost_at, ack_lost_at);
413bd1fdf8cSDavid Howells 				rxrpc_reduce_call_timer(call, ack_lost_at, nowj,
414bd1fdf8cSDavid Howells 							rxrpc_timer_set_for_lost_ack);
415bd1fdf8cSDavid Howells 			}
4168c3e34a4SDavid Howells 		}
4170d4b103cSDavid Howells 	}
418415f44e4SDavid Howells 
419415f44e4SDavid Howells 	rxrpc_set_keepalive(call);
420415f44e4SDavid Howells 
4215a924b89SDavid Howells 	_leave(" = %d [%u]", ret, call->peer->maxdata);
4225a924b89SDavid Howells 	return ret;
4238c3e34a4SDavid Howells 
4248c3e34a4SDavid Howells send_fragmentable:
4258c3e34a4SDavid Howells 	/* attempt to send this message with fragmentation enabled */
4268c3e34a4SDavid Howells 	_debug("send fragment");
4278c3e34a4SDavid Howells 
428985a5c82SDavid Howells 	down_write(&conn->params.local->defrag_sem);
429985a5c82SDavid Howells 
430985a5c82SDavid Howells 	switch (conn->params.local->srx.transport.family) {
431985a5c82SDavid Howells 	case AF_INET:
4328c3e34a4SDavid Howells 		opt = IP_PMTUDISC_DONT;
433985a5c82SDavid Howells 		ret = kernel_setsockopt(conn->params.local->socket,
434985a5c82SDavid Howells 					SOL_IP, IP_MTU_DISCOVER,
4358c3e34a4SDavid Howells 					(char *)&opt, sizeof(opt));
4368c3e34a4SDavid Howells 		if (ret == 0) {
4375a924b89SDavid Howells 			ret = kernel_sendmsg(conn->params.local->socket, &msg,
4385a924b89SDavid Howells 					     iov, 2, len);
439ace45becSDavid Howells 			conn->params.peer->last_tx_at = ktime_get_real();
4408c3e34a4SDavid Howells 
4418c3e34a4SDavid Howells 			opt = IP_PMTUDISC_DO;
442985a5c82SDavid Howells 			kernel_setsockopt(conn->params.local->socket, SOL_IP,
443985a5c82SDavid Howells 					  IP_MTU_DISCOVER,
444985a5c82SDavid Howells 					  (char *)&opt, sizeof(opt));
445985a5c82SDavid Howells 		}
446985a5c82SDavid Howells 		break;
44775b54cb5SDavid Howells 
448d1912747SDavid Howells #ifdef CONFIG_AF_RXRPC_IPV6
44975b54cb5SDavid Howells 	case AF_INET6:
45075b54cb5SDavid Howells 		opt = IPV6_PMTUDISC_DONT;
45175b54cb5SDavid Howells 		ret = kernel_setsockopt(conn->params.local->socket,
45275b54cb5SDavid Howells 					SOL_IPV6, IPV6_MTU_DISCOVER,
45375b54cb5SDavid Howells 					(char *)&opt, sizeof(opt));
45475b54cb5SDavid Howells 		if (ret == 0) {
45575b54cb5SDavid Howells 			ret = kernel_sendmsg(conn->params.local->socket, &msg,
45693c62c45SDavid Howells 					     iov, 2, len);
457ace45becSDavid Howells 			conn->params.peer->last_tx_at = ktime_get_real();
45875b54cb5SDavid Howells 
45975b54cb5SDavid Howells 			opt = IPV6_PMTUDISC_DO;
46075b54cb5SDavid Howells 			kernel_setsockopt(conn->params.local->socket,
46175b54cb5SDavid Howells 					  SOL_IPV6, IPV6_MTU_DISCOVER,
46275b54cb5SDavid Howells 					  (char *)&opt, sizeof(opt));
46375b54cb5SDavid Howells 		}
46475b54cb5SDavid Howells 		break;
465d1912747SDavid Howells #endif
4668c3e34a4SDavid Howells 	}
4678c3e34a4SDavid Howells 
468985a5c82SDavid Howells 	up_write(&conn->params.local->defrag_sem);
4695a924b89SDavid Howells 	goto done;
4708c3e34a4SDavid Howells }
471248f219cSDavid Howells 
472248f219cSDavid Howells /*
473248f219cSDavid Howells  * reject packets through the local endpoint
474248f219cSDavid Howells  */
475248f219cSDavid Howells void rxrpc_reject_packets(struct rxrpc_local *local)
476248f219cSDavid Howells {
4771c2bc7b9SDavid Howells 	struct sockaddr_rxrpc srx;
478248f219cSDavid Howells 	struct rxrpc_skb_priv *sp;
479248f219cSDavid Howells 	struct rxrpc_wire_header whdr;
480248f219cSDavid Howells 	struct sk_buff *skb;
481248f219cSDavid Howells 	struct msghdr msg;
482248f219cSDavid Howells 	struct kvec iov[2];
483248f219cSDavid Howells 	size_t size;
484248f219cSDavid Howells 	__be32 code;
485248f219cSDavid Howells 
486248f219cSDavid Howells 	_enter("%d", local->debug_id);
487248f219cSDavid Howells 
488248f219cSDavid Howells 	iov[0].iov_base = &whdr;
489248f219cSDavid Howells 	iov[0].iov_len = sizeof(whdr);
490248f219cSDavid Howells 	iov[1].iov_base = &code;
491248f219cSDavid Howells 	iov[1].iov_len = sizeof(code);
492248f219cSDavid Howells 	size = sizeof(whdr) + sizeof(code);
493248f219cSDavid Howells 
4941c2bc7b9SDavid Howells 	msg.msg_name = &srx.transport;
495248f219cSDavid Howells 	msg.msg_control = NULL;
496248f219cSDavid Howells 	msg.msg_controllen = 0;
497248f219cSDavid Howells 	msg.msg_flags = 0;
498248f219cSDavid Howells 
499248f219cSDavid Howells 	memset(&whdr, 0, sizeof(whdr));
500248f219cSDavid Howells 	whdr.type = RXRPC_PACKET_TYPE_ABORT;
501248f219cSDavid Howells 
502248f219cSDavid Howells 	while ((skb = skb_dequeue(&local->reject_queue))) {
50371f3ca40SDavid Howells 		rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
504248f219cSDavid Howells 		sp = rxrpc_skb(skb);
5051c2bc7b9SDavid Howells 
5067b674e39SDavid Howells 		if (rxrpc_extract_addr_from_skb(local, &srx, skb) == 0) {
5071c2bc7b9SDavid Howells 			msg.msg_namelen = srx.transport_len;
5081c2bc7b9SDavid Howells 
509248f219cSDavid Howells 			code = htonl(skb->priority);
510248f219cSDavid Howells 
511248f219cSDavid Howells 			whdr.epoch	= htonl(sp->hdr.epoch);
512248f219cSDavid Howells 			whdr.cid	= htonl(sp->hdr.cid);
513248f219cSDavid Howells 			whdr.callNumber	= htonl(sp->hdr.callNumber);
514248f219cSDavid Howells 			whdr.serviceId	= htons(sp->hdr.serviceId);
515248f219cSDavid Howells 			whdr.flags	= sp->hdr.flags;
516248f219cSDavid Howells 			whdr.flags	^= RXRPC_CLIENT_INITIATED;
517248f219cSDavid Howells 			whdr.flags	&= RXRPC_CLIENT_INITIATED;
518248f219cSDavid Howells 
519248f219cSDavid Howells 			kernel_sendmsg(local->socket, &msg, iov, 2, size);
520248f219cSDavid Howells 		}
521248f219cSDavid Howells 
52271f3ca40SDavid Howells 		rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
523248f219cSDavid Howells 	}
524248f219cSDavid Howells 
525248f219cSDavid Howells 	_leave("");
526248f219cSDavid Howells }
527ace45becSDavid Howells 
528ace45becSDavid Howells /*
529ace45becSDavid Howells  * Send a VERSION reply to a peer as a keepalive.
530ace45becSDavid Howells  */
531ace45becSDavid Howells void rxrpc_send_keepalive(struct rxrpc_peer *peer)
532ace45becSDavid Howells {
533ace45becSDavid Howells 	struct rxrpc_wire_header whdr;
534ace45becSDavid Howells 	struct msghdr msg;
535ace45becSDavid Howells 	struct kvec iov[2];
536ace45becSDavid Howells 	size_t len;
537ace45becSDavid Howells 	int ret;
538ace45becSDavid Howells 
539ace45becSDavid Howells 	_enter("");
540ace45becSDavid Howells 
541ace45becSDavid Howells 	msg.msg_name	= &peer->srx.transport;
542ace45becSDavid Howells 	msg.msg_namelen	= peer->srx.transport_len;
543ace45becSDavid Howells 	msg.msg_control	= NULL;
544ace45becSDavid Howells 	msg.msg_controllen = 0;
545ace45becSDavid Howells 	msg.msg_flags	= 0;
546ace45becSDavid Howells 
547ace45becSDavid Howells 	whdr.epoch	= htonl(peer->local->rxnet->epoch);
548ace45becSDavid Howells 	whdr.cid	= 0;
549ace45becSDavid Howells 	whdr.callNumber	= 0;
550ace45becSDavid Howells 	whdr.seq	= 0;
551ace45becSDavid Howells 	whdr.serial	= 0;
552ace45becSDavid Howells 	whdr.type	= RXRPC_PACKET_TYPE_VERSION; /* Not client-initiated */
553ace45becSDavid Howells 	whdr.flags	= RXRPC_LAST_PACKET;
554ace45becSDavid Howells 	whdr.userStatus	= 0;
555ace45becSDavid Howells 	whdr.securityIndex = 0;
556ace45becSDavid Howells 	whdr._rsvd	= 0;
557ace45becSDavid Howells 	whdr.serviceId	= 0;
558ace45becSDavid Howells 
559ace45becSDavid Howells 	iov[0].iov_base	= &whdr;
560ace45becSDavid Howells 	iov[0].iov_len	= sizeof(whdr);
561ace45becSDavid Howells 	iov[1].iov_base	= (char *)rxrpc_keepalive_string;
562ace45becSDavid Howells 	iov[1].iov_len	= sizeof(rxrpc_keepalive_string);
563ace45becSDavid Howells 
564ace45becSDavid Howells 	len = iov[0].iov_len + iov[1].iov_len;
565ace45becSDavid Howells 
566ace45becSDavid Howells 	_proto("Tx VERSION (keepalive)");
567ace45becSDavid Howells 
568ace45becSDavid Howells 	ret = kernel_sendmsg(peer->local->socket, &msg, iov, 2, len);
569ace45becSDavid Howells 	if (ret < 0)
570ace45becSDavid Howells 		_debug("sendmsg failed: %d", ret);
571ace45becSDavid Howells 
572ace45becSDavid Howells 	peer->last_tx_at = ktime_get_real();
573ace45becSDavid Howells 	_leave("");
574ace45becSDavid Howells }
575