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 
do_udp_sendmsg(struct socket * socket,struct msghdr * msg,size_t len)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  */
rxrpc_tx_backoff(struct rxrpc_call * call,int ret)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  */
rxrpc_set_keepalive(struct rxrpc_call * call)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  */
rxrpc_fill_out_ack(struct rxrpc_connection * conn,struct rxrpc_call * call,struct rxrpc_txbuf * txb,u16 * _rwind)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  */
rxrpc_begin_rtt_probe(struct rxrpc_call * call,rxrpc_serial_t serial,enum rxrpc_rtt_tx_trace why)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  */
rxrpc_cancel_rtt_probe(struct rxrpc_call * call,rxrpc_serial_t serial,int rtt_slot)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  */
rxrpc_send_ack_packet(struct rxrpc_call * call,struct rxrpc_txbuf * txb)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  */
rxrpc_send_abort_packet(struct rxrpc_call * call)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  */
rxrpc_send_data_packet(struct rxrpc_call * call,struct rxrpc_txbuf * txb)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 		ip_sock_set_mtu_discover(conn->local->socket->sk,
498 					 IP_PMTUDISC_DONT);
499 		rxrpc_inc_stat(call->rxnet, stat_tx_data_send_frag);
500 		ret = do_udp_sendmsg(conn->local->socket, &msg, len);
501 		conn->peer->last_tx_at = ktime_get_seconds();
502 
503 		ip_sock_set_mtu_discover(conn->local->socket->sk,
504 					 IP_PMTUDISC_DO);
505 		break;
506 
507 	default:
508 		BUG();
509 	}
510 
511 	if (ret < 0) {
512 		rxrpc_inc_stat(call->rxnet, stat_tx_data_send_fail);
513 		rxrpc_cancel_rtt_probe(call, serial, rtt_slot);
514 		trace_rxrpc_tx_fail(call->debug_id, serial, ret,
515 				    rxrpc_tx_point_call_data_frag);
516 	} else {
517 		trace_rxrpc_tx_packet(call->debug_id, &txb->wire,
518 				      rxrpc_tx_point_call_data_frag);
519 	}
520 	rxrpc_tx_backoff(call, ret);
521 	goto done;
522 }
523 
524 /*
525  * Transmit a connection-level abort.
526  */
rxrpc_send_conn_abort(struct rxrpc_connection * conn)527 void rxrpc_send_conn_abort(struct rxrpc_connection *conn)
528 {
529 	struct rxrpc_wire_header whdr;
530 	struct msghdr msg;
531 	struct kvec iov[2];
532 	__be32 word;
533 	size_t len;
534 	u32 serial;
535 	int ret;
536 
537 	msg.msg_name	= &conn->peer->srx.transport;
538 	msg.msg_namelen	= conn->peer->srx.transport_len;
539 	msg.msg_control	= NULL;
540 	msg.msg_controllen = 0;
541 	msg.msg_flags	= 0;
542 
543 	whdr.epoch	= htonl(conn->proto.epoch);
544 	whdr.cid	= htonl(conn->proto.cid);
545 	whdr.callNumber	= 0;
546 	whdr.seq	= 0;
547 	whdr.type	= RXRPC_PACKET_TYPE_ABORT;
548 	whdr.flags	= conn->out_clientflag;
549 	whdr.userStatus	= 0;
550 	whdr.securityIndex = conn->security_ix;
551 	whdr._rsvd	= 0;
552 	whdr.serviceId	= htons(conn->service_id);
553 
554 	word		= htonl(conn->abort_code);
555 
556 	iov[0].iov_base	= &whdr;
557 	iov[0].iov_len	= sizeof(whdr);
558 	iov[1].iov_base	= &word;
559 	iov[1].iov_len	= sizeof(word);
560 
561 	len = iov[0].iov_len + iov[1].iov_len;
562 
563 	serial = atomic_inc_return(&conn->serial);
564 	whdr.serial = htonl(serial);
565 
566 	iov_iter_kvec(&msg.msg_iter, WRITE, iov, 2, len);
567 	ret = do_udp_sendmsg(conn->local->socket, &msg, len);
568 	if (ret < 0) {
569 		trace_rxrpc_tx_fail(conn->debug_id, serial, ret,
570 				    rxrpc_tx_point_conn_abort);
571 		_debug("sendmsg failed: %d", ret);
572 		return;
573 	}
574 
575 	trace_rxrpc_tx_packet(conn->debug_id, &whdr, rxrpc_tx_point_conn_abort);
576 
577 	conn->peer->last_tx_at = ktime_get_seconds();
578 }
579 
580 /*
581  * Reject a packet through the local endpoint.
582  */
rxrpc_reject_packet(struct rxrpc_local * local,struct sk_buff * skb)583 void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
584 {
585 	struct rxrpc_wire_header whdr;
586 	struct sockaddr_rxrpc srx;
587 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
588 	struct msghdr msg;
589 	struct kvec iov[2];
590 	size_t size;
591 	__be32 code;
592 	int ret, ioc;
593 
594 	rxrpc_see_skb(skb, rxrpc_skb_see_reject);
595 
596 	iov[0].iov_base = &whdr;
597 	iov[0].iov_len = sizeof(whdr);
598 	iov[1].iov_base = &code;
599 	iov[1].iov_len = sizeof(code);
600 
601 	msg.msg_name = &srx.transport;
602 	msg.msg_control = NULL;
603 	msg.msg_controllen = 0;
604 	msg.msg_flags = 0;
605 
606 	memset(&whdr, 0, sizeof(whdr));
607 
608 	switch (skb->mark) {
609 	case RXRPC_SKB_MARK_REJECT_BUSY:
610 		whdr.type = RXRPC_PACKET_TYPE_BUSY;
611 		size = sizeof(whdr);
612 		ioc = 1;
613 		break;
614 	case RXRPC_SKB_MARK_REJECT_ABORT:
615 		whdr.type = RXRPC_PACKET_TYPE_ABORT;
616 		code = htonl(skb->priority);
617 		size = sizeof(whdr) + sizeof(code);
618 		ioc = 2;
619 		break;
620 	default:
621 		return;
622 	}
623 
624 	if (rxrpc_extract_addr_from_skb(&srx, skb) == 0) {
625 		msg.msg_namelen = srx.transport_len;
626 
627 		whdr.epoch	= htonl(sp->hdr.epoch);
628 		whdr.cid	= htonl(sp->hdr.cid);
629 		whdr.callNumber	= htonl(sp->hdr.callNumber);
630 		whdr.serviceId	= htons(sp->hdr.serviceId);
631 		whdr.flags	= sp->hdr.flags;
632 		whdr.flags	^= RXRPC_CLIENT_INITIATED;
633 		whdr.flags	&= RXRPC_CLIENT_INITIATED;
634 
635 		iov_iter_kvec(&msg.msg_iter, WRITE, iov, ioc, size);
636 		ret = do_udp_sendmsg(local->socket, &msg, size);
637 		if (ret < 0)
638 			trace_rxrpc_tx_fail(local->debug_id, 0, ret,
639 					    rxrpc_tx_point_reject);
640 		else
641 			trace_rxrpc_tx_packet(local->debug_id, &whdr,
642 					      rxrpc_tx_point_reject);
643 	}
644 }
645 
646 /*
647  * Send a VERSION reply to a peer as a keepalive.
648  */
rxrpc_send_keepalive(struct rxrpc_peer * peer)649 void rxrpc_send_keepalive(struct rxrpc_peer *peer)
650 {
651 	struct rxrpc_wire_header whdr;
652 	struct msghdr msg;
653 	struct kvec iov[2];
654 	size_t len;
655 	int ret;
656 
657 	_enter("");
658 
659 	msg.msg_name	= &peer->srx.transport;
660 	msg.msg_namelen	= peer->srx.transport_len;
661 	msg.msg_control	= NULL;
662 	msg.msg_controllen = 0;
663 	msg.msg_flags	= 0;
664 
665 	whdr.epoch	= htonl(peer->local->rxnet->epoch);
666 	whdr.cid	= 0;
667 	whdr.callNumber	= 0;
668 	whdr.seq	= 0;
669 	whdr.serial	= 0;
670 	whdr.type	= RXRPC_PACKET_TYPE_VERSION; /* Not client-initiated */
671 	whdr.flags	= RXRPC_LAST_PACKET;
672 	whdr.userStatus	= 0;
673 	whdr.securityIndex = 0;
674 	whdr._rsvd	= 0;
675 	whdr.serviceId	= 0;
676 
677 	iov[0].iov_base	= &whdr;
678 	iov[0].iov_len	= sizeof(whdr);
679 	iov[1].iov_base	= (char *)rxrpc_keepalive_string;
680 	iov[1].iov_len	= sizeof(rxrpc_keepalive_string);
681 
682 	len = iov[0].iov_len + iov[1].iov_len;
683 
684 	iov_iter_kvec(&msg.msg_iter, WRITE, iov, 2, len);
685 	ret = do_udp_sendmsg(peer->local->socket, &msg, len);
686 	if (ret < 0)
687 		trace_rxrpc_tx_fail(peer->debug_id, 0, ret,
688 				    rxrpc_tx_point_version_keepalive);
689 	else
690 		trace_rxrpc_tx_packet(peer->debug_id, &whdr,
691 				      rxrpc_tx_point_version_keepalive);
692 
693 	peer->last_tx_at = ktime_get_seconds();
694 	_leave("");
695 }
696 
697 /*
698  * Schedule an instant Tx resend.
699  */
rxrpc_instant_resend(struct rxrpc_call * call,struct rxrpc_txbuf * txb)700 static inline void rxrpc_instant_resend(struct rxrpc_call *call,
701 					struct rxrpc_txbuf *txb)
702 {
703 	if (!__rxrpc_call_is_complete(call))
704 		kdebug("resend");
705 }
706 
707 /*
708  * Transmit one packet.
709  */
rxrpc_transmit_one(struct rxrpc_call * call,struct rxrpc_txbuf * txb)710 void rxrpc_transmit_one(struct rxrpc_call *call, struct rxrpc_txbuf *txb)
711 {
712 	int ret;
713 
714 	ret = rxrpc_send_data_packet(call, txb);
715 	if (ret < 0) {
716 		switch (ret) {
717 		case -ENETUNREACH:
718 		case -EHOSTUNREACH:
719 		case -ECONNREFUSED:
720 			rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
721 						  0, ret);
722 			break;
723 		default:
724 			_debug("need instant resend %d", ret);
725 			rxrpc_instant_resend(call, txb);
726 		}
727 	} else {
728 		unsigned long now = jiffies;
729 		unsigned long resend_at = now + call->peer->rto_j;
730 
731 		WRITE_ONCE(call->resend_at, resend_at);
732 		rxrpc_reduce_call_timer(call, resend_at, now,
733 					rxrpc_timer_set_for_send);
734 	}
735 }
736