blob: 0c563e325c9d8e72ffad9abb24e8007ca2aef8f9 [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 ([email protected])
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 Howells75e421262016-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 Howells75e421262016-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:
David Howellsbe832ae2016-09-23 12:39:22 +0100180 trace_rxrpc_tx_ack(call, serial,
David Howellsb86e2182016-09-23 15:08:48 +0100181 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),
David Howells9c7ad432016-09-23 13:50:40 +0100213 true, true,
214 rxrpc_propose_ack_retry_tx);
David Howells248f2192016-09-08 11:10:12 +0100215 break;
216 case RXRPC_PACKET_TYPE_ABORT:
217 break;
218 }
219 }
220
David Howells8d94aa32016-09-07 09:19:31 +0100221out:
222 rxrpc_put_connection(conn);
223 kfree(pkt);
224 return ret;
225}
226
David Howells5873c082014-02-07 18:58:44 +0000227/*
David Howells17926a72007-04-26 15:48:28 -0700228 * send a packet through the transport endpoint
229 */
David Howells5a924b82016-09-22 00:29:31 +0100230int rxrpc_send_data_packet(struct rxrpc_call *call, struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700231{
David Howells5a924b82016-09-22 00:29:31 +0100232 struct rxrpc_connection *conn = call->conn;
233 struct rxrpc_wire_header whdr;
234 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700235 struct msghdr msg;
David Howells5a924b82016-09-22 00:29:31 +0100236 struct kvec iov[2];
237 rxrpc_serial_t serial;
238 size_t len;
David Howells17926a72007-04-26 15:48:28 -0700239 int ret, opt;
240
241 _enter(",{%d}", skb->len);
242
David Howells5a924b82016-09-22 00:29:31 +0100243 /* Each transmission of a Tx packet needs a new serial number */
244 serial = atomic_inc_return(&conn->serial);
David Howells17926a72007-04-26 15:48:28 -0700245
David Howells5a924b82016-09-22 00:29:31 +0100246 whdr.epoch = htonl(conn->proto.epoch);
247 whdr.cid = htonl(call->cid);
248 whdr.callNumber = htonl(call->call_id);
249 whdr.seq = htonl(sp->hdr.seq);
250 whdr.serial = htonl(serial);
251 whdr.type = RXRPC_PACKET_TYPE_DATA;
252 whdr.flags = sp->hdr.flags;
253 whdr.userStatus = 0;
254 whdr.securityIndex = call->security_ix;
255 whdr._rsvd = htons(sp->hdr._rsvd);
256 whdr.serviceId = htons(call->service_id);
257
258 iov[0].iov_base = &whdr;
259 iov[0].iov_len = sizeof(whdr);
260 iov[1].iov_base = skb->head;
261 iov[1].iov_len = skb->len;
262 len = iov[0].iov_len + iov[1].iov_len;
263
264 msg.msg_name = &call->peer->srx.transport;
265 msg.msg_namelen = call->peer->srx.transport_len;
David Howells17926a72007-04-26 15:48:28 -0700266 msg.msg_control = NULL;
267 msg.msg_controllen = 0;
268 msg.msg_flags = 0;
269
David Howells0d4b1032016-09-22 00:29:31 +0100270 /* If our RTT cache needs working on, request an ACK. */
271 if ((call->peer->rtt_usage < 3 && sp->hdr.seq & 1) ||
272 ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
273 ktime_get_real()))
274 whdr.flags |= RXRPC_REQUEST_ACK;
275
David Howells8a681c362016-09-17 10:49:15 +0100276 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
277 static int lose;
278 if ((lose++ & 7) == 7) {
David Howellsbe832ae2016-09-23 12:39:22 +0100279 trace_rxrpc_tx_data(call, sp->hdr.seq, serial,
280 whdr.flags, true);
David Howells8a681c362016-09-17 10:49:15 +0100281 rxrpc_lose_skb(skb, rxrpc_skb_tx_lost);
282 _leave(" = 0 [lose]");
283 return 0;
284 }
285 }
286
David Howells5a924b82016-09-22 00:29:31 +0100287 _proto("Tx DATA %%%u { #%u }", serial, sp->hdr.seq);
288
David Howells17926a72007-04-26 15:48:28 -0700289 /* send the packet with the don't fragment bit set if we currently
290 * think it's small enough */
David Howells5a924b82016-09-22 00:29:31 +0100291 if (iov[1].iov_len >= call->peer->maxdata)
292 goto send_fragmentable;
David Howells17926a72007-04-26 15:48:28 -0700293
David Howells5a924b82016-09-22 00:29:31 +0100294 down_read(&conn->params.local->defrag_sem);
295 /* send the packet by UDP
296 * - returns -EMSGSIZE if UDP would have to fragment the packet
297 * to go out of the interface
298 * - in which case, we'll have processed the ICMP error
299 * message and update the peer record
300 */
301 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
David Howells17926a72007-04-26 15:48:28 -0700302
David Howells5a924b82016-09-22 00:29:31 +0100303 up_read(&conn->params.local->defrag_sem);
304 if (ret == -EMSGSIZE)
305 goto send_fragmentable;
306
307done:
David Howellsbe832ae2016-09-23 12:39:22 +0100308 trace_rxrpc_tx_data(call, sp->hdr.seq, serial, whdr.flags, false);
David Howells50235c4b2016-09-22 00:29:31 +0100309 if (ret >= 0) {
David Howells0d4b1032016-09-22 00:29:31 +0100310 ktime_t now = ktime_get_real();
311 skb->tstamp = now;
David Howells50235c4b2016-09-22 00:29:31 +0100312 smp_wmb();
David Howells5a924b82016-09-22 00:29:31 +0100313 sp->hdr.serial = serial;
David Howells0d4b1032016-09-22 00:29:31 +0100314 if (whdr.flags & RXRPC_REQUEST_ACK) {
315 call->peer->rtt_last_req = now;
David Howells50235c4b2016-09-22 00:29:31 +0100316 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_data, serial);
David Howells0d4b1032016-09-22 00:29:31 +0100317 }
David Howells17926a72007-04-26 15:48:28 -0700318 }
David Howells5a924b82016-09-22 00:29:31 +0100319 _leave(" = %d [%u]", ret, call->peer->maxdata);
320 return ret;
David Howells17926a72007-04-26 15:48:28 -0700321
322send_fragmentable:
323 /* attempt to send this message with fragmentation enabled */
324 _debug("send fragment");
325
David Howells985a5c82016-06-17 11:53:37 +0100326 down_write(&conn->params.local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -0700327
David Howells985a5c82016-06-17 11:53:37 +0100328 switch (conn->params.local->srx.transport.family) {
329 case AF_INET:
330 opt = IP_PMTUDISC_DONT;
331 ret = kernel_setsockopt(conn->params.local->socket,
332 SOL_IP, IP_MTU_DISCOVER,
333 (char *)&opt, sizeof(opt));
334 if (ret == 0) {
David Howells5a924b82016-09-22 00:29:31 +0100335 ret = kernel_sendmsg(conn->params.local->socket, &msg,
336 iov, 2, len);
David Howells985a5c82016-06-17 11:53:37 +0100337
338 opt = IP_PMTUDISC_DO;
339 kernel_setsockopt(conn->params.local->socket, SOL_IP,
340 IP_MTU_DISCOVER,
341 (char *)&opt, sizeof(opt));
342 }
343 break;
David Howells75b54cb2016-09-13 08:49:05 +0100344
David Howellsd1912742016-09-17 07:26:01 +0100345#ifdef CONFIG_AF_RXRPC_IPV6
David Howells75b54cb2016-09-13 08:49:05 +0100346 case AF_INET6:
347 opt = IPV6_PMTUDISC_DONT;
348 ret = kernel_setsockopt(conn->params.local->socket,
349 SOL_IPV6, IPV6_MTU_DISCOVER,
350 (char *)&opt, sizeof(opt));
351 if (ret == 0) {
352 ret = kernel_sendmsg(conn->params.local->socket, &msg,
353 iov, 1, iov[0].iov_len);
354
355 opt = IPV6_PMTUDISC_DO;
356 kernel_setsockopt(conn->params.local->socket,
357 SOL_IPV6, IPV6_MTU_DISCOVER,
358 (char *)&opt, sizeof(opt));
359 }
360 break;
David Howellsd1912742016-09-17 07:26:01 +0100361#endif
David Howells17926a72007-04-26 15:48:28 -0700362 }
363
David Howells985a5c82016-06-17 11:53:37 +0100364 up_write(&conn->params.local->defrag_sem);
David Howells5a924b82016-09-22 00:29:31 +0100365 goto done;
David Howells17926a72007-04-26 15:48:28 -0700366}
David Howells248f2192016-09-08 11:10:12 +0100367
368/*
369 * reject packets through the local endpoint
370 */
371void rxrpc_reject_packets(struct rxrpc_local *local)
372{
David Howells1c2bc7b2016-09-13 08:49:05 +0100373 struct sockaddr_rxrpc srx;
David Howells248f2192016-09-08 11:10:12 +0100374 struct rxrpc_skb_priv *sp;
375 struct rxrpc_wire_header whdr;
376 struct sk_buff *skb;
377 struct msghdr msg;
378 struct kvec iov[2];
379 size_t size;
380 __be32 code;
381
382 _enter("%d", local->debug_id);
383
384 iov[0].iov_base = &whdr;
385 iov[0].iov_len = sizeof(whdr);
386 iov[1].iov_base = &code;
387 iov[1].iov_len = sizeof(code);
388 size = sizeof(whdr) + sizeof(code);
389
David Howells1c2bc7b2016-09-13 08:49:05 +0100390 msg.msg_name = &srx.transport;
David Howells248f2192016-09-08 11:10:12 +0100391 msg.msg_control = NULL;
392 msg.msg_controllen = 0;
393 msg.msg_flags = 0;
394
David Howells248f2192016-09-08 11:10:12 +0100395 memset(&whdr, 0, sizeof(whdr));
396 whdr.type = RXRPC_PACKET_TYPE_ABORT;
397
398 while ((skb = skb_dequeue(&local->reject_queue))) {
David Howells71f3ca42016-09-17 10:49:14 +0100399 rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
David Howells248f2192016-09-08 11:10:12 +0100400 sp = rxrpc_skb(skb);
David Howells1c2bc7b2016-09-13 08:49:05 +0100401
402 if (rxrpc_extract_addr_from_skb(&srx, skb) == 0) {
403 msg.msg_namelen = srx.transport_len;
404
David Howells248f2192016-09-08 11:10:12 +0100405 code = htonl(skb->priority);
406
407 whdr.epoch = htonl(sp->hdr.epoch);
408 whdr.cid = htonl(sp->hdr.cid);
409 whdr.callNumber = htonl(sp->hdr.callNumber);
410 whdr.serviceId = htons(sp->hdr.serviceId);
411 whdr.flags = sp->hdr.flags;
412 whdr.flags ^= RXRPC_CLIENT_INITIATED;
413 whdr.flags &= RXRPC_CLIENT_INITIATED;
414
415 kernel_sendmsg(local->socket, &msg, iov, 2, size);
David Howells248f2192016-09-08 11:10:12 +0100416 }
417
David Howells71f3ca42016-09-17 10:49:14 +0100418 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
David Howells248f2192016-09-08 11:10:12 +0100419 }
420
421 _leave("");
422}