12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f66d7490SDavid Howells /* Peer event handling, typically ICMP messages.
38c3e34a4SDavid Howells *
48c3e34a4SDavid Howells * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
58c3e34a4SDavid Howells * Written by David Howells (dhowells@redhat.com)
68c3e34a4SDavid Howells */
78c3e34a4SDavid Howells
88c3e34a4SDavid Howells #include <linux/module.h>
98c3e34a4SDavid Howells #include <linux/net.h>
108c3e34a4SDavid Howells #include <linux/skbuff.h>
118c3e34a4SDavid Howells #include <linux/errqueue.h>
128c3e34a4SDavid Howells #include <linux/udp.h>
138c3e34a4SDavid Howells #include <linux/in.h>
148c3e34a4SDavid Howells #include <linux/in6.h>
158c3e34a4SDavid Howells #include <linux/icmp.h>
168c3e34a4SDavid Howells #include <net/sock.h>
178c3e34a4SDavid Howells #include <net/af_rxrpc.h>
188c3e34a4SDavid Howells #include <net/ip.h>
198c3e34a4SDavid Howells #include "ar-internal.h"
208c3e34a4SDavid Howells
215e6ef4f1SDavid Howells static void rxrpc_store_error(struct rxrpc_peer *, struct sk_buff *);
225e6ef4f1SDavid Howells static void rxrpc_distribute_error(struct rxrpc_peer *, struct sk_buff *,
235e6ef4f1SDavid Howells enum rxrpc_call_completion, int);
24f66d7490SDavid Howells
258c3e34a4SDavid Howells /*
26ac56a0b4SDavid Howells * Find the peer associated with a local error.
27ac56a0b4SDavid Howells */
rxrpc_lookup_peer_local_rcu(struct rxrpc_local * local,const struct sk_buff * skb,struct sockaddr_rxrpc * srx)28ac56a0b4SDavid Howells static struct rxrpc_peer *rxrpc_lookup_peer_local_rcu(struct rxrpc_local *local,
29494337c9SDavid Howells const struct sk_buff *skb,
30494337c9SDavid Howells struct sockaddr_rxrpc *srx)
31be6e6707SDavid Howells {
32be6e6707SDavid Howells struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
33be6e6707SDavid Howells
34be6e6707SDavid Howells _enter("");
35be6e6707SDavid Howells
36494337c9SDavid Howells memset(srx, 0, sizeof(*srx));
37494337c9SDavid Howells srx->transport_type = local->srx.transport_type;
38494337c9SDavid Howells srx->transport_len = local->srx.transport_len;
39494337c9SDavid Howells srx->transport.family = local->srx.transport.family;
40be6e6707SDavid Howells
41b6c66c43SDavid Howells /* Can we see an ICMP4 packet on an ICMP6 listening socket? and vice
42b6c66c43SDavid Howells * versa?
43b6c66c43SDavid Howells */
44494337c9SDavid Howells switch (srx->transport.family) {
45be6e6707SDavid Howells case AF_INET:
4646894a13SDavid Howells srx->transport_len = sizeof(srx->transport.sin);
4746894a13SDavid Howells srx->transport.family = AF_INET;
48494337c9SDavid Howells srx->transport.sin.sin_port = serr->port;
49be6e6707SDavid Howells switch (serr->ee.ee_origin) {
50be6e6707SDavid Howells case SO_EE_ORIGIN_ICMP:
51494337c9SDavid Howells memcpy(&srx->transport.sin.sin_addr,
52be6e6707SDavid Howells skb_network_header(skb) + serr->addr_offset,
53be6e6707SDavid Howells sizeof(struct in_addr));
54be6e6707SDavid Howells break;
55be6e6707SDavid Howells case SO_EE_ORIGIN_ICMP6:
56494337c9SDavid Howells memcpy(&srx->transport.sin.sin_addr,
57be6e6707SDavid Howells skb_network_header(skb) + serr->addr_offset + 12,
58be6e6707SDavid Howells sizeof(struct in_addr));
59be6e6707SDavid Howells break;
60be6e6707SDavid Howells default:
61494337c9SDavid Howells memcpy(&srx->transport.sin.sin_addr, &ip_hdr(skb)->saddr,
62be6e6707SDavid Howells sizeof(struct in_addr));
63be6e6707SDavid Howells break;
64be6e6707SDavid Howells }
65be6e6707SDavid Howells break;
66be6e6707SDavid Howells
67d1912747SDavid Howells #ifdef CONFIG_AF_RXRPC_IPV6
6875b54cb5SDavid Howells case AF_INET6:
6975b54cb5SDavid Howells switch (serr->ee.ee_origin) {
7075b54cb5SDavid Howells case SO_EE_ORIGIN_ICMP6:
7146894a13SDavid Howells srx->transport.sin6.sin6_port = serr->port;
72494337c9SDavid Howells memcpy(&srx->transport.sin6.sin6_addr,
7375b54cb5SDavid Howells skb_network_header(skb) + serr->addr_offset,
7475b54cb5SDavid Howells sizeof(struct in6_addr));
7575b54cb5SDavid Howells break;
7675b54cb5SDavid Howells case SO_EE_ORIGIN_ICMP:
7746894a13SDavid Howells srx->transport_len = sizeof(srx->transport.sin);
7846894a13SDavid Howells srx->transport.family = AF_INET;
7946894a13SDavid Howells srx->transport.sin.sin_port = serr->port;
8046894a13SDavid Howells memcpy(&srx->transport.sin.sin_addr,
8175b54cb5SDavid Howells skb_network_header(skb) + serr->addr_offset,
8275b54cb5SDavid Howells sizeof(struct in_addr));
8375b54cb5SDavid Howells break;
8475b54cb5SDavid Howells default:
85494337c9SDavid Howells memcpy(&srx->transport.sin6.sin6_addr,
8675b54cb5SDavid Howells &ipv6_hdr(skb)->saddr,
8775b54cb5SDavid Howells sizeof(struct in6_addr));
8875b54cb5SDavid Howells break;
8975b54cb5SDavid Howells }
9075b54cb5SDavid Howells break;
91d1912747SDavid Howells #endif
9275b54cb5SDavid Howells
93be6e6707SDavid Howells default:
94be6e6707SDavid Howells BUG();
95be6e6707SDavid Howells }
96be6e6707SDavid Howells
97494337c9SDavid Howells return rxrpc_lookup_peer_rcu(local, srx);
98be6e6707SDavid Howells }
99be6e6707SDavid Howells
100be6e6707SDavid Howells /*
1011a70c05bSDavid Howells * Handle an MTU/fragmentation problem.
1021a70c05bSDavid Howells */
rxrpc_adjust_mtu(struct rxrpc_peer * peer,unsigned int mtu)103ac56a0b4SDavid Howells static void rxrpc_adjust_mtu(struct rxrpc_peer *peer, unsigned int mtu)
1041a70c05bSDavid Howells {
1051a70c05bSDavid Howells /* wind down the local interface MTU */
106e969c92cSDavid Howells if (mtu > 0 && peer->if_mtu == 65535 && mtu < peer->if_mtu)
1071a70c05bSDavid Howells peer->if_mtu = mtu;
1081a70c05bSDavid Howells
1091a70c05bSDavid Howells if (mtu == 0) {
1101a70c05bSDavid Howells /* they didn't give us a size, estimate one */
1111a70c05bSDavid Howells mtu = peer->if_mtu;
1121a70c05bSDavid Howells if (mtu > 1500) {
1131a70c05bSDavid Howells mtu >>= 1;
1141a70c05bSDavid Howells if (mtu < 1500)
1151a70c05bSDavid Howells mtu = 1500;
1161a70c05bSDavid Howells } else {
1171a70c05bSDavid Howells mtu -= 100;
1181a70c05bSDavid Howells if (mtu < peer->hdrsize)
1191a70c05bSDavid Howells mtu = peer->hdrsize + 4;
1201a70c05bSDavid Howells }
1211a70c05bSDavid Howells }
1221a70c05bSDavid Howells
1231a70c05bSDavid Howells if (mtu < peer->mtu) {
1243dd9c8b5SDavid Howells spin_lock(&peer->lock);
1251a70c05bSDavid Howells peer->mtu = mtu;
1261a70c05bSDavid Howells peer->maxdata = peer->mtu - peer->hdrsize;
1273dd9c8b5SDavid Howells spin_unlock(&peer->lock);
1281a70c05bSDavid Howells }
1291a70c05bSDavid Howells }
1301a70c05bSDavid Howells
1311a70c05bSDavid Howells /*
132f66d7490SDavid Howells * Handle an error received on the local endpoint.
1338c3e34a4SDavid Howells */
rxrpc_input_error(struct rxrpc_local * local,struct sk_buff * skb)134ff734825SDavid Howells void rxrpc_input_error(struct rxrpc_local *local, struct sk_buff *skb)
1358c3e34a4SDavid Howells {
136ff734825SDavid Howells struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
137494337c9SDavid Howells struct sockaddr_rxrpc srx;
138ac56a0b4SDavid Howells struct rxrpc_peer *peer = NULL;
1398c3e34a4SDavid Howells
140ff734825SDavid Howells _enter("L=%x", local->debug_id);
1418c3e34a4SDavid Howells
142b6c66c43SDavid Howells if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) {
143b6c66c43SDavid Howells _leave("UDP empty message");
144b6c66c43SDavid Howells return;
145b6c66c43SDavid Howells }
1468c3e34a4SDavid Howells
147ff734825SDavid Howells rcu_read_lock();
148ac56a0b4SDavid Howells peer = rxrpc_lookup_peer_local_rcu(local, skb, &srx);
14947c810a7SDavid Howells if (peer && !rxrpc_get_peer_maybe(peer, rxrpc_peer_get_input_error))
150be6e6707SDavid Howells peer = NULL;
151b6c66c43SDavid Howells rcu_read_unlock();
152ff734825SDavid Howells if (!peer)
153b6c66c43SDavid Howells return;
154f66d7490SDavid Howells
155b6c66c43SDavid Howells trace_rxrpc_rx_icmp(peer, &serr->ee, &srx);
156b6c66c43SDavid Howells
157b6c66c43SDavid Howells if ((serr->ee.ee_origin == SO_EE_ORIGIN_ICMP &&
158b6c66c43SDavid Howells serr->ee.ee_type == ICMP_DEST_UNREACH &&
159b6c66c43SDavid Howells serr->ee.ee_code == ICMP_FRAG_NEEDED)) {
160b6c66c43SDavid Howells rxrpc_adjust_mtu(peer, serr->ee.ee_info);
161b6c66c43SDavid Howells goto out;
162b6c66c43SDavid Howells }
163b6c66c43SDavid Howells
1645e6ef4f1SDavid Howells rxrpc_store_error(peer, skb);
165b6c66c43SDavid Howells out:
16647c810a7SDavid Howells rxrpc_put_peer(peer, rxrpc_peer_put_input_error);
1678c3e34a4SDavid Howells }
1688c3e34a4SDavid Howells
1698c3e34a4SDavid Howells /*
170f66d7490SDavid Howells * Map an error report to error codes on the peer record.
1718c3e34a4SDavid Howells */
rxrpc_store_error(struct rxrpc_peer * peer,struct sk_buff * skb)1725e6ef4f1SDavid Howells static void rxrpc_store_error(struct rxrpc_peer *peer, struct sk_buff *skb)
1738c3e34a4SDavid Howells {
174f3344303SDavid Howells enum rxrpc_call_completion compl = RXRPC_CALL_NETWORK_ERROR;
1755e6ef4f1SDavid Howells struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
1765e6ef4f1SDavid Howells struct sock_extended_err *ee = &serr->ee;
1775e6ef4f1SDavid Howells int err = ee->ee_errno;
1788c3e34a4SDavid Howells
1798c3e34a4SDavid Howells _enter("");
1808c3e34a4SDavid Howells
1818c3e34a4SDavid Howells switch (ee->ee_origin) {
182f66d7490SDavid Howells case SO_EE_ORIGIN_NONE:
1838c3e34a4SDavid Howells case SO_EE_ORIGIN_LOCAL:
184f3344303SDavid Howells compl = RXRPC_CALL_LOCAL_ERROR;
1858c3e34a4SDavid Howells break;
1868c3e34a4SDavid Howells
1878c3e34a4SDavid Howells case SO_EE_ORIGIN_ICMP6:
18823e2db31SDavid Howells if (err == EACCES)
18923e2db31SDavid Howells err = EHOSTUNREACH;
190df561f66SGustavo A. R. Silva fallthrough;
191e969c92cSDavid Howells case SO_EE_ORIGIN_ICMP:
1928c3e34a4SDavid Howells default:
1938c3e34a4SDavid Howells break;
1948c3e34a4SDavid Howells }
1958c3e34a4SDavid Howells
1965e6ef4f1SDavid Howells rxrpc_distribute_error(peer, skb, compl, err);
197f66d7490SDavid Howells }
1988c3e34a4SDavid Howells
199f66d7490SDavid Howells /*
200f3344303SDavid Howells * Distribute an error that occurred on a peer.
201f66d7490SDavid Howells */
rxrpc_distribute_error(struct rxrpc_peer * peer,struct sk_buff * skb,enum rxrpc_call_completion compl,int err)2025e6ef4f1SDavid Howells static void rxrpc_distribute_error(struct rxrpc_peer *peer, struct sk_buff *skb,
2035e6ef4f1SDavid Howells enum rxrpc_call_completion compl, int err)
204f66d7490SDavid Howells {
205f66d7490SDavid Howells struct rxrpc_call *call;
20629fb4ec3SDavid Howells HLIST_HEAD(error_targets);
2078c3e34a4SDavid Howells
20829fb4ec3SDavid Howells spin_lock(&peer->lock);
20929fb4ec3SDavid Howells hlist_move_list(&peer->error_targets, &error_targets);
21029fb4ec3SDavid Howells
21129fb4ec3SDavid Howells while (!hlist_empty(&error_targets)) {
21229fb4ec3SDavid Howells call = hlist_entry(error_targets.first,
21329fb4ec3SDavid Howells struct rxrpc_call, error_link);
21429fb4ec3SDavid Howells hlist_del_init(&call->error_link);
21529fb4ec3SDavid Howells spin_unlock(&peer->lock);
21629fb4ec3SDavid Howells
217cb0fc0c9SDavid Howells rxrpc_see_call(call, rxrpc_call_see_distribute_error);
2185e6ef4f1SDavid Howells rxrpc_set_call_completion(call, compl, 0, -err);
2195e6ef4f1SDavid Howells rxrpc_input_call_event(call, skb);
22029fb4ec3SDavid Howells
22129fb4ec3SDavid Howells spin_lock(&peer->lock);
2228c3e34a4SDavid Howells }
22329fb4ec3SDavid Howells
22429fb4ec3SDavid Howells spin_unlock(&peer->lock);
2258c3e34a4SDavid Howells }
226cf1a6474SDavid Howells
227cf1a6474SDavid Howells /*
228330bdcfaSDavid Howells * Perform keep-alive pings.
229330bdcfaSDavid Howells */
rxrpc_peer_keepalive_dispatch(struct rxrpc_net * rxnet,struct list_head * collector,time64_t base,u8 cursor)230330bdcfaSDavid Howells static void rxrpc_peer_keepalive_dispatch(struct rxrpc_net *rxnet,
231330bdcfaSDavid Howells struct list_head *collector,
232330bdcfaSDavid Howells time64_t base,
233330bdcfaSDavid Howells u8 cursor)
234330bdcfaSDavid Howells {
235330bdcfaSDavid Howells struct rxrpc_peer *peer;
236330bdcfaSDavid Howells const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1;
237330bdcfaSDavid Howells time64_t keepalive_at;
238*608aecd1SDavid Howells bool use;
239330bdcfaSDavid Howells int slot;
240330bdcfaSDavid Howells
2413dd9c8b5SDavid Howells spin_lock(&rxnet->peer_hash_lock);
242330bdcfaSDavid Howells
243330bdcfaSDavid Howells while (!list_empty(collector)) {
244330bdcfaSDavid Howells peer = list_entry(collector->next,
245330bdcfaSDavid Howells struct rxrpc_peer, keepalive_link);
246330bdcfaSDavid Howells
247330bdcfaSDavid Howells list_del_init(&peer->keepalive_link);
24847c810a7SDavid Howells if (!rxrpc_get_peer_maybe(peer, rxrpc_peer_get_keepalive))
249330bdcfaSDavid Howells continue;
250330bdcfaSDavid Howells
251*608aecd1SDavid Howells use = __rxrpc_use_local(peer->local, rxrpc_local_use_peer_keepalive);
2523dd9c8b5SDavid Howells spin_unlock(&rxnet->peer_hash_lock);
253330bdcfaSDavid Howells
254*608aecd1SDavid Howells if (use) {
255330bdcfaSDavid Howells keepalive_at = peer->last_tx_at + RXRPC_KEEPALIVE_TIME;
256330bdcfaSDavid Howells slot = keepalive_at - base;
257330bdcfaSDavid Howells _debug("%02x peer %u t=%d {%pISp}",
258330bdcfaSDavid Howells cursor, peer->debug_id, slot, &peer->srx.transport);
259330bdcfaSDavid Howells
260330bdcfaSDavid Howells if (keepalive_at <= base ||
261330bdcfaSDavid Howells keepalive_at > base + RXRPC_KEEPALIVE_TIME) {
262330bdcfaSDavid Howells rxrpc_send_keepalive(peer);
263330bdcfaSDavid Howells slot = RXRPC_KEEPALIVE_TIME;
264330bdcfaSDavid Howells }
265330bdcfaSDavid Howells
26604d36d74SDavid Howells /* A transmission to this peer occurred since last we
26704d36d74SDavid Howells * examined it so put it into the appropriate future
26804d36d74SDavid Howells * bucket.
269330bdcfaSDavid Howells */
270330bdcfaSDavid Howells slot += cursor;
271330bdcfaSDavid Howells slot &= mask;
2723dd9c8b5SDavid Howells spin_lock(&rxnet->peer_hash_lock);
273330bdcfaSDavid Howells list_add_tail(&peer->keepalive_link,
274330bdcfaSDavid Howells &rxnet->peer_keepalive[slot & mask]);
275*608aecd1SDavid Howells spin_unlock(&rxnet->peer_hash_lock);
2760fde882fSDavid Howells rxrpc_unuse_local(peer->local, rxrpc_local_unuse_peer_keepalive);
27704d36d74SDavid Howells }
278*608aecd1SDavid Howells rxrpc_put_peer(peer, rxrpc_peer_put_keepalive);
279*608aecd1SDavid Howells spin_lock(&rxnet->peer_hash_lock);
280330bdcfaSDavid Howells }
281330bdcfaSDavid Howells
2823dd9c8b5SDavid Howells spin_unlock(&rxnet->peer_hash_lock);
283330bdcfaSDavid Howells }
284330bdcfaSDavid Howells
285330bdcfaSDavid Howells /*
286ace45becSDavid Howells * Perform keep-alive pings with VERSION packets to keep any NAT alive.
287ace45becSDavid Howells */
rxrpc_peer_keepalive_worker(struct work_struct * work)288ace45becSDavid Howells void rxrpc_peer_keepalive_worker(struct work_struct *work)
289ace45becSDavid Howells {
290ace45becSDavid Howells struct rxrpc_net *rxnet =
291ace45becSDavid Howells container_of(work, struct rxrpc_net, peer_keepalive_work);
292330bdcfaSDavid Howells const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1;
293330bdcfaSDavid Howells time64_t base, now, delay;
294330bdcfaSDavid Howells u8 cursor, stop;
295330bdcfaSDavid Howells LIST_HEAD(collector);
296ace45becSDavid Howells
297330bdcfaSDavid Howells now = ktime_get_seconds();
298ace45becSDavid Howells base = rxnet->peer_keepalive_base;
299ace45becSDavid Howells cursor = rxnet->peer_keepalive_cursor;
300330bdcfaSDavid Howells _enter("%lld,%u", base - now, cursor);
301ace45becSDavid Howells
302330bdcfaSDavid Howells if (!rxnet->live)
303330bdcfaSDavid Howells return;
304ace45becSDavid Howells
305330bdcfaSDavid Howells /* Remove to a temporary list all the peers that are currently lodged
306330bdcfaSDavid Howells * in expired buckets plus all new peers.
307330bdcfaSDavid Howells *
308330bdcfaSDavid Howells * Everything in the bucket at the cursor is processed this
309330bdcfaSDavid Howells * second; the bucket at cursor + 1 goes at now + 1s and so
310330bdcfaSDavid Howells * on...
311ace45becSDavid Howells */
3123dd9c8b5SDavid Howells spin_lock(&rxnet->peer_hash_lock);
313330bdcfaSDavid Howells list_splice_init(&rxnet->peer_keepalive_new, &collector);
314ace45becSDavid Howells
315330bdcfaSDavid Howells stop = cursor + ARRAY_SIZE(rxnet->peer_keepalive);
316330bdcfaSDavid Howells while (base <= now && (s8)(cursor - stop) < 0) {
317330bdcfaSDavid Howells list_splice_tail_init(&rxnet->peer_keepalive[cursor & mask],
318330bdcfaSDavid Howells &collector);
319330bdcfaSDavid Howells base++;
320ace45becSDavid Howells cursor++;
321330bdcfaSDavid Howells }
322ace45becSDavid Howells
323330bdcfaSDavid Howells base = now;
3243dd9c8b5SDavid Howells spin_unlock(&rxnet->peer_hash_lock);
325330bdcfaSDavid Howells
326ace45becSDavid Howells rxnet->peer_keepalive_base = base;
327ace45becSDavid Howells rxnet->peer_keepalive_cursor = cursor;
328330bdcfaSDavid Howells rxrpc_peer_keepalive_dispatch(rxnet, &collector, base, cursor);
329330bdcfaSDavid Howells ASSERT(list_empty(&collector));
330330bdcfaSDavid Howells
331330bdcfaSDavid Howells /* Schedule the timer for the next occupied timeslot. */
332330bdcfaSDavid Howells cursor = rxnet->peer_keepalive_cursor;
333330bdcfaSDavid Howells stop = cursor + RXRPC_KEEPALIVE_TIME - 1;
334330bdcfaSDavid Howells for (; (s8)(cursor - stop) < 0; cursor++) {
335330bdcfaSDavid Howells if (!list_empty(&rxnet->peer_keepalive[cursor & mask]))
336330bdcfaSDavid Howells break;
337330bdcfaSDavid Howells base++;
338330bdcfaSDavid Howells }
339330bdcfaSDavid Howells
340330bdcfaSDavid Howells now = ktime_get_seconds();
341330bdcfaSDavid Howells delay = base - now;
342330bdcfaSDavid Howells if (delay < 1)
343330bdcfaSDavid Howells delay = 1;
344330bdcfaSDavid Howells delay *= HZ;
345330bdcfaSDavid Howells if (rxnet->live)
346ace45becSDavid Howells timer_reduce(&rxnet->peer_keepalive_timer, jiffies + delay);
347330bdcfaSDavid Howells
348ace45becSDavid Howells _leave("");
349ace45becSDavid Howells }
350