blob: 817fb0e82d6a6734fdbf1b4601ac5a0c4e9c8bf3 [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 Howells248f2192016-09-08 11:10:12 +010060 if (after(top, hard_ack)) {
61 seq = hard_ack + 1;
62 do {
63 ix = seq & RXRPC_RXTX_BUFF_MASK;
64 if (call->rxtx_buffer[ix])
65 *ackp++ = RXRPC_ACK_TYPE_ACK;
66 else
67 *ackp++ = RXRPC_ACK_TYPE_NACK;
68 seq++;
69 } while (before_eq(seq, top));
70 }
71
72 mtu = call->conn->params.peer->if_mtu;
73 mtu -= call->conn->params.peer->hdrsize;
David Howells75e42122016-09-13 22:36:22 +010074 jmax = (call->nr_jumbo_bad > 3) ? 1 : rxrpc_rx_jumbo_max;
David Howells8d94aa32016-09-07 09:19:31 +010075 pkt->ackinfo.rxMTU = htonl(rxrpc_rx_mtu);
76 pkt->ackinfo.maxMTU = htonl(mtu);
David Howells75e42122016-09-13 22:36:22 +010077 pkt->ackinfo.rwind = htonl(call->rx_winsize);
David Howells8d94aa32016-09-07 09:19:31 +010078 pkt->ackinfo.jumbo_max = htonl(jmax);
79
David Howellsf3639df2016-09-17 10:49:13 +010080 trace_rxrpc_tx_ack(call, hard_ack + 1, serial, call->ackr_reason,
81 top - hard_ack);
82
David Howells8d94aa32016-09-07 09:19:31 +010083 *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;
100 int ioc, ret;
101 u32 abort_code;
102
103 _enter("%u,%s", call->debug_id, rxrpc_pkts[type]);
104
105 spin_lock_bh(&call->lock);
106 if (call->conn)
107 conn = rxrpc_get_connection_maybe(call->conn);
108 spin_unlock_bh(&call->lock);
109 if (!conn)
110 return -ECONNRESET;
111
112 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
113 if (!pkt) {
114 rxrpc_put_connection(conn);
115 return -ENOMEM;
116 }
117
118 serial = atomic_inc_return(&conn->serial);
119
120 msg.msg_name = &call->peer->srx.transport;
121 msg.msg_namelen = call->peer->srx.transport_len;
122 msg.msg_control = NULL;
123 msg.msg_controllen = 0;
124 msg.msg_flags = 0;
125
126 pkt->whdr.epoch = htonl(conn->proto.epoch);
127 pkt->whdr.cid = htonl(call->cid);
128 pkt->whdr.callNumber = htonl(call->call_id);
129 pkt->whdr.seq = 0;
130 pkt->whdr.serial = htonl(serial);
131 pkt->whdr.type = type;
132 pkt->whdr.flags = conn->out_clientflag;
133 pkt->whdr.userStatus = 0;
134 pkt->whdr.securityIndex = call->security_ix;
135 pkt->whdr._rsvd = 0;
136 pkt->whdr.serviceId = htons(call->service_id);
137
138 iov[0].iov_base = pkt;
139 iov[0].iov_len = sizeof(pkt->whdr);
140 len = sizeof(pkt->whdr);
141
142 switch (type) {
143 case RXRPC_PACKET_TYPE_ACK:
144 spin_lock_bh(&call->lock);
David Howells27d0fc42016-09-17 10:49:13 +0100145 if (!call->ackr_reason) {
146 spin_unlock_bh(&call->lock);
147 ret = 0;
148 goto out;
149 }
David Howells8d94aa32016-09-07 09:19:31 +0100150 n = rxrpc_fill_out_ack(call, pkt);
151 call->ackr_reason = 0;
152
153 spin_unlock_bh(&call->lock);
154
155 _proto("Tx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
156 serial,
157 ntohs(pkt->ack.maxSkew),
158 ntohl(pkt->ack.firstPacket),
159 ntohl(pkt->ack.previousPacket),
160 ntohl(pkt->ack.serial),
161 rxrpc_acks(pkt->ack.reason),
162 pkt->ack.nAcks);
163
164 iov[0].iov_len += sizeof(pkt->ack) + n;
165 iov[1].iov_base = &pkt->ackinfo;
166 iov[1].iov_len = sizeof(pkt->ackinfo);
167 len += sizeof(pkt->ack) + n + sizeof(pkt->ackinfo);
168 ioc = 2;
169 break;
170
171 case RXRPC_PACKET_TYPE_ABORT:
172 abort_code = call->abort_code;
173 pkt->abort_code = htonl(abort_code);
174 _proto("Tx ABORT %%%u { %d }", serial, abort_code);
175 iov[0].iov_len += sizeof(pkt->abort_code);
176 len += sizeof(pkt->abort_code);
177 ioc = 1;
178 break;
179
180 default:
181 BUG();
182 ret = -ENOANO;
183 goto out;
184 }
185
186 ret = kernel_sendmsg(conn->params.local->socket,
187 &msg, iov, ioc, len);
188
David Howells248f2192016-09-08 11:10:12 +0100189 if (ret < 0 && call->state < RXRPC_CALL_COMPLETE) {
David Howells2311e322016-09-17 10:49:12 +0100190 switch (type) {
David Howells248f2192016-09-08 11:10:12 +0100191 case RXRPC_PACKET_TYPE_ACK:
192 rxrpc_propose_ACK(call, pkt->ack.reason,
193 ntohs(pkt->ack.maxSkew),
194 ntohl(pkt->ack.serial),
195 true, true);
196 break;
197 case RXRPC_PACKET_TYPE_ABORT:
198 break;
199 }
200 }
201
David Howells8d94aa32016-09-07 09:19:31 +0100202out:
203 rxrpc_put_connection(conn);
204 kfree(pkt);
205 return ret;
206}
207
David Howells5873c082014-02-07 18:58:44 +0000208/*
David Howells17926a72007-04-26 15:48:28 -0700209 * send a packet through the transport endpoint
210 */
David Howells5a924b82016-09-22 00:29:31 +0100211int rxrpc_send_data_packet(struct rxrpc_call *call, struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700212{
David Howells5a924b82016-09-22 00:29:31 +0100213 struct rxrpc_connection *conn = call->conn;
214 struct rxrpc_wire_header whdr;
215 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700216 struct msghdr msg;
David Howells5a924b82016-09-22 00:29:31 +0100217 struct kvec iov[2];
218 rxrpc_serial_t serial;
219 size_t len;
David Howells17926a72007-04-26 15:48:28 -0700220 int ret, opt;
221
222 _enter(",{%d}", skb->len);
223
David Howells5a924b82016-09-22 00:29:31 +0100224 /* Each transmission of a Tx packet needs a new serial number */
225 serial = atomic_inc_return(&conn->serial);
David Howells17926a72007-04-26 15:48:28 -0700226
David Howells5a924b82016-09-22 00:29:31 +0100227 whdr.epoch = htonl(conn->proto.epoch);
228 whdr.cid = htonl(call->cid);
229 whdr.callNumber = htonl(call->call_id);
230 whdr.seq = htonl(sp->hdr.seq);
231 whdr.serial = htonl(serial);
232 whdr.type = RXRPC_PACKET_TYPE_DATA;
233 whdr.flags = sp->hdr.flags;
234 whdr.userStatus = 0;
235 whdr.securityIndex = call->security_ix;
236 whdr._rsvd = htons(sp->hdr._rsvd);
237 whdr.serviceId = htons(call->service_id);
238
239 iov[0].iov_base = &whdr;
240 iov[0].iov_len = sizeof(whdr);
241 iov[1].iov_base = skb->head;
242 iov[1].iov_len = skb->len;
243 len = iov[0].iov_len + iov[1].iov_len;
244
245 msg.msg_name = &call->peer->srx.transport;
246 msg.msg_namelen = call->peer->srx.transport_len;
David Howells17926a72007-04-26 15:48:28 -0700247 msg.msg_control = NULL;
248 msg.msg_controllen = 0;
249 msg.msg_flags = 0;
250
David Howells8a681c362016-09-17 10:49:15 +0100251 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
252 static int lose;
253 if ((lose++ & 7) == 7) {
254 rxrpc_lose_skb(skb, rxrpc_skb_tx_lost);
255 _leave(" = 0 [lose]");
256 return 0;
257 }
258 }
259
David Howells5a924b82016-09-22 00:29:31 +0100260 _proto("Tx DATA %%%u { #%u }", serial, sp->hdr.seq);
261
David Howells17926a72007-04-26 15:48:28 -0700262 /* send the packet with the don't fragment bit set if we currently
263 * think it's small enough */
David Howells5a924b82016-09-22 00:29:31 +0100264 if (iov[1].iov_len >= call->peer->maxdata)
265 goto send_fragmentable;
David Howells17926a72007-04-26 15:48:28 -0700266
David Howells5a924b82016-09-22 00:29:31 +0100267 down_read(&conn->params.local->defrag_sem);
268 /* send the packet by UDP
269 * - returns -EMSGSIZE if UDP would have to fragment the packet
270 * to go out of the interface
271 * - in which case, we'll have processed the ICMP error
272 * message and update the peer record
273 */
274 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
David Howells17926a72007-04-26 15:48:28 -0700275
David Howells5a924b82016-09-22 00:29:31 +0100276 up_read(&conn->params.local->defrag_sem);
277 if (ret == -EMSGSIZE)
278 goto send_fragmentable;
279
280done:
281 if (ret == 0) {
282 sp->resend_at = jiffies + rxrpc_resend_timeout;
283 sp->hdr.serial = serial;
David Howells17926a72007-04-26 15:48:28 -0700284 }
David Howells5a924b82016-09-22 00:29:31 +0100285 _leave(" = %d [%u]", ret, call->peer->maxdata);
286 return ret;
David Howells17926a72007-04-26 15:48:28 -0700287
288send_fragmentable:
289 /* attempt to send this message with fragmentation enabled */
290 _debug("send fragment");
291
David Howells985a5c82016-06-17 11:53:37 +0100292 down_write(&conn->params.local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -0700293
David Howells985a5c82016-06-17 11:53:37 +0100294 switch (conn->params.local->srx.transport.family) {
295 case AF_INET:
296 opt = IP_PMTUDISC_DONT;
297 ret = kernel_setsockopt(conn->params.local->socket,
298 SOL_IP, IP_MTU_DISCOVER,
299 (char *)&opt, sizeof(opt));
300 if (ret == 0) {
David Howells5a924b82016-09-22 00:29:31 +0100301 ret = kernel_sendmsg(conn->params.local->socket, &msg,
302 iov, 2, len);
David Howells985a5c82016-06-17 11:53:37 +0100303
304 opt = IP_PMTUDISC_DO;
305 kernel_setsockopt(conn->params.local->socket, SOL_IP,
306 IP_MTU_DISCOVER,
307 (char *)&opt, sizeof(opt));
308 }
309 break;
David Howells75b54cb2016-09-13 08:49:05 +0100310
David Howellsd1912742016-09-17 07:26:01 +0100311#ifdef CONFIG_AF_RXRPC_IPV6
David Howells75b54cb2016-09-13 08:49:05 +0100312 case AF_INET6:
313 opt = IPV6_PMTUDISC_DONT;
314 ret = kernel_setsockopt(conn->params.local->socket,
315 SOL_IPV6, IPV6_MTU_DISCOVER,
316 (char *)&opt, sizeof(opt));
317 if (ret == 0) {
318 ret = kernel_sendmsg(conn->params.local->socket, &msg,
319 iov, 1, iov[0].iov_len);
320
321 opt = IPV6_PMTUDISC_DO;
322 kernel_setsockopt(conn->params.local->socket,
323 SOL_IPV6, IPV6_MTU_DISCOVER,
324 (char *)&opt, sizeof(opt));
325 }
326 break;
David Howellsd1912742016-09-17 07:26:01 +0100327#endif
David Howells17926a72007-04-26 15:48:28 -0700328 }
329
David Howells985a5c82016-06-17 11:53:37 +0100330 up_write(&conn->params.local->defrag_sem);
David Howells5a924b82016-09-22 00:29:31 +0100331 goto done;
David Howells17926a72007-04-26 15:48:28 -0700332}
David Howells248f2192016-09-08 11:10:12 +0100333
334/*
335 * reject packets through the local endpoint
336 */
337void rxrpc_reject_packets(struct rxrpc_local *local)
338{
David Howells1c2bc7b2016-09-13 08:49:05 +0100339 struct sockaddr_rxrpc srx;
David Howells248f2192016-09-08 11:10:12 +0100340 struct rxrpc_skb_priv *sp;
341 struct rxrpc_wire_header whdr;
342 struct sk_buff *skb;
343 struct msghdr msg;
344 struct kvec iov[2];
345 size_t size;
346 __be32 code;
347
348 _enter("%d", local->debug_id);
349
350 iov[0].iov_base = &whdr;
351 iov[0].iov_len = sizeof(whdr);
352 iov[1].iov_base = &code;
353 iov[1].iov_len = sizeof(code);
354 size = sizeof(whdr) + sizeof(code);
355
David Howells1c2bc7b2016-09-13 08:49:05 +0100356 msg.msg_name = &srx.transport;
David Howells248f2192016-09-08 11:10:12 +0100357 msg.msg_control = NULL;
358 msg.msg_controllen = 0;
359 msg.msg_flags = 0;
360
David Howells248f2192016-09-08 11:10:12 +0100361 memset(&whdr, 0, sizeof(whdr));
362 whdr.type = RXRPC_PACKET_TYPE_ABORT;
363
364 while ((skb = skb_dequeue(&local->reject_queue))) {
David Howells71f3ca42016-09-17 10:49:14 +0100365 rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
David Howells248f2192016-09-08 11:10:12 +0100366 sp = rxrpc_skb(skb);
David Howells1c2bc7b2016-09-13 08:49:05 +0100367
368 if (rxrpc_extract_addr_from_skb(&srx, skb) == 0) {
369 msg.msg_namelen = srx.transport_len;
370
David Howells248f2192016-09-08 11:10:12 +0100371 code = htonl(skb->priority);
372
373 whdr.epoch = htonl(sp->hdr.epoch);
374 whdr.cid = htonl(sp->hdr.cid);
375 whdr.callNumber = htonl(sp->hdr.callNumber);
376 whdr.serviceId = htons(sp->hdr.serviceId);
377 whdr.flags = sp->hdr.flags;
378 whdr.flags ^= RXRPC_CLIENT_INITIATED;
379 whdr.flags &= RXRPC_CLIENT_INITIATED;
380
381 kernel_sendmsg(local->socket, &msg, iov, 2, size);
David Howells248f2192016-09-08 11:10:12 +0100382 }
383
David Howells71f3ca42016-09-17 10:49:14 +0100384 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
David Howells248f2192016-09-08 11:10:12 +0100385 }
386
387 _leave("");
388}