blob: d7cd87f17f0d084da00b8dfeab67aa8ed91adc4e [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 Howells248f2192016-09-08 11:10:12 +010041 rxrpc_seq_t hard_ack, top, seq;
42 int ix;
David Howells8d94aa32016-09-07 09:19:31 +010043 u32 mtu, jmax;
44 u8 *ackp = pkt->acks;
45
David Howells248f2192016-09-08 11:10:12 +010046 /* Barrier against rxrpc_input_data(). */
47 hard_ack = READ_ONCE(call->rx_hard_ack);
48 top = smp_load_acquire(&call->rx_top);
49
David Howells8d94aa32016-09-07 09:19:31 +010050 pkt->ack.bufferSpace = htons(8);
David Howells248f2192016-09-08 11:10:12 +010051 pkt->ack.maxSkew = htons(call->ackr_skew);
52 pkt->ack.firstPacket = htonl(hard_ack + 1);
David Howells8d94aa32016-09-07 09:19:31 +010053 pkt->ack.previousPacket = htonl(call->ackr_prev_seq);
54 pkt->ack.serial = htonl(call->ackr_serial);
David Howells248f2192016-09-08 11:10:12 +010055 pkt->ack.reason = call->ackr_reason;
56 pkt->ack.nAcks = top - hard_ack;
David Howells8d94aa32016-09-07 09:19:31 +010057
David Howells248f2192016-09-08 11:10:12 +010058 if (after(top, hard_ack)) {
59 seq = hard_ack + 1;
60 do {
61 ix = seq & RXRPC_RXTX_BUFF_MASK;
62 if (call->rxtx_buffer[ix])
63 *ackp++ = RXRPC_ACK_TYPE_ACK;
64 else
65 *ackp++ = RXRPC_ACK_TYPE_NACK;
66 seq++;
67 } while (before_eq(seq, top));
68 }
69
70 mtu = call->conn->params.peer->if_mtu;
71 mtu -= call->conn->params.peer->hdrsize;
David Howells75e42122016-09-13 22:36:22 +010072 jmax = (call->nr_jumbo_bad > 3) ? 1 : rxrpc_rx_jumbo_max;
David Howells8d94aa32016-09-07 09:19:31 +010073 pkt->ackinfo.rxMTU = htonl(rxrpc_rx_mtu);
74 pkt->ackinfo.maxMTU = htonl(mtu);
David Howells75e42122016-09-13 22:36:22 +010075 pkt->ackinfo.rwind = htonl(call->rx_winsize);
David Howells8d94aa32016-09-07 09:19:31 +010076 pkt->ackinfo.jumbo_max = htonl(jmax);
77
78 *ackp++ = 0;
79 *ackp++ = 0;
80 *ackp++ = 0;
David Howells248f2192016-09-08 11:10:12 +010081 return top - hard_ack + 3;
David Howells8d94aa32016-09-07 09:19:31 +010082}
83
84/*
David Howells248f2192016-09-08 11:10:12 +010085 * Send an ACK or ABORT call packet.
David Howells8d94aa32016-09-07 09:19:31 +010086 */
87int rxrpc_send_call_packet(struct rxrpc_call *call, u8 type)
88{
89 struct rxrpc_connection *conn = NULL;
90 struct rxrpc_pkt_buffer *pkt;
91 struct msghdr msg;
92 struct kvec iov[2];
93 rxrpc_serial_t serial;
94 size_t len, n;
95 int ioc, ret;
96 u32 abort_code;
97
98 _enter("%u,%s", call->debug_id, rxrpc_pkts[type]);
99
100 spin_lock_bh(&call->lock);
101 if (call->conn)
102 conn = rxrpc_get_connection_maybe(call->conn);
103 spin_unlock_bh(&call->lock);
104 if (!conn)
105 return -ECONNRESET;
106
107 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
108 if (!pkt) {
109 rxrpc_put_connection(conn);
110 return -ENOMEM;
111 }
112
113 serial = atomic_inc_return(&conn->serial);
114
115 msg.msg_name = &call->peer->srx.transport;
116 msg.msg_namelen = call->peer->srx.transport_len;
117 msg.msg_control = NULL;
118 msg.msg_controllen = 0;
119 msg.msg_flags = 0;
120
121 pkt->whdr.epoch = htonl(conn->proto.epoch);
122 pkt->whdr.cid = htonl(call->cid);
123 pkt->whdr.callNumber = htonl(call->call_id);
124 pkt->whdr.seq = 0;
125 pkt->whdr.serial = htonl(serial);
126 pkt->whdr.type = type;
127 pkt->whdr.flags = conn->out_clientflag;
128 pkt->whdr.userStatus = 0;
129 pkt->whdr.securityIndex = call->security_ix;
130 pkt->whdr._rsvd = 0;
131 pkt->whdr.serviceId = htons(call->service_id);
132
133 iov[0].iov_base = pkt;
134 iov[0].iov_len = sizeof(pkt->whdr);
135 len = sizeof(pkt->whdr);
136
137 switch (type) {
138 case RXRPC_PACKET_TYPE_ACK:
139 spin_lock_bh(&call->lock);
140 n = rxrpc_fill_out_ack(call, pkt);
141 call->ackr_reason = 0;
142
143 spin_unlock_bh(&call->lock);
144
145 _proto("Tx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
146 serial,
147 ntohs(pkt->ack.maxSkew),
148 ntohl(pkt->ack.firstPacket),
149 ntohl(pkt->ack.previousPacket),
150 ntohl(pkt->ack.serial),
151 rxrpc_acks(pkt->ack.reason),
152 pkt->ack.nAcks);
153
154 iov[0].iov_len += sizeof(pkt->ack) + n;
155 iov[1].iov_base = &pkt->ackinfo;
156 iov[1].iov_len = sizeof(pkt->ackinfo);
157 len += sizeof(pkt->ack) + n + sizeof(pkt->ackinfo);
158 ioc = 2;
159 break;
160
161 case RXRPC_PACKET_TYPE_ABORT:
162 abort_code = call->abort_code;
163 pkt->abort_code = htonl(abort_code);
164 _proto("Tx ABORT %%%u { %d }", serial, abort_code);
165 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
176 ret = kernel_sendmsg(conn->params.local->socket,
177 &msg, iov, ioc, len);
178
David Howells248f2192016-09-08 11:10:12 +0100179 if (ret < 0 && call->state < RXRPC_CALL_COMPLETE) {
180 switch (pkt->whdr.type) {
181 case RXRPC_PACKET_TYPE_ACK:
182 rxrpc_propose_ACK(call, pkt->ack.reason,
183 ntohs(pkt->ack.maxSkew),
184 ntohl(pkt->ack.serial),
185 true, true);
186 break;
187 case RXRPC_PACKET_TYPE_ABORT:
188 break;
189 }
190 }
191
David Howells8d94aa32016-09-07 09:19:31 +0100192out:
193 rxrpc_put_connection(conn);
194 kfree(pkt);
195 return ret;
196}
197
David Howells5873c082014-02-07 18:58:44 +0000198/*
David Howells17926a72007-04-26 15:48:28 -0700199 * send a packet through the transport endpoint
200 */
David Howells985a5c82016-06-17 11:53:37 +0100201int rxrpc_send_data_packet(struct rxrpc_connection *conn, struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700202{
203 struct kvec iov[1];
204 struct msghdr msg;
205 int ret, opt;
206
207 _enter(",{%d}", skb->len);
208
209 iov[0].iov_base = skb->head;
210 iov[0].iov_len = skb->len;
211
David Howells985a5c82016-06-17 11:53:37 +0100212 msg.msg_name = &conn->params.peer->srx.transport;
213 msg.msg_namelen = conn->params.peer->srx.transport_len;
David Howells17926a72007-04-26 15:48:28 -0700214 msg.msg_control = NULL;
215 msg.msg_controllen = 0;
216 msg.msg_flags = 0;
217
218 /* send the packet with the don't fragment bit set if we currently
219 * think it's small enough */
David Howells985a5c82016-06-17 11:53:37 +0100220 if (skb->len - sizeof(struct rxrpc_wire_header) < conn->params.peer->maxdata) {
221 down_read(&conn->params.local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -0700222 /* send the packet by UDP
223 * - returns -EMSGSIZE if UDP would have to fragment the packet
224 * to go out of the interface
225 * - in which case, we'll have processed the ICMP error
226 * message and update the peer record
227 */
David Howells985a5c82016-06-17 11:53:37 +0100228 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 1,
David Howells17926a72007-04-26 15:48:28 -0700229 iov[0].iov_len);
230
David Howells985a5c82016-06-17 11:53:37 +0100231 up_read(&conn->params.local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -0700232 if (ret == -EMSGSIZE)
233 goto send_fragmentable;
234
David Howells985a5c82016-06-17 11:53:37 +0100235 _leave(" = %d [%u]", ret, conn->params.peer->maxdata);
David Howells17926a72007-04-26 15:48:28 -0700236 return ret;
237 }
238
239send_fragmentable:
240 /* attempt to send this message with fragmentation enabled */
241 _debug("send fragment");
242
David Howells985a5c82016-06-17 11:53:37 +0100243 down_write(&conn->params.local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -0700244
David Howells985a5c82016-06-17 11:53:37 +0100245 switch (conn->params.local->srx.transport.family) {
246 case AF_INET:
247 opt = IP_PMTUDISC_DONT;
248 ret = kernel_setsockopt(conn->params.local->socket,
249 SOL_IP, IP_MTU_DISCOVER,
250 (char *)&opt, sizeof(opt));
251 if (ret == 0) {
252 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 1,
253 iov[0].iov_len);
254
255 opt = IP_PMTUDISC_DO;
256 kernel_setsockopt(conn->params.local->socket, SOL_IP,
257 IP_MTU_DISCOVER,
258 (char *)&opt, sizeof(opt));
259 }
260 break;
David Howells75b54cb2016-09-13 08:49:05 +0100261
262 case AF_INET6:
263 opt = IPV6_PMTUDISC_DONT;
264 ret = kernel_setsockopt(conn->params.local->socket,
265 SOL_IPV6, IPV6_MTU_DISCOVER,
266 (char *)&opt, sizeof(opt));
267 if (ret == 0) {
268 ret = kernel_sendmsg(conn->params.local->socket, &msg,
269 iov, 1, iov[0].iov_len);
270
271 opt = IPV6_PMTUDISC_DO;
272 kernel_setsockopt(conn->params.local->socket,
273 SOL_IPV6, IPV6_MTU_DISCOVER,
274 (char *)&opt, sizeof(opt));
275 }
276 break;
David Howells17926a72007-04-26 15:48:28 -0700277 }
278
David Howells985a5c82016-06-17 11:53:37 +0100279 up_write(&conn->params.local->defrag_sem);
280 _leave(" = %d [frag %u]", ret, conn->params.peer->maxdata);
David Howells17926a72007-04-26 15:48:28 -0700281 return ret;
282}
David Howells248f2192016-09-08 11:10:12 +0100283
284/*
285 * reject packets through the local endpoint
286 */
287void rxrpc_reject_packets(struct rxrpc_local *local)
288{
David Howells1c2bc7b2016-09-13 08:49:05 +0100289 struct sockaddr_rxrpc srx;
David Howells248f2192016-09-08 11:10:12 +0100290 struct rxrpc_skb_priv *sp;
291 struct rxrpc_wire_header whdr;
292 struct sk_buff *skb;
293 struct msghdr msg;
294 struct kvec iov[2];
295 size_t size;
296 __be32 code;
297
298 _enter("%d", local->debug_id);
299
300 iov[0].iov_base = &whdr;
301 iov[0].iov_len = sizeof(whdr);
302 iov[1].iov_base = &code;
303 iov[1].iov_len = sizeof(code);
304 size = sizeof(whdr) + sizeof(code);
305
David Howells1c2bc7b2016-09-13 08:49:05 +0100306 msg.msg_name = &srx.transport;
David Howells248f2192016-09-08 11:10:12 +0100307 msg.msg_control = NULL;
308 msg.msg_controllen = 0;
309 msg.msg_flags = 0;
310
David Howells248f2192016-09-08 11:10:12 +0100311 memset(&whdr, 0, sizeof(whdr));
312 whdr.type = RXRPC_PACKET_TYPE_ABORT;
313
314 while ((skb = skb_dequeue(&local->reject_queue))) {
315 rxrpc_see_skb(skb);
316 sp = rxrpc_skb(skb);
David Howells1c2bc7b2016-09-13 08:49:05 +0100317
318 if (rxrpc_extract_addr_from_skb(&srx, skb) == 0) {
319 msg.msg_namelen = srx.transport_len;
320
David Howells248f2192016-09-08 11:10:12 +0100321 code = htonl(skb->priority);
322
323 whdr.epoch = htonl(sp->hdr.epoch);
324 whdr.cid = htonl(sp->hdr.cid);
325 whdr.callNumber = htonl(sp->hdr.callNumber);
326 whdr.serviceId = htons(sp->hdr.serviceId);
327 whdr.flags = sp->hdr.flags;
328 whdr.flags ^= RXRPC_CLIENT_INITIATED;
329 whdr.flags &= RXRPC_CLIENT_INITIATED;
330
331 kernel_sendmsg(local->socket, &msg, iov, 2, size);
David Howells248f2192016-09-08 11:10:12 +0100332 }
333
334 rxrpc_free_skb(skb);
335 }
336
337 _leave("");
338}