blob: 3eb01445e8146daf6c0f18941fbe746713214e05 [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
160 iov[0].iov_len += sizeof(pkt->ack) + n;
161 iov[1].iov_base = &pkt->ackinfo;
162 iov[1].iov_len = sizeof(pkt->ackinfo);
163 len += sizeof(pkt->ack) + n + sizeof(pkt->ackinfo);
164 ioc = 2;
165 break;
166
167 case RXRPC_PACKET_TYPE_ABORT:
168 abort_code = call->abort_code;
169 pkt->abort_code = htonl(abort_code);
David Howells8d94aa32016-09-07 09:19:31 +0100170 iov[0].iov_len += sizeof(pkt->abort_code);
171 len += sizeof(pkt->abort_code);
172 ioc = 1;
173 break;
174
175 default:
176 BUG();
177 ret = -ENOANO;
178 goto out;
179 }
180
David Howellsb86e2182016-09-23 15:08:48 +0100181 serial = atomic_inc_return(&conn->serial);
182 pkt->whdr.serial = htonl(serial);
183 switch (type) {
184 case RXRPC_PACKET_TYPE_ACK:
David Howellsbe832ae2016-09-23 12:39:22 +0100185 trace_rxrpc_tx_ack(call, serial,
David Howellsb86e2182016-09-23 15:08:48 +0100186 ntohl(pkt->ack.firstPacket),
187 ntohl(pkt->ack.serial),
188 pkt->ack.reason, pkt->ack.nAcks);
189 break;
190 }
191
David Howells8e831342016-09-22 00:29:31 +0100192 if (ping) {
193 call->ackr_ping = serial;
194 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
198 * time, so we update the time after, on the assumption that
199 * the packet transmission is more likely to happen towards the
200 * end of the kernel_sendmsg() call.
201 */
202 call->ackr_ping_time = ktime_get_real();
203 set_bit(RXRPC_CALL_PINGING, &call->flags);
204 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_ping, serial);
205 }
David Howells8d94aa32016-09-07 09:19:31 +0100206 ret = kernel_sendmsg(conn->params.local->socket,
207 &msg, iov, ioc, len);
David Howells8e831342016-09-22 00:29:31 +0100208 if (ping)
209 call->ackr_ping_time = ktime_get_real();
David Howells8d94aa32016-09-07 09:19:31 +0100210
David Howells805b21b2016-09-24 18:05:26 +0100211 if (type == RXRPC_PACKET_TYPE_ACK &&
212 call->state < RXRPC_CALL_COMPLETE) {
213 if (ret < 0) {
David Howells8e831342016-09-22 00:29:31 +0100214 clear_bit(RXRPC_CALL_PINGING, &call->flags);
David Howells248f2192016-09-08 11:10:12 +0100215 rxrpc_propose_ACK(call, pkt->ack.reason,
216 ntohs(pkt->ack.maxSkew),
217 ntohl(pkt->ack.serial),
David Howells9c7ad432016-09-23 13:50:40 +0100218 true, true,
219 rxrpc_propose_ack_retry_tx);
David Howells805b21b2016-09-24 18:05:26 +0100220 } else {
221 spin_lock_bh(&call->lock);
222 if (after(hard_ack, call->ackr_consumed))
223 call->ackr_consumed = hard_ack;
224 if (after(top, call->ackr_seen))
225 call->ackr_seen = top;
226 spin_unlock_bh(&call->lock);
David Howells248f2192016-09-08 11:10:12 +0100227 }
228 }
229
David Howells8d94aa32016-09-07 09:19:31 +0100230out:
231 rxrpc_put_connection(conn);
232 kfree(pkt);
233 return ret;
234}
235
David Howells5873c082014-02-07 18:58:44 +0000236/*
David Howells17926a72007-04-26 15:48:28 -0700237 * send a packet through the transport endpoint
238 */
David Howells5a924b82016-09-22 00:29:31 +0100239int rxrpc_send_data_packet(struct rxrpc_call *call, struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700240{
David Howells5a924b82016-09-22 00:29:31 +0100241 struct rxrpc_connection *conn = call->conn;
242 struct rxrpc_wire_header whdr;
243 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700244 struct msghdr msg;
David Howells5a924b82016-09-22 00:29:31 +0100245 struct kvec iov[2];
246 rxrpc_serial_t serial;
247 size_t len;
David Howells17926a72007-04-26 15:48:28 -0700248 int ret, opt;
249
250 _enter(",{%d}", skb->len);
251
David Howells5a924b82016-09-22 00:29:31 +0100252 /* Each transmission of a Tx packet needs a new serial number */
253 serial = atomic_inc_return(&conn->serial);
David Howells17926a72007-04-26 15:48:28 -0700254
David Howells5a924b82016-09-22 00:29:31 +0100255 whdr.epoch = htonl(conn->proto.epoch);
256 whdr.cid = htonl(call->cid);
257 whdr.callNumber = htonl(call->call_id);
258 whdr.seq = htonl(sp->hdr.seq);
259 whdr.serial = htonl(serial);
260 whdr.type = RXRPC_PACKET_TYPE_DATA;
261 whdr.flags = sp->hdr.flags;
262 whdr.userStatus = 0;
263 whdr.securityIndex = call->security_ix;
264 whdr._rsvd = htons(sp->hdr._rsvd);
265 whdr.serviceId = htons(call->service_id);
266
267 iov[0].iov_base = &whdr;
268 iov[0].iov_len = sizeof(whdr);
269 iov[1].iov_base = skb->head;
270 iov[1].iov_len = skb->len;
271 len = iov[0].iov_len + iov[1].iov_len;
272
273 msg.msg_name = &call->peer->srx.transport;
274 msg.msg_namelen = call->peer->srx.transport_len;
David Howells17926a72007-04-26 15:48:28 -0700275 msg.msg_control = NULL;
276 msg.msg_controllen = 0;
277 msg.msg_flags = 0;
278
David Howells0d4b1032016-09-22 00:29:31 +0100279 /* If our RTT cache needs working on, request an ACK. */
280 if ((call->peer->rtt_usage < 3 && sp->hdr.seq & 1) ||
281 ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
282 ktime_get_real()))
283 whdr.flags |= RXRPC_REQUEST_ACK;
284
David Howells8a681c362016-09-17 10:49:15 +0100285 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
286 static int lose;
287 if ((lose++ & 7) == 7) {
David Howellsbe832ae2016-09-23 12:39:22 +0100288 trace_rxrpc_tx_data(call, sp->hdr.seq, serial,
289 whdr.flags, true);
David Howells8a681c362016-09-17 10:49:15 +0100290 rxrpc_lose_skb(skb, rxrpc_skb_tx_lost);
291 _leave(" = 0 [lose]");
292 return 0;
293 }
294 }
295
David Howells5a924b82016-09-22 00:29:31 +0100296 _proto("Tx DATA %%%u { #%u }", serial, sp->hdr.seq);
297
David Howells17926a72007-04-26 15:48:28 -0700298 /* send the packet with the don't fragment bit set if we currently
299 * think it's small enough */
David Howells5a924b82016-09-22 00:29:31 +0100300 if (iov[1].iov_len >= call->peer->maxdata)
301 goto send_fragmentable;
David Howells17926a72007-04-26 15:48:28 -0700302
David Howells5a924b82016-09-22 00:29:31 +0100303 down_read(&conn->params.local->defrag_sem);
304 /* send the packet by UDP
305 * - returns -EMSGSIZE if UDP would have to fragment the packet
306 * to go out of the interface
307 * - in which case, we'll have processed the ICMP error
308 * message and update the peer record
309 */
310 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
David Howells17926a72007-04-26 15:48:28 -0700311
David Howells5a924b82016-09-22 00:29:31 +0100312 up_read(&conn->params.local->defrag_sem);
313 if (ret == -EMSGSIZE)
314 goto send_fragmentable;
315
316done:
David Howellsbe832ae2016-09-23 12:39:22 +0100317 trace_rxrpc_tx_data(call, sp->hdr.seq, serial, whdr.flags, false);
David Howells50235c42016-09-22 00:29:31 +0100318 if (ret >= 0) {
David Howells0d4b1032016-09-22 00:29:31 +0100319 ktime_t now = ktime_get_real();
320 skb->tstamp = now;
David Howells50235c42016-09-22 00:29:31 +0100321 smp_wmb();
David Howells5a924b82016-09-22 00:29:31 +0100322 sp->hdr.serial = serial;
David Howells0d4b1032016-09-22 00:29:31 +0100323 if (whdr.flags & RXRPC_REQUEST_ACK) {
324 call->peer->rtt_last_req = now;
David Howells50235c42016-09-22 00:29:31 +0100325 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_data, serial);
David Howells0d4b1032016-09-22 00:29:31 +0100326 }
David Howells17926a72007-04-26 15:48:28 -0700327 }
David Howells5a924b82016-09-22 00:29:31 +0100328 _leave(" = %d [%u]", ret, call->peer->maxdata);
329 return ret;
David Howells17926a72007-04-26 15:48:28 -0700330
331send_fragmentable:
332 /* attempt to send this message with fragmentation enabled */
333 _debug("send fragment");
334
David Howells985a5c82016-06-17 11:53:37 +0100335 down_write(&conn->params.local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -0700336
David Howells985a5c82016-06-17 11:53:37 +0100337 switch (conn->params.local->srx.transport.family) {
338 case AF_INET:
339 opt = IP_PMTUDISC_DONT;
340 ret = kernel_setsockopt(conn->params.local->socket,
341 SOL_IP, IP_MTU_DISCOVER,
342 (char *)&opt, sizeof(opt));
343 if (ret == 0) {
David Howells5a924b82016-09-22 00:29:31 +0100344 ret = kernel_sendmsg(conn->params.local->socket, &msg,
345 iov, 2, len);
David Howells985a5c82016-06-17 11:53:37 +0100346
347 opt = IP_PMTUDISC_DO;
348 kernel_setsockopt(conn->params.local->socket, SOL_IP,
349 IP_MTU_DISCOVER,
350 (char *)&opt, sizeof(opt));
351 }
352 break;
David Howells75b54cb2016-09-13 08:49:05 +0100353
David Howellsd1912742016-09-17 07:26:01 +0100354#ifdef CONFIG_AF_RXRPC_IPV6
David Howells75b54cb2016-09-13 08:49:05 +0100355 case AF_INET6:
356 opt = IPV6_PMTUDISC_DONT;
357 ret = kernel_setsockopt(conn->params.local->socket,
358 SOL_IPV6, IPV6_MTU_DISCOVER,
359 (char *)&opt, sizeof(opt));
360 if (ret == 0) {
361 ret = kernel_sendmsg(conn->params.local->socket, &msg,
362 iov, 1, iov[0].iov_len);
363
364 opt = IPV6_PMTUDISC_DO;
365 kernel_setsockopt(conn->params.local->socket,
366 SOL_IPV6, IPV6_MTU_DISCOVER,
367 (char *)&opt, sizeof(opt));
368 }
369 break;
David Howellsd1912742016-09-17 07:26:01 +0100370#endif
David Howells17926a72007-04-26 15:48:28 -0700371 }
372
David Howells985a5c82016-06-17 11:53:37 +0100373 up_write(&conn->params.local->defrag_sem);
David Howells5a924b82016-09-22 00:29:31 +0100374 goto done;
David Howells17926a72007-04-26 15:48:28 -0700375}
David Howells248f2192016-09-08 11:10:12 +0100376
377/*
378 * reject packets through the local endpoint
379 */
380void rxrpc_reject_packets(struct rxrpc_local *local)
381{
David Howells1c2bc7b2016-09-13 08:49:05 +0100382 struct sockaddr_rxrpc srx;
David Howells248f2192016-09-08 11:10:12 +0100383 struct rxrpc_skb_priv *sp;
384 struct rxrpc_wire_header whdr;
385 struct sk_buff *skb;
386 struct msghdr msg;
387 struct kvec iov[2];
388 size_t size;
389 __be32 code;
390
391 _enter("%d", local->debug_id);
392
393 iov[0].iov_base = &whdr;
394 iov[0].iov_len = sizeof(whdr);
395 iov[1].iov_base = &code;
396 iov[1].iov_len = sizeof(code);
397 size = sizeof(whdr) + sizeof(code);
398
David Howells1c2bc7b2016-09-13 08:49:05 +0100399 msg.msg_name = &srx.transport;
David Howells248f2192016-09-08 11:10:12 +0100400 msg.msg_control = NULL;
401 msg.msg_controllen = 0;
402 msg.msg_flags = 0;
403
David Howells248f2192016-09-08 11:10:12 +0100404 memset(&whdr, 0, sizeof(whdr));
405 whdr.type = RXRPC_PACKET_TYPE_ABORT;
406
407 while ((skb = skb_dequeue(&local->reject_queue))) {
David Howells71f3ca42016-09-17 10:49:14 +0100408 rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
David Howells248f2192016-09-08 11:10:12 +0100409 sp = rxrpc_skb(skb);
David Howells1c2bc7b2016-09-13 08:49:05 +0100410
411 if (rxrpc_extract_addr_from_skb(&srx, skb) == 0) {
412 msg.msg_namelen = srx.transport_len;
413
David Howells248f2192016-09-08 11:10:12 +0100414 code = htonl(skb->priority);
415
416 whdr.epoch = htonl(sp->hdr.epoch);
417 whdr.cid = htonl(sp->hdr.cid);
418 whdr.callNumber = htonl(sp->hdr.callNumber);
419 whdr.serviceId = htons(sp->hdr.serviceId);
420 whdr.flags = sp->hdr.flags;
421 whdr.flags ^= RXRPC_CLIENT_INITIATED;
422 whdr.flags &= RXRPC_CLIENT_INITIATED;
423
424 kernel_sendmsg(local->socket, &msg, iov, 2, size);
David Howells248f2192016-09-08 11:10:12 +0100425 }
426
David Howells71f3ca42016-09-17 10:49:14 +0100427 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
David Howells248f2192016-09-08 11:10:12 +0100428 }
429
430 _leave("");
431}