Commit | Line | Data |
---|---|---|
17926a79 DH |
1 | /* 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 | ||
9b6d5398 JP |
12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
13 | ||
17926a79 | 14 | #include <linux/net.h> |
5a0e3ad6 | 15 | #include <linux/gfp.h> |
17926a79 | 16 | #include <linux/skbuff.h> |
bc3b2d7f | 17 | #include <linux/export.h> |
17926a79 DH |
18 | #include <net/sock.h> |
19 | #include <net/af_rxrpc.h> | |
20 | #include "ar-internal.h" | |
21 | ||
8d94aa38 DH |
22 | struct 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 | */ | |
38 | static size_t rxrpc_fill_out_ack(struct rxrpc_call *call, | |
39 | struct rxrpc_pkt_buffer *pkt) | |
40 | { | |
f3639df2 | 41 | rxrpc_serial_t serial; |
248f219c DH |
42 | rxrpc_seq_t hard_ack, top, seq; |
43 | int ix; | |
8d94aa38 DH |
44 | u32 mtu, jmax; |
45 | u8 *ackp = pkt->acks; | |
46 | ||
248f219c | 47 | /* Barrier against rxrpc_input_data(). */ |
f3639df2 | 48 | serial = call->ackr_serial; |
248f219c DH |
49 | hard_ack = READ_ONCE(call->rx_hard_ack); |
50 | top = smp_load_acquire(&call->rx_top); | |
51 | ||
8d94aa38 | 52 | pkt->ack.bufferSpace = htons(8); |
248f219c DH |
53 | pkt->ack.maxSkew = htons(call->ackr_skew); |
54 | pkt->ack.firstPacket = htonl(hard_ack + 1); | |
8d94aa38 | 55 | pkt->ack.previousPacket = htonl(call->ackr_prev_seq); |
f3639df2 | 56 | pkt->ack.serial = htonl(serial); |
248f219c DH |
57 | pkt->ack.reason = call->ackr_reason; |
58 | pkt->ack.nAcks = top - hard_ack; | |
59 | ||
60 | 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 | } | |
8d94aa38 | 71 | |
248f219c DH |
72 | mtu = call->conn->params.peer->if_mtu; |
73 | mtu -= call->conn->params.peer->hdrsize; | |
75e42126 | 74 | jmax = (call->nr_jumbo_bad > 3) ? 1 : rxrpc_rx_jumbo_max; |
8d94aa38 DH |
75 | pkt->ackinfo.rxMTU = htonl(rxrpc_rx_mtu); |
76 | pkt->ackinfo.maxMTU = htonl(mtu); | |
75e42126 | 77 | pkt->ackinfo.rwind = htonl(call->rx_winsize); |
8d94aa38 DH |
78 | pkt->ackinfo.jumbo_max = htonl(jmax); |
79 | ||
f3639df2 DH |
80 | trace_rxrpc_tx_ack(call, hard_ack + 1, serial, call->ackr_reason, |
81 | top - hard_ack); | |
82 | ||
8d94aa38 DH |
83 | *ackp++ = 0; |
84 | *ackp++ = 0; | |
85 | *ackp++ = 0; | |
248f219c | 86 | return top - hard_ack + 3; |
8d94aa38 DH |
87 | } |
88 | ||
89 | /* | |
248f219c | 90 | * Send an ACK or ABORT call packet. |
8d94aa38 DH |
91 | */ |
92 | int 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); | |
27d0fc43 DH |
145 | if (!call->ackr_reason) { |
146 | spin_unlock_bh(&call->lock); | |
147 | ret = 0; | |
148 | goto out; | |
149 | } | |
8d94aa38 DH |
150 | 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 | ||
248f219c | 189 | if (ret < 0 && call->state < RXRPC_CALL_COMPLETE) { |
2311e327 | 190 | switch (type) { |
248f219c DH |
191 | 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 | ||
8d94aa38 DH |
202 | out: |
203 | rxrpc_put_connection(conn); | |
204 | kfree(pkt); | |
205 | return ret; | |
206 | } | |
207 | ||
17926a79 DH |
208 | /* |
209 | * send a packet through the transport endpoint | |
210 | */ | |
985a5c82 | 211 | int rxrpc_send_data_packet(struct rxrpc_connection *conn, struct sk_buff *skb) |
17926a79 DH |
212 | { |
213 | struct kvec iov[1]; | |
214 | struct msghdr msg; | |
215 | int ret, opt; | |
216 | ||
217 | _enter(",{%d}", skb->len); | |
218 | ||
219 | iov[0].iov_base = skb->head; | |
220 | iov[0].iov_len = skb->len; | |
221 | ||
985a5c82 DH |
222 | msg.msg_name = &conn->params.peer->srx.transport; |
223 | msg.msg_namelen = conn->params.peer->srx.transport_len; | |
17926a79 DH |
224 | msg.msg_control = NULL; |
225 | msg.msg_controllen = 0; | |
226 | msg.msg_flags = 0; | |
227 | ||
8a681c36 DH |
228 | if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) { |
229 | static int lose; | |
230 | if ((lose++ & 7) == 7) { | |
231 | rxrpc_lose_skb(skb, rxrpc_skb_tx_lost); | |
232 | _leave(" = 0 [lose]"); | |
233 | return 0; | |
234 | } | |
235 | } | |
236 | ||
17926a79 DH |
237 | /* send the packet with the don't fragment bit set if we currently |
238 | * think it's small enough */ | |
985a5c82 DH |
239 | if (skb->len - sizeof(struct rxrpc_wire_header) < conn->params.peer->maxdata) { |
240 | down_read(&conn->params.local->defrag_sem); | |
17926a79 DH |
241 | /* send the packet by UDP |
242 | * - returns -EMSGSIZE if UDP would have to fragment the packet | |
243 | * to go out of the interface | |
244 | * - in which case, we'll have processed the ICMP error | |
245 | * message and update the peer record | |
246 | */ | |
985a5c82 | 247 | ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 1, |
17926a79 DH |
248 | iov[0].iov_len); |
249 | ||
985a5c82 | 250 | up_read(&conn->params.local->defrag_sem); |
17926a79 DH |
251 | if (ret == -EMSGSIZE) |
252 | goto send_fragmentable; | |
253 | ||
985a5c82 | 254 | _leave(" = %d [%u]", ret, conn->params.peer->maxdata); |
17926a79 DH |
255 | return ret; |
256 | } | |
257 | ||
258 | send_fragmentable: | |
259 | /* attempt to send this message with fragmentation enabled */ | |
260 | _debug("send fragment"); | |
261 | ||
985a5c82 DH |
262 | down_write(&conn->params.local->defrag_sem); |
263 | ||
264 | switch (conn->params.local->srx.transport.family) { | |
265 | case AF_INET: | |
266 | opt = IP_PMTUDISC_DONT; | |
267 | ret = kernel_setsockopt(conn->params.local->socket, | |
268 | SOL_IP, IP_MTU_DISCOVER, | |
269 | (char *)&opt, sizeof(opt)); | |
270 | if (ret == 0) { | |
271 | ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 1, | |
272 | iov[0].iov_len); | |
273 | ||
274 | opt = IP_PMTUDISC_DO; | |
275 | kernel_setsockopt(conn->params.local->socket, SOL_IP, | |
276 | IP_MTU_DISCOVER, | |
277 | (char *)&opt, sizeof(opt)); | |
278 | } | |
279 | break; | |
75b54cb5 | 280 | |
d1912747 | 281 | #ifdef CONFIG_AF_RXRPC_IPV6 |
75b54cb5 DH |
282 | case AF_INET6: |
283 | opt = IPV6_PMTUDISC_DONT; | |
284 | ret = kernel_setsockopt(conn->params.local->socket, | |
285 | SOL_IPV6, IPV6_MTU_DISCOVER, | |
286 | (char *)&opt, sizeof(opt)); | |
287 | if (ret == 0) { | |
288 | ret = kernel_sendmsg(conn->params.local->socket, &msg, | |
289 | iov, 1, iov[0].iov_len); | |
290 | ||
291 | opt = IPV6_PMTUDISC_DO; | |
292 | kernel_setsockopt(conn->params.local->socket, | |
293 | SOL_IPV6, IPV6_MTU_DISCOVER, | |
294 | (char *)&opt, sizeof(opt)); | |
295 | } | |
296 | break; | |
d1912747 | 297 | #endif |
17926a79 DH |
298 | } |
299 | ||
985a5c82 DH |
300 | up_write(&conn->params.local->defrag_sem); |
301 | _leave(" = %d [frag %u]", ret, conn->params.peer->maxdata); | |
17926a79 DH |
302 | return ret; |
303 | } | |
248f219c DH |
304 | |
305 | /* | |
306 | * reject packets through the local endpoint | |
307 | */ | |
308 | void rxrpc_reject_packets(struct rxrpc_local *local) | |
309 | { | |
1c2bc7b9 | 310 | struct sockaddr_rxrpc srx; |
248f219c DH |
311 | struct rxrpc_skb_priv *sp; |
312 | struct rxrpc_wire_header whdr; | |
313 | struct sk_buff *skb; | |
314 | struct msghdr msg; | |
315 | struct kvec iov[2]; | |
316 | size_t size; | |
317 | __be32 code; | |
318 | ||
319 | _enter("%d", local->debug_id); | |
320 | ||
321 | iov[0].iov_base = &whdr; | |
322 | iov[0].iov_len = sizeof(whdr); | |
323 | iov[1].iov_base = &code; | |
324 | iov[1].iov_len = sizeof(code); | |
325 | size = sizeof(whdr) + sizeof(code); | |
326 | ||
1c2bc7b9 | 327 | msg.msg_name = &srx.transport; |
248f219c DH |
328 | msg.msg_control = NULL; |
329 | msg.msg_controllen = 0; | |
330 | msg.msg_flags = 0; | |
331 | ||
248f219c DH |
332 | memset(&whdr, 0, sizeof(whdr)); |
333 | whdr.type = RXRPC_PACKET_TYPE_ABORT; | |
334 | ||
335 | while ((skb = skb_dequeue(&local->reject_queue))) { | |
71f3ca40 | 336 | rxrpc_see_skb(skb, rxrpc_skb_rx_seen); |
248f219c | 337 | sp = rxrpc_skb(skb); |
1c2bc7b9 DH |
338 | |
339 | if (rxrpc_extract_addr_from_skb(&srx, skb) == 0) { | |
340 | msg.msg_namelen = srx.transport_len; | |
341 | ||
248f219c DH |
342 | code = htonl(skb->priority); |
343 | ||
344 | whdr.epoch = htonl(sp->hdr.epoch); | |
345 | whdr.cid = htonl(sp->hdr.cid); | |
346 | whdr.callNumber = htonl(sp->hdr.callNumber); | |
347 | whdr.serviceId = htons(sp->hdr.serviceId); | |
348 | whdr.flags = sp->hdr.flags; | |
349 | whdr.flags ^= RXRPC_CLIENT_INITIATED; | |
350 | whdr.flags &= RXRPC_CLIENT_INITIATED; | |
351 | ||
352 | kernel_sendmsg(local->socket, &msg, iov, 2, size); | |
248f219c DH |
353 | } |
354 | ||
71f3ca40 | 355 | rxrpc_free_skb(skb, rxrpc_skb_rx_freed); |
248f219c DH |
356 | } |
357 | ||
358 | _leave(""); | |
359 | } |