blob: ac9a58b619a62abd6c4888d3e97c5792a64cbcde [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 Howellsa1767072016-09-29 22:37:15 +0100241int rxrpc_send_data_packet(struct rxrpc_call *call, struct sk_buff *skb,
242 bool retrans)
David Howells17926a72007-04-26 15:48:28 -0700243{
David Howells5a924b82016-09-22 00:29:31 +0100244 struct rxrpc_connection *conn = call->conn;
245 struct rxrpc_wire_header whdr;
246 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700247 struct msghdr msg;
David Howells5a924b82016-09-22 00:29:31 +0100248 struct kvec iov[2];
249 rxrpc_serial_t serial;
250 size_t len;
David Howellsa1767072016-09-29 22:37:15 +0100251 bool lost = false;
David Howells17926a72007-04-26 15:48:28 -0700252 int ret, opt;
253
254 _enter(",{%d}", skb->len);
255
David Howells5a924b82016-09-22 00:29:31 +0100256 /* Each transmission of a Tx packet needs a new serial number */
257 serial = atomic_inc_return(&conn->serial);
David Howells17926a72007-04-26 15:48:28 -0700258
David Howells5a924b82016-09-22 00:29:31 +0100259 whdr.epoch = htonl(conn->proto.epoch);
260 whdr.cid = htonl(call->cid);
261 whdr.callNumber = htonl(call->call_id);
262 whdr.seq = htonl(sp->hdr.seq);
263 whdr.serial = htonl(serial);
264 whdr.type = RXRPC_PACKET_TYPE_DATA;
265 whdr.flags = sp->hdr.flags;
266 whdr.userStatus = 0;
267 whdr.securityIndex = call->security_ix;
268 whdr._rsvd = htons(sp->hdr._rsvd);
269 whdr.serviceId = htons(call->service_id);
270
271 iov[0].iov_base = &whdr;
272 iov[0].iov_len = sizeof(whdr);
273 iov[1].iov_base = skb->head;
274 iov[1].iov_len = skb->len;
275 len = iov[0].iov_len + iov[1].iov_len;
276
277 msg.msg_name = &call->peer->srx.transport;
278 msg.msg_namelen = call->peer->srx.transport_len;
David Howells17926a72007-04-26 15:48:28 -0700279 msg.msg_control = NULL;
280 msg.msg_controllen = 0;
281 msg.msg_flags = 0;
282
David Howells57494342016-09-24 18:05:27 +0100283 /* If our RTT cache needs working on, request an ACK. Also request
284 * ACKs if a DATA packet appears to have been lost.
285 */
David Howellsa1767072016-09-29 22:37:15 +0100286 if (retrans ||
David Howells57494342016-09-24 18:05:27 +0100287 (call->peer->rtt_usage < 3 && sp->hdr.seq & 1) ||
David Howells0d4b1032016-09-22 00:29:31 +0100288 ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
289 ktime_get_real()))
290 whdr.flags |= RXRPC_REQUEST_ACK;
291
David Howells8a681c362016-09-17 10:49:15 +0100292 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
293 static int lose;
294 if ((lose++ & 7) == 7) {
David Howellsa1767072016-09-29 22:37:15 +0100295 ret = 0;
296 lost = true;
297 goto done;
David Howells8a681c362016-09-17 10:49:15 +0100298 }
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 Howellsa1767072016-09-29 22:37:15 +0100322 trace_rxrpc_tx_data(call, sp->hdr.seq, serial, whdr.flags,
323 retrans, lost);
David Howells50235c42016-09-22 00:29:31 +0100324 if (ret >= 0) {
David Howells0d4b1032016-09-22 00:29:31 +0100325 ktime_t now = ktime_get_real();
326 skb->tstamp = now;
David Howells50235c42016-09-22 00:29:31 +0100327 smp_wmb();
David Howells5a924b82016-09-22 00:29:31 +0100328 sp->hdr.serial = serial;
David Howells0d4b1032016-09-22 00:29:31 +0100329 if (whdr.flags & RXRPC_REQUEST_ACK) {
330 call->peer->rtt_last_req = now;
David Howells50235c42016-09-22 00:29:31 +0100331 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_data, serial);
David Howells0d4b1032016-09-22 00:29:31 +0100332 }
David Howells17926a72007-04-26 15:48:28 -0700333 }
David Howells5a924b82016-09-22 00:29:31 +0100334 _leave(" = %d [%u]", ret, call->peer->maxdata);
335 return ret;
David Howells17926a72007-04-26 15:48:28 -0700336
337send_fragmentable:
338 /* attempt to send this message with fragmentation enabled */
339 _debug("send fragment");
340
David Howells985a5c82016-06-17 11:53:37 +0100341 down_write(&conn->params.local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -0700342
David Howells985a5c82016-06-17 11:53:37 +0100343 switch (conn->params.local->srx.transport.family) {
344 case AF_INET:
345 opt = IP_PMTUDISC_DONT;
346 ret = kernel_setsockopt(conn->params.local->socket,
347 SOL_IP, IP_MTU_DISCOVER,
348 (char *)&opt, sizeof(opt));
349 if (ret == 0) {
David Howells5a924b82016-09-22 00:29:31 +0100350 ret = kernel_sendmsg(conn->params.local->socket, &msg,
351 iov, 2, len);
David Howells985a5c82016-06-17 11:53:37 +0100352
353 opt = IP_PMTUDISC_DO;
354 kernel_setsockopt(conn->params.local->socket, SOL_IP,
355 IP_MTU_DISCOVER,
356 (char *)&opt, sizeof(opt));
357 }
358 break;
David Howells75b54cb2016-09-13 08:49:05 +0100359
David Howellsd1912742016-09-17 07:26:01 +0100360#ifdef CONFIG_AF_RXRPC_IPV6
David Howells75b54cb2016-09-13 08:49:05 +0100361 case AF_INET6:
362 opt = IPV6_PMTUDISC_DONT;
363 ret = kernel_setsockopt(conn->params.local->socket,
364 SOL_IPV6, IPV6_MTU_DISCOVER,
365 (char *)&opt, sizeof(opt));
366 if (ret == 0) {
367 ret = kernel_sendmsg(conn->params.local->socket, &msg,
368 iov, 1, iov[0].iov_len);
369
370 opt = IPV6_PMTUDISC_DO;
371 kernel_setsockopt(conn->params.local->socket,
372 SOL_IPV6, IPV6_MTU_DISCOVER,
373 (char *)&opt, sizeof(opt));
374 }
375 break;
David Howellsd1912742016-09-17 07:26:01 +0100376#endif
David Howells17926a72007-04-26 15:48:28 -0700377 }
378
David Howells985a5c82016-06-17 11:53:37 +0100379 up_write(&conn->params.local->defrag_sem);
David Howells5a924b82016-09-22 00:29:31 +0100380 goto done;
David Howells17926a72007-04-26 15:48:28 -0700381}
David Howells248f2192016-09-08 11:10:12 +0100382
383/*
384 * reject packets through the local endpoint
385 */
386void rxrpc_reject_packets(struct rxrpc_local *local)
387{
David Howells1c2bc7b2016-09-13 08:49:05 +0100388 struct sockaddr_rxrpc srx;
David Howells248f2192016-09-08 11:10:12 +0100389 struct rxrpc_skb_priv *sp;
390 struct rxrpc_wire_header whdr;
391 struct sk_buff *skb;
392 struct msghdr msg;
393 struct kvec iov[2];
394 size_t size;
395 __be32 code;
396
397 _enter("%d", local->debug_id);
398
399 iov[0].iov_base = &whdr;
400 iov[0].iov_len = sizeof(whdr);
401 iov[1].iov_base = &code;
402 iov[1].iov_len = sizeof(code);
403 size = sizeof(whdr) + sizeof(code);
404
David Howells1c2bc7b2016-09-13 08:49:05 +0100405 msg.msg_name = &srx.transport;
David Howells248f2192016-09-08 11:10:12 +0100406 msg.msg_control = NULL;
407 msg.msg_controllen = 0;
408 msg.msg_flags = 0;
409
David Howells248f2192016-09-08 11:10:12 +0100410 memset(&whdr, 0, sizeof(whdr));
411 whdr.type = RXRPC_PACKET_TYPE_ABORT;
412
413 while ((skb = skb_dequeue(&local->reject_queue))) {
David Howells71f3ca42016-09-17 10:49:14 +0100414 rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
David Howells248f2192016-09-08 11:10:12 +0100415 sp = rxrpc_skb(skb);
David Howells1c2bc7b2016-09-13 08:49:05 +0100416
417 if (rxrpc_extract_addr_from_skb(&srx, skb) == 0) {
418 msg.msg_namelen = srx.transport_len;
419
David Howells248f2192016-09-08 11:10:12 +0100420 code = htonl(skb->priority);
421
422 whdr.epoch = htonl(sp->hdr.epoch);
423 whdr.cid = htonl(sp->hdr.cid);
424 whdr.callNumber = htonl(sp->hdr.callNumber);
425 whdr.serviceId = htons(sp->hdr.serviceId);
426 whdr.flags = sp->hdr.flags;
427 whdr.flags ^= RXRPC_CLIENT_INITIATED;
428 whdr.flags &= RXRPC_CLIENT_INITIATED;
429
430 kernel_sendmsg(local->socket, &msg, iov, 2, size);
David Howells248f2192016-09-08 11:10:12 +0100431 }
432
David Howells71f3ca42016-09-17 10:49:14 +0100433 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
David Howells248f2192016-09-08 11:10:12 +0100434 }
435
436 _leave("");
437}