rxrpc: Fix locking issues in rxrpc_put_peer_locked()
[linux-block.git] / net / rxrpc / peer_event.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
f66d7490 2/* Peer event handling, typically ICMP messages.
17926a79
DH
3 *
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
17926a79
DH
6 */
7
8#include <linux/module.h>
9#include <linux/net.h>
10#include <linux/skbuff.h>
11#include <linux/errqueue.h>
12#include <linux/udp.h>
13#include <linux/in.h>
14#include <linux/in6.h>
15#include <linux/icmp.h>
16#include <net/sock.h>
17#include <net/af_rxrpc.h>
18#include <net/ip.h>
19#include "ar-internal.h"
20
5e6ef4f1
DH
21static void rxrpc_store_error(struct rxrpc_peer *, struct sk_buff *);
22static void rxrpc_distribute_error(struct rxrpc_peer *, struct sk_buff *,
23 enum rxrpc_call_completion, int);
f66d7490 24
ac56a0b4
DH
25/*
26 * Find the peer associated with a local error.
27 */
28static struct rxrpc_peer *rxrpc_lookup_peer_local_rcu(struct rxrpc_local *local,
29 const struct sk_buff *skb,
30 struct sockaddr_rxrpc *srx)
31{
32 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
33
34 _enter("");
35
36 memset(srx, 0, sizeof(*srx));
37 srx->transport_type = local->srx.transport_type;
38 srx->transport_len = local->srx.transport_len;
39 srx->transport.family = local->srx.transport.family;
40
b6c66c43
DH
41 /* Can we see an ICMP4 packet on an ICMP6 listening socket? and vice
42 * versa?
43 */
494337c9 44 switch (srx->transport.family) {
be6e6707 45 case AF_INET:
46894a13
DH
46 srx->transport_len = sizeof(srx->transport.sin);
47 srx->transport.family = AF_INET;
494337c9 48 srx->transport.sin.sin_port = serr->port;
be6e6707
DH
49 switch (serr->ee.ee_origin) {
50 case SO_EE_ORIGIN_ICMP:
494337c9 51 memcpy(&srx->transport.sin.sin_addr,
be6e6707
DH
52 skb_network_header(skb) + serr->addr_offset,
53 sizeof(struct in_addr));
54 break;
55 case SO_EE_ORIGIN_ICMP6:
494337c9 56 memcpy(&srx->transport.sin.sin_addr,
be6e6707
DH
57 skb_network_header(skb) + serr->addr_offset + 12,
58 sizeof(struct in_addr));
59 break;
60 default:
494337c9 61 memcpy(&srx->transport.sin.sin_addr, &ip_hdr(skb)->saddr,
be6e6707
DH
62 sizeof(struct in_addr));
63 break;
64 }
65 break;
66
d1912747 67#ifdef CONFIG_AF_RXRPC_IPV6
75b54cb5 68 case AF_INET6:
75b54cb5
DH
69 switch (serr->ee.ee_origin) {
70 case SO_EE_ORIGIN_ICMP6:
46894a13 71 srx->transport.sin6.sin6_port = serr->port;
494337c9 72 memcpy(&srx->transport.sin6.sin6_addr,
75b54cb5
DH
73 skb_network_header(skb) + serr->addr_offset,
74 sizeof(struct in6_addr));
75 break;
76 case SO_EE_ORIGIN_ICMP:
46894a13
DH
77 srx->transport_len = sizeof(srx->transport.sin);
78 srx->transport.family = AF_INET;
79 srx->transport.sin.sin_port = serr->port;
80 memcpy(&srx->transport.sin.sin_addr,
75b54cb5
DH
81 skb_network_header(skb) + serr->addr_offset,
82 sizeof(struct in_addr));
83 break;
84 default:
494337c9 85 memcpy(&srx->transport.sin6.sin6_addr,
75b54cb5
DH
86 &ipv6_hdr(skb)->saddr,
87 sizeof(struct in6_addr));
88 break;
89 }
90 break;
d1912747 91#endif
75b54cb5 92
be6e6707
DH
93 default:
94 BUG();
95 }
96
494337c9 97 return rxrpc_lookup_peer_rcu(local, srx);
be6e6707
DH
98}
99
1a70c05b
DH
100/*
101 * Handle an MTU/fragmentation problem.
102 */
ac56a0b4 103static void rxrpc_adjust_mtu(struct rxrpc_peer *peer, unsigned int mtu)
1a70c05b 104{
1a70c05b 105 /* wind down the local interface MTU */
e969c92c 106 if (mtu > 0 && peer->if_mtu == 65535 && mtu < peer->if_mtu)
1a70c05b 107 peer->if_mtu = mtu;
1a70c05b
DH
108
109 if (mtu == 0) {
110 /* they didn't give us a size, estimate one */
111 mtu = peer->if_mtu;
112 if (mtu > 1500) {
113 mtu >>= 1;
114 if (mtu < 1500)
115 mtu = 1500;
116 } else {
117 mtu -= 100;
118 if (mtu < peer->hdrsize)
119 mtu = peer->hdrsize + 4;
120 }
121 }
122
123 if (mtu < peer->mtu) {
3dd9c8b5 124 spin_lock(&peer->lock);
1a70c05b
DH
125 peer->mtu = mtu;
126 peer->maxdata = peer->mtu - peer->hdrsize;
3dd9c8b5 127 spin_unlock(&peer->lock);
1a70c05b
DH
128 }
129}
130
17926a79 131/*
f66d7490 132 * Handle an error received on the local endpoint.
17926a79 133 */
ff734825 134void rxrpc_input_error(struct rxrpc_local *local, struct sk_buff *skb)
17926a79 135{
ff734825 136 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
494337c9 137 struct sockaddr_rxrpc srx;
ac56a0b4 138 struct rxrpc_peer *peer = NULL;
17926a79 139
ff734825 140 _enter("L=%x", local->debug_id);
17926a79 141
b6c66c43
DH
142 if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) {
143 _leave("UDP empty message");
b6c66c43
DH
144 return;
145 }
494337c9 146
ff734825 147 rcu_read_lock();
b6c66c43 148 peer = rxrpc_lookup_peer_local_rcu(local, skb, &srx);
47c810a7 149 if (peer && !rxrpc_get_peer_maybe(peer, rxrpc_peer_get_input_error))
b6c66c43 150 peer = NULL;
ff734825
DH
151 rcu_read_unlock();
152 if (!peer)
b6c66c43 153 return;
17926a79 154
b6c66c43
DH
155 trace_rxrpc_rx_icmp(peer, &serr->ee, &srx);
156
157 if ((serr->ee.ee_origin == SO_EE_ORIGIN_ICMP &&
158 serr->ee.ee_type == ICMP_DEST_UNREACH &&
159 serr->ee.ee_code == ICMP_FRAG_NEEDED)) {
160 rxrpc_adjust_mtu(peer, serr->ee.ee_info);
161 goto out;
162 }
163
5e6ef4f1 164 rxrpc_store_error(peer, skb);
b6c66c43 165out:
47c810a7 166 rxrpc_put_peer(peer, rxrpc_peer_put_input_error);
17926a79
DH
167}
168
169/*
f66d7490 170 * Map an error report to error codes on the peer record.
17926a79 171 */
5e6ef4f1 172static void rxrpc_store_error(struct rxrpc_peer *peer, struct sk_buff *skb)
17926a79 173{
f3344303 174 enum rxrpc_call_completion compl = RXRPC_CALL_NETWORK_ERROR;
5e6ef4f1
DH
175 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
176 struct sock_extended_err *ee = &serr->ee;
177 int err = ee->ee_errno;
17926a79
DH
178
179 _enter("");
180
17926a79 181 switch (ee->ee_origin) {
f66d7490 182 case SO_EE_ORIGIN_NONE:
17926a79 183 case SO_EE_ORIGIN_LOCAL:
f3344303 184 compl = RXRPC_CALL_LOCAL_ERROR;
17926a79
DH
185 break;
186
17926a79 187 case SO_EE_ORIGIN_ICMP6:
23e2db31
DH
188 if (err == EACCES)
189 err = EHOSTUNREACH;
df561f66 190 fallthrough;
e969c92c 191 case SO_EE_ORIGIN_ICMP:
17926a79 192 default:
17926a79
DH
193 break;
194 }
195
5e6ef4f1 196 rxrpc_distribute_error(peer, skb, compl, err);
f66d7490
DH
197}
198
199/*
f3344303 200 * Distribute an error that occurred on a peer.
f66d7490 201 */
5e6ef4f1
DH
202static void rxrpc_distribute_error(struct rxrpc_peer *peer, struct sk_buff *skb,
203 enum rxrpc_call_completion compl, int err)
f66d7490 204{
f66d7490 205 struct rxrpc_call *call;
29fb4ec3
DH
206 HLIST_HEAD(error_targets);
207
208 spin_lock(&peer->lock);
209 hlist_move_list(&peer->error_targets, &error_targets);
210
211 while (!hlist_empty(&error_targets)) {
212 call = hlist_entry(error_targets.first,
213 struct rxrpc_call, error_link);
214 hlist_del_init(&call->error_link);
215 spin_unlock(&peer->lock);
17926a79 216
cb0fc0c9 217 rxrpc_see_call(call, rxrpc_call_see_distribute_error);
5e6ef4f1
DH
218 rxrpc_set_call_completion(call, compl, 0, -err);
219 rxrpc_input_call_event(call, skb);
29fb4ec3
DH
220
221 spin_lock(&peer->lock);
17926a79 222 }
29fb4ec3
DH
223
224 spin_unlock(&peer->lock);
17926a79 225}
cf1a6474 226
ace45bec 227/*
330bdcfa 228 * Perform keep-alive pings.
ace45bec 229 */
330bdcfa
DH
230static void rxrpc_peer_keepalive_dispatch(struct rxrpc_net *rxnet,
231 struct list_head *collector,
232 time64_t base,
233 u8 cursor)
ace45bec 234{
ace45bec 235 struct rxrpc_peer *peer;
330bdcfa
DH
236 const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1;
237 time64_t keepalive_at;
608aecd1 238 bool use;
330bdcfa 239 int slot;
ace45bec 240
3dd9c8b5 241 spin_lock(&rxnet->peer_hash_lock);
ace45bec 242
330bdcfa
DH
243 while (!list_empty(collector)) {
244 peer = list_entry(collector->next,
245 struct rxrpc_peer, keepalive_link);
ace45bec 246
330bdcfa 247 list_del_init(&peer->keepalive_link);
47c810a7 248 if (!rxrpc_get_peer_maybe(peer, rxrpc_peer_get_keepalive))
330bdcfa 249 continue;
ace45bec 250
608aecd1
DH
251 use = __rxrpc_use_local(peer->local, rxrpc_local_use_peer_keepalive);
252 spin_unlock(&rxnet->peer_hash_lock);
04d36d74 253
608aecd1 254 if (use) {
04d36d74
DH
255 keepalive_at = peer->last_tx_at + RXRPC_KEEPALIVE_TIME;
256 slot = keepalive_at - base;
257 _debug("%02x peer %u t=%d {%pISp}",
258 cursor, peer->debug_id, slot, &peer->srx.transport);
259
260 if (keepalive_at <= base ||
261 keepalive_at > base + RXRPC_KEEPALIVE_TIME) {
262 rxrpc_send_keepalive(peer);
263 slot = RXRPC_KEEPALIVE_TIME;
264 }
330bdcfa 265
04d36d74
DH
266 /* A transmission to this peer occurred since last we
267 * examined it so put it into the appropriate future
268 * bucket.
269 */
270 slot += cursor;
271 slot &= mask;
3dd9c8b5 272 spin_lock(&rxnet->peer_hash_lock);
04d36d74
DH
273 list_add_tail(&peer->keepalive_link,
274 &rxnet->peer_keepalive[slot & mask]);
608aecd1 275 spin_unlock(&rxnet->peer_hash_lock);
0fde882f 276 rxrpc_unuse_local(peer->local, rxrpc_local_unuse_peer_keepalive);
ace45bec 277 }
608aecd1
DH
278 rxrpc_put_peer(peer, rxrpc_peer_put_keepalive);
279 spin_lock(&rxnet->peer_hash_lock);
ace45bec
DH
280 }
281
3dd9c8b5 282 spin_unlock(&rxnet->peer_hash_lock);
330bdcfa 283}
ace45bec 284
330bdcfa
DH
285/*
286 * Perform keep-alive pings with VERSION packets to keep any NAT alive.
287 */
288void rxrpc_peer_keepalive_worker(struct work_struct *work)
289{
290 struct rxrpc_net *rxnet =
291 container_of(work, struct rxrpc_net, peer_keepalive_work);
292 const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1;
293 time64_t base, now, delay;
294 u8 cursor, stop;
295 LIST_HEAD(collector);
ace45bec 296
330bdcfa
DH
297 now = ktime_get_seconds();
298 base = rxnet->peer_keepalive_base;
299 cursor = rxnet->peer_keepalive_cursor;
300 _enter("%lld,%u", base - now, cursor);
ace45bec 301
330bdcfa
DH
302 if (!rxnet->live)
303 return;
ace45bec 304
330bdcfa
DH
305 /* Remove to a temporary list all the peers that are currently lodged
306 * in expired buckets plus all new peers.
307 *
308 * Everything in the bucket at the cursor is processed this
309 * second; the bucket at cursor + 1 goes at now + 1s and so
310 * on...
ace45bec 311 */
3dd9c8b5 312 spin_lock(&rxnet->peer_hash_lock);
330bdcfa
DH
313 list_splice_init(&rxnet->peer_keepalive_new, &collector);
314
315 stop = cursor + ARRAY_SIZE(rxnet->peer_keepalive);
316 while (base <= now && (s8)(cursor - stop) < 0) {
317 list_splice_tail_init(&rxnet->peer_keepalive[cursor & mask],
318 &collector);
319 base++;
320 cursor++;
321 }
ace45bec 322
330bdcfa 323 base = now;
3dd9c8b5 324 spin_unlock(&rxnet->peer_hash_lock);
ace45bec 325
ace45bec
DH
326 rxnet->peer_keepalive_base = base;
327 rxnet->peer_keepalive_cursor = cursor;
330bdcfa
DH
328 rxrpc_peer_keepalive_dispatch(rxnet, &collector, base, cursor);
329 ASSERT(list_empty(&collector));
330
331 /* Schedule the timer for the next occupied timeslot. */
332 cursor = rxnet->peer_keepalive_cursor;
333 stop = cursor + RXRPC_KEEPALIVE_TIME - 1;
334 for (; (s8)(cursor - stop) < 0; cursor++) {
335 if (!list_empty(&rxnet->peer_keepalive[cursor & mask]))
336 break;
337 base++;
338 }
339
340 now = ktime_get_seconds();
341 delay = base - now;
342 if (delay < 1)
343 delay = 1;
344 delay *= HZ;
345 if (rxnet->live)
346 timer_reduce(&rxnet->peer_keepalive_timer, jiffies + delay);
347
ace45bec
DH
348 _leave("");
349}