]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/rxrpc/output.c
Merge tag 'mmc-v4.15-2' of git://git.kernel.org/pub/scm/linux/kernel/git/ulfh/mmc
[mirror_ubuntu-bionic-kernel.git] / net / rxrpc / output.c
CommitLineData
17926a79
DH
1/* 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
9b6d5398
JP
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
17926a79 14#include <linux/net.h>
5a0e3ad6 15#include <linux/gfp.h>
17926a79 16#include <linux/skbuff.h>
bc3b2d7f 17#include <linux/export.h>
17926a79
DH
18#include <net/sock.h>
19#include <net/af_rxrpc.h>
20#include "ar-internal.h"
21
26cb02aa 22struct rxrpc_ack_buffer {
8d94aa38 23 struct rxrpc_wire_header whdr;
26cb02aa
DH
24 struct rxrpc_ackpacket ack;
25 u8 acks[255];
26 u8 pad[3];
8d94aa38
DH
27 struct rxrpc_ackinfo ackinfo;
28};
29
26cb02aa
DH
30struct rxrpc_abort_buffer {
31 struct rxrpc_wire_header whdr;
32 __be32 abort_code;
33};
34
415f44e4
DH
35/*
36 * Arrange for a keepalive ping a certain time after we last transmitted. This
37 * lets the far side know we're still interested in this call and helps keep
38 * the route through any intervening firewall open.
39 *
40 * Receiving a response to the ping will prevent the ->expect_rx_by timer from
41 * expiring.
42 */
43static void rxrpc_set_keepalive(struct rxrpc_call *call)
44{
45 unsigned long now = jiffies, keepalive_at = call->next_rx_timo / 6;
46
47 keepalive_at += now;
48 WRITE_ONCE(call->keepalive_at, keepalive_at);
49 rxrpc_reduce_call_timer(call, keepalive_at, now,
50 rxrpc_timer_set_for_keepalive);
51}
52
8d94aa38
DH
53/*
54 * Fill out an ACK packet.
55 */
1457cc4c
DH
56static size_t rxrpc_fill_out_ack(struct rxrpc_connection *conn,
57 struct rxrpc_call *call,
26cb02aa 58 struct rxrpc_ack_buffer *pkt,
805b21b9 59 rxrpc_seq_t *_hard_ack,
a5af7e1f
DH
60 rxrpc_seq_t *_top,
61 u8 reason)
8d94aa38 62{
f3639df2 63 rxrpc_serial_t serial;
248f219c
DH
64 rxrpc_seq_t hard_ack, top, seq;
65 int ix;
8d94aa38
DH
66 u32 mtu, jmax;
67 u8 *ackp = pkt->acks;
68
248f219c 69 /* Barrier against rxrpc_input_data(). */
f3639df2 70 serial = call->ackr_serial;
248f219c
DH
71 hard_ack = READ_ONCE(call->rx_hard_ack);
72 top = smp_load_acquire(&call->rx_top);
805b21b9
DH
73 *_hard_ack = hard_ack;
74 *_top = top;
248f219c 75
8d94aa38 76 pkt->ack.bufferSpace = htons(8);
248f219c
DH
77 pkt->ack.maxSkew = htons(call->ackr_skew);
78 pkt->ack.firstPacket = htonl(hard_ack + 1);
8d94aa38 79 pkt->ack.previousPacket = htonl(call->ackr_prev_seq);
f3639df2 80 pkt->ack.serial = htonl(serial);
a5af7e1f 81 pkt->ack.reason = reason;
248f219c
DH
82 pkt->ack.nAcks = top - hard_ack;
83
a5af7e1f 84 if (reason == RXRPC_ACK_PING)
8e83134d
DH
85 pkt->whdr.flags |= RXRPC_REQUEST_ACK;
86
248f219c
DH
87 if (after(top, hard_ack)) {
88 seq = hard_ack + 1;
89 do {
90 ix = seq & RXRPC_RXTX_BUFF_MASK;
91 if (call->rxtx_buffer[ix])
92 *ackp++ = RXRPC_ACK_TYPE_ACK;
93 else
94 *ackp++ = RXRPC_ACK_TYPE_NACK;
95 seq++;
96 } while (before_eq(seq, top));
97 }
8d94aa38 98
1457cc4c
DH
99 mtu = conn->params.peer->if_mtu;
100 mtu -= conn->params.peer->hdrsize;
75e42126 101 jmax = (call->nr_jumbo_bad > 3) ? 1 : rxrpc_rx_jumbo_max;
8d94aa38
DH
102 pkt->ackinfo.rxMTU = htonl(rxrpc_rx_mtu);
103 pkt->ackinfo.maxMTU = htonl(mtu);
75e42126 104 pkt->ackinfo.rwind = htonl(call->rx_winsize);
8d94aa38
DH
105 pkt->ackinfo.jumbo_max = htonl(jmax);
106
107 *ackp++ = 0;
108 *ackp++ = 0;
109 *ackp++ = 0;
248f219c 110 return top - hard_ack + 3;
8d94aa38
DH
111}
112
113/*
26cb02aa 114 * Send an ACK call packet.
8d94aa38 115 */
bd1fdf8c
DH
116int rxrpc_send_ack_packet(struct rxrpc_call *call, bool ping,
117 rxrpc_serial_t *_serial)
8d94aa38
DH
118{
119 struct rxrpc_connection *conn = NULL;
26cb02aa 120 struct rxrpc_ack_buffer *pkt;
8d94aa38
DH
121 struct msghdr msg;
122 struct kvec iov[2];
123 rxrpc_serial_t serial;
805b21b9 124 rxrpc_seq_t hard_ack, top;
8d94aa38 125 size_t len, n;
26cb02aa 126 int ret;
a5af7e1f 127 u8 reason;
8d94aa38
DH
128
129 spin_lock_bh(&call->lock);
130 if (call->conn)
131 conn = rxrpc_get_connection_maybe(call->conn);
132 spin_unlock_bh(&call->lock);
133 if (!conn)
134 return -ECONNRESET;
135
136 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
137 if (!pkt) {
138 rxrpc_put_connection(conn);
139 return -ENOMEM;
140 }
141
8d94aa38
DH
142 msg.msg_name = &call->peer->srx.transport;
143 msg.msg_namelen = call->peer->srx.transport_len;
144 msg.msg_control = NULL;
145 msg.msg_controllen = 0;
146 msg.msg_flags = 0;
147
148 pkt->whdr.epoch = htonl(conn->proto.epoch);
149 pkt->whdr.cid = htonl(call->cid);
150 pkt->whdr.callNumber = htonl(call->call_id);
151 pkt->whdr.seq = 0;
26cb02aa
DH
152 pkt->whdr.type = RXRPC_PACKET_TYPE_ACK;
153 pkt->whdr.flags = RXRPC_SLOW_START_OK | conn->out_clientflag;
8d94aa38
DH
154 pkt->whdr.userStatus = 0;
155 pkt->whdr.securityIndex = call->security_ix;
156 pkt->whdr._rsvd = 0;
157 pkt->whdr.serviceId = htons(call->service_id);
158
26cb02aa 159 spin_lock_bh(&call->lock);
a5af7e1f
DH
160 if (ping) {
161 reason = RXRPC_ACK_PING;
162 } else {
163 reason = call->ackr_reason;
164 if (!call->ackr_reason) {
165 spin_unlock_bh(&call->lock);
166 ret = 0;
167 goto out;
168 }
169 call->ackr_reason = 0;
8d94aa38 170 }
1457cc4c 171 n = rxrpc_fill_out_ack(conn, call, pkt, &hard_ack, &top, reason);
26cb02aa
DH
172
173 spin_unlock_bh(&call->lock);
174
175 iov[0].iov_base = pkt;
176 iov[0].iov_len = sizeof(pkt->whdr) + sizeof(pkt->ack) + n;
177 iov[1].iov_base = &pkt->ackinfo;
178 iov[1].iov_len = sizeof(pkt->ackinfo);
179 len = iov[0].iov_len + iov[1].iov_len;
8d94aa38 180
b86e218e
DH
181 serial = atomic_inc_return(&conn->serial);
182 pkt->whdr.serial = htonl(serial);
26cb02aa
DH
183 trace_rxrpc_tx_ack(call, serial,
184 ntohl(pkt->ack.firstPacket),
185 ntohl(pkt->ack.serial),
186 pkt->ack.reason, pkt->ack.nAcks);
bd1fdf8c
DH
187 if (_serial)
188 *_serial = serial;
b86e218e 189
8e83134d 190 if (ping) {
a5af7e1f 191 call->ping_serial = serial;
8e83134d
DH
192 smp_wmb();
193 /* We need to stick a time in before we send the packet in case
194 * the reply gets back before kernel_sendmsg() completes - but
195 * asking UDP to send the packet can take a relatively long
196 * time, so we update the time after, on the assumption that
197 * the packet transmission is more likely to happen towards the
198 * end of the kernel_sendmsg() call.
199 */
a5af7e1f 200 call->ping_time = ktime_get_real();
8e83134d
DH
201 set_bit(RXRPC_CALL_PINGING, &call->flags);
202 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_ping, serial);
203 }
26cb02aa
DH
204
205 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
8e83134d 206 if (ping)
a5af7e1f 207 call->ping_time = ktime_get_real();
8d94aa38 208
26cb02aa 209 if (call->state < RXRPC_CALL_COMPLETE) {
805b21b9 210 if (ret < 0) {
a5af7e1f
DH
211 if (ping)
212 clear_bit(RXRPC_CALL_PINGING, &call->flags);
248f219c
DH
213 rxrpc_propose_ACK(call, pkt->ack.reason,
214 ntohs(pkt->ack.maxSkew),
215 ntohl(pkt->ack.serial),
9c7ad434
DH
216 true, true,
217 rxrpc_propose_ack_retry_tx);
805b21b9
DH
218 } else {
219 spin_lock_bh(&call->lock);
220 if (after(hard_ack, call->ackr_consumed))
221 call->ackr_consumed = hard_ack;
222 if (after(top, call->ackr_seen))
223 call->ackr_seen = top;
224 spin_unlock_bh(&call->lock);
248f219c 225 }
415f44e4
DH
226
227 rxrpc_set_keepalive(call);
248f219c
DH
228 }
229
8d94aa38
DH
230out:
231 rxrpc_put_connection(conn);
232 kfree(pkt);
233 return ret;
234}
235
26cb02aa
DH
236/*
237 * Send an ABORT call packet.
238 */
239int rxrpc_send_abort_packet(struct rxrpc_call *call)
240{
241 struct rxrpc_connection *conn = NULL;
242 struct rxrpc_abort_buffer pkt;
243 struct msghdr msg;
244 struct kvec iov[1];
245 rxrpc_serial_t serial;
246 int ret;
247
dcbefc30
DH
248 /* Don't bother sending aborts for a client call once the server has
249 * hard-ACK'd all of its request data. After that point, we're not
250 * going to stop the operation proceeding, and whilst we might limit
251 * the reply, it's not worth it if we can send a new call on the same
252 * channel instead, thereby closing off this call.
253 */
254 if (rxrpc_is_client_call(call) &&
255 test_bit(RXRPC_CALL_TX_LAST, &call->flags))
256 return 0;
257
26cb02aa
DH
258 spin_lock_bh(&call->lock);
259 if (call->conn)
260 conn = rxrpc_get_connection_maybe(call->conn);
261 spin_unlock_bh(&call->lock);
262 if (!conn)
263 return -ECONNRESET;
264
265 msg.msg_name = &call->peer->srx.transport;
266 msg.msg_namelen = call->peer->srx.transport_len;
267 msg.msg_control = NULL;
268 msg.msg_controllen = 0;
269 msg.msg_flags = 0;
270
271 pkt.whdr.epoch = htonl(conn->proto.epoch);
272 pkt.whdr.cid = htonl(call->cid);
273 pkt.whdr.callNumber = htonl(call->call_id);
274 pkt.whdr.seq = 0;
275 pkt.whdr.type = RXRPC_PACKET_TYPE_ABORT;
276 pkt.whdr.flags = conn->out_clientflag;
277 pkt.whdr.userStatus = 0;
278 pkt.whdr.securityIndex = call->security_ix;
279 pkt.whdr._rsvd = 0;
280 pkt.whdr.serviceId = htons(call->service_id);
281 pkt.abort_code = htonl(call->abort_code);
282
283 iov[0].iov_base = &pkt;
284 iov[0].iov_len = sizeof(pkt);
285
286 serial = atomic_inc_return(&conn->serial);
287 pkt.whdr.serial = htonl(serial);
288
289 ret = kernel_sendmsg(conn->params.local->socket,
290 &msg, iov, 1, sizeof(pkt));
291
292 rxrpc_put_connection(conn);
293 return ret;
294}
295
17926a79
DH
296/*
297 * send a packet through the transport endpoint
298 */
a1767077
DH
299int rxrpc_send_data_packet(struct rxrpc_call *call, struct sk_buff *skb,
300 bool retrans)
17926a79 301{
5a924b89
DH
302 struct rxrpc_connection *conn = call->conn;
303 struct rxrpc_wire_header whdr;
304 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
17926a79 305 struct msghdr msg;
5a924b89
DH
306 struct kvec iov[2];
307 rxrpc_serial_t serial;
308 size_t len;
a1767077 309 bool lost = false;
17926a79
DH
310 int ret, opt;
311
312 _enter(",{%d}", skb->len);
313
5a924b89
DH
314 /* Each transmission of a Tx packet needs a new serial number */
315 serial = atomic_inc_return(&conn->serial);
17926a79 316
5a924b89
DH
317 whdr.epoch = htonl(conn->proto.epoch);
318 whdr.cid = htonl(call->cid);
319 whdr.callNumber = htonl(call->call_id);
320 whdr.seq = htonl(sp->hdr.seq);
321 whdr.serial = htonl(serial);
322 whdr.type = RXRPC_PACKET_TYPE_DATA;
323 whdr.flags = sp->hdr.flags;
324 whdr.userStatus = 0;
325 whdr.securityIndex = call->security_ix;
326 whdr._rsvd = htons(sp->hdr._rsvd);
327 whdr.serviceId = htons(call->service_id);
328
4e255721
DH
329 if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) &&
330 sp->hdr.seq == 1)
331 whdr.userStatus = RXRPC_USERSTATUS_SERVICE_UPGRADE;
332
5a924b89
DH
333 iov[0].iov_base = &whdr;
334 iov[0].iov_len = sizeof(whdr);
335 iov[1].iov_base = skb->head;
336 iov[1].iov_len = skb->len;
337 len = iov[0].iov_len + iov[1].iov_len;
338
339 msg.msg_name = &call->peer->srx.transport;
340 msg.msg_namelen = call->peer->srx.transport_len;
17926a79
DH
341 msg.msg_control = NULL;
342 msg.msg_controllen = 0;
343 msg.msg_flags = 0;
344
57494343
DH
345 /* If our RTT cache needs working on, request an ACK. Also request
346 * ACKs if a DATA packet appears to have been lost.
347 */
bf7d620a 348 if (!(sp->hdr.flags & RXRPC_LAST_PACKET) &&
bd1fdf8c
DH
349 (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events) ||
350 retrans ||
bf7d620a
DH
351 call->cong_mode == RXRPC_CALL_SLOW_START ||
352 (call->peer->rtt_usage < 3 && sp->hdr.seq & 1) ||
353 ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
354 ktime_get_real())))
0d4b103c
DH
355 whdr.flags |= RXRPC_REQUEST_ACK;
356
8a681c36
DH
357 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
358 static int lose;
359 if ((lose++ & 7) == 7) {
a1767077
DH
360 ret = 0;
361 lost = true;
362 goto done;
8a681c36
DH
363 }
364 }
365
5a924b89
DH
366 _proto("Tx DATA %%%u { #%u }", serial, sp->hdr.seq);
367
17926a79
DH
368 /* send the packet with the don't fragment bit set if we currently
369 * think it's small enough */
5a924b89
DH
370 if (iov[1].iov_len >= call->peer->maxdata)
371 goto send_fragmentable;
372
373 down_read(&conn->params.local->defrag_sem);
374 /* send the packet by UDP
375 * - returns -EMSGSIZE if UDP would have to fragment the packet
376 * to go out of the interface
377 * - in which case, we'll have processed the ICMP error
378 * message and update the peer record
379 */
380 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
381
382 up_read(&conn->params.local->defrag_sem);
383 if (ret == -EMSGSIZE)
384 goto send_fragmentable;
385
386done:
a1767077
DH
387 trace_rxrpc_tx_data(call, sp->hdr.seq, serial, whdr.flags,
388 retrans, lost);
50235c4b 389 if (ret >= 0) {
0d4b103c
DH
390 ktime_t now = ktime_get_real();
391 skb->tstamp = now;
50235c4b 392 smp_wmb();
5a924b89 393 sp->hdr.serial = serial;
0d4b103c
DH
394 if (whdr.flags & RXRPC_REQUEST_ACK) {
395 call->peer->rtt_last_req = now;
50235c4b 396 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_data, serial);
bd1fdf8c
DH
397 if (call->peer->rtt_usage > 1) {
398 unsigned long nowj = jiffies, ack_lost_at;
399
400 ack_lost_at = nsecs_to_jiffies(2 * call->peer->rtt);
401 if (ack_lost_at < 1)
402 ack_lost_at = 1;
403
404 ack_lost_at += nowj;
405 WRITE_ONCE(call->ack_lost_at, ack_lost_at);
406 rxrpc_reduce_call_timer(call, ack_lost_at, nowj,
407 rxrpc_timer_set_for_lost_ack);
408 }
0d4b103c 409 }
17926a79 410 }
415f44e4
DH
411
412 rxrpc_set_keepalive(call);
413
5a924b89
DH
414 _leave(" = %d [%u]", ret, call->peer->maxdata);
415 return ret;
17926a79
DH
416
417send_fragmentable:
418 /* attempt to send this message with fragmentation enabled */
419 _debug("send fragment");
420
985a5c82
DH
421 down_write(&conn->params.local->defrag_sem);
422
423 switch (conn->params.local->srx.transport.family) {
424 case AF_INET:
425 opt = IP_PMTUDISC_DONT;
426 ret = kernel_setsockopt(conn->params.local->socket,
427 SOL_IP, IP_MTU_DISCOVER,
428 (char *)&opt, sizeof(opt));
429 if (ret == 0) {
5a924b89
DH
430 ret = kernel_sendmsg(conn->params.local->socket, &msg,
431 iov, 2, len);
985a5c82
DH
432
433 opt = IP_PMTUDISC_DO;
434 kernel_setsockopt(conn->params.local->socket, SOL_IP,
435 IP_MTU_DISCOVER,
436 (char *)&opt, sizeof(opt));
437 }
438 break;
75b54cb5 439
d1912747 440#ifdef CONFIG_AF_RXRPC_IPV6
75b54cb5
DH
441 case AF_INET6:
442 opt = IPV6_PMTUDISC_DONT;
443 ret = kernel_setsockopt(conn->params.local->socket,
444 SOL_IPV6, IPV6_MTU_DISCOVER,
445 (char *)&opt, sizeof(opt));
446 if (ret == 0) {
447 ret = kernel_sendmsg(conn->params.local->socket, &msg,
448 iov, 1, iov[0].iov_len);
449
450 opt = IPV6_PMTUDISC_DO;
451 kernel_setsockopt(conn->params.local->socket,
452 SOL_IPV6, IPV6_MTU_DISCOVER,
453 (char *)&opt, sizeof(opt));
454 }
455 break;
d1912747 456#endif
17926a79
DH
457 }
458
985a5c82 459 up_write(&conn->params.local->defrag_sem);
5a924b89 460 goto done;
17926a79 461}
248f219c
DH
462
463/*
464 * reject packets through the local endpoint
465 */
466void rxrpc_reject_packets(struct rxrpc_local *local)
467{
1c2bc7b9 468 struct sockaddr_rxrpc srx;
248f219c
DH
469 struct rxrpc_skb_priv *sp;
470 struct rxrpc_wire_header whdr;
471 struct sk_buff *skb;
472 struct msghdr msg;
473 struct kvec iov[2];
474 size_t size;
475 __be32 code;
476
477 _enter("%d", local->debug_id);
478
479 iov[0].iov_base = &whdr;
480 iov[0].iov_len = sizeof(whdr);
481 iov[1].iov_base = &code;
482 iov[1].iov_len = sizeof(code);
483 size = sizeof(whdr) + sizeof(code);
484
1c2bc7b9 485 msg.msg_name = &srx.transport;
248f219c
DH
486 msg.msg_control = NULL;
487 msg.msg_controllen = 0;
488 msg.msg_flags = 0;
489
248f219c
DH
490 memset(&whdr, 0, sizeof(whdr));
491 whdr.type = RXRPC_PACKET_TYPE_ABORT;
492
493 while ((skb = skb_dequeue(&local->reject_queue))) {
71f3ca40 494 rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
248f219c 495 sp = rxrpc_skb(skb);
1c2bc7b9 496
7b674e39 497 if (rxrpc_extract_addr_from_skb(local, &srx, skb) == 0) {
1c2bc7b9
DH
498 msg.msg_namelen = srx.transport_len;
499
248f219c
DH
500 code = htonl(skb->priority);
501
502 whdr.epoch = htonl(sp->hdr.epoch);
503 whdr.cid = htonl(sp->hdr.cid);
504 whdr.callNumber = htonl(sp->hdr.callNumber);
505 whdr.serviceId = htons(sp->hdr.serviceId);
506 whdr.flags = sp->hdr.flags;
507 whdr.flags ^= RXRPC_CLIENT_INITIATED;
508 whdr.flags &= RXRPC_CLIENT_INITIATED;
509
510 kernel_sendmsg(local->socket, &msg, iov, 2, size);
248f219c
DH
511 }
512
71f3ca40 513 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
248f219c
DH
514 }
515
516 _leave("");
517}