blob: 8a4da3fe96df5c55ba857af6511c0b58811989b9 [file] [log] [blame]
David Howells17926a72007-04-26 15:48:28 -07001/* RxRPC packet transmission
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
Joe Perches9b6d5392016-06-02 12:08:52 -070012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
David Howells17926a72007-04-26 15:48:28 -070014#include <linux/net.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/gfp.h>
David Howells17926a72007-04-26 15:48:28 -070016#include <linux/skbuff.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040017#include <linux/export.h>
David Howells17926a72007-04-26 15:48:28 -070018#include <net/sock.h>
19#include <net/af_rxrpc.h>
20#include "ar-internal.h"
21
David Howells26cb02a2016-10-06 08:11:49 +010022struct rxrpc_ack_buffer {
David Howells8d94aa32016-09-07 09:19:31 +010023 struct rxrpc_wire_header whdr;
David Howells26cb02a2016-10-06 08:11:49 +010024 struct rxrpc_ackpacket ack;
25 u8 acks[255];
26 u8 pad[3];
David Howells8d94aa32016-09-07 09:19:31 +010027 struct rxrpc_ackinfo ackinfo;
28};
29
David Howells26cb02a2016-10-06 08:11:49 +010030struct rxrpc_abort_buffer {
31 struct rxrpc_wire_header whdr;
32 __be32 abort_code;
33};
34
David Howellsace45be2018-03-30 21:04:43 +010035static const char rxrpc_keepalive_string[] = "";
36
David Howells8d94aa32016-09-07 09:19:31 +010037/*
David Howells415f44e2017-11-24 10:18:42 +000038 * Arrange for a keepalive ping a certain time after we last transmitted. This
39 * lets the far side know we're still interested in this call and helps keep
40 * the route through any intervening firewall open.
41 *
42 * Receiving a response to the ping will prevent the ->expect_rx_by timer from
43 * expiring.
44 */
45static void rxrpc_set_keepalive(struct rxrpc_call *call)
46{
47 unsigned long now = jiffies, keepalive_at = call->next_rx_timo / 6;
48
49 keepalive_at += now;
50 WRITE_ONCE(call->keepalive_at, keepalive_at);
51 rxrpc_reduce_call_timer(call, keepalive_at, now,
52 rxrpc_timer_set_for_keepalive);
53}
54
55/*
David Howells8d94aa32016-09-07 09:19:31 +010056 * Fill out an ACK packet.
57 */
David Howells1457cc42017-11-02 15:06:55 +000058static size_t rxrpc_fill_out_ack(struct rxrpc_connection *conn,
59 struct rxrpc_call *call,
David Howells26cb02a2016-10-06 08:11:49 +010060 struct rxrpc_ack_buffer *pkt,
David Howells805b21b2016-09-24 18:05:26 +010061 rxrpc_seq_t *_hard_ack,
David Howellsa5af7e12016-10-06 08:11:49 +010062 rxrpc_seq_t *_top,
63 u8 reason)
David Howells8d94aa32016-09-07 09:19:31 +010064{
David Howellsf3639df2016-09-17 10:49:13 +010065 rxrpc_serial_t serial;
David Howells248f2192016-09-08 11:10:12 +010066 rxrpc_seq_t hard_ack, top, seq;
67 int ix;
David Howells8d94aa32016-09-07 09:19:31 +010068 u32 mtu, jmax;
69 u8 *ackp = pkt->acks;
70
David Howells248f2192016-09-08 11:10:12 +010071 /* Barrier against rxrpc_input_data(). */
David Howellsf3639df2016-09-17 10:49:13 +010072 serial = call->ackr_serial;
David Howells248f2192016-09-08 11:10:12 +010073 hard_ack = READ_ONCE(call->rx_hard_ack);
74 top = smp_load_acquire(&call->rx_top);
David Howells805b21b2016-09-24 18:05:26 +010075 *_hard_ack = hard_ack;
76 *_top = top;
David Howells248f2192016-09-08 11:10:12 +010077
David Howells8d94aa32016-09-07 09:19:31 +010078 pkt->ack.bufferSpace = htons(8);
David Howells248f2192016-09-08 11:10:12 +010079 pkt->ack.maxSkew = htons(call->ackr_skew);
80 pkt->ack.firstPacket = htonl(hard_ack + 1);
David Howells8d94aa32016-09-07 09:19:31 +010081 pkt->ack.previousPacket = htonl(call->ackr_prev_seq);
David Howellsf3639df2016-09-17 10:49:13 +010082 pkt->ack.serial = htonl(serial);
David Howellsa5af7e12016-10-06 08:11:49 +010083 pkt->ack.reason = reason;
David Howells248f2192016-09-08 11:10:12 +010084 pkt->ack.nAcks = top - hard_ack;
David Howells8d94aa32016-09-07 09:19:31 +010085
David Howellsa5af7e12016-10-06 08:11:49 +010086 if (reason == RXRPC_ACK_PING)
David Howells8e831342016-09-22 00:29:31 +010087 pkt->whdr.flags |= RXRPC_REQUEST_ACK;
88
David Howells248f2192016-09-08 11:10:12 +010089 if (after(top, hard_ack)) {
90 seq = hard_ack + 1;
91 do {
92 ix = seq & RXRPC_RXTX_BUFF_MASK;
93 if (call->rxtx_buffer[ix])
94 *ackp++ = RXRPC_ACK_TYPE_ACK;
95 else
96 *ackp++ = RXRPC_ACK_TYPE_NACK;
97 seq++;
98 } while (before_eq(seq, top));
99 }
100
David Howells1457cc42017-11-02 15:06:55 +0000101 mtu = conn->params.peer->if_mtu;
102 mtu -= conn->params.peer->hdrsize;
David Howells75e42122016-09-13 22:36:22 +0100103 jmax = (call->nr_jumbo_bad > 3) ? 1 : rxrpc_rx_jumbo_max;
David Howells8d94aa32016-09-07 09:19:31 +0100104 pkt->ackinfo.rxMTU = htonl(rxrpc_rx_mtu);
105 pkt->ackinfo.maxMTU = htonl(mtu);
David Howells75e42122016-09-13 22:36:22 +0100106 pkt->ackinfo.rwind = htonl(call->rx_winsize);
David Howells8d94aa32016-09-07 09:19:31 +0100107 pkt->ackinfo.jumbo_max = htonl(jmax);
108
109 *ackp++ = 0;
110 *ackp++ = 0;
111 *ackp++ = 0;
David Howells248f2192016-09-08 11:10:12 +0100112 return top - hard_ack + 3;
David Howells8d94aa32016-09-07 09:19:31 +0100113}
114
115/*
David Howells26cb02a2016-10-06 08:11:49 +0100116 * Send an ACK call packet.
David Howells8d94aa32016-09-07 09:19:31 +0100117 */
David Howellsbd1fdf82017-11-24 10:18:42 +0000118int rxrpc_send_ack_packet(struct rxrpc_call *call, bool ping,
119 rxrpc_serial_t *_serial)
David Howells8d94aa32016-09-07 09:19:31 +0100120{
121 struct rxrpc_connection *conn = NULL;
David Howells26cb02a2016-10-06 08:11:49 +0100122 struct rxrpc_ack_buffer *pkt;
David Howells8d94aa32016-09-07 09:19:31 +0100123 struct msghdr msg;
124 struct kvec iov[2];
125 rxrpc_serial_t serial;
David Howells805b21b2016-09-24 18:05:26 +0100126 rxrpc_seq_t hard_ack, top;
David Howells8d94aa32016-09-07 09:19:31 +0100127 size_t len, n;
David Howells26cb02a2016-10-06 08:11:49 +0100128 int ret;
David Howellsa5af7e12016-10-06 08:11:49 +0100129 u8 reason;
David Howells8d94aa32016-09-07 09:19:31 +0100130
131 spin_lock_bh(&call->lock);
132 if (call->conn)
133 conn = rxrpc_get_connection_maybe(call->conn);
134 spin_unlock_bh(&call->lock);
135 if (!conn)
136 return -ECONNRESET;
137
138 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
139 if (!pkt) {
140 rxrpc_put_connection(conn);
141 return -ENOMEM;
142 }
143
David Howells8d94aa32016-09-07 09:19:31 +0100144 msg.msg_name = &call->peer->srx.transport;
145 msg.msg_namelen = call->peer->srx.transport_len;
146 msg.msg_control = NULL;
147 msg.msg_controllen = 0;
148 msg.msg_flags = 0;
149
150 pkt->whdr.epoch = htonl(conn->proto.epoch);
151 pkt->whdr.cid = htonl(call->cid);
152 pkt->whdr.callNumber = htonl(call->call_id);
153 pkt->whdr.seq = 0;
David Howells26cb02a2016-10-06 08:11:49 +0100154 pkt->whdr.type = RXRPC_PACKET_TYPE_ACK;
155 pkt->whdr.flags = RXRPC_SLOW_START_OK | conn->out_clientflag;
David Howells8d94aa32016-09-07 09:19:31 +0100156 pkt->whdr.userStatus = 0;
157 pkt->whdr.securityIndex = call->security_ix;
158 pkt->whdr._rsvd = 0;
159 pkt->whdr.serviceId = htons(call->service_id);
160
David Howells26cb02a2016-10-06 08:11:49 +0100161 spin_lock_bh(&call->lock);
David Howellsa5af7e12016-10-06 08:11:49 +0100162 if (ping) {
163 reason = RXRPC_ACK_PING;
164 } else {
165 reason = call->ackr_reason;
166 if (!call->ackr_reason) {
167 spin_unlock_bh(&call->lock);
168 ret = 0;
169 goto out;
170 }
171 call->ackr_reason = 0;
David Howells8d94aa32016-09-07 09:19:31 +0100172 }
David Howells1457cc42017-11-02 15:06:55 +0000173 n = rxrpc_fill_out_ack(conn, call, pkt, &hard_ack, &top, reason);
David Howells26cb02a2016-10-06 08:11:49 +0100174
175 spin_unlock_bh(&call->lock);
176
177 iov[0].iov_base = pkt;
178 iov[0].iov_len = sizeof(pkt->whdr) + sizeof(pkt->ack) + n;
179 iov[1].iov_base = &pkt->ackinfo;
180 iov[1].iov_len = sizeof(pkt->ackinfo);
181 len = iov[0].iov_len + iov[1].iov_len;
David Howells8d94aa32016-09-07 09:19:31 +0100182
David Howellsb86e2182016-09-23 15:08:48 +0100183 serial = atomic_inc_return(&conn->serial);
184 pkt->whdr.serial = htonl(serial);
David Howells4764c0d2018-07-23 17:18:37 +0100185 trace_rxrpc_tx_ack(call->debug_id, serial,
David Howells26cb02a2016-10-06 08:11:49 +0100186 ntohl(pkt->ack.firstPacket),
187 ntohl(pkt->ack.serial),
188 pkt->ack.reason, pkt->ack.nAcks);
David Howellsbd1fdf82017-11-24 10:18:42 +0000189 if (_serial)
190 *_serial = serial;
David Howellsb86e2182016-09-23 15:08:48 +0100191
David Howells8e831342016-09-22 00:29:31 +0100192 if (ping) {
David Howellsa5af7e12016-10-06 08:11:49 +0100193 call->ping_serial = serial;
David Howells8e831342016-09-22 00:29:31 +0100194 smp_wmb();
195 /* We need to stick a time in before we send the packet in case
196 * the reply gets back before kernel_sendmsg() completes - but
197 * asking UDP to send the packet can take a relatively long
David Howellsb604dd92018-09-27 15:13:08 +0100198 * time.
David Howells8e831342016-09-22 00:29:31 +0100199 */
David Howellsa5af7e12016-10-06 08:11:49 +0100200 call->ping_time = ktime_get_real();
David Howells8e831342016-09-22 00:29:31 +0100201 set_bit(RXRPC_CALL_PINGING, &call->flags);
202 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_ping, serial);
203 }
David Howells26cb02a2016-10-06 08:11:49 +0100204
205 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
David Howells330bdcf2018-08-08 11:30:02 +0100206 conn->params.peer->last_tx_at = ktime_get_seconds();
David Howells6b47fe12018-05-10 23:26:01 +0100207 if (ret < 0)
208 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
David Howells4764c0d2018-07-23 17:18:37 +0100209 rxrpc_tx_point_call_ack);
210 else
211 trace_rxrpc_tx_packet(call->debug_id, &pkt->whdr,
212 rxrpc_tx_point_call_ack);
David Howells8d94aa32016-09-07 09:19:31 +0100213
David Howells26cb02a2016-10-06 08:11:49 +0100214 if (call->state < RXRPC_CALL_COMPLETE) {
David Howells805b21b2016-09-24 18:05:26 +0100215 if (ret < 0) {
David Howellsa5af7e12016-10-06 08:11:49 +0100216 if (ping)
217 clear_bit(RXRPC_CALL_PINGING, &call->flags);
David Howells248f2192016-09-08 11:10:12 +0100218 rxrpc_propose_ACK(call, pkt->ack.reason,
219 ntohs(pkt->ack.maxSkew),
220 ntohl(pkt->ack.serial),
David Howells9c7ad432016-09-23 13:50:40 +0100221 true, true,
222 rxrpc_propose_ack_retry_tx);
David Howells805b21b2016-09-24 18:05:26 +0100223 } else {
224 spin_lock_bh(&call->lock);
225 if (after(hard_ack, call->ackr_consumed))
226 call->ackr_consumed = hard_ack;
227 if (after(top, call->ackr_seen))
228 call->ackr_seen = top;
229 spin_unlock_bh(&call->lock);
David Howells248f2192016-09-08 11:10:12 +0100230 }
David Howells415f44e2017-11-24 10:18:42 +0000231
232 rxrpc_set_keepalive(call);
David Howells248f2192016-09-08 11:10:12 +0100233 }
234
David Howells8d94aa32016-09-07 09:19:31 +0100235out:
236 rxrpc_put_connection(conn);
237 kfree(pkt);
238 return ret;
239}
240
David Howells5873c082014-02-07 18:58:44 +0000241/*
David Howells26cb02a2016-10-06 08:11:49 +0100242 * Send an ABORT call packet.
243 */
244int rxrpc_send_abort_packet(struct rxrpc_call *call)
245{
246 struct rxrpc_connection *conn = NULL;
247 struct rxrpc_abort_buffer pkt;
248 struct msghdr msg;
249 struct kvec iov[1];
250 rxrpc_serial_t serial;
251 int ret;
252
David Howellsdcbefc32017-11-02 15:06:26 +0000253 /* Don't bother sending aborts for a client call once the server has
254 * hard-ACK'd all of its request data. After that point, we're not
255 * going to stop the operation proceeding, and whilst we might limit
256 * the reply, it's not worth it if we can send a new call on the same
257 * channel instead, thereby closing off this call.
258 */
259 if (rxrpc_is_client_call(call) &&
260 test_bit(RXRPC_CALL_TX_LAST, &call->flags))
261 return 0;
262
David Howells26cb02a2016-10-06 08:11:49 +0100263 spin_lock_bh(&call->lock);
264 if (call->conn)
265 conn = rxrpc_get_connection_maybe(call->conn);
266 spin_unlock_bh(&call->lock);
267 if (!conn)
268 return -ECONNRESET;
269
270 msg.msg_name = &call->peer->srx.transport;
271 msg.msg_namelen = call->peer->srx.transport_len;
272 msg.msg_control = NULL;
273 msg.msg_controllen = 0;
274 msg.msg_flags = 0;
275
276 pkt.whdr.epoch = htonl(conn->proto.epoch);
277 pkt.whdr.cid = htonl(call->cid);
278 pkt.whdr.callNumber = htonl(call->call_id);
279 pkt.whdr.seq = 0;
280 pkt.whdr.type = RXRPC_PACKET_TYPE_ABORT;
281 pkt.whdr.flags = conn->out_clientflag;
282 pkt.whdr.userStatus = 0;
283 pkt.whdr.securityIndex = call->security_ix;
284 pkt.whdr._rsvd = 0;
285 pkt.whdr.serviceId = htons(call->service_id);
286 pkt.abort_code = htonl(call->abort_code);
287
288 iov[0].iov_base = &pkt;
289 iov[0].iov_len = sizeof(pkt);
290
291 serial = atomic_inc_return(&conn->serial);
292 pkt.whdr.serial = htonl(serial);
293
294 ret = kernel_sendmsg(conn->params.local->socket,
295 &msg, iov, 1, sizeof(pkt));
David Howells330bdcf2018-08-08 11:30:02 +0100296 conn->params.peer->last_tx_at = ktime_get_seconds();
David Howells6b47fe12018-05-10 23:26:01 +0100297 if (ret < 0)
298 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
David Howells4764c0d2018-07-23 17:18:37 +0100299 rxrpc_tx_point_call_abort);
300 else
301 trace_rxrpc_tx_packet(call->debug_id, &pkt.whdr,
302 rxrpc_tx_point_call_abort);
David Howells6b47fe12018-05-10 23:26:01 +0100303
David Howells26cb02a2016-10-06 08:11:49 +0100304
305 rxrpc_put_connection(conn);
306 return ret;
307}
308
309/*
David Howells17926a72007-04-26 15:48:28 -0700310 * send a packet through the transport endpoint
311 */
David Howellsa1767072016-09-29 22:37:15 +0100312int rxrpc_send_data_packet(struct rxrpc_call *call, struct sk_buff *skb,
313 bool retrans)
David Howells17926a72007-04-26 15:48:28 -0700314{
David Howells5a924b82016-09-22 00:29:31 +0100315 struct rxrpc_connection *conn = call->conn;
316 struct rxrpc_wire_header whdr;
317 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700318 struct msghdr msg;
David Howells5a924b82016-09-22 00:29:31 +0100319 struct kvec iov[2];
320 rxrpc_serial_t serial;
321 size_t len;
David Howellsa1767072016-09-29 22:37:15 +0100322 bool lost = false;
David Howells17926a72007-04-26 15:48:28 -0700323 int ret, opt;
324
325 _enter(",{%d}", skb->len);
326
David Howells5a924b82016-09-22 00:29:31 +0100327 /* Each transmission of a Tx packet needs a new serial number */
328 serial = atomic_inc_return(&conn->serial);
David Howells17926a72007-04-26 15:48:28 -0700329
David Howells5a924b82016-09-22 00:29:31 +0100330 whdr.epoch = htonl(conn->proto.epoch);
331 whdr.cid = htonl(call->cid);
332 whdr.callNumber = htonl(call->call_id);
333 whdr.seq = htonl(sp->hdr.seq);
334 whdr.serial = htonl(serial);
335 whdr.type = RXRPC_PACKET_TYPE_DATA;
336 whdr.flags = sp->hdr.flags;
337 whdr.userStatus = 0;
338 whdr.securityIndex = call->security_ix;
339 whdr._rsvd = htons(sp->hdr._rsvd);
340 whdr.serviceId = htons(call->service_id);
341
David Howells4e255722017-06-05 14:30:49 +0100342 if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) &&
343 sp->hdr.seq == 1)
344 whdr.userStatus = RXRPC_USERSTATUS_SERVICE_UPGRADE;
345
David Howells5a924b82016-09-22 00:29:31 +0100346 iov[0].iov_base = &whdr;
347 iov[0].iov_len = sizeof(whdr);
348 iov[1].iov_base = skb->head;
349 iov[1].iov_len = skb->len;
350 len = iov[0].iov_len + iov[1].iov_len;
351
352 msg.msg_name = &call->peer->srx.transport;
353 msg.msg_namelen = call->peer->srx.transport_len;
David Howells17926a72007-04-26 15:48:28 -0700354 msg.msg_control = NULL;
355 msg.msg_controllen = 0;
356 msg.msg_flags = 0;
357
David Howells57494342016-09-24 18:05:27 +0100358 /* If our RTT cache needs working on, request an ACK. Also request
359 * ACKs if a DATA packet appears to have been lost.
David Howellsb604dd92018-09-27 15:13:08 +0100360 *
361 * However, we mustn't request an ACK on the last reply packet of a
362 * service call, lest OpenAFS incorrectly send us an ACK with some
363 * soft-ACKs in it and then never follow up with a proper hard ACK.
David Howells57494342016-09-24 18:05:27 +0100364 */
David Howellsb604dd92018-09-27 15:13:08 +0100365 if ((!(sp->hdr.flags & RXRPC_LAST_PACKET) ||
366 rxrpc_to_server(sp)
367 ) &&
David Howellsbd1fdf82017-11-24 10:18:42 +0000368 (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events) ||
369 retrans ||
David Howellsbf7d6202016-10-06 08:11:51 +0100370 call->cong_mode == RXRPC_CALL_SLOW_START ||
371 (call->peer->rtt_usage < 3 && sp->hdr.seq & 1) ||
372 ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
373 ktime_get_real())))
David Howells0d4b1032016-09-22 00:29:31 +0100374 whdr.flags |= RXRPC_REQUEST_ACK;
375
David Howells8a681c362016-09-17 10:49:15 +0100376 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
377 static int lose;
378 if ((lose++ & 7) == 7) {
David Howellsa1767072016-09-29 22:37:15 +0100379 ret = 0;
380 lost = true;
381 goto done;
David Howells8a681c362016-09-17 10:49:15 +0100382 }
383 }
384
David Howells5a924b82016-09-22 00:29:31 +0100385 _proto("Tx DATA %%%u { #%u }", serial, sp->hdr.seq);
386
David Howells17926a72007-04-26 15:48:28 -0700387 /* send the packet with the don't fragment bit set if we currently
388 * think it's small enough */
David Howells5a924b82016-09-22 00:29:31 +0100389 if (iov[1].iov_len >= call->peer->maxdata)
390 goto send_fragmentable;
David Howells17926a72007-04-26 15:48:28 -0700391
David Howells5a924b82016-09-22 00:29:31 +0100392 down_read(&conn->params.local->defrag_sem);
David Howellsb604dd92018-09-27 15:13:08 +0100393
394 sp->hdr.serial = serial;
395 smp_wmb(); /* Set serial before timestamp */
396 skb->tstamp = ktime_get_real();
397
David Howells5a924b82016-09-22 00:29:31 +0100398 /* send the packet by UDP
399 * - returns -EMSGSIZE if UDP would have to fragment the packet
400 * to go out of the interface
401 * - in which case, we'll have processed the ICMP error
402 * message and update the peer record
403 */
404 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
David Howells330bdcf2018-08-08 11:30:02 +0100405 conn->params.peer->last_tx_at = ktime_get_seconds();
David Howells17926a72007-04-26 15:48:28 -0700406
David Howells5a924b82016-09-22 00:29:31 +0100407 up_read(&conn->params.local->defrag_sem);
David Howells6b47fe12018-05-10 23:26:01 +0100408 if (ret < 0)
409 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
David Howells4764c0d2018-07-23 17:18:37 +0100410 rxrpc_tx_point_call_data_nofrag);
411 else
412 trace_rxrpc_tx_packet(call->debug_id, &whdr,
413 rxrpc_tx_point_call_data_nofrag);
David Howells5a924b82016-09-22 00:29:31 +0100414 if (ret == -EMSGSIZE)
415 goto send_fragmentable;
416
417done:
David Howellsa1767072016-09-29 22:37:15 +0100418 trace_rxrpc_tx_data(call, sp->hdr.seq, serial, whdr.flags,
419 retrans, lost);
David Howells50235c42016-09-22 00:29:31 +0100420 if (ret >= 0) {
David Howells0d4b1032016-09-22 00:29:31 +0100421 if (whdr.flags & RXRPC_REQUEST_ACK) {
David Howellsb604dd92018-09-27 15:13:08 +0100422 call->peer->rtt_last_req = skb->tstamp;
David Howells50235c42016-09-22 00:29:31 +0100423 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_data, serial);
David Howellsbd1fdf82017-11-24 10:18:42 +0000424 if (call->peer->rtt_usage > 1) {
425 unsigned long nowj = jiffies, ack_lost_at;
426
427 ack_lost_at = nsecs_to_jiffies(2 * call->peer->rtt);
428 if (ack_lost_at < 1)
429 ack_lost_at = 1;
430
431 ack_lost_at += nowj;
432 WRITE_ONCE(call->ack_lost_at, ack_lost_at);
433 rxrpc_reduce_call_timer(call, ack_lost_at, nowj,
434 rxrpc_timer_set_for_lost_ack);
435 }
David Howells0d4b1032016-09-22 00:29:31 +0100436 }
David Howellsc54e43d2018-05-10 23:26:00 +0100437
438 if (sp->hdr.seq == 1 &&
439 !test_and_set_bit(RXRPC_CALL_BEGAN_RX_TIMER,
440 &call->flags)) {
441 unsigned long nowj = jiffies, expect_rx_by;
442
443 expect_rx_by = nowj + call->next_rx_timo;
444 WRITE_ONCE(call->expect_rx_by, expect_rx_by);
445 rxrpc_reduce_call_timer(call, expect_rx_by, nowj,
446 rxrpc_timer_set_for_normal);
447 }
David Howells17926a72007-04-26 15:48:28 -0700448 }
David Howells415f44e2017-11-24 10:18:42 +0000449
450 rxrpc_set_keepalive(call);
451
David Howells5a924b82016-09-22 00:29:31 +0100452 _leave(" = %d [%u]", ret, call->peer->maxdata);
453 return ret;
David Howells17926a72007-04-26 15:48:28 -0700454
455send_fragmentable:
456 /* attempt to send this message with fragmentation enabled */
457 _debug("send fragment");
458
David Howells985a5c82016-06-17 11:53:37 +0100459 down_write(&conn->params.local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -0700460
David Howellsb604dd92018-09-27 15:13:08 +0100461 sp->hdr.serial = serial;
462 smp_wmb(); /* Set serial before timestamp */
463 skb->tstamp = ktime_get_real();
464
David Howells985a5c82016-06-17 11:53:37 +0100465 switch (conn->params.local->srx.transport.family) {
466 case AF_INET:
467 opt = IP_PMTUDISC_DONT;
468 ret = kernel_setsockopt(conn->params.local->socket,
469 SOL_IP, IP_MTU_DISCOVER,
470 (char *)&opt, sizeof(opt));
471 if (ret == 0) {
David Howells5a924b82016-09-22 00:29:31 +0100472 ret = kernel_sendmsg(conn->params.local->socket, &msg,
473 iov, 2, len);
David Howells330bdcf2018-08-08 11:30:02 +0100474 conn->params.peer->last_tx_at = ktime_get_seconds();
David Howells985a5c82016-06-17 11:53:37 +0100475
476 opt = IP_PMTUDISC_DO;
477 kernel_setsockopt(conn->params.local->socket, SOL_IP,
478 IP_MTU_DISCOVER,
479 (char *)&opt, sizeof(opt));
480 }
481 break;
David Howells75b54cb2016-09-13 08:49:05 +0100482
David Howellsd1912742016-09-17 07:26:01 +0100483#ifdef CONFIG_AF_RXRPC_IPV6
David Howells75b54cb2016-09-13 08:49:05 +0100484 case AF_INET6:
485 opt = IPV6_PMTUDISC_DONT;
486 ret = kernel_setsockopt(conn->params.local->socket,
487 SOL_IPV6, IPV6_MTU_DISCOVER,
488 (char *)&opt, sizeof(opt));
489 if (ret == 0) {
490 ret = kernel_sendmsg(conn->params.local->socket, &msg,
David Howells93c62c42018-02-22 14:38:14 +0000491 iov, 2, len);
David Howells330bdcf2018-08-08 11:30:02 +0100492 conn->params.peer->last_tx_at = ktime_get_seconds();
David Howells75b54cb2016-09-13 08:49:05 +0100493
494 opt = IPV6_PMTUDISC_DO;
495 kernel_setsockopt(conn->params.local->socket,
496 SOL_IPV6, IPV6_MTU_DISCOVER,
497 (char *)&opt, sizeof(opt));
498 }
499 break;
David Howellsd1912742016-09-17 07:26:01 +0100500#endif
David Howells17926a72007-04-26 15:48:28 -0700501 }
502
David Howells6b47fe12018-05-10 23:26:01 +0100503 if (ret < 0)
504 trace_rxrpc_tx_fail(call->debug_id, serial, ret,
David Howells4764c0d2018-07-23 17:18:37 +0100505 rxrpc_tx_point_call_data_frag);
506 else
507 trace_rxrpc_tx_packet(call->debug_id, &whdr,
508 rxrpc_tx_point_call_data_frag);
David Howells6b47fe12018-05-10 23:26:01 +0100509
David Howells985a5c82016-06-17 11:53:37 +0100510 up_write(&conn->params.local->defrag_sem);
David Howells5a924b82016-09-22 00:29:31 +0100511 goto done;
David Howells17926a72007-04-26 15:48:28 -0700512}
David Howells248f2192016-09-08 11:10:12 +0100513
514/*
515 * reject packets through the local endpoint
516 */
517void rxrpc_reject_packets(struct rxrpc_local *local)
518{
David Howells1c2bc7b2016-09-13 08:49:05 +0100519 struct sockaddr_rxrpc srx;
David Howells248f2192016-09-08 11:10:12 +0100520 struct rxrpc_skb_priv *sp;
521 struct rxrpc_wire_header whdr;
522 struct sk_buff *skb;
523 struct msghdr msg;
524 struct kvec iov[2];
525 size_t size;
526 __be32 code;
David Howells6b47fe12018-05-10 23:26:01 +0100527 int ret;
David Howells248f2192016-09-08 11:10:12 +0100528
529 _enter("%d", local->debug_id);
530
531 iov[0].iov_base = &whdr;
532 iov[0].iov_len = sizeof(whdr);
533 iov[1].iov_base = &code;
534 iov[1].iov_len = sizeof(code);
535 size = sizeof(whdr) + sizeof(code);
536
David Howells1c2bc7b2016-09-13 08:49:05 +0100537 msg.msg_name = &srx.transport;
David Howells248f2192016-09-08 11:10:12 +0100538 msg.msg_control = NULL;
539 msg.msg_controllen = 0;
540 msg.msg_flags = 0;
541
David Howells248f2192016-09-08 11:10:12 +0100542 memset(&whdr, 0, sizeof(whdr));
543 whdr.type = RXRPC_PACKET_TYPE_ABORT;
544
545 while ((skb = skb_dequeue(&local->reject_queue))) {
David Howells71f3ca42016-09-17 10:49:14 +0100546 rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
David Howells248f2192016-09-08 11:10:12 +0100547 sp = rxrpc_skb(skb);
David Howells1c2bc7b2016-09-13 08:49:05 +0100548
David Howells7b674e32017-08-29 10:18:37 +0100549 if (rxrpc_extract_addr_from_skb(local, &srx, skb) == 0) {
David Howells1c2bc7b2016-09-13 08:49:05 +0100550 msg.msg_namelen = srx.transport_len;
551
David Howells248f2192016-09-08 11:10:12 +0100552 code = htonl(skb->priority);
553
554 whdr.epoch = htonl(sp->hdr.epoch);
555 whdr.cid = htonl(sp->hdr.cid);
556 whdr.callNumber = htonl(sp->hdr.callNumber);
557 whdr.serviceId = htons(sp->hdr.serviceId);
558 whdr.flags = sp->hdr.flags;
559 whdr.flags ^= RXRPC_CLIENT_INITIATED;
560 whdr.flags &= RXRPC_CLIENT_INITIATED;
561
David Howells6b47fe12018-05-10 23:26:01 +0100562 ret = kernel_sendmsg(local->socket, &msg, iov, 2, size);
563 if (ret < 0)
564 trace_rxrpc_tx_fail(local->debug_id, 0, ret,
David Howells4764c0d2018-07-23 17:18:37 +0100565 rxrpc_tx_point_reject);
566 else
567 trace_rxrpc_tx_packet(local->debug_id, &whdr,
568 rxrpc_tx_point_reject);
David Howells248f2192016-09-08 11:10:12 +0100569 }
570
David Howells71f3ca42016-09-17 10:49:14 +0100571 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
David Howells248f2192016-09-08 11:10:12 +0100572 }
573
574 _leave("");
575}
David Howellsace45be2018-03-30 21:04:43 +0100576
577/*
578 * Send a VERSION reply to a peer as a keepalive.
579 */
580void rxrpc_send_keepalive(struct rxrpc_peer *peer)
581{
582 struct rxrpc_wire_header whdr;
583 struct msghdr msg;
584 struct kvec iov[2];
585 size_t len;
586 int ret;
587
588 _enter("");
589
590 msg.msg_name = &peer->srx.transport;
591 msg.msg_namelen = peer->srx.transport_len;
592 msg.msg_control = NULL;
593 msg.msg_controllen = 0;
594 msg.msg_flags = 0;
595
596 whdr.epoch = htonl(peer->local->rxnet->epoch);
597 whdr.cid = 0;
598 whdr.callNumber = 0;
599 whdr.seq = 0;
600 whdr.serial = 0;
601 whdr.type = RXRPC_PACKET_TYPE_VERSION; /* Not client-initiated */
602 whdr.flags = RXRPC_LAST_PACKET;
603 whdr.userStatus = 0;
604 whdr.securityIndex = 0;
605 whdr._rsvd = 0;
606 whdr.serviceId = 0;
607
608 iov[0].iov_base = &whdr;
609 iov[0].iov_len = sizeof(whdr);
610 iov[1].iov_base = (char *)rxrpc_keepalive_string;
611 iov[1].iov_len = sizeof(rxrpc_keepalive_string);
612
613 len = iov[0].iov_len + iov[1].iov_len;
614
615 _proto("Tx VERSION (keepalive)");
616
617 ret = kernel_sendmsg(peer->local->socket, &msg, iov, 2, len);
618 if (ret < 0)
David Howells6b47fe12018-05-10 23:26:01 +0100619 trace_rxrpc_tx_fail(peer->debug_id, 0, ret,
David Howells4764c0d2018-07-23 17:18:37 +0100620 rxrpc_tx_point_version_keepalive);
621 else
622 trace_rxrpc_tx_packet(peer->debug_id, &whdr,
623 rxrpc_tx_point_version_keepalive);
David Howellsace45be2018-03-30 21:04:43 +0100624
David Howells330bdcf2018-08-08 11:30:02 +0100625 peer->last_tx_at = ktime_get_seconds();
David Howellsace45be2018-03-30 21:04:43 +0100626 _leave("");
627}