blob: cf43a715685eab1deae47340e341d823dab8797e [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 Howells8d94aa32016-09-07 09:19:31 +010022struct rxrpc_pkt_buffer {
23 struct rxrpc_wire_header whdr;
24 union {
25 struct {
26 struct rxrpc_ackpacket ack;
27 u8 acks[255];
28 u8 pad[3];
29 };
30 __be32 abort_code;
31 };
32 struct rxrpc_ackinfo ackinfo;
33};
34
35/*
36 * Fill out an ACK packet.
37 */
38static size_t rxrpc_fill_out_ack(struct rxrpc_call *call,
David Howells805b21b2016-09-24 18:05:26 +010039 struct rxrpc_pkt_buffer *pkt,
40 rxrpc_seq_t *_hard_ack,
41 rxrpc_seq_t *_top)
David Howells8d94aa32016-09-07 09:19:31 +010042{
David Howellsf3639df2016-09-17 10:49:13 +010043 rxrpc_serial_t serial;
David Howells248f2192016-09-08 11:10:12 +010044 rxrpc_seq_t hard_ack, top, seq;
45 int ix;
David Howells8d94aa32016-09-07 09:19:31 +010046 u32 mtu, jmax;
47 u8 *ackp = pkt->acks;
48
David Howells248f2192016-09-08 11:10:12 +010049 /* Barrier against rxrpc_input_data(). */
David Howellsf3639df2016-09-17 10:49:13 +010050 serial = call->ackr_serial;
David Howells248f2192016-09-08 11:10:12 +010051 hard_ack = READ_ONCE(call->rx_hard_ack);
52 top = smp_load_acquire(&call->rx_top);
David Howells805b21b2016-09-24 18:05:26 +010053 *_hard_ack = hard_ack;
54 *_top = top;
David Howells248f2192016-09-08 11:10:12 +010055
David Howells8d94aa32016-09-07 09:19:31 +010056 pkt->ack.bufferSpace = htons(8);
David Howells248f2192016-09-08 11:10:12 +010057 pkt->ack.maxSkew = htons(call->ackr_skew);
58 pkt->ack.firstPacket = htonl(hard_ack + 1);
David Howells8d94aa32016-09-07 09:19:31 +010059 pkt->ack.previousPacket = htonl(call->ackr_prev_seq);
David Howellsf3639df2016-09-17 10:49:13 +010060 pkt->ack.serial = htonl(serial);
David Howells248f2192016-09-08 11:10:12 +010061 pkt->ack.reason = call->ackr_reason;
62 pkt->ack.nAcks = top - hard_ack;
David Howells8d94aa32016-09-07 09:19:31 +010063
David Howells8e831342016-09-22 00:29:31 +010064 if (pkt->ack.reason == RXRPC_ACK_PING)
65 pkt->whdr.flags |= RXRPC_REQUEST_ACK;
66
David Howells248f2192016-09-08 11:10:12 +010067 if (after(top, hard_ack)) {
68 seq = hard_ack + 1;
69 do {
70 ix = seq & RXRPC_RXTX_BUFF_MASK;
71 if (call->rxtx_buffer[ix])
72 *ackp++ = RXRPC_ACK_TYPE_ACK;
73 else
74 *ackp++ = RXRPC_ACK_TYPE_NACK;
75 seq++;
76 } while (before_eq(seq, top));
77 }
78
79 mtu = call->conn->params.peer->if_mtu;
80 mtu -= call->conn->params.peer->hdrsize;
David Howells75e42122016-09-13 22:36:22 +010081 jmax = (call->nr_jumbo_bad > 3) ? 1 : rxrpc_rx_jumbo_max;
David Howells8d94aa32016-09-07 09:19:31 +010082 pkt->ackinfo.rxMTU = htonl(rxrpc_rx_mtu);
83 pkt->ackinfo.maxMTU = htonl(mtu);
David Howells75e42122016-09-13 22:36:22 +010084 pkt->ackinfo.rwind = htonl(call->rx_winsize);
David Howells8d94aa32016-09-07 09:19:31 +010085 pkt->ackinfo.jumbo_max = htonl(jmax);
86
87 *ackp++ = 0;
88 *ackp++ = 0;
89 *ackp++ = 0;
David Howells248f2192016-09-08 11:10:12 +010090 return top - hard_ack + 3;
David Howells8d94aa32016-09-07 09:19:31 +010091}
92
93/*
David Howells248f2192016-09-08 11:10:12 +010094 * Send an ACK or ABORT call packet.
David Howells8d94aa32016-09-07 09:19:31 +010095 */
96int rxrpc_send_call_packet(struct rxrpc_call *call, u8 type)
97{
98 struct rxrpc_connection *conn = NULL;
99 struct rxrpc_pkt_buffer *pkt;
100 struct msghdr msg;
101 struct kvec iov[2];
102 rxrpc_serial_t serial;
David Howells805b21b2016-09-24 18:05:26 +0100103 rxrpc_seq_t hard_ack, top;
David Howells8d94aa32016-09-07 09:19:31 +0100104 size_t len, n;
David Howells8e831342016-09-22 00:29:31 +0100105 bool ping = false;
David Howells8d94aa32016-09-07 09:19:31 +0100106 int ioc, ret;
107 u32 abort_code;
108
109 _enter("%u,%s", call->debug_id, rxrpc_pkts[type]);
110
111 spin_lock_bh(&call->lock);
112 if (call->conn)
113 conn = rxrpc_get_connection_maybe(call->conn);
114 spin_unlock_bh(&call->lock);
115 if (!conn)
116 return -ECONNRESET;
117
118 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
119 if (!pkt) {
120 rxrpc_put_connection(conn);
121 return -ENOMEM;
122 }
123
David Howells8d94aa32016-09-07 09:19:31 +0100124 msg.msg_name = &call->peer->srx.transport;
125 msg.msg_namelen = call->peer->srx.transport_len;
126 msg.msg_control = NULL;
127 msg.msg_controllen = 0;
128 msg.msg_flags = 0;
129
130 pkt->whdr.epoch = htonl(conn->proto.epoch);
131 pkt->whdr.cid = htonl(call->cid);
132 pkt->whdr.callNumber = htonl(call->call_id);
133 pkt->whdr.seq = 0;
David Howells8d94aa32016-09-07 09:19:31 +0100134 pkt->whdr.type = type;
135 pkt->whdr.flags = conn->out_clientflag;
136 pkt->whdr.userStatus = 0;
137 pkt->whdr.securityIndex = call->security_ix;
138 pkt->whdr._rsvd = 0;
139 pkt->whdr.serviceId = htons(call->service_id);
140
141 iov[0].iov_base = pkt;
142 iov[0].iov_len = sizeof(pkt->whdr);
143 len = sizeof(pkt->whdr);
144
145 switch (type) {
146 case RXRPC_PACKET_TYPE_ACK:
147 spin_lock_bh(&call->lock);
David Howells27d0fc42016-09-17 10:49:13 +0100148 if (!call->ackr_reason) {
149 spin_unlock_bh(&call->lock);
150 ret = 0;
151 goto out;
152 }
David Howells8e831342016-09-22 00:29:31 +0100153 ping = (call->ackr_reason == RXRPC_ACK_PING);
David Howells805b21b2016-09-24 18:05:26 +0100154 n = rxrpc_fill_out_ack(call, pkt, &hard_ack, &top);
David Howells8d94aa32016-09-07 09:19:31 +0100155 call->ackr_reason = 0;
156
157 spin_unlock_bh(&call->lock);
158
David Howells8d94aa32016-09-07 09:19:31 +0100159
David Howells57494342016-09-24 18:05:27 +0100160 pkt->whdr.flags |= RXRPC_SLOW_START_OK;
161
David Howells8d94aa32016-09-07 09:19:31 +0100162 iov[0].iov_len += sizeof(pkt->ack) + n;
163 iov[1].iov_base = &pkt->ackinfo;
164 iov[1].iov_len = sizeof(pkt->ackinfo);
165 len += sizeof(pkt->ack) + n + sizeof(pkt->ackinfo);
166 ioc = 2;
167 break;
168
169 case RXRPC_PACKET_TYPE_ABORT:
170 abort_code = call->abort_code;
171 pkt->abort_code = htonl(abort_code);
David Howells8d94aa32016-09-07 09:19:31 +0100172 iov[0].iov_len += sizeof(pkt->abort_code);
173 len += sizeof(pkt->abort_code);
174 ioc = 1;
175 break;
176
177 default:
178 BUG();
179 ret = -ENOANO;
180 goto out;
181 }
182
David Howellsb86e2182016-09-23 15:08:48 +0100183 serial = atomic_inc_return(&conn->serial);
184 pkt->whdr.serial = htonl(serial);
185 switch (type) {
186 case RXRPC_PACKET_TYPE_ACK:
David Howellsbe832ae2016-09-23 12:39:22 +0100187 trace_rxrpc_tx_ack(call, serial,
David Howellsb86e2182016-09-23 15:08:48 +0100188 ntohl(pkt->ack.firstPacket),
189 ntohl(pkt->ack.serial),
190 pkt->ack.reason, pkt->ack.nAcks);
191 break;
192 }
193
David Howells8e831342016-09-22 00:29:31 +0100194 if (ping) {
195 call->ackr_ping = serial;
196 smp_wmb();
197 /* We need to stick a time in before we send the packet in case
198 * the reply gets back before kernel_sendmsg() completes - but
199 * asking UDP to send the packet can take a relatively long
200 * time, so we update the time after, on the assumption that
201 * the packet transmission is more likely to happen towards the
202 * end of the kernel_sendmsg() call.
203 */
204 call->ackr_ping_time = ktime_get_real();
205 set_bit(RXRPC_CALL_PINGING, &call->flags);
206 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_ping, serial);
207 }
David Howells8d94aa32016-09-07 09:19:31 +0100208 ret = kernel_sendmsg(conn->params.local->socket,
209 &msg, iov, ioc, len);
David Howells8e831342016-09-22 00:29:31 +0100210 if (ping)
211 call->ackr_ping_time = ktime_get_real();
David Howells8d94aa32016-09-07 09:19:31 +0100212
David Howells805b21b2016-09-24 18:05:26 +0100213 if (type == RXRPC_PACKET_TYPE_ACK &&
214 call->state < RXRPC_CALL_COMPLETE) {
215 if (ret < 0) {
David Howells8e831342016-09-22 00:29:31 +0100216 clear_bit(RXRPC_CALL_PINGING, &call->flags);
David Howells248f2192016-09-08 11:10:12 +0100217 rxrpc_propose_ACK(call, pkt->ack.reason,
218 ntohs(pkt->ack.maxSkew),
219 ntohl(pkt->ack.serial),
David Howells9c7ad432016-09-23 13:50:40 +0100220 true, true,
221 rxrpc_propose_ack_retry_tx);
David Howells805b21b2016-09-24 18:05:26 +0100222 } else {
223 spin_lock_bh(&call->lock);
224 if (after(hard_ack, call->ackr_consumed))
225 call->ackr_consumed = hard_ack;
226 if (after(top, call->ackr_seen))
227 call->ackr_seen = top;
228 spin_unlock_bh(&call->lock);
David Howells248f2192016-09-08 11:10:12 +0100229 }
230 }
231
David Howells8d94aa32016-09-07 09:19:31 +0100232out:
233 rxrpc_put_connection(conn);
234 kfree(pkt);
235 return ret;
236}
237
David Howells5873c082014-02-07 18:58:44 +0000238/*
David Howells17926a72007-04-26 15:48:28 -0700239 * send a packet through the transport endpoint
240 */
David Howells5a924b82016-09-22 00:29:31 +0100241int rxrpc_send_data_packet(struct rxrpc_call *call, struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700242{
David Howells5a924b82016-09-22 00:29:31 +0100243 struct rxrpc_connection *conn = call->conn;
244 struct rxrpc_wire_header whdr;
245 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700246 struct msghdr msg;
David Howells5a924b82016-09-22 00:29:31 +0100247 struct kvec iov[2];
248 rxrpc_serial_t serial;
249 size_t len;
David Howells17926a72007-04-26 15:48:28 -0700250 int ret, opt;
251
252 _enter(",{%d}", skb->len);
253
David Howells5a924b82016-09-22 00:29:31 +0100254 /* Each transmission of a Tx packet needs a new serial number */
255 serial = atomic_inc_return(&conn->serial);
David Howells17926a72007-04-26 15:48:28 -0700256
David Howells5a924b82016-09-22 00:29:31 +0100257 whdr.epoch = htonl(conn->proto.epoch);
258 whdr.cid = htonl(call->cid);
259 whdr.callNumber = htonl(call->call_id);
260 whdr.seq = htonl(sp->hdr.seq);
261 whdr.serial = htonl(serial);
262 whdr.type = RXRPC_PACKET_TYPE_DATA;
263 whdr.flags = sp->hdr.flags;
264 whdr.userStatus = 0;
265 whdr.securityIndex = call->security_ix;
266 whdr._rsvd = htons(sp->hdr._rsvd);
267 whdr.serviceId = htons(call->service_id);
268
269 iov[0].iov_base = &whdr;
270 iov[0].iov_len = sizeof(whdr);
271 iov[1].iov_base = skb->head;
272 iov[1].iov_len = skb->len;
273 len = iov[0].iov_len + iov[1].iov_len;
274
275 msg.msg_name = &call->peer->srx.transport;
276 msg.msg_namelen = call->peer->srx.transport_len;
David Howells17926a72007-04-26 15:48:28 -0700277 msg.msg_control = NULL;
278 msg.msg_controllen = 0;
279 msg.msg_flags = 0;
280
David Howells57494342016-09-24 18:05:27 +0100281 /* If our RTT cache needs working on, request an ACK. Also request
282 * ACKs if a DATA packet appears to have been lost.
283 */
284 if (call->cong_mode == RXRPC_CALL_FAST_RETRANSMIT ||
285 (call->peer->rtt_usage < 3 && sp->hdr.seq & 1) ||
David Howells0d4b1032016-09-22 00:29:31 +0100286 ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
287 ktime_get_real()))
288 whdr.flags |= RXRPC_REQUEST_ACK;
289
David Howells8a681c362016-09-17 10:49:15 +0100290 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
291 static int lose;
292 if ((lose++ & 7) == 7) {
David Howellsbe832ae2016-09-23 12:39:22 +0100293 trace_rxrpc_tx_data(call, sp->hdr.seq, serial,
294 whdr.flags, true);
David Howells8a681c362016-09-17 10:49:15 +0100295 rxrpc_lose_skb(skb, rxrpc_skb_tx_lost);
296 _leave(" = 0 [lose]");
297 return 0;
298 }
299 }
300
David Howells5a924b82016-09-22 00:29:31 +0100301 _proto("Tx DATA %%%u { #%u }", serial, sp->hdr.seq);
302
David Howells17926a72007-04-26 15:48:28 -0700303 /* send the packet with the don't fragment bit set if we currently
304 * think it's small enough */
David Howells5a924b82016-09-22 00:29:31 +0100305 if (iov[1].iov_len >= call->peer->maxdata)
306 goto send_fragmentable;
David Howells17926a72007-04-26 15:48:28 -0700307
David Howells5a924b82016-09-22 00:29:31 +0100308 down_read(&conn->params.local->defrag_sem);
309 /* send the packet by UDP
310 * - returns -EMSGSIZE if UDP would have to fragment the packet
311 * to go out of the interface
312 * - in which case, we'll have processed the ICMP error
313 * message and update the peer record
314 */
315 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
David Howells17926a72007-04-26 15:48:28 -0700316
David Howells5a924b82016-09-22 00:29:31 +0100317 up_read(&conn->params.local->defrag_sem);
318 if (ret == -EMSGSIZE)
319 goto send_fragmentable;
320
321done:
David Howellsbe832ae2016-09-23 12:39:22 +0100322 trace_rxrpc_tx_data(call, sp->hdr.seq, serial, whdr.flags, false);
David Howells50235c42016-09-22 00:29:31 +0100323 if (ret >= 0) {
David Howells0d4b1032016-09-22 00:29:31 +0100324 ktime_t now = ktime_get_real();
325 skb->tstamp = now;
David Howells50235c42016-09-22 00:29:31 +0100326 smp_wmb();
David Howells5a924b82016-09-22 00:29:31 +0100327 sp->hdr.serial = serial;
David Howells0d4b1032016-09-22 00:29:31 +0100328 if (whdr.flags & RXRPC_REQUEST_ACK) {
329 call->peer->rtt_last_req = now;
David Howells50235c42016-09-22 00:29:31 +0100330 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_data, serial);
David Howells0d4b1032016-09-22 00:29:31 +0100331 }
David Howells17926a72007-04-26 15:48:28 -0700332 }
David Howells5a924b82016-09-22 00:29:31 +0100333 _leave(" = %d [%u]", ret, call->peer->maxdata);
334 return ret;
David Howells17926a72007-04-26 15:48:28 -0700335
336send_fragmentable:
337 /* attempt to send this message with fragmentation enabled */
338 _debug("send fragment");
339
David Howells985a5c82016-06-17 11:53:37 +0100340 down_write(&conn->params.local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -0700341
David Howells985a5c82016-06-17 11:53:37 +0100342 switch (conn->params.local->srx.transport.family) {
343 case AF_INET:
344 opt = IP_PMTUDISC_DONT;
345 ret = kernel_setsockopt(conn->params.local->socket,
346 SOL_IP, IP_MTU_DISCOVER,
347 (char *)&opt, sizeof(opt));
348 if (ret == 0) {
David Howells5a924b82016-09-22 00:29:31 +0100349 ret = kernel_sendmsg(conn->params.local->socket, &msg,
350 iov, 2, len);
David Howells985a5c82016-06-17 11:53:37 +0100351
352 opt = IP_PMTUDISC_DO;
353 kernel_setsockopt(conn->params.local->socket, SOL_IP,
354 IP_MTU_DISCOVER,
355 (char *)&opt, sizeof(opt));
356 }
357 break;
David Howells75b54cb2016-09-13 08:49:05 +0100358
David Howellsd1912742016-09-17 07:26:01 +0100359#ifdef CONFIG_AF_RXRPC_IPV6
David Howells75b54cb2016-09-13 08:49:05 +0100360 case AF_INET6:
361 opt = IPV6_PMTUDISC_DONT;
362 ret = kernel_setsockopt(conn->params.local->socket,
363 SOL_IPV6, IPV6_MTU_DISCOVER,
364 (char *)&opt, sizeof(opt));
365 if (ret == 0) {
366 ret = kernel_sendmsg(conn->params.local->socket, &msg,
367 iov, 1, iov[0].iov_len);
368
369 opt = IPV6_PMTUDISC_DO;
370 kernel_setsockopt(conn->params.local->socket,
371 SOL_IPV6, IPV6_MTU_DISCOVER,
372 (char *)&opt, sizeof(opt));
373 }
374 break;
David Howellsd1912742016-09-17 07:26:01 +0100375#endif
David Howells17926a72007-04-26 15:48:28 -0700376 }
377
David Howells985a5c82016-06-17 11:53:37 +0100378 up_write(&conn->params.local->defrag_sem);
David Howells5a924b82016-09-22 00:29:31 +0100379 goto done;
David Howells17926a72007-04-26 15:48:28 -0700380}
David Howells248f2192016-09-08 11:10:12 +0100381
382/*
383 * reject packets through the local endpoint
384 */
385void rxrpc_reject_packets(struct rxrpc_local *local)
386{
David Howells1c2bc7b2016-09-13 08:49:05 +0100387 struct sockaddr_rxrpc srx;
David Howells248f2192016-09-08 11:10:12 +0100388 struct rxrpc_skb_priv *sp;
389 struct rxrpc_wire_header whdr;
390 struct sk_buff *skb;
391 struct msghdr msg;
392 struct kvec iov[2];
393 size_t size;
394 __be32 code;
395
396 _enter("%d", local->debug_id);
397
398 iov[0].iov_base = &whdr;
399 iov[0].iov_len = sizeof(whdr);
400 iov[1].iov_base = &code;
401 iov[1].iov_len = sizeof(code);
402 size = sizeof(whdr) + sizeof(code);
403
David Howells1c2bc7b2016-09-13 08:49:05 +0100404 msg.msg_name = &srx.transport;
David Howells248f2192016-09-08 11:10:12 +0100405 msg.msg_control = NULL;
406 msg.msg_controllen = 0;
407 msg.msg_flags = 0;
408
David Howells248f2192016-09-08 11:10:12 +0100409 memset(&whdr, 0, sizeof(whdr));
410 whdr.type = RXRPC_PACKET_TYPE_ABORT;
411
412 while ((skb = skb_dequeue(&local->reject_queue))) {
David Howells71f3ca42016-09-17 10:49:14 +0100413 rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
David Howells248f2192016-09-08 11:10:12 +0100414 sp = rxrpc_skb(skb);
David Howells1c2bc7b2016-09-13 08:49:05 +0100415
416 if (rxrpc_extract_addr_from_skb(&srx, skb) == 0) {
417 msg.msg_namelen = srx.transport_len;
418
David Howells248f2192016-09-08 11:10:12 +0100419 code = htonl(skb->priority);
420
421 whdr.epoch = htonl(sp->hdr.epoch);
422 whdr.cid = htonl(sp->hdr.cid);
423 whdr.callNumber = htonl(sp->hdr.callNumber);
424 whdr.serviceId = htons(sp->hdr.serviceId);
425 whdr.flags = sp->hdr.flags;
426 whdr.flags ^= RXRPC_CLIENT_INITIATED;
427 whdr.flags &= RXRPC_CLIENT_INITIATED;
428
429 kernel_sendmsg(local->socket, &msg, iov, 2, size);
David Howells248f2192016-09-08 11:10:12 +0100430 }
431
David Howells71f3ca42016-09-17 10:49:14 +0100432 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
David Howells248f2192016-09-08 11:10:12 +0100433 }
434
435 _leave("");
436}