blob: aa0507214b3123bffe6ed13fb071a767f0f22099 [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);
David Howells27d0fc42016-09-17 10:49:13 +0100140 if (!call->ackr_reason) {
141 spin_unlock_bh(&call->lock);
142 ret = 0;
143 goto out;
144 }
David Howells8d94aa32016-09-07 09:19:31 +0100145 n = rxrpc_fill_out_ack(call, pkt);
146 call->ackr_reason = 0;
147
148 spin_unlock_bh(&call->lock);
149
150 _proto("Tx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
151 serial,
152 ntohs(pkt->ack.maxSkew),
153 ntohl(pkt->ack.firstPacket),
154 ntohl(pkt->ack.previousPacket),
155 ntohl(pkt->ack.serial),
156 rxrpc_acks(pkt->ack.reason),
157 pkt->ack.nAcks);
158
159 iov[0].iov_len += sizeof(pkt->ack) + n;
160 iov[1].iov_base = &pkt->ackinfo;
161 iov[1].iov_len = sizeof(pkt->ackinfo);
162 len += sizeof(pkt->ack) + n + sizeof(pkt->ackinfo);
163 ioc = 2;
164 break;
165
166 case RXRPC_PACKET_TYPE_ABORT:
167 abort_code = call->abort_code;
168 pkt->abort_code = htonl(abort_code);
169 _proto("Tx ABORT %%%u { %d }", serial, abort_code);
170 iov[0].iov_len += sizeof(pkt->abort_code);
171 len += sizeof(pkt->abort_code);
172 ioc = 1;
173 break;
174
175 default:
176 BUG();
177 ret = -ENOANO;
178 goto out;
179 }
180
181 ret = kernel_sendmsg(conn->params.local->socket,
182 &msg, iov, ioc, len);
183
David Howells248f2192016-09-08 11:10:12 +0100184 if (ret < 0 && call->state < RXRPC_CALL_COMPLETE) {
185 switch (pkt->whdr.type) {
186 case RXRPC_PACKET_TYPE_ACK:
187 rxrpc_propose_ACK(call, pkt->ack.reason,
188 ntohs(pkt->ack.maxSkew),
189 ntohl(pkt->ack.serial),
190 true, true);
191 break;
192 case RXRPC_PACKET_TYPE_ABORT:
193 break;
194 }
195 }
196
David Howells8d94aa32016-09-07 09:19:31 +0100197out:
198 rxrpc_put_connection(conn);
199 kfree(pkt);
200 return ret;
201}
202
David Howells5873c082014-02-07 18:58:44 +0000203/*
David Howells17926a72007-04-26 15:48:28 -0700204 * send a packet through the transport endpoint
205 */
David Howells985a5c82016-06-17 11:53:37 +0100206int rxrpc_send_data_packet(struct rxrpc_connection *conn, struct sk_buff *skb)
David Howells17926a72007-04-26 15:48:28 -0700207{
208 struct kvec iov[1];
209 struct msghdr msg;
210 int ret, opt;
211
212 _enter(",{%d}", skb->len);
213
214 iov[0].iov_base = skb->head;
215 iov[0].iov_len = skb->len;
216
David Howells985a5c82016-06-17 11:53:37 +0100217 msg.msg_name = &conn->params.peer->srx.transport;
218 msg.msg_namelen = conn->params.peer->srx.transport_len;
David Howells17926a72007-04-26 15:48:28 -0700219 msg.msg_control = NULL;
220 msg.msg_controllen = 0;
221 msg.msg_flags = 0;
222
223 /* send the packet with the don't fragment bit set if we currently
224 * think it's small enough */
David Howells985a5c82016-06-17 11:53:37 +0100225 if (skb->len - sizeof(struct rxrpc_wire_header) < conn->params.peer->maxdata) {
226 down_read(&conn->params.local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -0700227 /* send the packet by UDP
228 * - returns -EMSGSIZE if UDP would have to fragment the packet
229 * to go out of the interface
230 * - in which case, we'll have processed the ICMP error
231 * message and update the peer record
232 */
David Howells985a5c82016-06-17 11:53:37 +0100233 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 1,
David Howells17926a72007-04-26 15:48:28 -0700234 iov[0].iov_len);
235
David Howells985a5c82016-06-17 11:53:37 +0100236 up_read(&conn->params.local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -0700237 if (ret == -EMSGSIZE)
238 goto send_fragmentable;
239
David Howells985a5c82016-06-17 11:53:37 +0100240 _leave(" = %d [%u]", ret, conn->params.peer->maxdata);
David Howells17926a72007-04-26 15:48:28 -0700241 return ret;
242 }
243
244send_fragmentable:
245 /* attempt to send this message with fragmentation enabled */
246 _debug("send fragment");
247
David Howells985a5c82016-06-17 11:53:37 +0100248 down_write(&conn->params.local->defrag_sem);
David Howells17926a72007-04-26 15:48:28 -0700249
David Howells985a5c82016-06-17 11:53:37 +0100250 switch (conn->params.local->srx.transport.family) {
251 case AF_INET:
252 opt = IP_PMTUDISC_DONT;
253 ret = kernel_setsockopt(conn->params.local->socket,
254 SOL_IP, IP_MTU_DISCOVER,
255 (char *)&opt, sizeof(opt));
256 if (ret == 0) {
257 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 1,
258 iov[0].iov_len);
259
260 opt = IP_PMTUDISC_DO;
261 kernel_setsockopt(conn->params.local->socket, SOL_IP,
262 IP_MTU_DISCOVER,
263 (char *)&opt, sizeof(opt));
264 }
265 break;
David Howells75b54cb2016-09-13 08:49:05 +0100266
David Howellsd1912742016-09-17 07:26:01 +0100267#ifdef CONFIG_AF_RXRPC_IPV6
David Howells75b54cb2016-09-13 08:49:05 +0100268 case AF_INET6:
269 opt = IPV6_PMTUDISC_DONT;
270 ret = kernel_setsockopt(conn->params.local->socket,
271 SOL_IPV6, IPV6_MTU_DISCOVER,
272 (char *)&opt, sizeof(opt));
273 if (ret == 0) {
274 ret = kernel_sendmsg(conn->params.local->socket, &msg,
275 iov, 1, iov[0].iov_len);
276
277 opt = IPV6_PMTUDISC_DO;
278 kernel_setsockopt(conn->params.local->socket,
279 SOL_IPV6, IPV6_MTU_DISCOVER,
280 (char *)&opt, sizeof(opt));
281 }
282 break;
David Howellsd1912742016-09-17 07:26:01 +0100283#endif
David Howells17926a72007-04-26 15:48:28 -0700284 }
285
David Howells985a5c82016-06-17 11:53:37 +0100286 up_write(&conn->params.local->defrag_sem);
287 _leave(" = %d [frag %u]", ret, conn->params.peer->maxdata);
David Howells17926a72007-04-26 15:48:28 -0700288 return ret;
289}
David Howells248f2192016-09-08 11:10:12 +0100290
291/*
292 * reject packets through the local endpoint
293 */
294void rxrpc_reject_packets(struct rxrpc_local *local)
295{
David Howells1c2bc7b2016-09-13 08:49:05 +0100296 struct sockaddr_rxrpc srx;
David Howells248f2192016-09-08 11:10:12 +0100297 struct rxrpc_skb_priv *sp;
298 struct rxrpc_wire_header whdr;
299 struct sk_buff *skb;
300 struct msghdr msg;
301 struct kvec iov[2];
302 size_t size;
303 __be32 code;
304
305 _enter("%d", local->debug_id);
306
307 iov[0].iov_base = &whdr;
308 iov[0].iov_len = sizeof(whdr);
309 iov[1].iov_base = &code;
310 iov[1].iov_len = sizeof(code);
311 size = sizeof(whdr) + sizeof(code);
312
David Howells1c2bc7b2016-09-13 08:49:05 +0100313 msg.msg_name = &srx.transport;
David Howells248f2192016-09-08 11:10:12 +0100314 msg.msg_control = NULL;
315 msg.msg_controllen = 0;
316 msg.msg_flags = 0;
317
David Howells248f2192016-09-08 11:10:12 +0100318 memset(&whdr, 0, sizeof(whdr));
319 whdr.type = RXRPC_PACKET_TYPE_ABORT;
320
321 while ((skb = skb_dequeue(&local->reject_queue))) {
322 rxrpc_see_skb(skb);
323 sp = rxrpc_skb(skb);
David Howells1c2bc7b2016-09-13 08:49:05 +0100324
325 if (rxrpc_extract_addr_from_skb(&srx, skb) == 0) {
326 msg.msg_namelen = srx.transport_len;
327
David Howells248f2192016-09-08 11:10:12 +0100328 code = htonl(skb->priority);
329
330 whdr.epoch = htonl(sp->hdr.epoch);
331 whdr.cid = htonl(sp->hdr.cid);
332 whdr.callNumber = htonl(sp->hdr.callNumber);
333 whdr.serviceId = htons(sp->hdr.serviceId);
334 whdr.flags = sp->hdr.flags;
335 whdr.flags ^= RXRPC_CLIENT_INITIATED;
336 whdr.flags &= RXRPC_CLIENT_INITIATED;
337
338 kernel_sendmsg(local->socket, &msg, iov, 2, size);
David Howells248f2192016-09-08 11:10:12 +0100339 }
340
341 rxrpc_free_skb(skb);
342 }
343
344 _leave("");
345}