xref: /openbmc/linux/net/rxrpc/peer_event.c (revision 56d282d9)
1f66d7490SDavid Howells /* Peer event handling, typically ICMP messages.
28c3e34a4SDavid Howells  *
38c3e34a4SDavid Howells  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
48c3e34a4SDavid Howells  * Written by David Howells (dhowells@redhat.com)
58c3e34a4SDavid Howells  *
68c3e34a4SDavid Howells  * This program is free software; you can redistribute it and/or
78c3e34a4SDavid Howells  * modify it under the terms of the GNU General Public License
88c3e34a4SDavid Howells  * as published by the Free Software Foundation; either version
98c3e34a4SDavid Howells  * 2 of the License, or (at your option) any later version.
108c3e34a4SDavid Howells  */
118c3e34a4SDavid Howells 
128c3e34a4SDavid Howells #include <linux/module.h>
138c3e34a4SDavid Howells #include <linux/net.h>
148c3e34a4SDavid Howells #include <linux/skbuff.h>
158c3e34a4SDavid Howells #include <linux/errqueue.h>
168c3e34a4SDavid Howells #include <linux/udp.h>
178c3e34a4SDavid Howells #include <linux/in.h>
188c3e34a4SDavid Howells #include <linux/in6.h>
198c3e34a4SDavid Howells #include <linux/icmp.h>
208c3e34a4SDavid Howells #include <net/sock.h>
218c3e34a4SDavid Howells #include <net/af_rxrpc.h>
228c3e34a4SDavid Howells #include <net/ip.h>
238c3e34a4SDavid Howells #include "ar-internal.h"
248c3e34a4SDavid Howells 
25f66d7490SDavid Howells static void rxrpc_store_error(struct rxrpc_peer *, struct sock_exterr_skb *);
26f3344303SDavid Howells static void rxrpc_distribute_error(struct rxrpc_peer *, int,
27f3344303SDavid Howells 				   enum rxrpc_call_completion);
28f66d7490SDavid Howells 
298c3e34a4SDavid Howells /*
30be6e6707SDavid Howells  * Find the peer associated with an ICMP packet.
31be6e6707SDavid Howells  */
32be6e6707SDavid Howells static struct rxrpc_peer *rxrpc_lookup_peer_icmp_rcu(struct rxrpc_local *local,
33494337c9SDavid Howells 						     const struct sk_buff *skb,
34494337c9SDavid Howells 						     struct sockaddr_rxrpc *srx)
35be6e6707SDavid Howells {
36be6e6707SDavid Howells 	struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
37be6e6707SDavid Howells 
38be6e6707SDavid Howells 	_enter("");
39be6e6707SDavid Howells 
40494337c9SDavid Howells 	memset(srx, 0, sizeof(*srx));
41494337c9SDavid Howells 	srx->transport_type = local->srx.transport_type;
42494337c9SDavid Howells 	srx->transport_len = local->srx.transport_len;
43494337c9SDavid Howells 	srx->transport.family = local->srx.transport.family;
44be6e6707SDavid Howells 
45be6e6707SDavid Howells 	/* Can we see an ICMP4 packet on an ICMP6 listening socket?  and vice
46be6e6707SDavid Howells 	 * versa?
47be6e6707SDavid Howells 	 */
48494337c9SDavid Howells 	switch (srx->transport.family) {
49be6e6707SDavid Howells 	case AF_INET:
5046894a13SDavid Howells 		srx->transport_len = sizeof(srx->transport.sin);
5146894a13SDavid Howells 		srx->transport.family = AF_INET;
52494337c9SDavid Howells 		srx->transport.sin.sin_port = serr->port;
53be6e6707SDavid Howells 		switch (serr->ee.ee_origin) {
54be6e6707SDavid Howells 		case SO_EE_ORIGIN_ICMP:
55be6e6707SDavid Howells 			_net("Rx ICMP");
56494337c9SDavid Howells 			memcpy(&srx->transport.sin.sin_addr,
57be6e6707SDavid Howells 			       skb_network_header(skb) + serr->addr_offset,
58be6e6707SDavid Howells 			       sizeof(struct in_addr));
59be6e6707SDavid Howells 			break;
60be6e6707SDavid Howells 		case SO_EE_ORIGIN_ICMP6:
61be6e6707SDavid Howells 			_net("Rx ICMP6 on v4 sock");
62494337c9SDavid Howells 			memcpy(&srx->transport.sin.sin_addr,
63be6e6707SDavid Howells 			       skb_network_header(skb) + serr->addr_offset + 12,
64be6e6707SDavid Howells 			       sizeof(struct in_addr));
65be6e6707SDavid Howells 			break;
66be6e6707SDavid Howells 		default:
67494337c9SDavid Howells 			memcpy(&srx->transport.sin.sin_addr, &ip_hdr(skb)->saddr,
68be6e6707SDavid Howells 			       sizeof(struct in_addr));
69be6e6707SDavid Howells 			break;
70be6e6707SDavid Howells 		}
71be6e6707SDavid Howells 		break;
72be6e6707SDavid Howells 
73d1912747SDavid Howells #ifdef CONFIG_AF_RXRPC_IPV6
7475b54cb5SDavid Howells 	case AF_INET6:
7575b54cb5SDavid Howells 		switch (serr->ee.ee_origin) {
7675b54cb5SDavid Howells 		case SO_EE_ORIGIN_ICMP6:
7775b54cb5SDavid Howells 			_net("Rx ICMP6");
7846894a13SDavid Howells 			srx->transport.sin6.sin6_port = serr->port;
79494337c9SDavid Howells 			memcpy(&srx->transport.sin6.sin6_addr,
8075b54cb5SDavid Howells 			       skb_network_header(skb) + serr->addr_offset,
8175b54cb5SDavid Howells 			       sizeof(struct in6_addr));
8275b54cb5SDavid Howells 			break;
8375b54cb5SDavid Howells 		case SO_EE_ORIGIN_ICMP:
8475b54cb5SDavid Howells 			_net("Rx ICMP on v6 sock");
8546894a13SDavid Howells 			srx->transport_len = sizeof(srx->transport.sin);
8646894a13SDavid Howells 			srx->transport.family = AF_INET;
8746894a13SDavid Howells 			srx->transport.sin.sin_port = serr->port;
8846894a13SDavid Howells 			memcpy(&srx->transport.sin.sin_addr,
8975b54cb5SDavid Howells 			       skb_network_header(skb) + serr->addr_offset,
9075b54cb5SDavid Howells 			       sizeof(struct in_addr));
9175b54cb5SDavid Howells 			break;
9275b54cb5SDavid Howells 		default:
93494337c9SDavid Howells 			memcpy(&srx->transport.sin6.sin6_addr,
9475b54cb5SDavid Howells 			       &ipv6_hdr(skb)->saddr,
9575b54cb5SDavid Howells 			       sizeof(struct in6_addr));
9675b54cb5SDavid Howells 			break;
9775b54cb5SDavid Howells 		}
9875b54cb5SDavid Howells 		break;
99d1912747SDavid Howells #endif
10075b54cb5SDavid Howells 
101be6e6707SDavid Howells 	default:
102be6e6707SDavid Howells 		BUG();
103be6e6707SDavid Howells 	}
104be6e6707SDavid Howells 
105494337c9SDavid Howells 	return rxrpc_lookup_peer_rcu(local, srx);
106be6e6707SDavid Howells }
107be6e6707SDavid Howells 
108be6e6707SDavid Howells /*
1091a70c05bSDavid Howells  * Handle an MTU/fragmentation problem.
1101a70c05bSDavid Howells  */
1111a70c05bSDavid Howells static void rxrpc_adjust_mtu(struct rxrpc_peer *peer, struct sock_exterr_skb *serr)
1121a70c05bSDavid Howells {
1131a70c05bSDavid Howells 	u32 mtu = serr->ee.ee_info;
1141a70c05bSDavid Howells 
1151a70c05bSDavid Howells 	_net("Rx ICMP Fragmentation Needed (%d)", mtu);
1161a70c05bSDavid Howells 
1171a70c05bSDavid Howells 	/* wind down the local interface MTU */
1181a70c05bSDavid Howells 	if (mtu > 0 && peer->if_mtu == 65535 && mtu < peer->if_mtu) {
1191a70c05bSDavid Howells 		peer->if_mtu = mtu;
1201a70c05bSDavid Howells 		_net("I/F MTU %u", mtu);
1211a70c05bSDavid Howells 	}
1221a70c05bSDavid Howells 
1231a70c05bSDavid Howells 	if (mtu == 0) {
1241a70c05bSDavid Howells 		/* they didn't give us a size, estimate one */
1251a70c05bSDavid Howells 		mtu = peer->if_mtu;
1261a70c05bSDavid Howells 		if (mtu > 1500) {
1271a70c05bSDavid Howells 			mtu >>= 1;
1281a70c05bSDavid Howells 			if (mtu < 1500)
1291a70c05bSDavid Howells 				mtu = 1500;
1301a70c05bSDavid Howells 		} else {
1311a70c05bSDavid Howells 			mtu -= 100;
1321a70c05bSDavid Howells 			if (mtu < peer->hdrsize)
1331a70c05bSDavid Howells 				mtu = peer->hdrsize + 4;
1341a70c05bSDavid Howells 		}
1351a70c05bSDavid Howells 	}
1361a70c05bSDavid Howells 
1371a70c05bSDavid Howells 	if (mtu < peer->mtu) {
1381a70c05bSDavid Howells 		spin_lock_bh(&peer->lock);
1391a70c05bSDavid Howells 		peer->mtu = mtu;
1401a70c05bSDavid Howells 		peer->maxdata = peer->mtu - peer->hdrsize;
1411a70c05bSDavid Howells 		spin_unlock_bh(&peer->lock);
1421a70c05bSDavid Howells 		_net("Net MTU %u (maxdata %u)",
1431a70c05bSDavid Howells 		     peer->mtu, peer->maxdata);
1441a70c05bSDavid Howells 	}
1451a70c05bSDavid Howells }
1461a70c05bSDavid Howells 
1471a70c05bSDavid Howells /*
148f66d7490SDavid Howells  * Handle an error received on the local endpoint.
1498c3e34a4SDavid Howells  */
150abe89ef0SDavid Howells void rxrpc_error_report(struct sock *sk)
1518c3e34a4SDavid Howells {
1528c3e34a4SDavid Howells 	struct sock_exterr_skb *serr;
153494337c9SDavid Howells 	struct sockaddr_rxrpc srx;
1548c3e34a4SDavid Howells 	struct rxrpc_local *local = sk->sk_user_data;
1558c3e34a4SDavid Howells 	struct rxrpc_peer *peer;
1568c3e34a4SDavid Howells 	struct sk_buff *skb;
1578c3e34a4SDavid Howells 
1588c3e34a4SDavid Howells 	_enter("%p{%d}", sk, local->debug_id);
1598c3e34a4SDavid Howells 
16056d282d9SMarc Dionne 	/* Clear the outstanding error value on the socket so that it doesn't
16156d282d9SMarc Dionne 	 * cause kernel_sendmsg() to return it later.
16256d282d9SMarc Dionne 	 */
16356d282d9SMarc Dionne 	sock_error(sk);
16456d282d9SMarc Dionne 
1658c3e34a4SDavid Howells 	skb = sock_dequeue_err_skb(sk);
1668c3e34a4SDavid Howells 	if (!skb) {
1678c3e34a4SDavid Howells 		_leave("UDP socket errqueue empty");
1688c3e34a4SDavid Howells 		return;
1698c3e34a4SDavid Howells 	}
17071f3ca40SDavid Howells 	rxrpc_new_skb(skb, rxrpc_skb_rx_received);
1718c3e34a4SDavid Howells 	serr = SKB_EXT_ERR(skb);
1728c3e34a4SDavid Howells 	if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) {
1738c3e34a4SDavid Howells 		_leave("UDP empty message");
17471f3ca40SDavid Howells 		rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
1758c3e34a4SDavid Howells 		return;
1768c3e34a4SDavid Howells 	}
1778c3e34a4SDavid Howells 
178be6e6707SDavid Howells 	rcu_read_lock();
179494337c9SDavid Howells 	peer = rxrpc_lookup_peer_icmp_rcu(local, skb, &srx);
180be6e6707SDavid Howells 	if (peer && !rxrpc_get_peer_maybe(peer))
181be6e6707SDavid Howells 		peer = NULL;
182be6e6707SDavid Howells 	if (!peer) {
183be6e6707SDavid Howells 		rcu_read_unlock();
18471f3ca40SDavid Howells 		rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
1858c3e34a4SDavid Howells 		_leave(" [no peer]");
1868c3e34a4SDavid Howells 		return;
1878c3e34a4SDavid Howells 	}
1888c3e34a4SDavid Howells 
189494337c9SDavid Howells 	trace_rxrpc_rx_icmp(peer, &serr->ee, &srx);
190494337c9SDavid Howells 
1911a70c05bSDavid Howells 	if ((serr->ee.ee_origin == SO_EE_ORIGIN_ICMP &&
1928c3e34a4SDavid Howells 	     serr->ee.ee_type == ICMP_DEST_UNREACH &&
1931a70c05bSDavid Howells 	     serr->ee.ee_code == ICMP_FRAG_NEEDED)) {
1941a70c05bSDavid Howells 		rxrpc_adjust_mtu(peer, serr);
195be6e6707SDavid Howells 		rcu_read_unlock();
19671f3ca40SDavid Howells 		rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
1978c3e34a4SDavid Howells 		rxrpc_put_peer(peer);
198f66d7490SDavid Howells 		_leave(" [MTU update]");
199f66d7490SDavid Howells 		return;
2001a70c05bSDavid Howells 	}
201f66d7490SDavid Howells 
202f66d7490SDavid Howells 	rxrpc_store_error(peer, serr);
203f66d7490SDavid Howells 	rcu_read_unlock();
20471f3ca40SDavid Howells 	rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
2051890fea7SDavid Howells 	rxrpc_put_peer(peer);
206f66d7490SDavid Howells 
2078c3e34a4SDavid Howells 	_leave("");
2088c3e34a4SDavid Howells }
2098c3e34a4SDavid Howells 
2108c3e34a4SDavid Howells /*
211f66d7490SDavid Howells  * Map an error report to error codes on the peer record.
2128c3e34a4SDavid Howells  */
213f66d7490SDavid Howells static void rxrpc_store_error(struct rxrpc_peer *peer,
214f66d7490SDavid Howells 			      struct sock_exterr_skb *serr)
2158c3e34a4SDavid Howells {
216f3344303SDavid Howells 	enum rxrpc_call_completion compl = RXRPC_CALL_NETWORK_ERROR;
2178c3e34a4SDavid Howells 	struct sock_extended_err *ee;
2188c3e34a4SDavid Howells 	int err;
2198c3e34a4SDavid Howells 
2208c3e34a4SDavid Howells 	_enter("");
2218c3e34a4SDavid Howells 
2228c3e34a4SDavid Howells 	ee = &serr->ee;
2238c3e34a4SDavid Howells 
2248c3e34a4SDavid Howells 	err = ee->ee_errno;
2258c3e34a4SDavid Howells 
2268c3e34a4SDavid Howells 	switch (ee->ee_origin) {
2278c3e34a4SDavid Howells 	case SO_EE_ORIGIN_ICMP:
2288c3e34a4SDavid Howells 		switch (ee->ee_type) {
2298c3e34a4SDavid Howells 		case ICMP_DEST_UNREACH:
2308c3e34a4SDavid Howells 			switch (ee->ee_code) {
2318c3e34a4SDavid Howells 			case ICMP_NET_UNREACH:
2328c3e34a4SDavid Howells 				_net("Rx Received ICMP Network Unreachable");
2338c3e34a4SDavid Howells 				break;
2348c3e34a4SDavid Howells 			case ICMP_HOST_UNREACH:
2358c3e34a4SDavid Howells 				_net("Rx Received ICMP Host Unreachable");
2368c3e34a4SDavid Howells 				break;
2378c3e34a4SDavid Howells 			case ICMP_PORT_UNREACH:
2388c3e34a4SDavid Howells 				_net("Rx Received ICMP Port Unreachable");
2398c3e34a4SDavid Howells 				break;
2408c3e34a4SDavid Howells 			case ICMP_NET_UNKNOWN:
2418c3e34a4SDavid Howells 				_net("Rx Received ICMP Unknown Network");
2428c3e34a4SDavid Howells 				break;
2438c3e34a4SDavid Howells 			case ICMP_HOST_UNKNOWN:
2448c3e34a4SDavid Howells 				_net("Rx Received ICMP Unknown Host");
2458c3e34a4SDavid Howells 				break;
2468c3e34a4SDavid Howells 			default:
2478c3e34a4SDavid Howells 				_net("Rx Received ICMP DestUnreach code=%u",
2488c3e34a4SDavid Howells 				     ee->ee_code);
2498c3e34a4SDavid Howells 				break;
2508c3e34a4SDavid Howells 			}
2518c3e34a4SDavid Howells 			break;
2528c3e34a4SDavid Howells 
2538c3e34a4SDavid Howells 		case ICMP_TIME_EXCEEDED:
2548c3e34a4SDavid Howells 			_net("Rx Received ICMP TTL Exceeded");
2558c3e34a4SDavid Howells 			break;
2568c3e34a4SDavid Howells 
2578c3e34a4SDavid Howells 		default:
2588c3e34a4SDavid Howells 			_proto("Rx Received ICMP error { type=%u code=%u }",
2598c3e34a4SDavid Howells 			       ee->ee_type, ee->ee_code);
2608c3e34a4SDavid Howells 			break;
2618c3e34a4SDavid Howells 		}
2628c3e34a4SDavid Howells 		break;
2638c3e34a4SDavid Howells 
264f66d7490SDavid Howells 	case SO_EE_ORIGIN_NONE:
2658c3e34a4SDavid Howells 	case SO_EE_ORIGIN_LOCAL:
266fe77d5fcSDavid Howells 		_proto("Rx Received local error { error=%d }", err);
267f3344303SDavid Howells 		compl = RXRPC_CALL_LOCAL_ERROR;
2688c3e34a4SDavid Howells 		break;
2698c3e34a4SDavid Howells 
2708c3e34a4SDavid Howells 	case SO_EE_ORIGIN_ICMP6:
2718c3e34a4SDavid Howells 	default:
272fe77d5fcSDavid Howells 		_proto("Rx Received error report { orig=%u }", ee->ee_origin);
2738c3e34a4SDavid Howells 		break;
2748c3e34a4SDavid Howells 	}
2758c3e34a4SDavid Howells 
276f3344303SDavid Howells 	rxrpc_distribute_error(peer, err, compl);
277f66d7490SDavid Howells }
2788c3e34a4SDavid Howells 
279f66d7490SDavid Howells /*
280f3344303SDavid Howells  * Distribute an error that occurred on a peer.
281f66d7490SDavid Howells  */
282f3344303SDavid Howells static void rxrpc_distribute_error(struct rxrpc_peer *peer, int error,
283f3344303SDavid Howells 				   enum rxrpc_call_completion compl)
284f66d7490SDavid Howells {
285f66d7490SDavid Howells 	struct rxrpc_call *call;
2868c3e34a4SDavid Howells 
287f3344303SDavid Howells 	hlist_for_each_entry_rcu(call, &peer->error_targets, error_link) {
288e34d4234SDavid Howells 		rxrpc_see_call(call);
289f3344303SDavid Howells 		if (call->state < RXRPC_CALL_COMPLETE &&
290f3344303SDavid Howells 		    rxrpc_set_call_completion(call, compl, 0, -error))
291248f219cSDavid Howells 			rxrpc_notify_socket(call);
2928c3e34a4SDavid Howells 	}
2938c3e34a4SDavid Howells }
294cf1a6474SDavid Howells 
295cf1a6474SDavid Howells /*
296cf1a6474SDavid Howells  * Add RTT information to cache.  This is called in softirq mode and has
297cf1a6474SDavid Howells  * exclusive access to the peer RTT data.
298cf1a6474SDavid Howells  */
299cf1a6474SDavid Howells void rxrpc_peer_add_rtt(struct rxrpc_call *call, enum rxrpc_rtt_rx_trace why,
300cf1a6474SDavid Howells 			rxrpc_serial_t send_serial, rxrpc_serial_t resp_serial,
301cf1a6474SDavid Howells 			ktime_t send_time, ktime_t resp_time)
302cf1a6474SDavid Howells {
303cf1a6474SDavid Howells 	struct rxrpc_peer *peer = call->peer;
304cf1a6474SDavid Howells 	s64 rtt;
305cf1a6474SDavid Howells 	u64 sum = peer->rtt_sum, avg;
306cf1a6474SDavid Howells 	u8 cursor = peer->rtt_cursor, usage = peer->rtt_usage;
307cf1a6474SDavid Howells 
308cf1a6474SDavid Howells 	rtt = ktime_to_ns(ktime_sub(resp_time, send_time));
309cf1a6474SDavid Howells 	if (rtt < 0)
310cf1a6474SDavid Howells 		return;
311cf1a6474SDavid Howells 
312c1e15b49SDavid Howells 	spin_lock(&peer->rtt_input_lock);
313c1e15b49SDavid Howells 
314cf1a6474SDavid Howells 	/* Replace the oldest datum in the RTT buffer */
315cf1a6474SDavid Howells 	sum -= peer->rtt_cache[cursor];
316cf1a6474SDavid Howells 	sum += rtt;
317cf1a6474SDavid Howells 	peer->rtt_cache[cursor] = rtt;
318cf1a6474SDavid Howells 	peer->rtt_cursor = (cursor + 1) & (RXRPC_RTT_CACHE_SIZE - 1);
319cf1a6474SDavid Howells 	peer->rtt_sum = sum;
320cf1a6474SDavid Howells 	if (usage < RXRPC_RTT_CACHE_SIZE) {
321cf1a6474SDavid Howells 		usage++;
322cf1a6474SDavid Howells 		peer->rtt_usage = usage;
323cf1a6474SDavid Howells 	}
324cf1a6474SDavid Howells 
325c1e15b49SDavid Howells 	spin_unlock(&peer->rtt_input_lock);
326c1e15b49SDavid Howells 
327cf1a6474SDavid Howells 	/* Now recalculate the average */
328cf1a6474SDavid Howells 	if (usage == RXRPC_RTT_CACHE_SIZE) {
329cf1a6474SDavid Howells 		avg = sum / RXRPC_RTT_CACHE_SIZE;
330cf1a6474SDavid Howells 	} else {
331cf1a6474SDavid Howells 		avg = sum;
332cf1a6474SDavid Howells 		do_div(avg, usage);
333cf1a6474SDavid Howells 	}
334cf1a6474SDavid Howells 
335c1e15b49SDavid Howells 	/* Don't need to update this under lock */
336cf1a6474SDavid Howells 	peer->rtt = avg;
337cf1a6474SDavid Howells 	trace_rxrpc_rtt_rx(call, why, send_serial, resp_serial, rtt,
338cf1a6474SDavid Howells 			   usage, avg);
339cf1a6474SDavid Howells }
340ace45becSDavid Howells 
341ace45becSDavid Howells /*
342330bdcfaSDavid Howells  * Perform keep-alive pings.
343330bdcfaSDavid Howells  */
344330bdcfaSDavid Howells static void rxrpc_peer_keepalive_dispatch(struct rxrpc_net *rxnet,
345330bdcfaSDavid Howells 					  struct list_head *collector,
346330bdcfaSDavid Howells 					  time64_t base,
347330bdcfaSDavid Howells 					  u8 cursor)
348330bdcfaSDavid Howells {
349330bdcfaSDavid Howells 	struct rxrpc_peer *peer;
350330bdcfaSDavid Howells 	const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1;
351330bdcfaSDavid Howells 	time64_t keepalive_at;
352330bdcfaSDavid Howells 	int slot;
353330bdcfaSDavid Howells 
354330bdcfaSDavid Howells 	spin_lock_bh(&rxnet->peer_hash_lock);
355330bdcfaSDavid Howells 
356330bdcfaSDavid Howells 	while (!list_empty(collector)) {
357330bdcfaSDavid Howells 		peer = list_entry(collector->next,
358330bdcfaSDavid Howells 				  struct rxrpc_peer, keepalive_link);
359330bdcfaSDavid Howells 
360330bdcfaSDavid Howells 		list_del_init(&peer->keepalive_link);
361330bdcfaSDavid Howells 		if (!rxrpc_get_peer_maybe(peer))
362330bdcfaSDavid Howells 			continue;
363330bdcfaSDavid Howells 
364330bdcfaSDavid Howells 		spin_unlock_bh(&rxnet->peer_hash_lock);
365330bdcfaSDavid Howells 
366330bdcfaSDavid Howells 		keepalive_at = peer->last_tx_at + RXRPC_KEEPALIVE_TIME;
367330bdcfaSDavid Howells 		slot = keepalive_at - base;
368330bdcfaSDavid Howells 		_debug("%02x peer %u t=%d {%pISp}",
369330bdcfaSDavid Howells 		       cursor, peer->debug_id, slot, &peer->srx.transport);
370330bdcfaSDavid Howells 
371330bdcfaSDavid Howells 		if (keepalive_at <= base ||
372330bdcfaSDavid Howells 		    keepalive_at > base + RXRPC_KEEPALIVE_TIME) {
373330bdcfaSDavid Howells 			rxrpc_send_keepalive(peer);
374330bdcfaSDavid Howells 			slot = RXRPC_KEEPALIVE_TIME;
375330bdcfaSDavid Howells 		}
376330bdcfaSDavid Howells 
377330bdcfaSDavid Howells 		/* A transmission to this peer occurred since last we examined
378330bdcfaSDavid Howells 		 * it so put it into the appropriate future bucket.
379330bdcfaSDavid Howells 		 */
380330bdcfaSDavid Howells 		slot += cursor;
381330bdcfaSDavid Howells 		slot &= mask;
382330bdcfaSDavid Howells 		spin_lock_bh(&rxnet->peer_hash_lock);
383330bdcfaSDavid Howells 		list_add_tail(&peer->keepalive_link,
384330bdcfaSDavid Howells 			      &rxnet->peer_keepalive[slot & mask]);
385330bdcfaSDavid Howells 		rxrpc_put_peer(peer);
386330bdcfaSDavid Howells 	}
387330bdcfaSDavid Howells 
388330bdcfaSDavid Howells 	spin_unlock_bh(&rxnet->peer_hash_lock);
389330bdcfaSDavid Howells }
390330bdcfaSDavid Howells 
391330bdcfaSDavid Howells /*
392ace45becSDavid Howells  * Perform keep-alive pings with VERSION packets to keep any NAT alive.
393ace45becSDavid Howells  */
394ace45becSDavid Howells void rxrpc_peer_keepalive_worker(struct work_struct *work)
395ace45becSDavid Howells {
396ace45becSDavid Howells 	struct rxrpc_net *rxnet =
397ace45becSDavid Howells 		container_of(work, struct rxrpc_net, peer_keepalive_work);
398330bdcfaSDavid Howells 	const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1;
399330bdcfaSDavid Howells 	time64_t base, now, delay;
400330bdcfaSDavid Howells 	u8 cursor, stop;
401330bdcfaSDavid Howells 	LIST_HEAD(collector);
402ace45becSDavid Howells 
403330bdcfaSDavid Howells 	now = ktime_get_seconds();
404ace45becSDavid Howells 	base = rxnet->peer_keepalive_base;
405ace45becSDavid Howells 	cursor = rxnet->peer_keepalive_cursor;
406330bdcfaSDavid Howells 	_enter("%lld,%u", base - now, cursor);
407ace45becSDavid Howells 
408330bdcfaSDavid Howells 	if (!rxnet->live)
409330bdcfaSDavid Howells 		return;
410ace45becSDavid Howells 
411330bdcfaSDavid Howells 	/* Remove to a temporary list all the peers that are currently lodged
412330bdcfaSDavid Howells 	 * in expired buckets plus all new peers.
413330bdcfaSDavid Howells 	 *
414330bdcfaSDavid Howells 	 * Everything in the bucket at the cursor is processed this
415330bdcfaSDavid Howells 	 * second; the bucket at cursor + 1 goes at now + 1s and so
416330bdcfaSDavid Howells 	 * on...
417ace45becSDavid Howells 	 */
418ace45becSDavid Howells 	spin_lock_bh(&rxnet->peer_hash_lock);
419330bdcfaSDavid Howells 	list_splice_init(&rxnet->peer_keepalive_new, &collector);
420ace45becSDavid Howells 
421330bdcfaSDavid Howells 	stop = cursor + ARRAY_SIZE(rxnet->peer_keepalive);
422330bdcfaSDavid Howells 	while (base <= now && (s8)(cursor - stop) < 0) {
423330bdcfaSDavid Howells 		list_splice_tail_init(&rxnet->peer_keepalive[cursor & mask],
424330bdcfaSDavid Howells 				      &collector);
425330bdcfaSDavid Howells 		base++;
426ace45becSDavid Howells 		cursor++;
427330bdcfaSDavid Howells 	}
428ace45becSDavid Howells 
429330bdcfaSDavid Howells 	base = now;
430330bdcfaSDavid Howells 	spin_unlock_bh(&rxnet->peer_hash_lock);
431330bdcfaSDavid Howells 
432ace45becSDavid Howells 	rxnet->peer_keepalive_base = base;
433ace45becSDavid Howells 	rxnet->peer_keepalive_cursor = cursor;
434330bdcfaSDavid Howells 	rxrpc_peer_keepalive_dispatch(rxnet, &collector, base, cursor);
435330bdcfaSDavid Howells 	ASSERT(list_empty(&collector));
436330bdcfaSDavid Howells 
437330bdcfaSDavid Howells 	/* Schedule the timer for the next occupied timeslot. */
438330bdcfaSDavid Howells 	cursor = rxnet->peer_keepalive_cursor;
439330bdcfaSDavid Howells 	stop = cursor + RXRPC_KEEPALIVE_TIME - 1;
440330bdcfaSDavid Howells 	for (; (s8)(cursor - stop) < 0; cursor++) {
441330bdcfaSDavid Howells 		if (!list_empty(&rxnet->peer_keepalive[cursor & mask]))
442330bdcfaSDavid Howells 			break;
443330bdcfaSDavid Howells 		base++;
444330bdcfaSDavid Howells 	}
445330bdcfaSDavid Howells 
446330bdcfaSDavid Howells 	now = ktime_get_seconds();
447330bdcfaSDavid Howells 	delay = base - now;
448330bdcfaSDavid Howells 	if (delay < 1)
449330bdcfaSDavid Howells 		delay = 1;
450330bdcfaSDavid Howells 	delay *= HZ;
451330bdcfaSDavid Howells 	if (rxnet->live)
452ace45becSDavid Howells 		timer_reduce(&rxnet->peer_keepalive_timer, jiffies + delay);
453330bdcfaSDavid Howells 
454ace45becSDavid Howells 	_leave("");
455ace45becSDavid Howells }
456