blob: bad3d242034466d9b3a21b9d92bd26d30d829c4a [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells17926a72007-04-26 15:48:28 -07002/* RxRPC packet transmission
3 *
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howells17926a72007-04-26 15:48:28 -07006 */
7
Joe Perches9b6d5392016-06-02 12:08:52 -07008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
David Howells17926a72007-04-26 15:48:28 -070010#include <linux/net.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/gfp.h>
David Howells17926a72007-04-26 15:48:28 -070012#include <linux/skbuff.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040013#include <linux/export.h>
David Howells17926a72007-04-26 15:48:28 -070014#include <net/sock.h>
15#include <net/af_rxrpc.h>
16#include "ar-internal.h"
17
David Howells26cb02a2016-10-06 08:11:49 +010018struct rxrpc_ack_buffer {
David Howells8d94aa32016-09-07 09:19:31 +010019 struct rxrpc_wire_header whdr;
David Howells26cb02a2016-10-06 08:11:49 +010020 struct rxrpc_ackpacket ack;
21 u8 acks[255];
22 u8 pad[3];
David Howells8d94aa32016-09-07 09:19:31 +010023 struct rxrpc_ackinfo ackinfo;
24};
25
David Howells26cb02a2016-10-06 08:11:49 +010026struct rxrpc_abort_buffer {
27 struct rxrpc_wire_header whdr;
28 __be32 abort_code;
29};
30
David Howellsace45be2018-03-30 21:04:43 +010031static const char rxrpc_keepalive_string[] = "";
32
David Howells8d94aa32016-09-07 09:19:31 +010033/*
David Howellsc7e86ac2018-11-01 13:39:53 +000034 * Increase Tx backoff on transmission failure and clear it on success.
35 */
36static void rxrpc_tx_backoff(struct rxrpc_call *call, int ret)
37{
38 if (ret < 0) {
39 u16 tx_backoff = READ_ONCE(call->tx_backoff);
40
41 if (tx_backoff < HZ)
42 WRITE_ONCE(call->tx_backoff, tx_backoff + 1);
43 } else {
44 WRITE_ONCE(call->tx_backoff, 0);
45 }
46}
47
48/*
David Howells415f44e2017-11-24 10:18:42 +000049 * Arrange for a keepalive ping a certain time after we last transmitted. This
50 * lets the far side know we're still interested in this call and helps keep
51 * the route through any intervening firewall open.
52 *
53 * Receiving a response to the ping will prevent the ->expect_rx_by timer from
54 * expiring.
55 */
56static void rxrpc_set_keepalive(struct rxrpc_call *call)
57{
58 unsigned long now = jiffies, keepalive_at = call->next_rx_timo / 6;
59
60 keepalive_at += now;
61 WRITE_ONCE(call->keepalive_at, keepalive_at);
62 rxrpc_reduce_call_timer(call, keepalive_at, now,
63 rxrpc_timer_set_for_keepalive);
64}
65
66/*
David Howells8d94aa32016-09-07 09:19:31 +010067 * Fill out an ACK packet.
68 */
David Howells1457cc42017-11-02 15:06:55 +000069static size_t rxrpc_fill_out_ack(struct rxrpc_connection *conn,
70 struct rxrpc_call *call,
David Howells26cb02a2016-10-06 08:11:49 +010071 struct rxrpc_ack_buffer *pkt,
David Howells805b21b2016-09-24 18:05:26 +010072 rxrpc_seq_t *_hard_ack,
David Howellsa5af7e12016-10-06 08:11:49 +010073 rxrpc_seq_t *_top,
74 u8 reason)
David Howells8d94aa32016-09-07 09:19:31 +010075{
David Howellsf3639df2016-09-17 10:49:13 +010076 rxrpc_serial_t serial;
David Howells248f2192016-09-08 11:10:12 +010077 rxrpc_seq_t hard_ack, top, seq;
78 int ix;
David Howells8d94aa32016-09-07 09:19:31 +010079 u32 mtu, jmax;
80 u8 *ackp = pkt->acks;
81
David Howells248f2192016-09-08 11:10:12 +010082 /* Barrier against rxrpc_input_data(). */
David Howellsf3639df2016-09-17 10:49:13 +010083 serial = call->ackr_serial;
David Howells248f2192016-09-08 11:10:12 +010084 hard_ack = READ_ONCE(call->rx_hard_ack);
85 top = smp_load_acquire(&call->rx_top);
David Howells805b21b2016-09-24 18:05:26 +010086 *_hard_ack = hard_ack;
87 *_top = top;
David Howells248f2192016-09-08 11:10:12 +010088
David Howells8d94aa32016-09-07 09:19:31 +010089 pkt->ack.bufferSpace = htons(8);
David Howellse8c3af62019-08-09 15:20:41 +010090 pkt->ack.maxSkew = htons(0);
David Howells248f2192016-09-08 11:10:12 +010091 pkt->ack.firstPacket = htonl(hard_ack + 1);
David Howells8d94aa32016-09-07 09:19:31 +010092 pkt->ack.previousPacket = htonl(call->ackr_prev_seq);
David Howellsf3639df2016-09-17 10:49:13 +010093 pkt->ack.serial = htonl(serial);
David Howellsa5af7e12016-10-06 08:11:49 +010094 pkt->ack.reason = reason;
David Howells248f2192016-09-08 11:10:12 +010095 pkt->ack.nAcks = top - hard_ack;
David Howells8d94aa32016-09-07 09:19:31 +010096
David Howellsa5af7e12016-10-06 08:11:49 +010097 if (reason == RXRPC_ACK_PING)
David Howells8e831342016-09-22 00:29:31 +010098 pkt->whdr.flags |= RXRPC_REQUEST_ACK;
99
David Howells248f2192016-09-08 11:10:12 +0100100 if (after(top, hard_ack)) {
101 seq = hard_ack + 1;
102 do {
103 ix = seq & RXRPC_RXTX_BUFF_MASK;
104 if (call->rxtx_buffer[ix])
105 *ackp++ = RXRPC_ACK_TYPE_ACK;
106 else
107 *ackp++ = RXRPC_ACK_TYPE_NACK;
108 seq++;
109 } while (before_eq(seq, top));
110 }
111
David Howells1457cc42017-11-02 15:06:55 +0000112 mtu = conn->params.peer->if_mtu;
113 mtu -= conn->params.peer->hdrsize;
David Howells75e42122016-09-13 22:36:22 +0100114 jmax = (call->nr_jumbo_bad > 3) ? 1 : rxrpc_rx_jumbo_max;
David Howells8d94aa32016-09-07 09:19:31 +0100115 pkt->ackinfo.rxMTU = htonl(rxrpc_rx_mtu);
116 pkt->ackinfo.maxMTU = htonl(mtu);
David Howells75e42122016-09-13 22:36:22 +0100117 pkt->ackinfo.rwind = htonl(call->rx_winsize);
David Howells8d94aa32016-09-07 09:19:31 +0100118 pkt->ackinfo.jumbo_max = htonl(jmax);
119
120 *ackp++ = 0;
121 *ackp++ = 0;
122 *ackp++ = 0;
David Howells248f2192016-09-08 11:10:12 +0100123 return top - hard_ack + 3;
David Howells8d94aa32016-09-07 09:19:31 +0100124}
125
126/*
David Howells26cb02a2016-10-06 08:11:49 +0100127 * Send an ACK call packet.
David Howells8d94aa32016-09-07 09:19:31 +0100128 */
David Howellsbd1fdf82017-11-24 10:18:42 +0000129int rxrpc_send_ack_packet(struct rxrpc_call *call, bool ping,
130 rxrpc_serial_t *_serial)
David Howells8d94aa32016-09-07 09:19:31 +0100131{
David Howells5273a192020-01-30 21:50:36 +0000132 struct rxrpc_connection *conn;
David Howells26cb02a2016-10-06 08:11:49 +0100133 struct rxrpc_ack_buffer *pkt;
David Howells8d94aa32016-09-07 09:19:31 +0100134 struct msghdr msg;
135 struct kvec iov[2];
136 rxrpc_serial_t serial;
David Howells805b21b2016-09-24 18:05:26 +0100137 rxrpc_seq_t hard_ack, top;
David Howells8d94aa32016-09-07 09:19:31 +0100138 size_t len, n;
David Howells26cb02a2016-10-06 08:11:49 +0100139 int ret;
David Howellsa5af7e12016-10-06 08:11:49 +0100140 u8 reason;
David Howells8d94aa32016-09-07 09:19:31 +0100141
David Howells5273a192020-01-30 21:50:36 +0000142 if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
David Howells8d94aa32016-09-07 09:19:31 +0100143 return -ECONNRESET;
144
145 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
David Howells5273a192020-01-30 21:50:36 +0000146 if (!pkt)
David Howells8d94aa32016-09-07 09:19:31 +0100147 return -ENOMEM;
David Howells5273a192020-01-30 21:50:36 +0000148
149 conn = call->conn;
David Howells8d94aa32016-09-07 09:19:31 +0100150
David Howells8d94aa32016-09-07 09:19:31 +0100151 msg.msg_name = &call->peer->srx.transport;
152 msg.msg_namelen = call->peer->srx.transport_len;
153 msg.msg_control = NULL;
154 msg.msg_controllen = 0;
155 msg.msg_flags = 0;
156
157 pkt->whdr.epoch = htonl(conn->proto.epoch);
158 pkt->whdr.cid = htonl(call->cid);
159 pkt->whdr.callNumber = htonl(call->call_id);
160 pkt->whdr.seq = 0;
David Howells26cb02a2016-10-06 08:11:49 +0100161 pkt->whdr.type = RXRPC_PACKET_TYPE_ACK;
162 pkt->whdr.flags = RXRPC_SLOW_START_OK | conn->out_clientflag;
David Howells8d94aa32016-09-07 09:19:31 +0100163 pkt->whdr.userStatus = 0;
164 pkt->whdr.securityIndex = call->security_ix;
165 pkt->whdr._rsvd = 0;
166 pkt->whdr.serviceId = htons(call->service_id);
167
David Howells26cb02a2016-10-06 08:11:49 +0100168 spin_lock_bh(&call->lock);
David Howellsa5af7e12016-10-06 08:11:49 +0100169 if (ping) {
170 reason = RXRPC_ACK_PING;
171 } else {
172 reason = call->ackr_reason;
173 if (!call->ackr_reason) {
174 spin_unlock_bh(&call->lock);
175 ret = 0;
176 goto out;
177 }
178 call->ackr_reason = 0;
David Howells8d94aa32016-09-07 09:19:31 +0100179 }
David Howells1457cc42017-11-02 15:06:55 +0000180 n = rxrpc_fill_out_ack(conn, call, pkt, &hard_ack, &top, reason);
David Howells26cb02a2016-10-06 08:11:49 +0100181
182 spin_unlock_bh(&call->lock);
183
184 iov[0].iov_base = pkt;
185 iov[0].iov_len = sizeof(pkt->whdr) + sizeof(pkt->ack) + n;
186 iov[1].iov_base = &pkt->ackinfo;
187 iov[1].iov_len = sizeof(pkt->ackinfo);
188 len = iov[0].iov_len + iov[1].iov_len;
David Howells8d94aa32016-09-07 09:19:31 +0100189
David Howellsb86e2182016-09-23 15:08:48 +0100190 serial = atomic_inc_return(&conn->serial);
191 pkt->whdr.serial = htonl(serial);
David Howells4764c0d2018-07-23 17:18:37 +0100192 trace_rxrpc_tx_ack(call->debug_id, serial,
David Howells26cb02a2016-10-06 08:11:49 +0100193 ntohl(pkt->ack.firstPacket),
194 ntohl(pkt->ack.serial),
195 pkt->ack.reason, pkt->ack.nAcks);
David Howellsbd1fdf82017-11-24 10:18:42 +0000196 if (_serial)
197 *_serial = serial;
David Howellsb86e2182016-09-23 15:08:48 +0100198
David Howells8e831342016-09-22 00:29:31 +0100199 if (ping) {
David Howellsa5af7e12016-10-06 08:11:49 +0100200 call->ping_serial = serial;
David Howells8e831342016-09-22 00:29:31 +0100201 smp_wmb();
202 /* We need to stick a time in before we send the packet in case
203 * the reply gets back before kernel_sendmsg() completes - but
204 * asking UDP to send the packet can take a relatively long
David Howellsb604dd92018-09-27 15:13:08 +0100205 * time.
David Howells8e831342016-09-22 00:29:31 +0100206 */
David Howellsa5af7e12016-10-06 08:11:49 +0100207 call->ping_time = ktime_get_real();
David Howells8e831342016-09-22 00:29:31 +0100208 set_bit(RXRPC_CALL_PINGING, &call->flags);
209 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_ping, serial);
210 }
David Howells26cb02a2016-10-06 08:11:49 +0100211
212 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
David Howells330bdcf2018-08-08 11:30:02 +0100213 conn->params.peer->last_tx_at = ktime_get_seconds();
David Howells6b47fe12018-05-10 23:26:01 +0100214 if (ret < 0)
215 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
David Howells4764c0d2018-07-23 17:18:37 +0100216 rxrpc_tx_point_call_ack);
217 else
218 trace_rxrpc_tx_packet(call->debug_id, &pkt->whdr,
219 rxrpc_tx_point_call_ack);
David Howellsc7e86ac2018-11-01 13:39:53 +0000220 rxrpc_tx_backoff(call, ret);
David Howells8d94aa32016-09-07 09:19:31 +0100221
David Howells26cb02a2016-10-06 08:11:49 +0100222 if (call->state < RXRPC_CALL_COMPLETE) {
David Howells805b21b2016-09-24 18:05:26 +0100223 if (ret < 0) {
David Howellsa5af7e12016-10-06 08:11:49 +0100224 if (ping)
225 clear_bit(RXRPC_CALL_PINGING, &call->flags);
David Howells248f2192016-09-08 11:10:12 +0100226 rxrpc_propose_ACK(call, pkt->ack.reason,
David Howells248f2192016-09-08 11:10:12 +0100227 ntohl(pkt->ack.serial),
David Howellsc7e86ac2018-11-01 13:39:53 +0000228 false, true,
David Howells9c7ad432016-09-23 13:50:40 +0100229 rxrpc_propose_ack_retry_tx);
David Howells805b21b2016-09-24 18:05:26 +0100230 } else {
231 spin_lock_bh(&call->lock);
232 if (after(hard_ack, call->ackr_consumed))
233 call->ackr_consumed = hard_ack;
234 if (after(top, call->ackr_seen))
235 call->ackr_seen = top;
236 spin_unlock_bh(&call->lock);
David Howells248f2192016-09-08 11:10:12 +0100237 }
David Howells415f44e2017-11-24 10:18:42 +0000238
239 rxrpc_set_keepalive(call);
David Howells248f2192016-09-08 11:10:12 +0100240 }
241
David Howells8d94aa32016-09-07 09:19:31 +0100242out:
David Howells8d94aa32016-09-07 09:19:31 +0100243 kfree(pkt);
244 return ret;
245}
246
David Howells5873c082014-02-07 18:58:44 +0000247/*
David Howells26cb02a2016-10-06 08:11:49 +0100248 * Send an ABORT call packet.
249 */
250int rxrpc_send_abort_packet(struct rxrpc_call *call)
251{
David Howells5273a192020-01-30 21:50:36 +0000252 struct rxrpc_connection *conn;
David Howells26cb02a2016-10-06 08:11:49 +0100253 struct rxrpc_abort_buffer pkt;
254 struct msghdr msg;
255 struct kvec iov[1];
256 rxrpc_serial_t serial;
257 int ret;
258
David Howellsdcbefc32017-11-02 15:06:26 +0000259 /* Don't bother sending aborts for a client call once the server has
260 * hard-ACK'd all of its request data. After that point, we're not
261 * going to stop the operation proceeding, and whilst we might limit
262 * the reply, it's not worth it if we can send a new call on the same
263 * channel instead, thereby closing off this call.
264 */
265 if (rxrpc_is_client_call(call) &&
266 test_bit(RXRPC_CALL_TX_LAST, &call->flags))
267 return 0;
268
David Howells5273a192020-01-30 21:50:36 +0000269 if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
David Howells26cb02a2016-10-06 08:11:49 +0100270 return -ECONNRESET;
271
David Howells5273a192020-01-30 21:50:36 +0000272 conn = call->conn;
273
David Howells26cb02a2016-10-06 08:11:49 +0100274 msg.msg_name = &call->peer->srx.transport;
275 msg.msg_namelen = call->peer->srx.transport_len;
276 msg.msg_control = NULL;
277 msg.msg_controllen = 0;
278 msg.msg_flags = 0;
279
280 pkt.whdr.epoch = htonl(conn->proto.epoch);
281 pkt.whdr.cid = htonl(call->cid);
282 pkt.whdr.callNumber = htonl(call->call_id);
283 pkt.whdr.seq = 0;
284 pkt.whdr.type = RXRPC_PACKET_TYPE_ABORT;
285 pkt.whdr.flags = conn->out_clientflag;
286 pkt.whdr.userStatus = 0;
287 pkt.whdr.securityIndex = call->security_ix;
288 pkt.whdr._rsvd = 0;
289 pkt.whdr.serviceId = htons(call->service_id);
290 pkt.abort_code = htonl(call->abort_code);
291
292 iov[0].iov_base = &pkt;
293 iov[0].iov_len = sizeof(pkt);
294
295 serial = atomic_inc_return(&conn->serial);
296 pkt.whdr.serial = htonl(serial);
297
298 ret = kernel_sendmsg(conn->params.local->socket,
299 &msg, iov, 1, sizeof(pkt));
David Howells330bdcf2018-08-08 11:30:02 +0100300 conn->params.peer->last_tx_at = ktime_get_seconds();
David Howells6b47fe12018-05-10 23:26:01 +0100301 if (ret < 0)
302 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
David Howells4764c0d2018-07-23 17:18:37 +0100303 rxrpc_tx_point_call_abort);
304 else
305 trace_rxrpc_tx_packet(call->debug_id, &pkt.whdr,
306 rxrpc_tx_point_call_abort);
David Howellsc7e86ac2018-11-01 13:39:53 +0000307 rxrpc_tx_backoff(call, ret);
David Howells26cb02a2016-10-06 08:11:49 +0100308 return ret;
309}
310
311/*
David Howells17926a72007-04-26 15:48:28 -0700312 * send a packet through the transport endpoint
313 */
David Howellsa1767072016-09-29 22:37:15 +0100314int rxrpc_send_data_packet(struct rxrpc_call *call, struct sk_buff *skb,
315 bool retrans)
David Howells17926a72007-04-26 15:48:28 -0700316{
David Howells5a924b82016-09-22 00:29:31 +0100317 struct rxrpc_connection *conn = call->conn;
318 struct rxrpc_wire_header whdr;
319 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700320 struct msghdr msg;
David Howells5a924b82016-09-22 00:29:31 +0100321 struct kvec iov[2];
322 rxrpc_serial_t serial;
323 size_t len;
David Howells17926a72007-04-26 15:48:28 -0700324 int ret, opt;
325
326 _enter(",{%d}", skb->len);
327
David Howells5a924b82016-09-22 00:29:31 +0100328 /* Each transmission of a Tx packet needs a new serial number */
329 serial = atomic_inc_return(&conn->serial);
David Howells17926a72007-04-26 15:48:28 -0700330
David Howells5a924b82016-09-22 00:29:31 +0100331 whdr.epoch = htonl(conn->proto.epoch);
332 whdr.cid = htonl(call->cid);
333 whdr.callNumber = htonl(call->call_id);
334 whdr.seq = htonl(sp->hdr.seq);
335 whdr.serial = htonl(serial);
336 whdr.type = RXRPC_PACKET_TYPE_DATA;
337 whdr.flags = sp->hdr.flags;
338 whdr.userStatus = 0;
339 whdr.securityIndex = call->security_ix;
340 whdr._rsvd = htons(sp->hdr._rsvd);
341 whdr.serviceId = htons(call->service_id);
342
David Howells4e255722017-06-05 14:30:49 +0100343 if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) &&
344 sp->hdr.seq == 1)
345 whdr.userStatus = RXRPC_USERSTATUS_SERVICE_UPGRADE;
346
David Howells5a924b82016-09-22 00:29:31 +0100347 iov[0].iov_base = &whdr;
348 iov[0].iov_len = sizeof(whdr);
349 iov[1].iov_base = skb->head;
350 iov[1].iov_len = skb->len;
351 len = iov[0].iov_len + iov[1].iov_len;
352
353 msg.msg_name = &call->peer->srx.transport;
354 msg.msg_namelen = call->peer->srx.transport_len;
David Howells17926a72007-04-26 15:48:28 -0700355 msg.msg_control = NULL;
356 msg.msg_controllen = 0;
357 msg.msg_flags = 0;
358
David Howells57494342016-09-24 18:05:27 +0100359 /* If our RTT cache needs working on, request an ACK. Also request
360 * ACKs if a DATA packet appears to have been lost.
David Howellsb604dd92018-09-27 15:13:08 +0100361 *
362 * However, we mustn't request an ACK on the last reply packet of a
363 * service call, lest OpenAFS incorrectly send us an ACK with some
364 * soft-ACKs in it and then never follow up with a proper hard ACK.
David Howells57494342016-09-24 18:05:27 +0100365 */
David Howellsb604dd92018-09-27 15:13:08 +0100366 if ((!(sp->hdr.flags & RXRPC_LAST_PACKET) ||
367 rxrpc_to_server(sp)
368 ) &&
David Howellsbd1fdf82017-11-24 10:18:42 +0000369 (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events) ||
370 retrans ||
David Howellsbf7d6202016-10-06 08:11:51 +0100371 call->cong_mode == RXRPC_CALL_SLOW_START ||
372 (call->peer->rtt_usage < 3 && sp->hdr.seq & 1) ||
373 ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
374 ktime_get_real())))
David Howells0d4b1032016-09-22 00:29:31 +0100375 whdr.flags |= RXRPC_REQUEST_ACK;
376
David Howells8a681c362016-09-17 10:49:15 +0100377 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
378 static int lose;
379 if ((lose++ & 7) == 7) {
David Howellsa1767072016-09-29 22:37:15 +0100380 ret = 0;
Arnd Bergmann526949e2019-03-22 15:18:43 +0100381 trace_rxrpc_tx_data(call, sp->hdr.seq, serial,
382 whdr.flags, retrans, true);
383 goto done;
David Howells8a681c362016-09-17 10:49:15 +0100384 }
385 }
386
Arnd Bergmann526949e2019-03-22 15:18:43 +0100387 trace_rxrpc_tx_data(call, sp->hdr.seq, serial, whdr.flags, retrans,
388 false);
David Howells5a924b82016-09-22 00:29:31 +0100389
David Howells17926a72007-04-26 15:48:28 -0700390 /* send the packet with the don't fragment bit set if we currently
391 * think it's small enough */
David Howells5a924b82016-09-22 00:29:31 +0100392 if (iov[1].iov_len >= call->peer->maxdata)
393 goto send_fragmentable;
David Howells17926a72007-04-26 15:48:28 -0700394
David Howells5a924b82016-09-22 00:29:31 +0100395 down_read(&conn->params.local->defrag_sem);
David Howellsb604dd92018-09-27 15:13:08 +0100396
397 sp->hdr.serial = serial;
398 smp_wmb(); /* Set serial before timestamp */
399 skb->tstamp = ktime_get_real();
400
David Howells5a924b82016-09-22 00:29:31 +0100401 /* send the packet by UDP
402 * - returns -EMSGSIZE if UDP would have to fragment the packet
403 * to go out of the interface
404 * - in which case, we'll have processed the ICMP error
405 * message and update the peer record
406 */
407 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
David Howells330bdcf2018-08-08 11:30:02 +0100408 conn->params.peer->last_tx_at = ktime_get_seconds();
David Howells17926a72007-04-26 15:48:28 -0700409
David Howells5a924b82016-09-22 00:29:31 +0100410 up_read(&conn->params.local->defrag_sem);
David Howells6b47fe12018-05-10 23:26:01 +0100411 if (ret < 0)
412 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
David Howells4764c0d2018-07-23 17:18:37 +0100413 rxrpc_tx_point_call_data_nofrag);
414 else
415 trace_rxrpc_tx_packet(call->debug_id, &whdr,
416 rxrpc_tx_point_call_data_nofrag);
David Howellsc7e86ac2018-11-01 13:39:53 +0000417 rxrpc_tx_backoff(call, ret);
David Howells5a924b82016-09-22 00:29:31 +0100418 if (ret == -EMSGSIZE)
419 goto send_fragmentable;
420
421done:
David Howells50235c42016-09-22 00:29:31 +0100422 if (ret >= 0) {
David Howells0d4b1032016-09-22 00:29:31 +0100423 if (whdr.flags & RXRPC_REQUEST_ACK) {
David Howellsb604dd92018-09-27 15:13:08 +0100424 call->peer->rtt_last_req = skb->tstamp;
David Howells50235c42016-09-22 00:29:31 +0100425 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_data, serial);
David Howellsbd1fdf82017-11-24 10:18:42 +0000426 if (call->peer->rtt_usage > 1) {
427 unsigned long nowj = jiffies, ack_lost_at;
428
429 ack_lost_at = nsecs_to_jiffies(2 * call->peer->rtt);
430 if (ack_lost_at < 1)
431 ack_lost_at = 1;
432
433 ack_lost_at += nowj;
434 WRITE_ONCE(call->ack_lost_at, ack_lost_at);
435 rxrpc_reduce_call_timer(call, ack_lost_at, nowj,
436 rxrpc_timer_set_for_lost_ack);
437 }
David Howells0d4b1032016-09-22 00:29:31 +0100438 }
David Howellsc54e43d2018-05-10 23:26:00 +0100439
440 if (sp->hdr.seq == 1 &&
441 !test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER,
442 &call->flags)) {
443 unsigned long nowj = jiffies, expect_rx_by;
444
445 expect_rx_by = nowj + call->next_rx_timo;
446 WRITE_ONCE(call->expect_rx_by, expect_rx_by);
447 rxrpc_reduce_call_timer(call, expect_rx_by, nowj,
448 rxrpc_timer_set_for_normal);
449 }
David Howells415f44e2017-11-24 10:18:42 +0000450
David Howellsc7e86ac2018-11-01 13:39:53 +0000451 rxrpc_set_keepalive(call);
452 } else {
453 /* Cancel the call if the initial transmission fails,
454 * particularly if that's due to network routing issues that
455 * aren't going away anytime soon. The layer above can arrange
456 * the retransmission.
457 */
458 if (!test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER, &call->flags))
459 rxrpc_set_call_completion(call, RXRPC_CALL_LOCAL_ERROR,
460 RX_USER_ABORT, ret);
461 }
David Howells415f44e2017-11-24 10:18:42 +0000462
David Howells5a924b82016-09-22 00:29:31 +0100463 _leave(" = %d [%u]", ret, call->peer->maxdata);
464 return ret;
David Howells17926a72007-04-26 15:48:28 -0700465
466send_fragmentable:
467 /* attempt to send this message with fragmentation enabled */
468 _debug("send fragment");
469
David Howells985a5c82016-06-17 11:53:37 +0100470 down_write(&conn->params.local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -0700471
David Howellsb604dd92018-09-27 15:13:08 +0100472 sp->hdr.serial = serial;
473 smp_wmb(); /* Set serial before timestamp */
474 skb->tstamp = ktime_get_real();
475
David Howells985a5c82016-06-17 11:53:37 +0100476 switch (conn->params.local->srx.transport.family) {
477 case AF_INET:
478 opt = IP_PMTUDISC_DONT;
479 ret = kernel_setsockopt(conn->params.local->socket,
480 SOL_IP, IP_MTU_DISCOVER,
481 (char *)&opt, sizeof(opt));
482 if (ret == 0) {
David Howells5a924b82016-09-22 00:29:31 +0100483 ret = kernel_sendmsg(conn->params.local->socket, &msg,
484 iov, 2, len);
David Howells330bdcf2018-08-08 11:30:02 +0100485 conn->params.peer->last_tx_at = ktime_get_seconds();
David Howells985a5c82016-06-17 11:53:37 +0100486
487 opt = IP_PMTUDISC_DO;
488 kernel_setsockopt(conn->params.local->socket, SOL_IP,
489 IP_MTU_DISCOVER,
490 (char *)&opt, sizeof(opt));
491 }
492 break;
David Howells75b54cb2016-09-13 08:49:05 +0100493
David Howellsd1912742016-09-17 07:26:01 +0100494#ifdef CONFIG_AF_RXRPC_IPV6
David Howells75b54cb2016-09-13 08:49:05 +0100495 case AF_INET6:
496 opt = IPV6_PMTUDISC_DONT;
497 ret = kernel_setsockopt(conn->params.local->socket,
498 SOL_IPV6, IPV6_MTU_DISCOVER,
499 (char *)&opt, sizeof(opt));
500 if (ret == 0) {
501 ret = kernel_sendmsg(conn->params.local->socket, &msg,
David Howells93c62c42018-02-22 14:38:14 +0000502 iov, 2, len);
David Howells330bdcf2018-08-08 11:30:02 +0100503 conn->params.peer->last_tx_at = ktime_get_seconds();
David Howells75b54cb2016-09-13 08:49:05 +0100504
505 opt = IPV6_PMTUDISC_DO;
506 kernel_setsockopt(conn->params.local->socket,
507 SOL_IPV6, IPV6_MTU_DISCOVER,
508 (char *)&opt, sizeof(opt));
509 }
510 break;
David Howellsd1912742016-09-17 07:26:01 +0100511#endif
David Howells3427beb2019-07-02 15:55:28 +0100512
513 default:
514 BUG();
David Howells17926a72007-04-26 15:48:28 -0700515 }
516
David Howells6b47fe12018-05-10 23:26:01 +0100517 if (ret < 0)
518 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
David Howells4764c0d2018-07-23 17:18:37 +0100519 rxrpc_tx_point_call_data_frag);
520 else
521 trace_rxrpc_tx_packet(call->debug_id, &whdr,
522 rxrpc_tx_point_call_data_frag);
David Howellsc7e86ac2018-11-01 13:39:53 +0000523 rxrpc_tx_backoff(call, ret);
David Howells6b47fe12018-05-10 23:26:01 +0100524
David Howells985a5c82016-06-17 11:53:37 +0100525 up_write(&conn->params.local->defrag_sem);
David Howells5a924b82016-09-22 00:29:31 +0100526 goto done;
David Howells17926a72007-04-26 15:48:28 -0700527}
David Howells248f2192016-09-08 11:10:12 +0100528
529/*
530 * reject packets through the local endpoint
531 */
532void rxrpc_reject_packets(struct rxrpc_local *local)
533{
David Howells1c2bc7b2016-09-13 08:49:05 +0100534 struct sockaddr_rxrpc srx;
David Howells248f2192016-09-08 11:10:12 +0100535 struct rxrpc_skb_priv *sp;
536 struct rxrpc_wire_header whdr;
537 struct sk_buff *skb;
538 struct msghdr msg;
539 struct kvec iov[2];
540 size_t size;
541 __be32 code;
David Howellsece64fe2018-09-27 15:13:08 +0100542 int ret, ioc;
David Howells248f2192016-09-08 11:10:12 +0100543
544 _enter("%d", local->debug_id);
545
546 iov[0].iov_base = &whdr;
547 iov[0].iov_len = sizeof(whdr);
548 iov[1].iov_base = &code;
549 iov[1].iov_len = sizeof(code);
David Howells248f2192016-09-08 11:10:12 +0100550
David Howells1c2bc7b2016-09-13 08:49:05 +0100551 msg.msg_name = &srx.transport;
David Howells248f2192016-09-08 11:10:12 +0100552 msg.msg_control = NULL;
553 msg.msg_controllen = 0;
554 msg.msg_flags = 0;
555
David Howells248f2192016-09-08 11:10:12 +0100556 memset(&whdr, 0, sizeof(whdr));
David Howells248f2192016-09-08 11:10:12 +0100557
558 while ((skb = skb_dequeue(&local->reject_queue))) {
David Howells987db9f2019-08-19 09:25:38 +0100559 rxrpc_see_skb(skb, rxrpc_skb_seen);
David Howells248f2192016-09-08 11:10:12 +0100560 sp = rxrpc_skb(skb);
David Howells1c2bc7b2016-09-13 08:49:05 +0100561
David Howellsece64fe2018-09-27 15:13:08 +0100562 switch (skb->mark) {
563 case RXRPC_SKB_MARK_REJECT_BUSY:
564 whdr.type = RXRPC_PACKET_TYPE_BUSY;
565 size = sizeof(whdr);
566 ioc = 1;
567 break;
568 case RXRPC_SKB_MARK_REJECT_ABORT:
569 whdr.type = RXRPC_PACKET_TYPE_ABORT;
570 code = htonl(skb->priority);
571 size = sizeof(whdr) + sizeof(code);
572 ioc = 2;
573 break;
574 default:
David Howells987db9f2019-08-19 09:25:38 +0100575 rxrpc_free_skb(skb, rxrpc_skb_freed);
David Howellsece64fe2018-09-27 15:13:08 +0100576 continue;
577 }
578
David Howells5a790b72018-10-04 09:32:28 +0100579 if (rxrpc_extract_addr_from_skb(&srx, skb) == 0) {
David Howells1c2bc7b2016-09-13 08:49:05 +0100580 msg.msg_namelen = srx.transport_len;
581
David Howells248f2192016-09-08 11:10:12 +0100582 whdr.epoch = htonl(sp->hdr.epoch);
583 whdr.cid = htonl(sp->hdr.cid);
584 whdr.callNumber = htonl(sp->hdr.callNumber);
585 whdr.serviceId = htons(sp->hdr.serviceId);
586 whdr.flags = sp->hdr.flags;
587 whdr.flags ^= RXRPC_CLIENT_INITIATED;
588 whdr.flags &= RXRPC_CLIENT_INITIATED;
589
YueHaibingd6672a52018-10-11 22:32:39 +0100590 ret = kernel_sendmsg(local->socket, &msg,
591 iov, ioc, size);
David Howells6b47fe12018-05-10 23:26:01 +0100592 if (ret < 0)
593 trace_rxrpc_tx_fail(local->debug_id, 0, ret,
David Howells4764c0d2018-07-23 17:18:37 +0100594 rxrpc_tx_point_reject);
595 else
596 trace_rxrpc_tx_packet(local->debug_id, &whdr,
597 rxrpc_tx_point_reject);
David Howells248f2192016-09-08 11:10:12 +0100598 }
599
David Howells987db9f2019-08-19 09:25:38 +0100600 rxrpc_free_skb(skb, rxrpc_skb_freed);
David Howells248f2192016-09-08 11:10:12 +0100601 }
602
603 _leave("");
604}
David Howellsace45be2018-03-30 21:04:43 +0100605
606/*
607 * Send a VERSION reply to a peer as a keepalive.
608 */
609void rxrpc_send_keepalive(struct rxrpc_peer *peer)
610{
611 struct rxrpc_wire_header whdr;
612 struct msghdr msg;
613 struct kvec iov[2];
614 size_t len;
615 int ret;
616
617 _enter("");
618
619 msg.msg_name = &peer->srx.transport;
620 msg.msg_namelen = peer->srx.transport_len;
621 msg.msg_control = NULL;
622 msg.msg_controllen = 0;
623 msg.msg_flags = 0;
624
625 whdr.epoch = htonl(peer->local->rxnet->epoch);
626 whdr.cid = 0;
627 whdr.callNumber = 0;
628 whdr.seq = 0;
629 whdr.serial = 0;
630 whdr.type = RXRPC_PACKET_TYPE_VERSION; /* Not client-initiated */
631 whdr.flags = RXRPC_LAST_PACKET;
632 whdr.userStatus = 0;
633 whdr.securityIndex = 0;
634 whdr._rsvd = 0;
635 whdr.serviceId = 0;
636
637 iov[0].iov_base = &whdr;
638 iov[0].iov_len = sizeof(whdr);
639 iov[1].iov_base = (char *)rxrpc_keepalive_string;
640 iov[1].iov_len = sizeof(rxrpc_keepalive_string);
641
642 len = iov[0].iov_len + iov[1].iov_len;
643
644 _proto("Tx VERSION (keepalive)");
645
646 ret = kernel_sendmsg(peer->local->socket, &msg, iov, 2, len);
647 if (ret < 0)
David Howells6b47fe12018-05-10 23:26:01 +0100648 trace_rxrpc_tx_fail(peer->debug_id, 0, ret,
David Howells4764c0d2018-07-23 17:18:37 +0100649 rxrpc_tx_point_version_keepalive);
650 else
651 trace_rxrpc_tx_packet(peer->debug_id, &whdr,
652 rxrpc_tx_point_version_keepalive);
David Howellsace45be2018-03-30 21:04:43 +0100653
David Howells330bdcf2018-08-08 11:30:02 +0100654 peer->last_tx_at = ktime_get_seconds();
David Howellsace45be2018-03-30 21:04:43 +0100655 _leave("");
656}