xref: /openbmc/linux/net/rxrpc/output.c (revision c410bf01)
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);
90e8c3af6bSDavid Howells 	pkt->ack.maxSkew	= htons(0);
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 {
1325273a191SDavid Howells 	struct rxrpc_connection *conn;
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 
1425273a191SDavid Howells 	if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
1438d94aa38SDavid Howells 		return -ECONNRESET;
1448d94aa38SDavid Howells 
1458d94aa38SDavid Howells 	pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
1465273a191SDavid Howells 	if (!pkt)
1478d94aa38SDavid Howells 		return -ENOMEM;
1485273a191SDavid Howells 
1495273a191SDavid Howells 	conn = call->conn;
1508d94aa38SDavid Howells 
1518d94aa38SDavid Howells 	msg.msg_name	= &call->peer->srx.transport;
1528d94aa38SDavid Howells 	msg.msg_namelen	= call->peer->srx.transport_len;
1538d94aa38SDavid Howells 	msg.msg_control	= NULL;
1548d94aa38SDavid Howells 	msg.msg_controllen = 0;
1558d94aa38SDavid Howells 	msg.msg_flags	= 0;
1568d94aa38SDavid Howells 
1578d94aa38SDavid Howells 	pkt->whdr.epoch		= htonl(conn->proto.epoch);
1588d94aa38SDavid Howells 	pkt->whdr.cid		= htonl(call->cid);
1598d94aa38SDavid Howells 	pkt->whdr.callNumber	= htonl(call->call_id);
1608d94aa38SDavid Howells 	pkt->whdr.seq		= 0;
16126cb02aaSDavid Howells 	pkt->whdr.type		= RXRPC_PACKET_TYPE_ACK;
16226cb02aaSDavid Howells 	pkt->whdr.flags		= RXRPC_SLOW_START_OK | conn->out_clientflag;
1638d94aa38SDavid Howells 	pkt->whdr.userStatus	= 0;
1648d94aa38SDavid Howells 	pkt->whdr.securityIndex	= call->security_ix;
1658d94aa38SDavid Howells 	pkt->whdr._rsvd		= 0;
1668d94aa38SDavid Howells 	pkt->whdr.serviceId	= htons(call->service_id);
1678d94aa38SDavid Howells 
1688d94aa38SDavid Howells 	spin_lock_bh(&call->lock);
169a5af7e1fSDavid Howells 	if (ping) {
170a5af7e1fSDavid Howells 		reason = RXRPC_ACK_PING;
171a5af7e1fSDavid Howells 	} else {
172a5af7e1fSDavid Howells 		reason = call->ackr_reason;
17327d0fc43SDavid Howells 		if (!call->ackr_reason) {
17427d0fc43SDavid Howells 			spin_unlock_bh(&call->lock);
17527d0fc43SDavid Howells 			ret = 0;
17627d0fc43SDavid Howells 			goto out;
17727d0fc43SDavid Howells 		}
1788d94aa38SDavid Howells 		call->ackr_reason = 0;
179a5af7e1fSDavid Howells 	}
1801457cc4cSDavid Howells 	n = rxrpc_fill_out_ack(conn, call, pkt, &hard_ack, &top, reason);
1818d94aa38SDavid Howells 
1828d94aa38SDavid Howells 	spin_unlock_bh(&call->lock);
1838d94aa38SDavid Howells 
18426cb02aaSDavid Howells 	iov[0].iov_base	= pkt;
18526cb02aaSDavid Howells 	iov[0].iov_len	= sizeof(pkt->whdr) + sizeof(pkt->ack) + n;
1868d94aa38SDavid Howells 	iov[1].iov_base = &pkt->ackinfo;
1878d94aa38SDavid Howells 	iov[1].iov_len	= sizeof(pkt->ackinfo);
18826cb02aaSDavid Howells 	len = iov[0].iov_len + iov[1].iov_len;
1898d94aa38SDavid Howells 
190b86e218eSDavid Howells 	serial = atomic_inc_return(&conn->serial);
191b86e218eSDavid Howells 	pkt->whdr.serial = htonl(serial);
1924764c0daSDavid Howells 	trace_rxrpc_tx_ack(call->debug_id, serial,
193b86e218eSDavid Howells 			   ntohl(pkt->ack.firstPacket),
194b86e218eSDavid Howells 			   ntohl(pkt->ack.serial),
195b86e218eSDavid Howells 			   pkt->ack.reason, pkt->ack.nAcks);
196bd1fdf8cSDavid Howells 	if (_serial)
197bd1fdf8cSDavid Howells 		*_serial = serial;
198b86e218eSDavid Howells 
1998e83134dSDavid Howells 	if (ping) {
200a5af7e1fSDavid Howells 		call->ping_serial = serial;
2018e83134dSDavid Howells 		smp_wmb();
2028e83134dSDavid Howells 		/* We need to stick a time in before we send the packet in case
2038e83134dSDavid Howells 		 * the reply gets back before kernel_sendmsg() completes - but
2048e83134dSDavid Howells 		 * asking UDP to send the packet can take a relatively long
205b604dd98SDavid Howells 		 * time.
2068e83134dSDavid Howells 		 */
207a5af7e1fSDavid Howells 		call->ping_time = ktime_get_real();
2088e83134dSDavid Howells 		set_bit(RXRPC_CALL_PINGING, &call->flags);
2098e83134dSDavid Howells 		trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_ping, serial);
2108e83134dSDavid Howells 	}
21126cb02aaSDavid Howells 
21226cb02aaSDavid Howells 	ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
213330bdcfaSDavid Howells 	conn->params.peer->last_tx_at = ktime_get_seconds();
2146b47fe1dSDavid Howells 	if (ret < 0)
2156b47fe1dSDavid Howells 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
2164764c0daSDavid Howells 				    rxrpc_tx_point_call_ack);
2174764c0daSDavid Howells 	else
2184764c0daSDavid Howells 		trace_rxrpc_tx_packet(call->debug_id, &pkt->whdr,
2194764c0daSDavid Howells 				      rxrpc_tx_point_call_ack);
220c7e86acfSDavid Howells 	rxrpc_tx_backoff(call, ret);
2218d94aa38SDavid Howells 
22226cb02aaSDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE) {
223805b21b9SDavid Howells 		if (ret < 0) {
224a5af7e1fSDavid Howells 			if (ping)
2258e83134dSDavid Howells 				clear_bit(RXRPC_CALL_PINGING, &call->flags);
226248f219cSDavid Howells 			rxrpc_propose_ACK(call, pkt->ack.reason,
227248f219cSDavid Howells 					  ntohl(pkt->ack.serial),
228c7e86acfSDavid Howells 					  false, true,
2299c7ad434SDavid Howells 					  rxrpc_propose_ack_retry_tx);
230805b21b9SDavid Howells 		} else {
231805b21b9SDavid Howells 			spin_lock_bh(&call->lock);
232805b21b9SDavid Howells 			if (after(hard_ack, call->ackr_consumed))
233805b21b9SDavid Howells 				call->ackr_consumed = hard_ack;
234805b21b9SDavid Howells 			if (after(top, call->ackr_seen))
235805b21b9SDavid Howells 				call->ackr_seen = top;
236805b21b9SDavid Howells 			spin_unlock_bh(&call->lock);
237248f219cSDavid Howells 		}
238415f44e4SDavid Howells 
239415f44e4SDavid Howells 		rxrpc_set_keepalive(call);
240248f219cSDavid Howells 	}
241248f219cSDavid Howells 
2428d94aa38SDavid Howells out:
2438d94aa38SDavid Howells 	kfree(pkt);
2448d94aa38SDavid Howells 	return ret;
2458d94aa38SDavid Howells }
2468d94aa38SDavid Howells 
2478c3e34a4SDavid Howells /*
24826cb02aaSDavid Howells  * Send an ABORT call packet.
24926cb02aaSDavid Howells  */
25026cb02aaSDavid Howells int rxrpc_send_abort_packet(struct rxrpc_call *call)
25126cb02aaSDavid Howells {
2525273a191SDavid Howells 	struct rxrpc_connection *conn;
25326cb02aaSDavid Howells 	struct rxrpc_abort_buffer pkt;
25426cb02aaSDavid Howells 	struct msghdr msg;
25526cb02aaSDavid Howells 	struct kvec iov[1];
25626cb02aaSDavid Howells 	rxrpc_serial_t serial;
25726cb02aaSDavid Howells 	int ret;
25826cb02aaSDavid Howells 
259dcbefc30SDavid Howells 	/* Don't bother sending aborts for a client call once the server has
260dcbefc30SDavid Howells 	 * hard-ACK'd all of its request data.  After that point, we're not
261dcbefc30SDavid Howells 	 * going to stop the operation proceeding, and whilst we might limit
262dcbefc30SDavid Howells 	 * the reply, it's not worth it if we can send a new call on the same
263dcbefc30SDavid Howells 	 * channel instead, thereby closing off this call.
264dcbefc30SDavid Howells 	 */
265dcbefc30SDavid Howells 	if (rxrpc_is_client_call(call) &&
266dcbefc30SDavid Howells 	    test_bit(RXRPC_CALL_TX_LAST, &call->flags))
267dcbefc30SDavid Howells 		return 0;
268dcbefc30SDavid Howells 
2695273a191SDavid Howells 	if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
27026cb02aaSDavid Howells 		return -ECONNRESET;
27126cb02aaSDavid Howells 
2725273a191SDavid Howells 	conn = call->conn;
2735273a191SDavid Howells 
27426cb02aaSDavid Howells 	msg.msg_name	= &call->peer->srx.transport;
27526cb02aaSDavid Howells 	msg.msg_namelen	= call->peer->srx.transport_len;
27626cb02aaSDavid Howells 	msg.msg_control	= NULL;
27726cb02aaSDavid Howells 	msg.msg_controllen = 0;
27826cb02aaSDavid Howells 	msg.msg_flags	= 0;
27926cb02aaSDavid Howells 
28026cb02aaSDavid Howells 	pkt.whdr.epoch		= htonl(conn->proto.epoch);
28126cb02aaSDavid Howells 	pkt.whdr.cid		= htonl(call->cid);
28226cb02aaSDavid Howells 	pkt.whdr.callNumber	= htonl(call->call_id);
28326cb02aaSDavid Howells 	pkt.whdr.seq		= 0;
28426cb02aaSDavid Howells 	pkt.whdr.type		= RXRPC_PACKET_TYPE_ABORT;
28526cb02aaSDavid Howells 	pkt.whdr.flags		= conn->out_clientflag;
28626cb02aaSDavid Howells 	pkt.whdr.userStatus	= 0;
28726cb02aaSDavid Howells 	pkt.whdr.securityIndex	= call->security_ix;
28826cb02aaSDavid Howells 	pkt.whdr._rsvd		= 0;
28926cb02aaSDavid Howells 	pkt.whdr.serviceId	= htons(call->service_id);
29026cb02aaSDavid Howells 	pkt.abort_code		= htonl(call->abort_code);
29126cb02aaSDavid Howells 
29226cb02aaSDavid Howells 	iov[0].iov_base	= &pkt;
29326cb02aaSDavid Howells 	iov[0].iov_len	= sizeof(pkt);
29426cb02aaSDavid Howells 
29526cb02aaSDavid Howells 	serial = atomic_inc_return(&conn->serial);
29626cb02aaSDavid Howells 	pkt.whdr.serial = htonl(serial);
29726cb02aaSDavid Howells 
29826cb02aaSDavid Howells 	ret = kernel_sendmsg(conn->params.local->socket,
29926cb02aaSDavid Howells 			     &msg, iov, 1, sizeof(pkt));
300330bdcfaSDavid Howells 	conn->params.peer->last_tx_at = ktime_get_seconds();
3016b47fe1dSDavid Howells 	if (ret < 0)
3026b47fe1dSDavid Howells 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
3034764c0daSDavid Howells 				    rxrpc_tx_point_call_abort);
3044764c0daSDavid Howells 	else
3054764c0daSDavid Howells 		trace_rxrpc_tx_packet(call->debug_id, &pkt.whdr,
3064764c0daSDavid Howells 				      rxrpc_tx_point_call_abort);
307c7e86acfSDavid Howells 	rxrpc_tx_backoff(call, ret);
30826cb02aaSDavid Howells 	return ret;
30926cb02aaSDavid Howells }
31026cb02aaSDavid Howells 
31126cb02aaSDavid Howells /*
3128c3e34a4SDavid Howells  * send a packet through the transport endpoint
3138c3e34a4SDavid Howells  */
314a1767077SDavid Howells int rxrpc_send_data_packet(struct rxrpc_call *call, struct sk_buff *skb,
315a1767077SDavid Howells 			   bool retrans)
3168c3e34a4SDavid Howells {
3175a924b89SDavid Howells 	struct rxrpc_connection *conn = call->conn;
3185a924b89SDavid Howells 	struct rxrpc_wire_header whdr;
3195a924b89SDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
3208c3e34a4SDavid Howells 	struct msghdr msg;
3215a924b89SDavid Howells 	struct kvec iov[2];
3225a924b89SDavid Howells 	rxrpc_serial_t serial;
3235a924b89SDavid Howells 	size_t len;
3248c3e34a4SDavid Howells 	int ret, opt;
3258c3e34a4SDavid Howells 
3268c3e34a4SDavid Howells 	_enter(",{%d}", skb->len);
3278c3e34a4SDavid Howells 
3285a924b89SDavid Howells 	/* Each transmission of a Tx packet needs a new serial number */
3295a924b89SDavid Howells 	serial = atomic_inc_return(&conn->serial);
3308c3e34a4SDavid Howells 
3315a924b89SDavid Howells 	whdr.epoch	= htonl(conn->proto.epoch);
3325a924b89SDavid Howells 	whdr.cid	= htonl(call->cid);
3335a924b89SDavid Howells 	whdr.callNumber	= htonl(call->call_id);
3345a924b89SDavid Howells 	whdr.seq	= htonl(sp->hdr.seq);
3355a924b89SDavid Howells 	whdr.serial	= htonl(serial);
3365a924b89SDavid Howells 	whdr.type	= RXRPC_PACKET_TYPE_DATA;
3375a924b89SDavid Howells 	whdr.flags	= sp->hdr.flags;
3385a924b89SDavid Howells 	whdr.userStatus	= 0;
3395a924b89SDavid Howells 	whdr.securityIndex = call->security_ix;
3405a924b89SDavid Howells 	whdr._rsvd	= htons(sp->hdr._rsvd);
3415a924b89SDavid Howells 	whdr.serviceId	= htons(call->service_id);
3425a924b89SDavid Howells 
3434e255721SDavid Howells 	if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) &&
3444e255721SDavid Howells 	    sp->hdr.seq == 1)
3454e255721SDavid Howells 		whdr.userStatus	= RXRPC_USERSTATUS_SERVICE_UPGRADE;
3464e255721SDavid Howells 
3475a924b89SDavid Howells 	iov[0].iov_base = &whdr;
3485a924b89SDavid Howells 	iov[0].iov_len = sizeof(whdr);
3495a924b89SDavid Howells 	iov[1].iov_base = skb->head;
3505a924b89SDavid Howells 	iov[1].iov_len = skb->len;
3515a924b89SDavid Howells 	len = iov[0].iov_len + iov[1].iov_len;
3525a924b89SDavid Howells 
3535a924b89SDavid Howells 	msg.msg_name = &call->peer->srx.transport;
3545a924b89SDavid Howells 	msg.msg_namelen = call->peer->srx.transport_len;
3558c3e34a4SDavid Howells 	msg.msg_control = NULL;
3568c3e34a4SDavid Howells 	msg.msg_controllen = 0;
3578c3e34a4SDavid Howells 	msg.msg_flags = 0;
3588c3e34a4SDavid Howells 
35957494343SDavid Howells 	/* If our RTT cache needs working on, request an ACK.  Also request
36057494343SDavid Howells 	 * ACKs if a DATA packet appears to have been lost.
361b604dd98SDavid Howells 	 *
362b604dd98SDavid Howells 	 * However, we mustn't request an ACK on the last reply packet of a
363b604dd98SDavid Howells 	 * service call, lest OpenAFS incorrectly send us an ACK with some
364b604dd98SDavid Howells 	 * soft-ACKs in it and then never follow up with a proper hard ACK.
36557494343SDavid Howells 	 */
366b604dd98SDavid Howells 	if ((!(sp->hdr.flags & RXRPC_LAST_PACKET) ||
367b604dd98SDavid Howells 	     rxrpc_to_server(sp)
368b604dd98SDavid Howells 	     ) &&
369bd1fdf8cSDavid Howells 	    (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events) ||
370bd1fdf8cSDavid Howells 	     retrans ||
371b112a670SDavid Howells 	     call->cong_mode == RXRPC_CALL_SLOW_START ||
372c410bf01SDavid Howells 	     (call->peer->rtt_count < 3 && sp->hdr.seq & 1) ||
3730d4b103cSDavid Howells 	     ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
374bf7d620aSDavid Howells 			  ktime_get_real())))
3750d4b103cSDavid Howells 		whdr.flags |= RXRPC_REQUEST_ACK;
3760d4b103cSDavid Howells 
3778a681c36SDavid Howells 	if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
3788a681c36SDavid Howells 		static int lose;
3798a681c36SDavid Howells 		if ((lose++ & 7) == 7) {
380a1767077SDavid Howells 			ret = 0;
381526949e8SArnd Bergmann 			trace_rxrpc_tx_data(call, sp->hdr.seq, serial,
382526949e8SArnd Bergmann 					    whdr.flags, retrans, true);
383526949e8SArnd Bergmann 			goto done;
3848a681c36SDavid Howells 		}
3858a681c36SDavid Howells 	}
3868a681c36SDavid Howells 
387526949e8SArnd Bergmann 	trace_rxrpc_tx_data(call, sp->hdr.seq, serial, whdr.flags, retrans,
388526949e8SArnd Bergmann 			    false);
3895a924b89SDavid Howells 
3908c3e34a4SDavid Howells 	/* send the packet with the don't fragment bit set if we currently
3918c3e34a4SDavid Howells 	 * think it's small enough */
3925a924b89SDavid Howells 	if (iov[1].iov_len >= call->peer->maxdata)
3935a924b89SDavid Howells 		goto send_fragmentable;
3945a924b89SDavid Howells 
395985a5c82SDavid Howells 	down_read(&conn->params.local->defrag_sem);
396b604dd98SDavid Howells 
397b604dd98SDavid Howells 	sp->hdr.serial = serial;
398b604dd98SDavid Howells 	smp_wmb(); /* Set serial before timestamp */
399b604dd98SDavid Howells 	skb->tstamp = ktime_get_real();
400b604dd98SDavid Howells 
4018c3e34a4SDavid Howells 	/* send the packet by UDP
4028c3e34a4SDavid Howells 	 * - returns -EMSGSIZE if UDP would have to fragment the packet
4038c3e34a4SDavid Howells 	 *   to go out of the interface
4048c3e34a4SDavid Howells 	 *   - in which case, we'll have processed the ICMP error
4058c3e34a4SDavid Howells 	 *     message and update the peer record
4068c3e34a4SDavid Howells 	 */
4075a924b89SDavid Howells 	ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
408330bdcfaSDavid Howells 	conn->params.peer->last_tx_at = ktime_get_seconds();
4098c3e34a4SDavid Howells 
410985a5c82SDavid Howells 	up_read(&conn->params.local->defrag_sem);
4116b47fe1dSDavid Howells 	if (ret < 0)
4126b47fe1dSDavid Howells 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
4134764c0daSDavid Howells 				    rxrpc_tx_point_call_data_nofrag);
4144764c0daSDavid Howells 	else
4154764c0daSDavid Howells 		trace_rxrpc_tx_packet(call->debug_id, &whdr,
4164764c0daSDavid Howells 				      rxrpc_tx_point_call_data_nofrag);
417c7e86acfSDavid Howells 	rxrpc_tx_backoff(call, ret);
4188c3e34a4SDavid Howells 	if (ret == -EMSGSIZE)
4198c3e34a4SDavid Howells 		goto send_fragmentable;
4208c3e34a4SDavid Howells 
4215a924b89SDavid Howells done:
42250235c4bSDavid Howells 	if (ret >= 0) {
4230d4b103cSDavid Howells 		if (whdr.flags & RXRPC_REQUEST_ACK) {
424b604dd98SDavid Howells 			call->peer->rtt_last_req = skb->tstamp;
42550235c4bSDavid Howells 			trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_data, serial);
426c410bf01SDavid Howells 			if (call->peer->rtt_count > 1) {
427bd1fdf8cSDavid Howells 				unsigned long nowj = jiffies, ack_lost_at;
428bd1fdf8cSDavid Howells 
429c410bf01SDavid Howells 				ack_lost_at = rxrpc_get_rto_backoff(call->peer, retrans);
430bd1fdf8cSDavid Howells 				ack_lost_at += nowj;
431bd1fdf8cSDavid Howells 				WRITE_ONCE(call->ack_lost_at, ack_lost_at);
432bd1fdf8cSDavid Howells 				rxrpc_reduce_call_timer(call, ack_lost_at, nowj,
433bd1fdf8cSDavid Howells 							rxrpc_timer_set_for_lost_ack);
434bd1fdf8cSDavid Howells 			}
4358c3e34a4SDavid Howells 		}
436c54e43d7SDavid Howells 
437c54e43d7SDavid Howells 		if (sp->hdr.seq == 1 &&
438c54e43d7SDavid Howells 		    !test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER,
439c54e43d7SDavid Howells 				      &call->flags)) {
440c54e43d7SDavid Howells 			unsigned long nowj = jiffies, expect_rx_by;
441c54e43d7SDavid Howells 
442c54e43d7SDavid Howells 			expect_rx_by = nowj + call->next_rx_timo;
443c54e43d7SDavid Howells 			WRITE_ONCE(call->expect_rx_by, expect_rx_by);
444c54e43d7SDavid Howells 			rxrpc_reduce_call_timer(call, expect_rx_by, nowj,
445c54e43d7SDavid Howells 						rxrpc_timer_set_for_normal);
446c54e43d7SDavid Howells 		}
447415f44e4SDavid Howells 
448415f44e4SDavid Howells 		rxrpc_set_keepalive(call);
449c7e86acfSDavid Howells 	} else {
450c7e86acfSDavid Howells 		/* Cancel the call if the initial transmission fails,
451c7e86acfSDavid Howells 		 * particularly if that's due to network routing issues that
452c7e86acfSDavid Howells 		 * aren't going away anytime soon.  The layer above can arrange
453c7e86acfSDavid Howells 		 * the retransmission.
454c7e86acfSDavid Howells 		 */
455c7e86acfSDavid Howells 		if (!test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER, &call->flags))
456c7e86acfSDavid Howells 			rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
457c7e86acfSDavid Howells 						  RX_USER_ABORT, ret);
458c7e86acfSDavid Howells 	}
459415f44e4SDavid Howells 
4605a924b89SDavid Howells 	_leave(" = %d [%u]", ret, call->peer->maxdata);
4615a924b89SDavid Howells 	return ret;
4628c3e34a4SDavid Howells 
4638c3e34a4SDavid Howells send_fragmentable:
4648c3e34a4SDavid Howells 	/* attempt to send this message with fragmentation enabled */
4658c3e34a4SDavid Howells 	_debug("send fragment");
4668c3e34a4SDavid Howells 
467985a5c82SDavid Howells 	down_write(&conn->params.local->defrag_sem);
468985a5c82SDavid Howells 
469b604dd98SDavid Howells 	sp->hdr.serial = serial;
470b604dd98SDavid Howells 	smp_wmb(); /* Set serial before timestamp */
471b604dd98SDavid Howells 	skb->tstamp = ktime_get_real();
472b604dd98SDavid Howells 
473985a5c82SDavid Howells 	switch (conn->params.local->srx.transport.family) {
4740e631eeeSDavid Howells 	case AF_INET6:
475985a5c82SDavid Howells 	case AF_INET:
4768c3e34a4SDavid Howells 		opt = IP_PMTUDISC_DONT;
4770e631eeeSDavid Howells 		kernel_setsockopt(conn->params.local->socket,
478985a5c82SDavid Howells 				  SOL_IP, IP_MTU_DISCOVER,
4798c3e34a4SDavid Howells 				  (char *)&opt, sizeof(opt));
4805a924b89SDavid Howells 		ret = kernel_sendmsg(conn->params.local->socket, &msg,
4815a924b89SDavid Howells 				     iov, 2, len);
482330bdcfaSDavid Howells 		conn->params.peer->last_tx_at = ktime_get_seconds();
4838c3e34a4SDavid Howells 
4848c3e34a4SDavid Howells 		opt = IP_PMTUDISC_DO;
48575b54cb5SDavid Howells 		kernel_setsockopt(conn->params.local->socket,
4860e631eeeSDavid Howells 				  SOL_IP, IP_MTU_DISCOVER,
48775b54cb5SDavid Howells 				  (char *)&opt, sizeof(opt));
48875b54cb5SDavid Howells 		break;
4893427beb6SDavid Howells 
4903427beb6SDavid Howells 	default:
4913427beb6SDavid Howells 		BUG();
4928c3e34a4SDavid Howells 	}
4938c3e34a4SDavid Howells 
4946b47fe1dSDavid Howells 	if (ret < 0)
4956b47fe1dSDavid Howells 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
4964764c0daSDavid Howells 				    rxrpc_tx_point_call_data_frag);
4974764c0daSDavid Howells 	else
4984764c0daSDavid Howells 		trace_rxrpc_tx_packet(call->debug_id, &whdr,
4994764c0daSDavid Howells 				      rxrpc_tx_point_call_data_frag);
500c7e86acfSDavid Howells 	rxrpc_tx_backoff(call, ret);
5016b47fe1dSDavid Howells 
502985a5c82SDavid Howells 	up_write(&conn->params.local->defrag_sem);
5035a924b89SDavid Howells 	goto done;
5048c3e34a4SDavid Howells }
505248f219cSDavid Howells 
506248f219cSDavid Howells /*
507248f219cSDavid Howells  * reject packets through the local endpoint
508248f219cSDavid Howells  */
509248f219cSDavid Howells void rxrpc_reject_packets(struct rxrpc_local *local)
510248f219cSDavid Howells {
5111c2bc7b9SDavid Howells 	struct sockaddr_rxrpc srx;
512248f219cSDavid Howells 	struct rxrpc_skb_priv *sp;
513248f219cSDavid Howells 	struct rxrpc_wire_header whdr;
514248f219cSDavid Howells 	struct sk_buff *skb;
515248f219cSDavid Howells 	struct msghdr msg;
516248f219cSDavid Howells 	struct kvec iov[2];
517248f219cSDavid Howells 	size_t size;
518248f219cSDavid Howells 	__be32 code;
519ece64fecSDavid Howells 	int ret, ioc;
520248f219cSDavid Howells 
521248f219cSDavid Howells 	_enter("%d", local->debug_id);
522248f219cSDavid Howells 
523248f219cSDavid Howells 	iov[0].iov_base = &whdr;
524248f219cSDavid Howells 	iov[0].iov_len = sizeof(whdr);
525248f219cSDavid Howells 	iov[1].iov_base = &code;
526248f219cSDavid Howells 	iov[1].iov_len = sizeof(code);
527248f219cSDavid Howells 
5281c2bc7b9SDavid Howells 	msg.msg_name = &srx.transport;
529248f219cSDavid Howells 	msg.msg_control = NULL;
530248f219cSDavid Howells 	msg.msg_controllen = 0;
531248f219cSDavid Howells 	msg.msg_flags = 0;
532248f219cSDavid Howells 
533248f219cSDavid Howells 	memset(&whdr, 0, sizeof(whdr));
534248f219cSDavid Howells 
535248f219cSDavid Howells 	while ((skb = skb_dequeue(&local->reject_queue))) {
536987db9f7SDavid Howells 		rxrpc_see_skb(skb, rxrpc_skb_seen);
537248f219cSDavid Howells 		sp = rxrpc_skb(skb);
5381c2bc7b9SDavid Howells 
539ece64fecSDavid Howells 		switch (skb->mark) {
540ece64fecSDavid Howells 		case RXRPC_SKB_MARK_REJECT_BUSY:
541ece64fecSDavid Howells 			whdr.type = RXRPC_PACKET_TYPE_BUSY;
542ece64fecSDavid Howells 			size = sizeof(whdr);
543ece64fecSDavid Howells 			ioc = 1;
544ece64fecSDavid Howells 			break;
545ece64fecSDavid Howells 		case RXRPC_SKB_MARK_REJECT_ABORT:
546ece64fecSDavid Howells 			whdr.type = RXRPC_PACKET_TYPE_ABORT;
547ece64fecSDavid Howells 			code = htonl(skb->priority);
548ece64fecSDavid Howells 			size = sizeof(whdr) + sizeof(code);
549ece64fecSDavid Howells 			ioc = 2;
550ece64fecSDavid Howells 			break;
551ece64fecSDavid Howells 		default:
552987db9f7SDavid Howells 			rxrpc_free_skb(skb, rxrpc_skb_freed);
553ece64fecSDavid Howells 			continue;
554ece64fecSDavid Howells 		}
555ece64fecSDavid Howells 
5565a790b73SDavid Howells 		if (rxrpc_extract_addr_from_skb(&srx, skb) == 0) {
5571c2bc7b9SDavid Howells 			msg.msg_namelen = srx.transport_len;
5581c2bc7b9SDavid Howells 
559248f219cSDavid Howells 			whdr.epoch	= htonl(sp->hdr.epoch);
560248f219cSDavid Howells 			whdr.cid	= htonl(sp->hdr.cid);
561248f219cSDavid Howells 			whdr.callNumber	= htonl(sp->hdr.callNumber);
562248f219cSDavid Howells 			whdr.serviceId	= htons(sp->hdr.serviceId);
563248f219cSDavid Howells 			whdr.flags	= sp->hdr.flags;
564248f219cSDavid Howells 			whdr.flags	^= RXRPC_CLIENT_INITIATED;
565248f219cSDavid Howells 			whdr.flags	&= RXRPC_CLIENT_INITIATED;
566248f219cSDavid Howells 
567d6672a5aSYueHaibing 			ret = kernel_sendmsg(local->socket, &msg,
568d6672a5aSYueHaibing 					     iov, ioc, size);
5696b47fe1dSDavid Howells 			if (ret < 0)
5706b47fe1dSDavid Howells 				trace_rxrpc_tx_fail(local->debug_id, 0, ret,
5714764c0daSDavid Howells 						    rxrpc_tx_point_reject);
5724764c0daSDavid Howells 			else
5734764c0daSDavid Howells 				trace_rxrpc_tx_packet(local->debug_id, &whdr,
5744764c0daSDavid Howells 						      rxrpc_tx_point_reject);
575248f219cSDavid Howells 		}
576248f219cSDavid Howells 
577987db9f7SDavid Howells 		rxrpc_free_skb(skb, rxrpc_skb_freed);
578248f219cSDavid Howells 	}
579248f219cSDavid Howells 
580248f219cSDavid Howells 	_leave("");
581248f219cSDavid Howells }
582ace45becSDavid Howells 
583ace45becSDavid Howells /*
584ace45becSDavid Howells  * Send a VERSION reply to a peer as a keepalive.
585ace45becSDavid Howells  */
586ace45becSDavid Howells void rxrpc_send_keepalive(struct rxrpc_peer *peer)
587ace45becSDavid Howells {
588ace45becSDavid Howells 	struct rxrpc_wire_header whdr;
589ace45becSDavid Howells 	struct msghdr msg;
590ace45becSDavid Howells 	struct kvec iov[2];
591ace45becSDavid Howells 	size_t len;
592ace45becSDavid Howells 	int ret;
593ace45becSDavid Howells 
594ace45becSDavid Howells 	_enter("");
595ace45becSDavid Howells 
596ace45becSDavid Howells 	msg.msg_name	= &peer->srx.transport;
597ace45becSDavid Howells 	msg.msg_namelen	= peer->srx.transport_len;
598ace45becSDavid Howells 	msg.msg_control	= NULL;
599ace45becSDavid Howells 	msg.msg_controllen = 0;
600ace45becSDavid Howells 	msg.msg_flags	= 0;
601ace45becSDavid Howells 
602ace45becSDavid Howells 	whdr.epoch	= htonl(peer->local->rxnet->epoch);
603ace45becSDavid Howells 	whdr.cid	= 0;
604ace45becSDavid Howells 	whdr.callNumber	= 0;
605ace45becSDavid Howells 	whdr.seq	= 0;
606ace45becSDavid Howells 	whdr.serial	= 0;
607ace45becSDavid Howells 	whdr.type	= RXRPC_PACKET_TYPE_VERSION; /* Not client-initiated */
608ace45becSDavid Howells 	whdr.flags	= RXRPC_LAST_PACKET;
609ace45becSDavid Howells 	whdr.userStatus	= 0;
610ace45becSDavid Howells 	whdr.securityIndex = 0;
611ace45becSDavid Howells 	whdr._rsvd	= 0;
612ace45becSDavid Howells 	whdr.serviceId	= 0;
613ace45becSDavid Howells 
614ace45becSDavid Howells 	iov[0].iov_base	= &whdr;
615ace45becSDavid Howells 	iov[0].iov_len	= sizeof(whdr);
616ace45becSDavid Howells 	iov[1].iov_base	= (char *)rxrpc_keepalive_string;
617ace45becSDavid Howells 	iov[1].iov_len	= sizeof(rxrpc_keepalive_string);
618ace45becSDavid Howells 
619ace45becSDavid Howells 	len = iov[0].iov_len + iov[1].iov_len;
620ace45becSDavid Howells 
621ace45becSDavid Howells 	_proto("Tx VERSION (keepalive)");
622ace45becSDavid Howells 
623ace45becSDavid Howells 	ret = kernel_sendmsg(peer->local->socket, &msg, iov, 2, len);
624ace45becSDavid Howells 	if (ret < 0)
6256b47fe1dSDavid Howells 		trace_rxrpc_tx_fail(peer->debug_id, 0, ret,
6264764c0daSDavid Howells 				    rxrpc_tx_point_version_keepalive);
6274764c0daSDavid Howells 	else
6284764c0daSDavid Howells 		trace_rxrpc_tx_packet(peer->debug_id, &whdr,
6294764c0daSDavid Howells 				      rxrpc_tx_point_version_keepalive);
630ace45becSDavid Howells 
631330bdcfaSDavid Howells 	peer->last_tx_at = ktime_get_seconds();
632ace45becSDavid Howells 	_leave("");
633ace45becSDavid Howells }
634