xref: /openbmc/linux/net/rxrpc/output.c (revision 4764c0da)
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);
1864764c0daSDavid Howells 	trace_rxrpc_tx_ack(call->debug_id, 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();
2136b47fe1dSDavid Howells 	if (ret < 0)
2146b47fe1dSDavid Howells 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
2154764c0daSDavid Howells 				    rxrpc_tx_point_call_ack);
2164764c0daSDavid Howells 	else
2174764c0daSDavid Howells 		trace_rxrpc_tx_packet(call->debug_id, &pkt->whdr,
2184764c0daSDavid Howells 				      rxrpc_tx_point_call_ack);
2198d94aa38SDavid Howells 
22026cb02aaSDavid Howells 	if (call->state < RXRPC_CALL_COMPLETE) {
221805b21b9SDavid Howells 		if (ret < 0) {
222a5af7e1fSDavid Howells 			if (ping)
2238e83134dSDavid Howells 				clear_bit(RXRPC_CALL_PINGING, &call->flags);
224248f219cSDavid Howells 			rxrpc_propose_ACK(call, pkt->ack.reason,
225248f219cSDavid Howells 					  ntohs(pkt->ack.maxSkew),
226248f219cSDavid Howells 					  ntohl(pkt->ack.serial),
2279c7ad434SDavid Howells 					  true, true,
2289c7ad434SDavid Howells 					  rxrpc_propose_ack_retry_tx);
229805b21b9SDavid Howells 		} else {
230805b21b9SDavid Howells 			spin_lock_bh(&call->lock);
231805b21b9SDavid Howells 			if (after(hard_ack, call->ackr_consumed))
232805b21b9SDavid Howells 				call->ackr_consumed = hard_ack;
233805b21b9SDavid Howells 			if (after(top, call->ackr_seen))
234805b21b9SDavid Howells 				call->ackr_seen = top;
235805b21b9SDavid Howells 			spin_unlock_bh(&call->lock);
236248f219cSDavid Howells 		}
237415f44e4SDavid Howells 
238415f44e4SDavid Howells 		rxrpc_set_keepalive(call);
239248f219cSDavid Howells 	}
240248f219cSDavid Howells 
2418d94aa38SDavid Howells out:
2428d94aa38SDavid Howells 	rxrpc_put_connection(conn);
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 {
25226cb02aaSDavid Howells 	struct rxrpc_connection *conn = NULL;
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 
26926cb02aaSDavid Howells 	spin_lock_bh(&call->lock);
27026cb02aaSDavid Howells 	if (call->conn)
27126cb02aaSDavid Howells 		conn = rxrpc_get_connection_maybe(call->conn);
27226cb02aaSDavid Howells 	spin_unlock_bh(&call->lock);
27326cb02aaSDavid Howells 	if (!conn)
27426cb02aaSDavid Howells 		return -ECONNRESET;
27526cb02aaSDavid Howells 
27626cb02aaSDavid Howells 	msg.msg_name	= &call->peer->srx.transport;
27726cb02aaSDavid Howells 	msg.msg_namelen	= call->peer->srx.transport_len;
27826cb02aaSDavid Howells 	msg.msg_control	= NULL;
27926cb02aaSDavid Howells 	msg.msg_controllen = 0;
28026cb02aaSDavid Howells 	msg.msg_flags	= 0;
28126cb02aaSDavid Howells 
28226cb02aaSDavid Howells 	pkt.whdr.epoch		= htonl(conn->proto.epoch);
28326cb02aaSDavid Howells 	pkt.whdr.cid		= htonl(call->cid);
28426cb02aaSDavid Howells 	pkt.whdr.callNumber	= htonl(call->call_id);
28526cb02aaSDavid Howells 	pkt.whdr.seq		= 0;
28626cb02aaSDavid Howells 	pkt.whdr.type		= RXRPC_PACKET_TYPE_ABORT;
28726cb02aaSDavid Howells 	pkt.whdr.flags		= conn->out_clientflag;
28826cb02aaSDavid Howells 	pkt.whdr.userStatus	= 0;
28926cb02aaSDavid Howells 	pkt.whdr.securityIndex	= call->security_ix;
29026cb02aaSDavid Howells 	pkt.whdr._rsvd		= 0;
29126cb02aaSDavid Howells 	pkt.whdr.serviceId	= htons(call->service_id);
29226cb02aaSDavid Howells 	pkt.abort_code		= htonl(call->abort_code);
29326cb02aaSDavid Howells 
29426cb02aaSDavid Howells 	iov[0].iov_base	= &pkt;
29526cb02aaSDavid Howells 	iov[0].iov_len	= sizeof(pkt);
29626cb02aaSDavid Howells 
29726cb02aaSDavid Howells 	serial = atomic_inc_return(&conn->serial);
29826cb02aaSDavid Howells 	pkt.whdr.serial = htonl(serial);
29926cb02aaSDavid Howells 
30026cb02aaSDavid Howells 	ret = kernel_sendmsg(conn->params.local->socket,
30126cb02aaSDavid Howells 			     &msg, iov, 1, sizeof(pkt));
302ace45becSDavid Howells 	conn->params.peer->last_tx_at = ktime_get_real();
3036b47fe1dSDavid Howells 	if (ret < 0)
3046b47fe1dSDavid Howells 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
3054764c0daSDavid Howells 				    rxrpc_tx_point_call_abort);
3064764c0daSDavid Howells 	else
3074764c0daSDavid Howells 		trace_rxrpc_tx_packet(call->debug_id, &pkt.whdr,
3084764c0daSDavid Howells 				      rxrpc_tx_point_call_abort);
3096b47fe1dSDavid Howells 
31026cb02aaSDavid Howells 
31126cb02aaSDavid Howells 	rxrpc_put_connection(conn);
31226cb02aaSDavid Howells 	return ret;
31326cb02aaSDavid Howells }
31426cb02aaSDavid Howells 
31526cb02aaSDavid Howells /*
3168c3e34a4SDavid Howells  * send a packet through the transport endpoint
3178c3e34a4SDavid Howells  */
318a1767077SDavid Howells int rxrpc_send_data_packet(struct rxrpc_call *call, struct sk_buff *skb,
319a1767077SDavid Howells 			   bool retrans)
3208c3e34a4SDavid Howells {
3215a924b89SDavid Howells 	struct rxrpc_connection *conn = call->conn;
3225a924b89SDavid Howells 	struct rxrpc_wire_header whdr;
3235a924b89SDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
3248c3e34a4SDavid Howells 	struct msghdr msg;
3255a924b89SDavid Howells 	struct kvec iov[2];
3265a924b89SDavid Howells 	rxrpc_serial_t serial;
3275a924b89SDavid Howells 	size_t len;
328a1767077SDavid Howells 	bool lost = false;
3298c3e34a4SDavid Howells 	int ret, opt;
3308c3e34a4SDavid Howells 
3318c3e34a4SDavid Howells 	_enter(",{%d}", skb->len);
3328c3e34a4SDavid Howells 
3335a924b89SDavid Howells 	/* Each transmission of a Tx packet needs a new serial number */
3345a924b89SDavid Howells 	serial = atomic_inc_return(&conn->serial);
3358c3e34a4SDavid Howells 
3365a924b89SDavid Howells 	whdr.epoch	= htonl(conn->proto.epoch);
3375a924b89SDavid Howells 	whdr.cid	= htonl(call->cid);
3385a924b89SDavid Howells 	whdr.callNumber	= htonl(call->call_id);
3395a924b89SDavid Howells 	whdr.seq	= htonl(sp->hdr.seq);
3405a924b89SDavid Howells 	whdr.serial	= htonl(serial);
3415a924b89SDavid Howells 	whdr.type	= RXRPC_PACKET_TYPE_DATA;
3425a924b89SDavid Howells 	whdr.flags	= sp->hdr.flags;
3435a924b89SDavid Howells 	whdr.userStatus	= 0;
3445a924b89SDavid Howells 	whdr.securityIndex = call->security_ix;
3455a924b89SDavid Howells 	whdr._rsvd	= htons(sp->hdr._rsvd);
3465a924b89SDavid Howells 	whdr.serviceId	= htons(call->service_id);
3475a924b89SDavid Howells 
3484e255721SDavid Howells 	if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) &&
3494e255721SDavid Howells 	    sp->hdr.seq == 1)
3504e255721SDavid Howells 		whdr.userStatus	= RXRPC_USERSTATUS_SERVICE_UPGRADE;
3514e255721SDavid Howells 
3525a924b89SDavid Howells 	iov[0].iov_base = &whdr;
3535a924b89SDavid Howells 	iov[0].iov_len = sizeof(whdr);
3545a924b89SDavid Howells 	iov[1].iov_base = skb->head;
3555a924b89SDavid Howells 	iov[1].iov_len = skb->len;
3565a924b89SDavid Howells 	len = iov[0].iov_len + iov[1].iov_len;
3575a924b89SDavid Howells 
3585a924b89SDavid Howells 	msg.msg_name = &call->peer->srx.transport;
3595a924b89SDavid Howells 	msg.msg_namelen = call->peer->srx.transport_len;
3608c3e34a4SDavid Howells 	msg.msg_control = NULL;
3618c3e34a4SDavid Howells 	msg.msg_controllen = 0;
3628c3e34a4SDavid Howells 	msg.msg_flags = 0;
3638c3e34a4SDavid Howells 
36457494343SDavid Howells 	/* If our RTT cache needs working on, request an ACK.  Also request
36557494343SDavid Howells 	 * ACKs if a DATA packet appears to have been lost.
36657494343SDavid Howells 	 */
367bf7d620aSDavid Howells 	if (!(sp->hdr.flags & RXRPC_LAST_PACKET) &&
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);
3938c3e34a4SDavid Howells 	/* send the packet by UDP
3948c3e34a4SDavid Howells 	 * - returns -EMSGSIZE if UDP would have to fragment the packet
3958c3e34a4SDavid Howells 	 *   to go out of the interface
3968c3e34a4SDavid Howells 	 *   - in which case, we'll have processed the ICMP error
3978c3e34a4SDavid Howells 	 *     message and update the peer record
3988c3e34a4SDavid Howells 	 */
3995a924b89SDavid Howells 	ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
400ace45becSDavid Howells 	conn->params.peer->last_tx_at = ktime_get_real();
4018c3e34a4SDavid Howells 
402985a5c82SDavid Howells 	up_read(&conn->params.local->defrag_sem);
4036b47fe1dSDavid Howells 	if (ret < 0)
4046b47fe1dSDavid Howells 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
4054764c0daSDavid Howells 				    rxrpc_tx_point_call_data_nofrag);
4064764c0daSDavid Howells 	else
4074764c0daSDavid Howells 		trace_rxrpc_tx_packet(call->debug_id, &whdr,
4084764c0daSDavid Howells 				      rxrpc_tx_point_call_data_nofrag);
4098c3e34a4SDavid Howells 	if (ret == -EMSGSIZE)
4108c3e34a4SDavid Howells 		goto send_fragmentable;
4118c3e34a4SDavid Howells 
4125a924b89SDavid Howells done:
413a1767077SDavid Howells 	trace_rxrpc_tx_data(call, sp->hdr.seq, serial, whdr.flags,
414a1767077SDavid Howells 			    retrans, lost);
41550235c4bSDavid Howells 	if (ret >= 0) {
4160d4b103cSDavid Howells 		ktime_t now = ktime_get_real();
4170d4b103cSDavid Howells 		skb->tstamp = now;
41850235c4bSDavid Howells 		smp_wmb();
4195a924b89SDavid Howells 		sp->hdr.serial = serial;
4200d4b103cSDavid Howells 		if (whdr.flags & RXRPC_REQUEST_ACK) {
4210d4b103cSDavid Howells 			call->peer->rtt_last_req = now;
42250235c4bSDavid Howells 			trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_data, serial);
423bd1fdf8cSDavid Howells 			if (call->peer->rtt_usage > 1) {
424bd1fdf8cSDavid Howells 				unsigned long nowj = jiffies, ack_lost_at;
425bd1fdf8cSDavid Howells 
426bd1fdf8cSDavid Howells 				ack_lost_at = nsecs_to_jiffies(2 * call->peer->rtt);
427bd1fdf8cSDavid Howells 				if (ack_lost_at < 1)
428bd1fdf8cSDavid Howells 					ack_lost_at = 1;
429bd1fdf8cSDavid Howells 
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 		}
4470d4b103cSDavid Howells 	}
448415f44e4SDavid Howells 
449415f44e4SDavid Howells 	rxrpc_set_keepalive(call);
450415f44e4SDavid Howells 
4515a924b89SDavid Howells 	_leave(" = %d [%u]", ret, call->peer->maxdata);
4525a924b89SDavid Howells 	return ret;
4538c3e34a4SDavid Howells 
4548c3e34a4SDavid Howells send_fragmentable:
4558c3e34a4SDavid Howells 	/* attempt to send this message with fragmentation enabled */
4568c3e34a4SDavid Howells 	_debug("send fragment");
4578c3e34a4SDavid Howells 
458985a5c82SDavid Howells 	down_write(&conn->params.local->defrag_sem);
459985a5c82SDavid Howells 
460985a5c82SDavid Howells 	switch (conn->params.local->srx.transport.family) {
461985a5c82SDavid Howells 	case AF_INET:
4628c3e34a4SDavid Howells 		opt = IP_PMTUDISC_DONT;
463985a5c82SDavid Howells 		ret = kernel_setsockopt(conn->params.local->socket,
464985a5c82SDavid Howells 					SOL_IP, IP_MTU_DISCOVER,
4658c3e34a4SDavid Howells 					(char *)&opt, sizeof(opt));
4668c3e34a4SDavid Howells 		if (ret == 0) {
4675a924b89SDavid Howells 			ret = kernel_sendmsg(conn->params.local->socket, &msg,
4685a924b89SDavid Howells 					     iov, 2, len);
469ace45becSDavid Howells 			conn->params.peer->last_tx_at = ktime_get_real();
4708c3e34a4SDavid Howells 
4718c3e34a4SDavid Howells 			opt = IP_PMTUDISC_DO;
472985a5c82SDavid Howells 			kernel_setsockopt(conn->params.local->socket, SOL_IP,
473985a5c82SDavid Howells 					  IP_MTU_DISCOVER,
474985a5c82SDavid Howells 					  (char *)&opt, sizeof(opt));
475985a5c82SDavid Howells 		}
476985a5c82SDavid Howells 		break;
47775b54cb5SDavid Howells 
478d1912747SDavid Howells #ifdef CONFIG_AF_RXRPC_IPV6
47975b54cb5SDavid Howells 	case AF_INET6:
48075b54cb5SDavid Howells 		opt = IPV6_PMTUDISC_DONT;
48175b54cb5SDavid Howells 		ret = kernel_setsockopt(conn->params.local->socket,
48275b54cb5SDavid Howells 					SOL_IPV6, IPV6_MTU_DISCOVER,
48375b54cb5SDavid Howells 					(char *)&opt, sizeof(opt));
48475b54cb5SDavid Howells 		if (ret == 0) {
48575b54cb5SDavid Howells 			ret = kernel_sendmsg(conn->params.local->socket, &msg,
48693c62c45SDavid Howells 					     iov, 2, len);
487ace45becSDavid Howells 			conn->params.peer->last_tx_at = ktime_get_real();
48875b54cb5SDavid Howells 
48975b54cb5SDavid Howells 			opt = IPV6_PMTUDISC_DO;
49075b54cb5SDavid Howells 			kernel_setsockopt(conn->params.local->socket,
49175b54cb5SDavid Howells 					  SOL_IPV6, IPV6_MTU_DISCOVER,
49275b54cb5SDavid Howells 					  (char *)&opt, sizeof(opt));
49375b54cb5SDavid Howells 		}
49475b54cb5SDavid Howells 		break;
495d1912747SDavid Howells #endif
4968c3e34a4SDavid Howells 	}
4978c3e34a4SDavid Howells 
4986b47fe1dSDavid Howells 	if (ret < 0)
4996b47fe1dSDavid Howells 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
5004764c0daSDavid Howells 				    rxrpc_tx_point_call_data_frag);
5014764c0daSDavid Howells 	else
5024764c0daSDavid Howells 		trace_rxrpc_tx_packet(call->debug_id, &whdr,
5034764c0daSDavid Howells 				      rxrpc_tx_point_call_data_frag);
5046b47fe1dSDavid Howells 
505985a5c82SDavid Howells 	up_write(&conn->params.local->defrag_sem);
5065a924b89SDavid Howells 	goto done;
5078c3e34a4SDavid Howells }
508248f219cSDavid Howells 
509248f219cSDavid Howells /*
510248f219cSDavid Howells  * reject packets through the local endpoint
511248f219cSDavid Howells  */
512248f219cSDavid Howells void rxrpc_reject_packets(struct rxrpc_local *local)
513248f219cSDavid Howells {
5141c2bc7b9SDavid Howells 	struct sockaddr_rxrpc srx;
515248f219cSDavid Howells 	struct rxrpc_skb_priv *sp;
516248f219cSDavid Howells 	struct rxrpc_wire_header whdr;
517248f219cSDavid Howells 	struct sk_buff *skb;
518248f219cSDavid Howells 	struct msghdr msg;
519248f219cSDavid Howells 	struct kvec iov[2];
520248f219cSDavid Howells 	size_t size;
521248f219cSDavid Howells 	__be32 code;
5226b47fe1dSDavid Howells 	int ret;
523248f219cSDavid Howells 
524248f219cSDavid Howells 	_enter("%d", local->debug_id);
525248f219cSDavid Howells 
526248f219cSDavid Howells 	iov[0].iov_base = &whdr;
527248f219cSDavid Howells 	iov[0].iov_len = sizeof(whdr);
528248f219cSDavid Howells 	iov[1].iov_base = &code;
529248f219cSDavid Howells 	iov[1].iov_len = sizeof(code);
530248f219cSDavid Howells 	size = sizeof(whdr) + sizeof(code);
531248f219cSDavid Howells 
5321c2bc7b9SDavid Howells 	msg.msg_name = &srx.transport;
533248f219cSDavid Howells 	msg.msg_control = NULL;
534248f219cSDavid Howells 	msg.msg_controllen = 0;
535248f219cSDavid Howells 	msg.msg_flags = 0;
536248f219cSDavid Howells 
537248f219cSDavid Howells 	memset(&whdr, 0, sizeof(whdr));
538248f219cSDavid Howells 	whdr.type = RXRPC_PACKET_TYPE_ABORT;
539248f219cSDavid Howells 
540248f219cSDavid Howells 	while ((skb = skb_dequeue(&local->reject_queue))) {
54171f3ca40SDavid Howells 		rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
542248f219cSDavid Howells 		sp = rxrpc_skb(skb);
5431c2bc7b9SDavid Howells 
5447b674e39SDavid Howells 		if (rxrpc_extract_addr_from_skb(local, &srx, skb) == 0) {
5451c2bc7b9SDavid Howells 			msg.msg_namelen = srx.transport_len;
5461c2bc7b9SDavid Howells 
547248f219cSDavid Howells 			code = htonl(skb->priority);
548248f219cSDavid Howells 
549248f219cSDavid Howells 			whdr.epoch	= htonl(sp->hdr.epoch);
550248f219cSDavid Howells 			whdr.cid	= htonl(sp->hdr.cid);
551248f219cSDavid Howells 			whdr.callNumber	= htonl(sp->hdr.callNumber);
552248f219cSDavid Howells 			whdr.serviceId	= htons(sp->hdr.serviceId);
553248f219cSDavid Howells 			whdr.flags	= sp->hdr.flags;
554248f219cSDavid Howells 			whdr.flags	^= RXRPC_CLIENT_INITIATED;
555248f219cSDavid Howells 			whdr.flags	&= RXRPC_CLIENT_INITIATED;
556248f219cSDavid Howells 
5576b47fe1dSDavid Howells 			ret = kernel_sendmsg(local->socket, &msg, iov, 2, size);
5586b47fe1dSDavid Howells 			if (ret < 0)
5596b47fe1dSDavid Howells 				trace_rxrpc_tx_fail(local->debug_id, 0, ret,
5604764c0daSDavid Howells 						    rxrpc_tx_point_reject);
5614764c0daSDavid Howells 			else
5624764c0daSDavid Howells 				trace_rxrpc_tx_packet(local->debug_id, &whdr,
5634764c0daSDavid Howells 						      rxrpc_tx_point_reject);
564248f219cSDavid Howells 		}
565248f219cSDavid Howells 
56671f3ca40SDavid Howells 		rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
567248f219cSDavid Howells 	}
568248f219cSDavid Howells 
569248f219cSDavid Howells 	_leave("");
570248f219cSDavid Howells }
571ace45becSDavid Howells 
572ace45becSDavid Howells /*
573ace45becSDavid Howells  * Send a VERSION reply to a peer as a keepalive.
574ace45becSDavid Howells  */
575ace45becSDavid Howells void rxrpc_send_keepalive(struct rxrpc_peer *peer)
576ace45becSDavid Howells {
577ace45becSDavid Howells 	struct rxrpc_wire_header whdr;
578ace45becSDavid Howells 	struct msghdr msg;
579ace45becSDavid Howells 	struct kvec iov[2];
580ace45becSDavid Howells 	size_t len;
581ace45becSDavid Howells 	int ret;
582ace45becSDavid Howells 
583ace45becSDavid Howells 	_enter("");
584ace45becSDavid Howells 
585ace45becSDavid Howells 	msg.msg_name	= &peer->srx.transport;
586ace45becSDavid Howells 	msg.msg_namelen	= peer->srx.transport_len;
587ace45becSDavid Howells 	msg.msg_control	= NULL;
588ace45becSDavid Howells 	msg.msg_controllen = 0;
589ace45becSDavid Howells 	msg.msg_flags	= 0;
590ace45becSDavid Howells 
591ace45becSDavid Howells 	whdr.epoch	= htonl(peer->local->rxnet->epoch);
592ace45becSDavid Howells 	whdr.cid	= 0;
593ace45becSDavid Howells 	whdr.callNumber	= 0;
594ace45becSDavid Howells 	whdr.seq	= 0;
595ace45becSDavid Howells 	whdr.serial	= 0;
596ace45becSDavid Howells 	whdr.type	= RXRPC_PACKET_TYPE_VERSION; /* Not client-initiated */
597ace45becSDavid Howells 	whdr.flags	= RXRPC_LAST_PACKET;
598ace45becSDavid Howells 	whdr.userStatus	= 0;
599ace45becSDavid Howells 	whdr.securityIndex = 0;
600ace45becSDavid Howells 	whdr._rsvd	= 0;
601ace45becSDavid Howells 	whdr.serviceId	= 0;
602ace45becSDavid Howells 
603ace45becSDavid Howells 	iov[0].iov_base	= &whdr;
604ace45becSDavid Howells 	iov[0].iov_len	= sizeof(whdr);
605ace45becSDavid Howells 	iov[1].iov_base	= (char *)rxrpc_keepalive_string;
606ace45becSDavid Howells 	iov[1].iov_len	= sizeof(rxrpc_keepalive_string);
607ace45becSDavid Howells 
608ace45becSDavid Howells 	len = iov[0].iov_len + iov[1].iov_len;
609ace45becSDavid Howells 
610ace45becSDavid Howells 	_proto("Tx VERSION (keepalive)");
611ace45becSDavid Howells 
612ace45becSDavid Howells 	ret = kernel_sendmsg(peer->local->socket, &msg, iov, 2, len);
613ace45becSDavid Howells 	if (ret < 0)
6146b47fe1dSDavid Howells 		trace_rxrpc_tx_fail(peer->debug_id, 0, ret,
6154764c0daSDavid Howells 				    rxrpc_tx_point_version_keepalive);
6164764c0daSDavid Howells 	else
6174764c0daSDavid Howells 		trace_rxrpc_tx_packet(peer->debug_id, &whdr,
6184764c0daSDavid Howells 				      rxrpc_tx_point_version_keepalive);
619ace45becSDavid Howells 
620ace45becSDavid Howells 	peer->last_tx_at = ktime_get_real();
621ace45becSDavid Howells 	_leave("");
622ace45becSDavid Howells }
623