blob: 9619c56ef4cd5f03a090156f747d770ce1fcd78f [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/* 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
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14#include <linux/net.h>
15#include <linux/gfp.h>
16#include <linux/skbuff.h>
17#include <linux/export.h>
18#include <net/sock.h>
19#include <net/af_rxrpc.h>
20#include "ar-internal.h"
21
22struct rxrpc_ack_buffer {
23 struct rxrpc_wire_header whdr;
24 struct rxrpc_ackpacket ack;
25 u8 acks[255];
26 u8 pad[3];
27 struct rxrpc_ackinfo ackinfo;
28};
29
30struct rxrpc_abort_buffer {
31 struct rxrpc_wire_header whdr;
32 __be32 abort_code;
33};
34
35/*
36 * Fill out an ACK packet.
37 */
38static size_t rxrpc_fill_out_ack(struct rxrpc_call *call,
39 struct rxrpc_ack_buffer *pkt,
40 rxrpc_seq_t *_hard_ack,
41 rxrpc_seq_t *_top,
42 u8 reason)
43{
44 rxrpc_serial_t serial;
45 rxrpc_seq_t hard_ack, top, seq;
46 int ix;
47 u32 mtu, jmax;
48 u8 *ackp = pkt->acks;
49
50 /* Barrier against rxrpc_input_data(). */
51 serial = call->ackr_serial;
52 hard_ack = READ_ONCE(call->rx_hard_ack);
53 top = smp_load_acquire(&call->rx_top);
54 *_hard_ack = hard_ack;
55 *_top = top;
56
57 pkt->ack.bufferSpace = htons(8);
58 pkt->ack.maxSkew = htons(call->ackr_skew);
59 pkt->ack.firstPacket = htonl(hard_ack + 1);
60 pkt->ack.previousPacket = htonl(call->ackr_prev_seq);
61 pkt->ack.serial = htonl(serial);
62 pkt->ack.reason = reason;
63 pkt->ack.nAcks = top - hard_ack;
64
65 if (reason == RXRPC_ACK_PING)
66 pkt->whdr.flags |= RXRPC_REQUEST_ACK;
67
68 if (after(top, hard_ack)) {
69 seq = hard_ack + 1;
70 do {
71 ix = seq & RXRPC_RXTX_BUFF_MASK;
72 if (call->rxtx_buffer[ix])
73 *ackp++ = RXRPC_ACK_TYPE_ACK;
74 else
75 *ackp++ = RXRPC_ACK_TYPE_NACK;
76 seq++;
77 } while (before_eq(seq, top));
78 }
79
80 mtu = call->conn->params.peer->if_mtu;
81 mtu -= call->conn->params.peer->hdrsize;
82 jmax = (call->nr_jumbo_bad > 3) ? 1 : rxrpc_rx_jumbo_max;
83 pkt->ackinfo.rxMTU = htonl(rxrpc_rx_mtu);
84 pkt->ackinfo.maxMTU = htonl(mtu);
85 pkt->ackinfo.rwind = htonl(call->rx_winsize);
86 pkt->ackinfo.jumbo_max = htonl(jmax);
87
88 *ackp++ = 0;
89 *ackp++ = 0;
90 *ackp++ = 0;
91 return top - hard_ack + 3;
92}
93
94/*
95 * Send an ACK call packet.
96 */
97int rxrpc_send_ack_packet(struct rxrpc_call *call, bool ping)
98{
99 struct rxrpc_connection *conn;
100 struct rxrpc_ack_buffer *pkt;
101 struct msghdr msg;
102 struct kvec iov[2];
103 rxrpc_serial_t serial;
104 rxrpc_seq_t hard_ack, top;
105 size_t len, n;
106 int ret;
107 u8 reason;
108
109 if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
110 return -ECONNRESET;
111
112 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
113 if (!pkt)
114 return -ENOMEM;
115
116 conn = call->conn;
117
118 msg.msg_name = &call->peer->srx.transport;
119 msg.msg_namelen = call->peer->srx.transport_len;
120 msg.msg_control = NULL;
121 msg.msg_controllen = 0;
122 msg.msg_flags = 0;
123
124 pkt->whdr.epoch = htonl(conn->proto.epoch);
125 pkt->whdr.cid = htonl(call->cid);
126 pkt->whdr.callNumber = htonl(call->call_id);
127 pkt->whdr.seq = 0;
128 pkt->whdr.type = RXRPC_PACKET_TYPE_ACK;
129 pkt->whdr.flags = RXRPC_SLOW_START_OK | conn->out_clientflag;
130 pkt->whdr.userStatus = 0;
131 pkt->whdr.securityIndex = call->security_ix;
132 pkt->whdr._rsvd = 0;
133 pkt->whdr.serviceId = htons(call->service_id);
134
135 spin_lock_bh(&call->lock);
136 if (ping) {
137 reason = RXRPC_ACK_PING;
138 } else {
139 reason = call->ackr_reason;
140 if (!call->ackr_reason) {
141 spin_unlock_bh(&call->lock);
142 ret = 0;
143 goto out;
144 }
145 call->ackr_reason = 0;
146 }
147 n = rxrpc_fill_out_ack(call, pkt, &hard_ack, &top, reason);
148
149 spin_unlock_bh(&call->lock);
150
151 iov[0].iov_base = pkt;
152 iov[0].iov_len = sizeof(pkt->whdr) + sizeof(pkt->ack) + n;
153 iov[1].iov_base = &pkt->ackinfo;
154 iov[1].iov_len = sizeof(pkt->ackinfo);
155 len = iov[0].iov_len + iov[1].iov_len;
156
157 serial = atomic_inc_return(&conn->serial);
158 pkt->whdr.serial = htonl(serial);
159 trace_rxrpc_tx_ack(call, serial,
160 ntohl(pkt->ack.firstPacket),
161 ntohl(pkt->ack.serial),
162 pkt->ack.reason, pkt->ack.nAcks);
163
164 if (ping) {
165 call->ping_serial = serial;
166 smp_wmb();
167 /* We need to stick a time in before we send the packet in case
168 * the reply gets back before kernel_sendmsg() completes - but
169 * asking UDP to send the packet can take a relatively long
170 * time, so we update the time after, on the assumption that
171 * the packet transmission is more likely to happen towards the
172 * end of the kernel_sendmsg() call.
173 */
174 call->ping_time = ktime_get_real();
175 set_bit(RXRPC_CALL_PINGING, &call->flags);
176 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_ping, serial);
177 }
178
179 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
180 if (ping)
181 call->ping_time = ktime_get_real();
182
183 if (call->state < RXRPC_CALL_COMPLETE) {
184 if (ret < 0) {
185 if (ping)
186 clear_bit(RXRPC_CALL_PINGING, &call->flags);
187 rxrpc_propose_ACK(call, pkt->ack.reason,
188 ntohs(pkt->ack.maxSkew),
189 ntohl(pkt->ack.serial),
190 true, true,
191 rxrpc_propose_ack_retry_tx);
192 } else {
193 spin_lock_bh(&call->lock);
194 if (after(hard_ack, call->ackr_consumed))
195 call->ackr_consumed = hard_ack;
196 if (after(top, call->ackr_seen))
197 call->ackr_seen = top;
198 spin_unlock_bh(&call->lock);
199 }
200 }
201
202out:
203 kfree(pkt);
204 return ret;
205}
206
207/*
208 * Send an ABORT call packet.
209 */
210int rxrpc_send_abort_packet(struct rxrpc_call *call)
211{
212 struct rxrpc_connection *conn;
213 struct rxrpc_abort_buffer pkt;
214 struct msghdr msg;
215 struct kvec iov[1];
216 rxrpc_serial_t serial;
217 int ret;
218
219 if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
220 return -ECONNRESET;
221
222 conn = call->conn;
223
224 msg.msg_name = &call->peer->srx.transport;
225 msg.msg_namelen = call->peer->srx.transport_len;
226 msg.msg_control = NULL;
227 msg.msg_controllen = 0;
228 msg.msg_flags = 0;
229
230 pkt.whdr.epoch = htonl(conn->proto.epoch);
231 pkt.whdr.cid = htonl(call->cid);
232 pkt.whdr.callNumber = htonl(call->call_id);
233 pkt.whdr.seq = 0;
234 pkt.whdr.type = RXRPC_PACKET_TYPE_ABORT;
235 pkt.whdr.flags = conn->out_clientflag;
236 pkt.whdr.userStatus = 0;
237 pkt.whdr.securityIndex = call->security_ix;
238 pkt.whdr._rsvd = 0;
239 pkt.whdr.serviceId = htons(call->service_id);
240 pkt.abort_code = htonl(call->abort_code);
241
242 iov[0].iov_base = &pkt;
243 iov[0].iov_len = sizeof(pkt);
244
245 serial = atomic_inc_return(&conn->serial);
246 pkt.whdr.serial = htonl(serial);
247
248 ret = kernel_sendmsg(conn->params.local->socket,
249 &msg, iov, 1, sizeof(pkt));
250
251 return ret;
252}
253
254/*
255 * send a packet through the transport endpoint
256 */
257int rxrpc_send_data_packet(struct rxrpc_call *call, struct sk_buff *skb,
258 bool retrans)
259{
260 struct rxrpc_connection *conn = call->conn;
261 struct rxrpc_wire_header whdr;
262 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
263 struct msghdr msg;
264 struct kvec iov[2];
265 rxrpc_serial_t serial;
266 size_t len;
267 bool lost = false;
268 int ret, opt;
269
270 _enter(",{%d}", skb->len);
271
272 /* Each transmission of a Tx packet needs a new serial number */
273 serial = atomic_inc_return(&conn->serial);
274
275 whdr.epoch = htonl(conn->proto.epoch);
276 whdr.cid = htonl(call->cid);
277 whdr.callNumber = htonl(call->call_id);
278 whdr.seq = htonl(sp->hdr.seq);
279 whdr.serial = htonl(serial);
280 whdr.type = RXRPC_PACKET_TYPE_DATA;
281 whdr.flags = sp->hdr.flags;
282 whdr.userStatus = 0;
283 whdr.securityIndex = call->security_ix;
284 whdr._rsvd = htons(sp->hdr._rsvd);
285 whdr.serviceId = htons(call->service_id);
286
287 if (test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags) &&
288 sp->hdr.seq == 1)
289 whdr.userStatus = RXRPC_USERSTATUS_SERVICE_UPGRADE;
290
291 iov[0].iov_base = &whdr;
292 iov[0].iov_len = sizeof(whdr);
293 iov[1].iov_base = skb->head;
294 iov[1].iov_len = skb->len;
295 len = iov[0].iov_len + iov[1].iov_len;
296
297 msg.msg_name = &call->peer->srx.transport;
298 msg.msg_namelen = call->peer->srx.transport_len;
299 msg.msg_control = NULL;
300 msg.msg_controllen = 0;
301 msg.msg_flags = 0;
302
303 /* If our RTT cache needs working on, request an ACK. Also request
304 * ACKs if a DATA packet appears to have been lost.
305 */
306 if (!(sp->hdr.flags & RXRPC_LAST_PACKET) &&
307 (retrans ||
308 call->cong_mode == RXRPC_CALL_SLOW_START ||
309 (call->peer->rtt_usage < 3 && sp->hdr.seq & 1) ||
310 ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
311 ktime_get_real())))
312 whdr.flags |= RXRPC_REQUEST_ACK;
313
314 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
315 static int lose;
316 if ((lose++ & 7) == 7) {
317 ret = 0;
318 lost = true;
319 goto done;
320 }
321 }
322
323 _proto("Tx DATA %%%u { #%u }", serial, sp->hdr.seq);
324
325 /* send the packet with the don't fragment bit set if we currently
326 * think it's small enough */
327 if (iov[1].iov_len >= call->peer->maxdata)
328 goto send_fragmentable;
329
330 down_read(&conn->params.local->defrag_sem);
331 /* send the packet by UDP
332 * - returns -EMSGSIZE if UDP would have to fragment the packet
333 * to go out of the interface
334 * - in which case, we'll have processed the ICMP error
335 * message and update the peer record
336 */
337 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
338
339 up_read(&conn->params.local->defrag_sem);
340 if (ret == -EMSGSIZE)
341 goto send_fragmentable;
342
343done:
344 trace_rxrpc_tx_data(call, sp->hdr.seq, serial, whdr.flags,
345 retrans, lost);
346 if (ret >= 0) {
347 ktime_t now = ktime_get_real();
348 skb->tstamp = now;
349 smp_wmb();
350 sp->hdr.serial = serial;
351 if (whdr.flags & RXRPC_REQUEST_ACK) {
352 call->peer->rtt_last_req = now;
353 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_data, serial);
354 }
355 }
356 _leave(" = %d [%u]", ret, call->peer->maxdata);
357 return ret;
358
359send_fragmentable:
360 /* attempt to send this message with fragmentation enabled */
361 _debug("send fragment");
362
363 down_write(&conn->params.local->defrag_sem);
364
365 switch (conn->params.local->srx.transport.family) {
366 case AF_INET:
367 opt = IP_PMTUDISC_DONT;
368 ret = kernel_setsockopt(conn->params.local->socket,
369 SOL_IP, IP_MTU_DISCOVER,
370 (char *)&opt, sizeof(opt));
371 if (ret == 0) {
372 ret = kernel_sendmsg(conn->params.local->socket, &msg,
373 iov, 2, len);
374
375 opt = IP_PMTUDISC_DO;
376 kernel_setsockopt(conn->params.local->socket, SOL_IP,
377 IP_MTU_DISCOVER,
378 (char *)&opt, sizeof(opt));
379 }
380 break;
381
382#ifdef CONFIG_AF_RXRPC_IPV6
383 case AF_INET6:
384 opt = IPV6_PMTUDISC_DONT;
385 ret = kernel_setsockopt(conn->params.local->socket,
386 SOL_IPV6, IPV6_MTU_DISCOVER,
387 (char *)&opt, sizeof(opt));
388 if (ret == 0) {
389 ret = kernel_sendmsg(conn->params.local->socket, &msg,
390 iov, 2, len);
391
392 opt = IPV6_PMTUDISC_DO;
393 kernel_setsockopt(conn->params.local->socket,
394 SOL_IPV6, IPV6_MTU_DISCOVER,
395 (char *)&opt, sizeof(opt));
396 }
397 break;
398#endif
399
400 default:
401 BUG();
402 }
403
404 up_write(&conn->params.local->defrag_sem);
405 goto done;
406}
407
408/*
409 * reject packets through the local endpoint
410 */
411void rxrpc_reject_packets(struct rxrpc_local *local)
412{
413 struct sockaddr_rxrpc srx;
414 struct rxrpc_skb_priv *sp;
415 struct rxrpc_wire_header whdr;
416 struct sk_buff *skb;
417 struct msghdr msg;
418 struct kvec iov[2];
419 size_t size;
420 __be32 code;
421
422 _enter("%d", local->debug_id);
423
424 iov[0].iov_base = &whdr;
425 iov[0].iov_len = sizeof(whdr);
426 iov[1].iov_base = &code;
427 iov[1].iov_len = sizeof(code);
428 size = sizeof(whdr) + sizeof(code);
429
430 msg.msg_name = &srx.transport;
431 msg.msg_control = NULL;
432 msg.msg_controllen = 0;
433 msg.msg_flags = 0;
434
435 memset(&whdr, 0, sizeof(whdr));
436 whdr.type = RXRPC_PACKET_TYPE_ABORT;
437
438 while ((skb = skb_dequeue(&local->reject_queue))) {
439 rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
440 sp = rxrpc_skb(skb);
441
442 if (rxrpc_extract_addr_from_skb(local, &srx, skb) == 0) {
443 msg.msg_namelen = srx.transport_len;
444
445 code = htonl(skb->priority);
446
447 whdr.epoch = htonl(sp->hdr.epoch);
448 whdr.cid = htonl(sp->hdr.cid);
449 whdr.callNumber = htonl(sp->hdr.callNumber);
450 whdr.serviceId = htons(sp->hdr.serviceId);
451 whdr.flags = sp->hdr.flags;
452 whdr.flags ^= RXRPC_CLIENT_INITIATED;
453 whdr.flags &= RXRPC_CLIENT_INITIATED;
454
455 kernel_sendmsg(local->socket, &msg, iov, 2, size);
456 }
457
458 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
459 }
460
461 _leave("");
462}