blob: 5c1e008a5323dd766fa8f5381953141d6b9726dc [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,
39 struct rxrpc_pkt_buffer *pkt)
40{
David Howellsf3639df2016-09-17 10:49:13 +010041 rxrpc_serial_t serial;
David Howells248f2192016-09-08 11:10:12 +010042 rxrpc_seq_t hard_ack, top, seq;
43 int ix;
David Howells8d94aa32016-09-07 09:19:31 +010044 u32 mtu, jmax;
45 u8 *ackp = pkt->acks;
46
David Howells248f2192016-09-08 11:10:12 +010047 /* Barrier against rxrpc_input_data(). */
David Howellsf3639df2016-09-17 10:49:13 +010048 serial = call->ackr_serial;
David Howells248f2192016-09-08 11:10:12 +010049 hard_ack = READ_ONCE(call->rx_hard_ack);
50 top = smp_load_acquire(&call->rx_top);
51
David Howells8d94aa32016-09-07 09:19:31 +010052 pkt->ack.bufferSpace = htons(8);
David Howells248f2192016-09-08 11:10:12 +010053 pkt->ack.maxSkew = htons(call->ackr_skew);
54 pkt->ack.firstPacket = htonl(hard_ack + 1);
David Howells8d94aa32016-09-07 09:19:31 +010055 pkt->ack.previousPacket = htonl(call->ackr_prev_seq);
David Howellsf3639df2016-09-17 10:49:13 +010056 pkt->ack.serial = htonl(serial);
David Howells248f2192016-09-08 11:10:12 +010057 pkt->ack.reason = call->ackr_reason;
58 pkt->ack.nAcks = top - hard_ack;
David Howells8d94aa32016-09-07 09:19:31 +010059
David Howells8e831342016-09-22 00:29:31 +010060 if (pkt->ack.reason == RXRPC_ACK_PING)
61 pkt->whdr.flags |= RXRPC_REQUEST_ACK;
62
David Howells248f2192016-09-08 11:10:12 +010063 if (after(top, hard_ack)) {
64 seq = hard_ack + 1;
65 do {
66 ix = seq & RXRPC_RXTX_BUFF_MASK;
67 if (call->rxtx_buffer[ix])
68 *ackp++ = RXRPC_ACK_TYPE_ACK;
69 else
70 *ackp++ = RXRPC_ACK_TYPE_NACK;
71 seq++;
72 } while (before_eq(seq, top));
73 }
74
75 mtu = call->conn->params.peer->if_mtu;
76 mtu -= call->conn->params.peer->hdrsize;
David Howells75e42122016-09-13 22:36:22 +010077 jmax = (call->nr_jumbo_bad > 3) ? 1 : rxrpc_rx_jumbo_max;
David Howells8d94aa32016-09-07 09:19:31 +010078 pkt->ackinfo.rxMTU = htonl(rxrpc_rx_mtu);
79 pkt->ackinfo.maxMTU = htonl(mtu);
David Howells75e42122016-09-13 22:36:22 +010080 pkt->ackinfo.rwind = htonl(call->rx_winsize);
David Howells8d94aa32016-09-07 09:19:31 +010081 pkt->ackinfo.jumbo_max = htonl(jmax);
82
83 *ackp++ = 0;
84 *ackp++ = 0;
85 *ackp++ = 0;
David Howells248f2192016-09-08 11:10:12 +010086 return top - hard_ack + 3;
David Howells8d94aa32016-09-07 09:19:31 +010087}
88
89/*
David Howells248f2192016-09-08 11:10:12 +010090 * Send an ACK or ABORT call packet.
David Howells8d94aa32016-09-07 09:19:31 +010091 */
92int rxrpc_send_call_packet(struct rxrpc_call *call, u8 type)
93{
94 struct rxrpc_connection *conn = NULL;
95 struct rxrpc_pkt_buffer *pkt;
96 struct msghdr msg;
97 struct kvec iov[2];
98 rxrpc_serial_t serial;
99 size_t len, n;
David Howells8e831342016-09-22 00:29:31 +0100100 bool ping = false;
David Howells8d94aa32016-09-07 09:19:31 +0100101 int ioc, ret;
102 u32 abort_code;
103
104 _enter("%u,%s", call->debug_id, rxrpc_pkts[type]);
105
106 spin_lock_bh(&call->lock);
107 if (call->conn)
108 conn = rxrpc_get_connection_maybe(call->conn);
109 spin_unlock_bh(&call->lock);
110 if (!conn)
111 return -ECONNRESET;
112
113 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
114 if (!pkt) {
115 rxrpc_put_connection(conn);
116 return -ENOMEM;
117 }
118
David Howells8d94aa32016-09-07 09:19:31 +0100119 msg.msg_name = &call->peer->srx.transport;
120 msg.msg_namelen = call->peer->srx.transport_len;
121 msg.msg_control = NULL;
122 msg.msg_controllen = 0;
123 msg.msg_flags = 0;
124
125 pkt->whdr.epoch = htonl(conn->proto.epoch);
126 pkt->whdr.cid = htonl(call->cid);
127 pkt->whdr.callNumber = htonl(call->call_id);
128 pkt->whdr.seq = 0;
David Howells8d94aa32016-09-07 09:19:31 +0100129 pkt->whdr.type = type;
130 pkt->whdr.flags = conn->out_clientflag;
131 pkt->whdr.userStatus = 0;
132 pkt->whdr.securityIndex = call->security_ix;
133 pkt->whdr._rsvd = 0;
134 pkt->whdr.serviceId = htons(call->service_id);
135
136 iov[0].iov_base = pkt;
137 iov[0].iov_len = sizeof(pkt->whdr);
138 len = sizeof(pkt->whdr);
139
140 switch (type) {
141 case RXRPC_PACKET_TYPE_ACK:
142 spin_lock_bh(&call->lock);
David Howells27d0fc42016-09-17 10:49:13 +0100143 if (!call->ackr_reason) {
144 spin_unlock_bh(&call->lock);
145 ret = 0;
146 goto out;
147 }
David Howells8e831342016-09-22 00:29:31 +0100148 ping = (call->ackr_reason == RXRPC_ACK_PING);
David Howells8d94aa32016-09-07 09:19:31 +0100149 n = rxrpc_fill_out_ack(call, pkt);
150 call->ackr_reason = 0;
151
152 spin_unlock_bh(&call->lock);
153
David Howells8d94aa32016-09-07 09:19:31 +0100154
155 iov[0].iov_len += sizeof(pkt->ack) + n;
156 iov[1].iov_base = &pkt->ackinfo;
157 iov[1].iov_len = sizeof(pkt->ackinfo);
158 len += sizeof(pkt->ack) + n + sizeof(pkt->ackinfo);
159 ioc = 2;
160 break;
161
162 case RXRPC_PACKET_TYPE_ABORT:
163 abort_code = call->abort_code;
164 pkt->abort_code = htonl(abort_code);
David Howells8d94aa32016-09-07 09:19:31 +0100165 iov[0].iov_len += sizeof(pkt->abort_code);
166 len += sizeof(pkt->abort_code);
167 ioc = 1;
168 break;
169
170 default:
171 BUG();
172 ret = -ENOANO;
173 goto out;
174 }
175
David Howellsb86e2182016-09-23 15:08:48 +0100176 serial = atomic_inc_return(&conn->serial);
177 pkt->whdr.serial = htonl(serial);
178 switch (type) {
179 case RXRPC_PACKET_TYPE_ACK:
180 trace_rxrpc_tx_ack(call,
181 ntohl(pkt->ack.firstPacket),
182 ntohl(pkt->ack.serial),
183 pkt->ack.reason, pkt->ack.nAcks);
184 break;
185 }
186
David Howells8e831342016-09-22 00:29:31 +0100187 if (ping) {
188 call->ackr_ping = serial;
189 smp_wmb();
190 /* We need to stick a time in before we send the packet in case
191 * the reply gets back before kernel_sendmsg() completes - but
192 * asking UDP to send the packet can take a relatively long
193 * time, so we update the time after, on the assumption that
194 * the packet transmission is more likely to happen towards the
195 * end of the kernel_sendmsg() call.
196 */
197 call->ackr_ping_time = ktime_get_real();
198 set_bit(RXRPC_CALL_PINGING, &call->flags);
199 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_ping, serial);
200 }
David Howells8d94aa32016-09-07 09:19:31 +0100201 ret = kernel_sendmsg(conn->params.local->socket,
202 &msg, iov, ioc, len);
David Howells8e831342016-09-22 00:29:31 +0100203 if (ping)
204 call->ackr_ping_time = ktime_get_real();
David Howells8d94aa32016-09-07 09:19:31 +0100205
David Howells248f2192016-09-08 11:10:12 +0100206 if (ret < 0 && call->state < RXRPC_CALL_COMPLETE) {
David Howells2311e322016-09-17 10:49:12 +0100207 switch (type) {
David Howells248f2192016-09-08 11:10:12 +0100208 case RXRPC_PACKET_TYPE_ACK:
David Howells8e831342016-09-22 00:29:31 +0100209 clear_bit(RXRPC_CALL_PINGING, &call->flags);
David Howells248f2192016-09-08 11:10:12 +0100210 rxrpc_propose_ACK(call, pkt->ack.reason,
211 ntohs(pkt->ack.maxSkew),
212 ntohl(pkt->ack.serial),
213 true, true);
214 break;
215 case RXRPC_PACKET_TYPE_ABORT:
216 break;
217 }
218 }
219
David Howells8d94aa32016-09-07 09:19:31 +0100220out:
221 rxrpc_put_connection(conn);
222 kfree(pkt);
223 return ret;
224}
225
David Howells5873c082014-02-07 18:58:44 +0000226/*
David Howells17926a72007-04-26 15:48:28 -0700227 * send a packet through the transport endpoint
228 */
David Howells5a924b82016-09-22 00:29:31 +0100229int rxrpc_send_data_packet(struct rxrpc_call *call, struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700230{
David Howells5a924b82016-09-22 00:29:31 +0100231 struct rxrpc_connection *conn = call->conn;
232 struct rxrpc_wire_header whdr;
233 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700234 struct msghdr msg;
David Howells5a924b82016-09-22 00:29:31 +0100235 struct kvec iov[2];
236 rxrpc_serial_t serial;
237 size_t len;
David Howells17926a72007-04-26 15:48:28 -0700238 int ret, opt;
239
240 _enter(",{%d}", skb->len);
241
David Howells5a924b82016-09-22 00:29:31 +0100242 /* Each transmission of a Tx packet needs a new serial number */
243 serial = atomic_inc_return(&conn->serial);
David Howells17926a72007-04-26 15:48:28 -0700244
David Howells5a924b82016-09-22 00:29:31 +0100245 whdr.epoch = htonl(conn->proto.epoch);
246 whdr.cid = htonl(call->cid);
247 whdr.callNumber = htonl(call->call_id);
248 whdr.seq = htonl(sp->hdr.seq);
249 whdr.serial = htonl(serial);
250 whdr.type = RXRPC_PACKET_TYPE_DATA;
251 whdr.flags = sp->hdr.flags;
252 whdr.userStatus = 0;
253 whdr.securityIndex = call->security_ix;
254 whdr._rsvd = htons(sp->hdr._rsvd);
255 whdr.serviceId = htons(call->service_id);
256
257 iov[0].iov_base = &whdr;
258 iov[0].iov_len = sizeof(whdr);
259 iov[1].iov_base = skb->head;
260 iov[1].iov_len = skb->len;
261 len = iov[0].iov_len + iov[1].iov_len;
262
263 msg.msg_name = &call->peer->srx.transport;
264 msg.msg_namelen = call->peer->srx.transport_len;
David Howells17926a72007-04-26 15:48:28 -0700265 msg.msg_control = NULL;
266 msg.msg_controllen = 0;
267 msg.msg_flags = 0;
268
David Howells0d4b1032016-09-22 00:29:31 +0100269 /* If our RTT cache needs working on, request an ACK. */
270 if ((call->peer->rtt_usage < 3 && sp->hdr.seq & 1) ||
271 ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
272 ktime_get_real()))
273 whdr.flags |= RXRPC_REQUEST_ACK;
274
David Howells8a681c362016-09-17 10:49:15 +0100275 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
276 static int lose;
277 if ((lose++ & 7) == 7) {
278 rxrpc_lose_skb(skb, rxrpc_skb_tx_lost);
279 _leave(" = 0 [lose]");
280 return 0;
281 }
282 }
283
David Howells5a924b82016-09-22 00:29:31 +0100284 _proto("Tx DATA %%%u { #%u }", serial, sp->hdr.seq);
285
David Howells17926a72007-04-26 15:48:28 -0700286 /* send the packet with the don't fragment bit set if we currently
287 * think it's small enough */
David Howells5a924b82016-09-22 00:29:31 +0100288 if (iov[1].iov_len >= call->peer->maxdata)
289 goto send_fragmentable;
David Howells17926a72007-04-26 15:48:28 -0700290
David Howells5a924b82016-09-22 00:29:31 +0100291 down_read(&conn->params.local->defrag_sem);
292 /* send the packet by UDP
293 * - returns -EMSGSIZE if UDP would have to fragment the packet
294 * to go out of the interface
295 * - in which case, we'll have processed the ICMP error
296 * message and update the peer record
297 */
298 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
David Howells17926a72007-04-26 15:48:28 -0700299
David Howells5a924b82016-09-22 00:29:31 +0100300 up_read(&conn->params.local->defrag_sem);
301 if (ret == -EMSGSIZE)
302 goto send_fragmentable;
303
304done:
David Howells50235c42016-09-22 00:29:31 +0100305 if (ret >= 0) {
David Howells0d4b1032016-09-22 00:29:31 +0100306 ktime_t now = ktime_get_real();
307 skb->tstamp = now;
David Howells50235c42016-09-22 00:29:31 +0100308 smp_wmb();
David Howells5a924b82016-09-22 00:29:31 +0100309 sp->hdr.serial = serial;
David Howells0d4b1032016-09-22 00:29:31 +0100310 if (whdr.flags & RXRPC_REQUEST_ACK) {
311 call->peer->rtt_last_req = now;
David Howells50235c42016-09-22 00:29:31 +0100312 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_data, serial);
David Howells0d4b1032016-09-22 00:29:31 +0100313 }
David Howells17926a72007-04-26 15:48:28 -0700314 }
David Howells5a924b82016-09-22 00:29:31 +0100315 _leave(" = %d [%u]", ret, call->peer->maxdata);
316 return ret;
David Howells17926a72007-04-26 15:48:28 -0700317
318send_fragmentable:
319 /* attempt to send this message with fragmentation enabled */
320 _debug("send fragment");
321
David Howells985a5c82016-06-17 11:53:37 +0100322 down_write(&conn->params.local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -0700323
David Howells985a5c82016-06-17 11:53:37 +0100324 switch (conn->params.local->srx.transport.family) {
325 case AF_INET:
326 opt = IP_PMTUDISC_DONT;
327 ret = kernel_setsockopt(conn->params.local->socket,
328 SOL_IP, IP_MTU_DISCOVER,
329 (char *)&opt, sizeof(opt));
330 if (ret == 0) {
David Howells5a924b82016-09-22 00:29:31 +0100331 ret = kernel_sendmsg(conn->params.local->socket, &msg,
332 iov, 2, len);
David Howells985a5c82016-06-17 11:53:37 +0100333
334 opt = IP_PMTUDISC_DO;
335 kernel_setsockopt(conn->params.local->socket, SOL_IP,
336 IP_MTU_DISCOVER,
337 (char *)&opt, sizeof(opt));
338 }
339 break;
David Howells75b54cb2016-09-13 08:49:05 +0100340
David Howellsd1912742016-09-17 07:26:01 +0100341#ifdef CONFIG_AF_RXRPC_IPV6
David Howells75b54cb2016-09-13 08:49:05 +0100342 case AF_INET6:
343 opt = IPV6_PMTUDISC_DONT;
344 ret = kernel_setsockopt(conn->params.local->socket,
345 SOL_IPV6, IPV6_MTU_DISCOVER,
346 (char *)&opt, sizeof(opt));
347 if (ret == 0) {
348 ret = kernel_sendmsg(conn->params.local->socket, &msg,
349 iov, 1, iov[0].iov_len);
350
351 opt = IPV6_PMTUDISC_DO;
352 kernel_setsockopt(conn->params.local->socket,
353 SOL_IPV6, IPV6_MTU_DISCOVER,
354 (char *)&opt, sizeof(opt));
355 }
356 break;
David Howellsd1912742016-09-17 07:26:01 +0100357#endif
David Howells17926a72007-04-26 15:48:28 -0700358 }
359
David Howells985a5c82016-06-17 11:53:37 +0100360 up_write(&conn->params.local->defrag_sem);
David Howells5a924b82016-09-22 00:29:31 +0100361 goto done;
David Howells17926a72007-04-26 15:48:28 -0700362}
David Howells248f2192016-09-08 11:10:12 +0100363
364/*
365 * reject packets through the local endpoint
366 */
367void rxrpc_reject_packets(struct rxrpc_local *local)
368{
David Howells1c2bc7b2016-09-13 08:49:05 +0100369 struct sockaddr_rxrpc srx;
David Howells248f2192016-09-08 11:10:12 +0100370 struct rxrpc_skb_priv *sp;
371 struct rxrpc_wire_header whdr;
372 struct sk_buff *skb;
373 struct msghdr msg;
374 struct kvec iov[2];
375 size_t size;
376 __be32 code;
377
378 _enter("%d", local->debug_id);
379
380 iov[0].iov_base = &whdr;
381 iov[0].iov_len = sizeof(whdr);
382 iov[1].iov_base = &code;
383 iov[1].iov_len = sizeof(code);
384 size = sizeof(whdr) + sizeof(code);
385
David Howells1c2bc7b2016-09-13 08:49:05 +0100386 msg.msg_name = &srx.transport;
David Howells248f2192016-09-08 11:10:12 +0100387 msg.msg_control = NULL;
388 msg.msg_controllen = 0;
389 msg.msg_flags = 0;
390
David Howells248f2192016-09-08 11:10:12 +0100391 memset(&whdr, 0, sizeof(whdr));
392 whdr.type = RXRPC_PACKET_TYPE_ABORT;
393
394 while ((skb = skb_dequeue(&local->reject_queue))) {
David Howells71f3ca42016-09-17 10:49:14 +0100395 rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
David Howells248f2192016-09-08 11:10:12 +0100396 sp = rxrpc_skb(skb);
David Howells1c2bc7b2016-09-13 08:49:05 +0100397
398 if (rxrpc_extract_addr_from_skb(&srx, skb) == 0) {
399 msg.msg_namelen = srx.transport_len;
400
David Howells248f2192016-09-08 11:10:12 +0100401 code = htonl(skb->priority);
402
403 whdr.epoch = htonl(sp->hdr.epoch);
404 whdr.cid = htonl(sp->hdr.cid);
405 whdr.callNumber = htonl(sp->hdr.callNumber);
406 whdr.serviceId = htons(sp->hdr.serviceId);
407 whdr.flags = sp->hdr.flags;
408 whdr.flags ^= RXRPC_CLIENT_INITIATED;
409 whdr.flags &= RXRPC_CLIENT_INITIATED;
410
411 kernel_sendmsg(local->socket, &msg, iov, 2, size);
David Howells248f2192016-09-08 11:10:12 +0100412 }
413
David Howells71f3ca42016-09-17 10:49:14 +0100414 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
David Howells248f2192016-09-08 11:10:12 +0100415 }
416
417 _leave("");
418}