xref: /linux/net/rxrpc/output.c (revision b1a54551dd9ed5ef1763b97b35a0999ca002b95c)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RxRPC packet transmission
3  *
4  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
10 #include <linux/net.h>
11 #include <linux/gfp.h>
12 #include <linux/skbuff.h>
13 #include <linux/export.h>
14 #include <net/sock.h>
15 #include <net/af_rxrpc.h>
16 #include <net/udp.h>
17 #include "ar-internal.h"
18 
19 extern int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len);
20 
21 static ssize_t do_udp_sendmsg(struct socket *socket, struct msghdr *msg, size_t len)
22 {
23 	struct sockaddr *sa = msg->msg_name;
24 	struct sock *sk = socket->sk;
25 
26 	if (IS_ENABLED(CONFIG_AF_RXRPC_IPV6)) {
27 		if (sa->sa_family == AF_INET6) {
28 			if (sk->sk_family != AF_INET6) {
29 				pr_warn("AF_INET6 address on AF_INET socket\n");
30 				return -ENOPROTOOPT;
31 			}
32 			return udpv6_sendmsg(sk, msg, len);
33 		}
34 	}
35 	return udp_sendmsg(sk, msg, len);
36 }
37 
38 struct rxrpc_abort_buffer {
39 	struct rxrpc_wire_header whdr;
40 	__be32 abort_code;
41 };
42 
43 static const char rxrpc_keepalive_string[] = "";
44 
45 /*
46  * Increase Tx backoff on transmission failure and clear it on success.
47  */
48 static void rxrpc_tx_backoff(struct rxrpc_call *call, int ret)
49 {
50 	if (ret < 0) {
51 		u16 tx_backoff = READ_ONCE(call->tx_backoff);
52 
53 		if (tx_backoff < HZ)
54 			WRITE_ONCE(call->tx_backoff, tx_backoff + 1);
55 	} else {
56 		WRITE_ONCE(call->tx_backoff, 0);
57 	}
58 }
59 
60 /*
61  * Arrange for a keepalive ping a certain time after we last transmitted.  This
62  * lets the far side know we're still interested in this call and helps keep
63  * the route through any intervening firewall open.
64  *
65  * Receiving a response to the ping will prevent the ->expect_rx_by timer from
66  * expiring.
67  */
68 static void rxrpc_set_keepalive(struct rxrpc_call *call)
69 {
70 	unsigned long now = jiffies, keepalive_at = call->next_rx_timo / 6;
71 
72 	keepalive_at += now;
73 	WRITE_ONCE(call->keepalive_at, keepalive_at);
74 	rxrpc_reduce_call_timer(call, keepalive_at, now,
75 				rxrpc_timer_set_for_keepalive);
76 }
77 
78 /*
79  * Fill out an ACK packet.
80  */
81 static size_t rxrpc_fill_out_ack(struct rxrpc_connection *conn,
82 				 struct rxrpc_call *call,
83 				 struct rxrpc_txbuf *txb,
84 				 u16 *_rwind)
85 {
86 	struct rxrpc_ackinfo ackinfo;
87 	unsigned int qsize, sack, wrap, to;
88 	rxrpc_seq_t window, wtop;
89 	int rsize;
90 	u32 mtu, jmax;
91 	u8 *ackp = txb->acks;
92 
93 	call->ackr_nr_unacked = 0;
94 	atomic_set(&call->ackr_nr_consumed, 0);
95 	rxrpc_inc_stat(call->rxnet, stat_tx_ack_fill);
96 	clear_bit(RXRPC_CALL_RX_IS_IDLE, &call->flags);
97 
98 	window = call->ackr_window;
99 	wtop   = call->ackr_wtop;
100 	sack   = call->ackr_sack_base % RXRPC_SACK_SIZE;
101 	txb->ack.firstPacket = htonl(window);
102 	txb->ack.nAcks = wtop - window;
103 
104 	if (after(wtop, window)) {
105 		wrap = RXRPC_SACK_SIZE - sack;
106 		to = min_t(unsigned int, txb->ack.nAcks, RXRPC_SACK_SIZE);
107 
108 		if (sack + txb->ack.nAcks <= RXRPC_SACK_SIZE) {
109 			memcpy(txb->acks, call->ackr_sack_table + sack, txb->ack.nAcks);
110 		} else {
111 			memcpy(txb->acks, call->ackr_sack_table + sack, wrap);
112 			memcpy(txb->acks + wrap, call->ackr_sack_table,
113 			       to - wrap);
114 		}
115 
116 		ackp += to;
117 	} else if (before(wtop, window)) {
118 		pr_warn("ack window backward %x %x", window, wtop);
119 	} else if (txb->ack.reason == RXRPC_ACK_DELAY) {
120 		txb->ack.reason = RXRPC_ACK_IDLE;
121 	}
122 
123 	mtu = conn->peer->if_mtu;
124 	mtu -= conn->peer->hdrsize;
125 	jmax = rxrpc_rx_jumbo_max;
126 	qsize = (window - 1) - call->rx_consumed;
127 	rsize = max_t(int, call->rx_winsize - qsize, 0);
128 	*_rwind = rsize;
129 	ackinfo.rxMTU		= htonl(rxrpc_rx_mtu);
130 	ackinfo.maxMTU		= htonl(mtu);
131 	ackinfo.rwind		= htonl(rsize);
132 	ackinfo.jumbo_max	= htonl(jmax);
133 
134 	*ackp++ = 0;
135 	*ackp++ = 0;
136 	*ackp++ = 0;
137 	memcpy(ackp, &ackinfo, sizeof(ackinfo));
138 	return txb->ack.nAcks + 3 + sizeof(ackinfo);
139 }
140 
141 /*
142  * Record the beginning of an RTT probe.
143  */
144 static int rxrpc_begin_rtt_probe(struct rxrpc_call *call, rxrpc_serial_t serial,
145 				 enum rxrpc_rtt_tx_trace why)
146 {
147 	unsigned long avail = call->rtt_avail;
148 	int rtt_slot = 9;
149 
150 	if (!(avail & RXRPC_CALL_RTT_AVAIL_MASK))
151 		goto no_slot;
152 
153 	rtt_slot = __ffs(avail & RXRPC_CALL_RTT_AVAIL_MASK);
154 	if (!test_and_clear_bit(rtt_slot, &call->rtt_avail))
155 		goto no_slot;
156 
157 	call->rtt_serial[rtt_slot] = serial;
158 	call->rtt_sent_at[rtt_slot] = ktime_get_real();
159 	smp_wmb(); /* Write data before avail bit */
160 	set_bit(rtt_slot + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
161 
162 	trace_rxrpc_rtt_tx(call, why, rtt_slot, serial);
163 	return rtt_slot;
164 
165 no_slot:
166 	trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_no_slot, rtt_slot, serial);
167 	return -1;
168 }
169 
170 /*
171  * Cancel an RTT probe.
172  */
173 static void rxrpc_cancel_rtt_probe(struct rxrpc_call *call,
174 				   rxrpc_serial_t serial, int rtt_slot)
175 {
176 	if (rtt_slot != -1) {
177 		clear_bit(rtt_slot + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
178 		smp_wmb(); /* Clear pending bit before setting slot */
179 		set_bit(rtt_slot, &call->rtt_avail);
180 		trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_cancel, rtt_slot, serial);
181 	}
182 }
183 
184 /*
185  * Transmit an ACK packet.
186  */
187 int rxrpc_send_ack_packet(struct rxrpc_call *call, struct rxrpc_txbuf *txb)
188 {
189 	struct rxrpc_connection *conn;
190 	struct msghdr msg;
191 	struct kvec iov[1];
192 	rxrpc_serial_t serial;
193 	size_t len, n;
194 	int ret, rtt_slot = -1;
195 	u16 rwind;
196 
197 	if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
198 		return -ECONNRESET;
199 
200 	conn = call->conn;
201 
202 	msg.msg_name	= &call->peer->srx.transport;
203 	msg.msg_namelen	= call->peer->srx.transport_len;
204 	msg.msg_control	= NULL;
205 	msg.msg_controllen = 0;
206 	msg.msg_flags	= 0;
207 
208 	if (txb->ack.reason == RXRPC_ACK_PING)
209 		txb->wire.flags |= RXRPC_REQUEST_ACK;
210 
211 	n = rxrpc_fill_out_ack(conn, call, txb, &rwind);
212 	if (n == 0)
213 		return 0;
214 
215 	iov[0].iov_base	= &txb->wire;
216 	iov[0].iov_len	= sizeof(txb->wire) + sizeof(txb->ack) + n;
217 	len = iov[0].iov_len;
218 
219 	serial = atomic_inc_return(&conn->serial);
220 	txb->wire.serial = htonl(serial);
221 	trace_rxrpc_tx_ack(call->debug_id, serial,
222 			   ntohl(txb->ack.firstPacket),
223 			   ntohl(txb->ack.serial), txb->ack.reason, txb->ack.nAcks,
224 			   rwind);
225 
226 	if (txb->ack.reason == RXRPC_ACK_PING)
227 		rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_ping);
228 
229 	rxrpc_inc_stat(call->rxnet, stat_tx_ack_send);
230 
231 	/* Grab the highest received seq as late as possible */
232 	txb->ack.previousPacket	= htonl(call->rx_highest_seq);
233 
234 	iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
235 	ret = do_udp_sendmsg(conn->local->socket, &msg, len);
236 	call->peer->last_tx_at = ktime_get_seconds();
237 	if (ret < 0) {
238 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
239 				    rxrpc_tx_point_call_ack);
240 	} else {
241 		trace_rxrpc_tx_packet(call->debug_id, &txb->wire,
242 				      rxrpc_tx_point_call_ack);
243 		if (txb->wire.flags & RXRPC_REQUEST_ACK)
244 			call->peer->rtt_last_req = ktime_get_real();
245 	}
246 	rxrpc_tx_backoff(call, ret);
247 
248 	if (!__rxrpc_call_is_complete(call)) {
249 		if (ret < 0)
250 			rxrpc_cancel_rtt_probe(call, serial, rtt_slot);
251 		rxrpc_set_keepalive(call);
252 	}
253 
254 	return ret;
255 }
256 
257 /*
258  * Send an ABORT call packet.
259  */
260 int rxrpc_send_abort_packet(struct rxrpc_call *call)
261 {
262 	struct rxrpc_connection *conn;
263 	struct rxrpc_abort_buffer pkt;
264 	struct msghdr msg;
265 	struct kvec iov[1];
266 	rxrpc_serial_t serial;
267 	int ret;
268 
269 	/* Don't bother sending aborts for a client call once the server has
270 	 * hard-ACK'd all of its request data.  After that point, we're not
271 	 * going to stop the operation proceeding, and whilst we might limit
272 	 * the reply, it's not worth it if we can send a new call on the same
273 	 * channel instead, thereby closing off this call.
274 	 */
275 	if (rxrpc_is_client_call(call) &&
276 	    test_bit(RXRPC_CALL_TX_ALL_ACKED, &call->flags))
277 		return 0;
278 
279 	if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
280 		return -ECONNRESET;
281 
282 	conn = call->conn;
283 
284 	msg.msg_name	= &call->peer->srx.transport;
285 	msg.msg_namelen	= call->peer->srx.transport_len;
286 	msg.msg_control	= NULL;
287 	msg.msg_controllen = 0;
288 	msg.msg_flags	= 0;
289 
290 	pkt.whdr.epoch		= htonl(conn->proto.epoch);
291 	pkt.whdr.cid		= htonl(call->cid);
292 	pkt.whdr.callNumber	= htonl(call->call_id);
293 	pkt.whdr.seq		= 0;
294 	pkt.whdr.type		= RXRPC_PACKET_TYPE_ABORT;
295 	pkt.whdr.flags		= conn->out_clientflag;
296 	pkt.whdr.userStatus	= 0;
297 	pkt.whdr.securityIndex	= call->security_ix;
298 	pkt.whdr._rsvd		= 0;
299 	pkt.whdr.serviceId	= htons(call->dest_srx.srx_service);
300 	pkt.abort_code		= htonl(call->abort_code);
301 
302 	iov[0].iov_base	= &pkt;
303 	iov[0].iov_len	= sizeof(pkt);
304 
305 	serial = atomic_inc_return(&conn->serial);
306 	pkt.whdr.serial = htonl(serial);
307 
308 	iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, sizeof(pkt));
309 	ret = do_udp_sendmsg(conn->local->socket, &msg, sizeof(pkt));
310 	conn->peer->last_tx_at = ktime_get_seconds();
311 	if (ret < 0)
312 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
313 				    rxrpc_tx_point_call_abort);
314 	else
315 		trace_rxrpc_tx_packet(call->debug_id, &pkt.whdr,
316 				      rxrpc_tx_point_call_abort);
317 	rxrpc_tx_backoff(call, ret);
318 	return ret;
319 }
320 
321 /*
322  * send a packet through the transport endpoint
323  */
324 int rxrpc_send_data_packet(struct rxrpc_call *call, struct rxrpc_txbuf *txb)
325 {
326 	enum rxrpc_req_ack_trace why;
327 	struct rxrpc_connection *conn = call->conn;
328 	struct msghdr msg;
329 	struct kvec iov[1];
330 	rxrpc_serial_t serial;
331 	size_t len;
332 	int ret, rtt_slot = -1;
333 
334 	_enter("%x,{%d}", txb->seq, txb->len);
335 
336 	/* Each transmission of a Tx packet needs a new serial number */
337 	serial = atomic_inc_return(&conn->serial);
338 	txb->wire.serial = htonl(serial);
339 
340 	if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) &&
341 	    txb->seq == 1)
342 		txb->wire.userStatus = RXRPC_USERSTATUS_SERVICE_UPGRADE;
343 
344 	iov[0].iov_base = &txb->wire;
345 	iov[0].iov_len = sizeof(txb->wire) + txb->len;
346 	len = iov[0].iov_len;
347 	iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
348 
349 	msg.msg_name = &call->peer->srx.transport;
350 	msg.msg_namelen = call->peer->srx.transport_len;
351 	msg.msg_control = NULL;
352 	msg.msg_controllen = 0;
353 	msg.msg_flags = 0;
354 
355 	/* If our RTT cache needs working on, request an ACK.  Also request
356 	 * ACKs if a DATA packet appears to have been lost.
357 	 *
358 	 * However, we mustn't request an ACK on the last reply packet of a
359 	 * service call, lest OpenAFS incorrectly send us an ACK with some
360 	 * soft-ACKs in it and then never follow up with a proper hard ACK.
361 	 */
362 	if (txb->wire.flags & RXRPC_REQUEST_ACK)
363 		why = rxrpc_reqack_already_on;
364 	else if (test_bit(RXRPC_TXBUF_LAST, &txb->flags) && rxrpc_sending_to_client(txb))
365 		why = rxrpc_reqack_no_srv_last;
366 	else if (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events))
367 		why = rxrpc_reqack_ack_lost;
368 	else if (test_bit(RXRPC_TXBUF_RESENT, &txb->flags))
369 		why = rxrpc_reqack_retrans;
370 	else if (call->cong_mode == RXRPC_CALL_SLOW_START && call->cong_cwnd <= 2)
371 		why = rxrpc_reqack_slow_start;
372 	else if (call->tx_winsize <= 2)
373 		why = rxrpc_reqack_small_txwin;
374 	else if (call->peer->rtt_count < 3 && txb->seq & 1)
375 		why = rxrpc_reqack_more_rtt;
376 	else if (ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000), ktime_get_real()))
377 		why = rxrpc_reqack_old_rtt;
378 	else
379 		goto dont_set_request_ack;
380 
381 	rxrpc_inc_stat(call->rxnet, stat_why_req_ack[why]);
382 	trace_rxrpc_req_ack(call->debug_id, txb->seq, why);
383 	if (why != rxrpc_reqack_no_srv_last)
384 		txb->wire.flags |= RXRPC_REQUEST_ACK;
385 dont_set_request_ack:
386 
387 	if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
388 		static int lose;
389 		if ((lose++ & 7) == 7) {
390 			ret = 0;
391 			trace_rxrpc_tx_data(call, txb->seq, serial,
392 					    txb->wire.flags,
393 					    test_bit(RXRPC_TXBUF_RESENT, &txb->flags),
394 					    true);
395 			goto done;
396 		}
397 	}
398 
399 	trace_rxrpc_tx_data(call, txb->seq, serial, txb->wire.flags,
400 			    test_bit(RXRPC_TXBUF_RESENT, &txb->flags), false);
401 
402 	/* Track what we've attempted to transmit at least once so that the
403 	 * retransmission algorithm doesn't try to resend what we haven't sent
404 	 * yet.  However, this can race as we can receive an ACK before we get
405 	 * to this point.  But, OTOH, if we won't get an ACK mentioning this
406 	 * packet unless the far side received it (though it could have
407 	 * discarded it anyway and NAK'd it).
408 	 */
409 	cmpxchg(&call->tx_transmitted, txb->seq - 1, txb->seq);
410 
411 	/* send the packet with the don't fragment bit set if we currently
412 	 * think it's small enough */
413 	if (txb->len >= call->peer->maxdata)
414 		goto send_fragmentable;
415 
416 	txb->last_sent = ktime_get_real();
417 	if (txb->wire.flags & RXRPC_REQUEST_ACK)
418 		rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_data);
419 
420 	/* send the packet by UDP
421 	 * - returns -EMSGSIZE if UDP would have to fragment the packet
422 	 *   to go out of the interface
423 	 *   - in which case, we'll have processed the ICMP error
424 	 *     message and update the peer record
425 	 */
426 	rxrpc_inc_stat(call->rxnet, stat_tx_data_send);
427 	ret = do_udp_sendmsg(conn->local->socket, &msg, len);
428 	conn->peer->last_tx_at = ktime_get_seconds();
429 
430 	if (ret < 0) {
431 		rxrpc_inc_stat(call->rxnet, stat_tx_data_send_fail);
432 		rxrpc_cancel_rtt_probe(call, serial, rtt_slot);
433 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
434 				    rxrpc_tx_point_call_data_nofrag);
435 	} else {
436 		trace_rxrpc_tx_packet(call->debug_id, &txb->wire,
437 				      rxrpc_tx_point_call_data_nofrag);
438 	}
439 
440 	rxrpc_tx_backoff(call, ret);
441 	if (ret == -EMSGSIZE)
442 		goto send_fragmentable;
443 
444 done:
445 	if (ret >= 0) {
446 		call->tx_last_sent = txb->last_sent;
447 		if (txb->wire.flags & RXRPC_REQUEST_ACK) {
448 			call->peer->rtt_last_req = txb->last_sent;
449 			if (call->peer->rtt_count > 1) {
450 				unsigned long nowj = jiffies, ack_lost_at;
451 
452 				ack_lost_at = rxrpc_get_rto_backoff(call->peer, false);
453 				ack_lost_at += nowj;
454 				WRITE_ONCE(call->ack_lost_at, ack_lost_at);
455 				rxrpc_reduce_call_timer(call, ack_lost_at, nowj,
456 							rxrpc_timer_set_for_lost_ack);
457 			}
458 		}
459 
460 		if (txb->seq == 1 &&
461 		    !test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER,
462 				      &call->flags)) {
463 			unsigned long nowj = jiffies, expect_rx_by;
464 
465 			expect_rx_by = nowj + call->next_rx_timo;
466 			WRITE_ONCE(call->expect_rx_by, expect_rx_by);
467 			rxrpc_reduce_call_timer(call, expect_rx_by, nowj,
468 						rxrpc_timer_set_for_normal);
469 		}
470 
471 		rxrpc_set_keepalive(call);
472 	} else {
473 		/* Cancel the call if the initial transmission fails,
474 		 * particularly if that's due to network routing issues that
475 		 * aren't going away anytime soon.  The layer above can arrange
476 		 * the retransmission.
477 		 */
478 		if (!test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER, &call->flags))
479 			rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
480 						  RX_USER_ABORT, ret);
481 	}
482 
483 	_leave(" = %d [%u]", ret, call->peer->maxdata);
484 	return ret;
485 
486 send_fragmentable:
487 	/* attempt to send this message with fragmentation enabled */
488 	_debug("send fragment");
489 
490 	txb->last_sent = ktime_get_real();
491 	if (txb->wire.flags & RXRPC_REQUEST_ACK)
492 		rtt_slot = rxrpc_begin_rtt_probe(call, serial, rxrpc_rtt_tx_data);
493 
494 	switch (conn->local->srx.transport.family) {
495 	case AF_INET6:
496 	case AF_INET:
497 		rxrpc_local_dont_fragment(conn->local, false);
498 		rxrpc_inc_stat(call->rxnet, stat_tx_data_send_frag);
499 		ret = do_udp_sendmsg(conn->local->socket, &msg, len);
500 		conn->peer->last_tx_at = ktime_get_seconds();
501 
502 		rxrpc_local_dont_fragment(conn->local, true);
503 		break;
504 
505 	default:
506 		BUG();
507 	}
508 
509 	if (ret < 0) {
510 		rxrpc_inc_stat(call->rxnet, stat_tx_data_send_fail);
511 		rxrpc_cancel_rtt_probe(call, serial, rtt_slot);
512 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
513 				    rxrpc_tx_point_call_data_frag);
514 	} else {
515 		trace_rxrpc_tx_packet(call->debug_id, &txb->wire,
516 				      rxrpc_tx_point_call_data_frag);
517 	}
518 	rxrpc_tx_backoff(call, ret);
519 	goto done;
520 }
521 
522 /*
523  * Transmit a connection-level abort.
524  */
525 void rxrpc_send_conn_abort(struct rxrpc_connection *conn)
526 {
527 	struct rxrpc_wire_header whdr;
528 	struct msghdr msg;
529 	struct kvec iov[2];
530 	__be32 word;
531 	size_t len;
532 	u32 serial;
533 	int ret;
534 
535 	msg.msg_name	= &conn->peer->srx.transport;
536 	msg.msg_namelen	= conn->peer->srx.transport_len;
537 	msg.msg_control	= NULL;
538 	msg.msg_controllen = 0;
539 	msg.msg_flags	= 0;
540 
541 	whdr.epoch	= htonl(conn->proto.epoch);
542 	whdr.cid	= htonl(conn->proto.cid);
543 	whdr.callNumber	= 0;
544 	whdr.seq	= 0;
545 	whdr.type	= RXRPC_PACKET_TYPE_ABORT;
546 	whdr.flags	= conn->out_clientflag;
547 	whdr.userStatus	= 0;
548 	whdr.securityIndex = conn->security_ix;
549 	whdr._rsvd	= 0;
550 	whdr.serviceId	= htons(conn->service_id);
551 
552 	word		= htonl(conn->abort_code);
553 
554 	iov[0].iov_base	= &whdr;
555 	iov[0].iov_len	= sizeof(whdr);
556 	iov[1].iov_base	= &word;
557 	iov[1].iov_len	= sizeof(word);
558 
559 	len = iov[0].iov_len + iov[1].iov_len;
560 
561 	serial = atomic_inc_return(&conn->serial);
562 	whdr.serial = htonl(serial);
563 
564 	iov_iter_kvec(&msg.msg_iter, WRITE, iov, 2, len);
565 	ret = do_udp_sendmsg(conn->local->socket, &msg, len);
566 	if (ret < 0) {
567 		trace_rxrpc_tx_fail(conn->debug_id, serial, ret,
568 				    rxrpc_tx_point_conn_abort);
569 		_debug("sendmsg failed: %d", ret);
570 		return;
571 	}
572 
573 	trace_rxrpc_tx_packet(conn->debug_id, &whdr, rxrpc_tx_point_conn_abort);
574 
575 	conn->peer->last_tx_at = ktime_get_seconds();
576 }
577 
578 /*
579  * Reject a packet through the local endpoint.
580  */
581 void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
582 {
583 	struct rxrpc_wire_header whdr;
584 	struct sockaddr_rxrpc srx;
585 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
586 	struct msghdr msg;
587 	struct kvec iov[2];
588 	size_t size;
589 	__be32 code;
590 	int ret, ioc;
591 
592 	rxrpc_see_skb(skb, rxrpc_skb_see_reject);
593 
594 	iov[0].iov_base = &whdr;
595 	iov[0].iov_len = sizeof(whdr);
596 	iov[1].iov_base = &code;
597 	iov[1].iov_len = sizeof(code);
598 
599 	msg.msg_name = &srx.transport;
600 	msg.msg_control = NULL;
601 	msg.msg_controllen = 0;
602 	msg.msg_flags = 0;
603 
604 	memset(&whdr, 0, sizeof(whdr));
605 
606 	switch (skb->mark) {
607 	case RXRPC_SKB_MARK_REJECT_BUSY:
608 		whdr.type = RXRPC_PACKET_TYPE_BUSY;
609 		size = sizeof(whdr);
610 		ioc = 1;
611 		break;
612 	case RXRPC_SKB_MARK_REJECT_ABORT:
613 		whdr.type = RXRPC_PACKET_TYPE_ABORT;
614 		code = htonl(skb->priority);
615 		size = sizeof(whdr) + sizeof(code);
616 		ioc = 2;
617 		break;
618 	default:
619 		return;
620 	}
621 
622 	if (rxrpc_extract_addr_from_skb(&srx, skb) == 0) {
623 		msg.msg_namelen = srx.transport_len;
624 
625 		whdr.epoch	= htonl(sp->hdr.epoch);
626 		whdr.cid	= htonl(sp->hdr.cid);
627 		whdr.callNumber	= htonl(sp->hdr.callNumber);
628 		whdr.serviceId	= htons(sp->hdr.serviceId);
629 		whdr.flags	= sp->hdr.flags;
630 		whdr.flags	^= RXRPC_CLIENT_INITIATED;
631 		whdr.flags	&= RXRPC_CLIENT_INITIATED;
632 
633 		iov_iter_kvec(&msg.msg_iter, WRITE, iov, ioc, size);
634 		ret = do_udp_sendmsg(local->socket, &msg, size);
635 		if (ret < 0)
636 			trace_rxrpc_tx_fail(local->debug_id, 0, ret,
637 					    rxrpc_tx_point_reject);
638 		else
639 			trace_rxrpc_tx_packet(local->debug_id, &whdr,
640 					      rxrpc_tx_point_reject);
641 	}
642 }
643 
644 /*
645  * Send a VERSION reply to a peer as a keepalive.
646  */
647 void rxrpc_send_keepalive(struct rxrpc_peer *peer)
648 {
649 	struct rxrpc_wire_header whdr;
650 	struct msghdr msg;
651 	struct kvec iov[2];
652 	size_t len;
653 	int ret;
654 
655 	_enter("");
656 
657 	msg.msg_name	= &peer->srx.transport;
658 	msg.msg_namelen	= peer->srx.transport_len;
659 	msg.msg_control	= NULL;
660 	msg.msg_controllen = 0;
661 	msg.msg_flags	= 0;
662 
663 	whdr.epoch	= htonl(peer->local->rxnet->epoch);
664 	whdr.cid	= 0;
665 	whdr.callNumber	= 0;
666 	whdr.seq	= 0;
667 	whdr.serial	= 0;
668 	whdr.type	= RXRPC_PACKET_TYPE_VERSION; /* Not client-initiated */
669 	whdr.flags	= RXRPC_LAST_PACKET;
670 	whdr.userStatus	= 0;
671 	whdr.securityIndex = 0;
672 	whdr._rsvd	= 0;
673 	whdr.serviceId	= 0;
674 
675 	iov[0].iov_base	= &whdr;
676 	iov[0].iov_len	= sizeof(whdr);
677 	iov[1].iov_base	= (char *)rxrpc_keepalive_string;
678 	iov[1].iov_len	= sizeof(rxrpc_keepalive_string);
679 
680 	len = iov[0].iov_len + iov[1].iov_len;
681 
682 	iov_iter_kvec(&msg.msg_iter, WRITE, iov, 2, len);
683 	ret = do_udp_sendmsg(peer->local->socket, &msg, len);
684 	if (ret < 0)
685 		trace_rxrpc_tx_fail(peer->debug_id, 0, ret,
686 				    rxrpc_tx_point_version_keepalive);
687 	else
688 		trace_rxrpc_tx_packet(peer->debug_id, &whdr,
689 				      rxrpc_tx_point_version_keepalive);
690 
691 	peer->last_tx_at = ktime_get_seconds();
692 	_leave("");
693 }
694 
695 /*
696  * Schedule an instant Tx resend.
697  */
698 static inline void rxrpc_instant_resend(struct rxrpc_call *call,
699 					struct rxrpc_txbuf *txb)
700 {
701 	if (!__rxrpc_call_is_complete(call))
702 		kdebug("resend");
703 }
704 
705 /*
706  * Transmit one packet.
707  */
708 void rxrpc_transmit_one(struct rxrpc_call *call, struct rxrpc_txbuf *txb)
709 {
710 	int ret;
711 
712 	ret = rxrpc_send_data_packet(call, txb);
713 	if (ret < 0) {
714 		switch (ret) {
715 		case -ENETUNREACH:
716 		case -EHOSTUNREACH:
717 		case -ECONNREFUSED:
718 			rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
719 						  0, ret);
720 			break;
721 		default:
722 			_debug("need instant resend %d", ret);
723 			rxrpc_instant_resend(call, txb);
724 		}
725 	} else {
726 		unsigned long now = jiffies;
727 		unsigned long resend_at = now + call->peer->rto_j;
728 
729 		WRITE_ONCE(call->resend_at, resend_at);
730 		rxrpc_reduce_call_timer(call, resend_at, now,
731 					rxrpc_timer_set_for_send);
732 	}
733 }
734