blob: 282cb1e36d061175e64ca09006a17184812cdc05 [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
David Howellsf3639df2016-09-17 10:49:13 +010083 trace_rxrpc_tx_ack(call, hard_ack + 1, serial, call->ackr_reason,
84 top - hard_ack);
85
David Howells8d94aa32016-09-07 09:19:31 +010086 *ackp++ = 0;
87 *ackp++ = 0;
88 *ackp++ = 0;
David Howells248f2192016-09-08 11:10:12 +010089 return top - hard_ack + 3;
David Howells8d94aa32016-09-07 09:19:31 +010090}
91
92/*
David Howells248f2192016-09-08 11:10:12 +010093 * Send an ACK or ABORT call packet.
David Howells8d94aa32016-09-07 09:19:31 +010094 */
95int rxrpc_send_call_packet(struct rxrpc_call *call, u8 type)
96{
97 struct rxrpc_connection *conn = NULL;
98 struct rxrpc_pkt_buffer *pkt;
99 struct msghdr msg;
100 struct kvec iov[2];
101 rxrpc_serial_t serial;
102 size_t len, n;
David Howells8e831342016-09-22 00:29:31 +0100103 bool ping = false;
David Howells8d94aa32016-09-07 09:19:31 +0100104 int ioc, ret;
105 u32 abort_code;
106
107 _enter("%u,%s", call->debug_id, rxrpc_pkts[type]);
108
109 spin_lock_bh(&call->lock);
110 if (call->conn)
111 conn = rxrpc_get_connection_maybe(call->conn);
112 spin_unlock_bh(&call->lock);
113 if (!conn)
114 return -ECONNRESET;
115
116 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
117 if (!pkt) {
118 rxrpc_put_connection(conn);
119 return -ENOMEM;
120 }
121
122 serial = atomic_inc_return(&conn->serial);
123
124 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;
134 pkt->whdr.serial = htonl(serial);
135 pkt->whdr.type = type;
136 pkt->whdr.flags = conn->out_clientflag;
137 pkt->whdr.userStatus = 0;
138 pkt->whdr.securityIndex = call->security_ix;
139 pkt->whdr._rsvd = 0;
140 pkt->whdr.serviceId = htons(call->service_id);
141
142 iov[0].iov_base = pkt;
143 iov[0].iov_len = sizeof(pkt->whdr);
144 len = sizeof(pkt->whdr);
145
146 switch (type) {
147 case RXRPC_PACKET_TYPE_ACK:
148 spin_lock_bh(&call->lock);
David Howells27d0fc42016-09-17 10:49:13 +0100149 if (!call->ackr_reason) {
150 spin_unlock_bh(&call->lock);
151 ret = 0;
152 goto out;
153 }
David Howells8e831342016-09-22 00:29:31 +0100154 ping = (call->ackr_reason == RXRPC_ACK_PING);
David Howells8d94aa32016-09-07 09:19:31 +0100155 n = rxrpc_fill_out_ack(call, pkt);
156 call->ackr_reason = 0;
157
158 spin_unlock_bh(&call->lock);
159
160 _proto("Tx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
161 serial,
162 ntohs(pkt->ack.maxSkew),
163 ntohl(pkt->ack.firstPacket),
164 ntohl(pkt->ack.previousPacket),
165 ntohl(pkt->ack.serial),
166 rxrpc_acks(pkt->ack.reason),
167 pkt->ack.nAcks);
168
169 iov[0].iov_len += sizeof(pkt->ack) + n;
170 iov[1].iov_base = &pkt->ackinfo;
171 iov[1].iov_len = sizeof(pkt->ackinfo);
172 len += sizeof(pkt->ack) + n + sizeof(pkt->ackinfo);
173 ioc = 2;
174 break;
175
176 case RXRPC_PACKET_TYPE_ABORT:
177 abort_code = call->abort_code;
178 pkt->abort_code = htonl(abort_code);
179 _proto("Tx ABORT %%%u { %d }", serial, abort_code);
180 iov[0].iov_len += sizeof(pkt->abort_code);
181 len += sizeof(pkt->abort_code);
182 ioc = 1;
183 break;
184
185 default:
186 BUG();
187 ret = -ENOANO;
188 goto out;
189 }
190
David Howells8e831342016-09-22 00:29:31 +0100191 if (ping) {
192 call->ackr_ping = serial;
193 smp_wmb();
194 /* We need to stick a time in before we send the packet in case
195 * the reply gets back before kernel_sendmsg() completes - but
196 * asking UDP to send the packet can take a relatively long
197 * time, so we update the time after, on the assumption that
198 * the packet transmission is more likely to happen towards the
199 * end of the kernel_sendmsg() call.
200 */
201 call->ackr_ping_time = ktime_get_real();
202 set_bit(RXRPC_CALL_PINGING, &call->flags);
203 trace_rxrpc_rtt_tx(call, rxrpc_rtt_tx_ping, serial);
204 }
David Howells8d94aa32016-09-07 09:19:31 +0100205 ret = kernel_sendmsg(conn->params.local->socket,
206 &msg, iov, ioc, len);
David Howells8e831342016-09-22 00:29:31 +0100207 if (ping)
208 call->ackr_ping_time = ktime_get_real();
David Howells8d94aa32016-09-07 09:19:31 +0100209
David Howells248f2192016-09-08 11:10:12 +0100210 if (ret < 0 && call->state < RXRPC_CALL_COMPLETE) {
David Howells2311e322016-09-17 10:49:12 +0100211 switch (type) {
David Howells248f2192016-09-08 11:10:12 +0100212 case RXRPC_PACKET_TYPE_ACK:
David Howells8e831342016-09-22 00:29:31 +0100213 clear_bit(RXRPC_CALL_PINGING, &call->flags);
David Howells248f2192016-09-08 11:10:12 +0100214 rxrpc_propose_ACK(call, pkt->ack.reason,
215 ntohs(pkt->ack.maxSkew),
216 ntohl(pkt->ack.serial),
217 true, true);
218 break;
219 case RXRPC_PACKET_TYPE_ABORT:
220 break;
221 }
222 }
223
David Howells8d94aa32016-09-07 09:19:31 +0100224out:
225 rxrpc_put_connection(conn);
226 kfree(pkt);
227 return ret;
228}
229
David Howells5873c082014-02-07 18:58:44 +0000230/*
David Howells17926a72007-04-26 15:48:28 -0700231 * send a packet through the transport endpoint
232 */
David Howells5a924b82016-09-22 00:29:31 +0100233int rxrpc_send_data_packet(struct rxrpc_call *call, struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700234{
David Howells5a924b82016-09-22 00:29:31 +0100235 struct rxrpc_connection *conn = call->conn;
236 struct rxrpc_wire_header whdr;
237 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700238 struct msghdr msg;
David Howells5a924b82016-09-22 00:29:31 +0100239 struct kvec iov[2];
240 rxrpc_serial_t serial;
241 size_t len;
David Howells17926a72007-04-26 15:48:28 -0700242 int ret, opt;
243
244 _enter(",{%d}", skb->len);
245
David Howells5a924b82016-09-22 00:29:31 +0100246 /* Each transmission of a Tx packet needs a new serial number */
247 serial = atomic_inc_return(&conn->serial);
David Howells17926a72007-04-26 15:48:28 -0700248
David Howells5a924b82016-09-22 00:29:31 +0100249 whdr.epoch = htonl(conn->proto.epoch);
250 whdr.cid = htonl(call->cid);
251 whdr.callNumber = htonl(call->call_id);
252 whdr.seq = htonl(sp->hdr.seq);
253 whdr.serial = htonl(serial);
254 whdr.type = RXRPC_PACKET_TYPE_DATA;
255 whdr.flags = sp->hdr.flags;
256 whdr.userStatus = 0;
257 whdr.securityIndex = call->security_ix;
258 whdr._rsvd = htons(sp->hdr._rsvd);
259 whdr.serviceId = htons(call->service_id);
260
261 iov[0].iov_base = &whdr;
262 iov[0].iov_len = sizeof(whdr);
263 iov[1].iov_base = skb->head;
264 iov[1].iov_len = skb->len;
265 len = iov[0].iov_len + iov[1].iov_len;
266
267 msg.msg_name = &call->peer->srx.transport;
268 msg.msg_namelen = call->peer->srx.transport_len;
David Howells17926a72007-04-26 15:48:28 -0700269 msg.msg_control = NULL;
270 msg.msg_controllen = 0;
271 msg.msg_flags = 0;
272
David Howells0d4b1032016-09-22 00:29:31 +0100273 /* If our RTT cache needs working on, request an ACK. */
274 if ((call->peer->rtt_usage < 3 && sp->hdr.seq & 1) ||
275 ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
276 ktime_get_real()))
277 whdr.flags |= RXRPC_REQUEST_ACK;
278
David Howells8a681c362016-09-17 10:49:15 +0100279 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
280 static int lose;
281 if ((lose++ & 7) == 7) {
282 rxrpc_lose_skb(skb, rxrpc_skb_tx_lost);
283 _leave(" = 0 [lose]");
284 return 0;
285 }
286 }
287
David Howells5a924b82016-09-22 00:29:31 +0100288 _proto("Tx DATA %%%u { #%u }", serial, sp->hdr.seq);
289
David Howells17926a72007-04-26 15:48:28 -0700290 /* send the packet with the don't fragment bit set if we currently
291 * think it's small enough */
David Howells5a924b82016-09-22 00:29:31 +0100292 if (iov[1].iov_len >= call->peer->maxdata)
293 goto send_fragmentable;
David Howells17926a72007-04-26 15:48:28 -0700294
David Howells5a924b82016-09-22 00:29:31 +0100295 down_read(&conn->params.local->defrag_sem);
296 /* send the packet by UDP
297 * - returns -EMSGSIZE if UDP would have to fragment the packet
298 * to go out of the interface
299 * - in which case, we'll have processed the ICMP error
300 * message and update the peer record
301 */
302 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
David Howells17926a72007-04-26 15:48:28 -0700303
David Howells5a924b82016-09-22 00:29:31 +0100304 up_read(&conn->params.local->defrag_sem);
305 if (ret == -EMSGSIZE)
306 goto send_fragmentable;
307
308done:
David Howells50235c42016-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 Howells50235c42016-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 Howells50235c42016-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}