xref: /openbmc/linux/net/rxrpc/peer_event.c (revision b6c66c43)
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 
21f66d7490SDavid Howells static void rxrpc_store_error(struct rxrpc_peer *, struct sock_exterr_skb *);
22f3344303SDavid Howells static void rxrpc_distribute_error(struct rxrpc_peer *, int,
23f3344303SDavid Howells 				   enum rxrpc_call_completion);
24f66d7490SDavid Howells 
258c3e34a4SDavid Howells /*
26ac56a0b4SDavid Howells  * Find the peer associated with a local error.
27ac56a0b4SDavid Howells  */
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 
41*b6c66c43SDavid Howells 	/* Can we see an ICMP4 packet on an ICMP6 listening socket?  and vice
42*b6c66c43SDavid Howells 	 * versa?
43*b6c66c43SDavid 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:
51be6e6707SDavid Howells 			_net("Rx ICMP");
52494337c9SDavid Howells 			memcpy(&srx->transport.sin.sin_addr,
53be6e6707SDavid Howells 			       skb_network_header(skb) + serr->addr_offset,
54be6e6707SDavid Howells 			       sizeof(struct in_addr));
55be6e6707SDavid Howells 			break;
56be6e6707SDavid Howells 		case SO_EE_ORIGIN_ICMP6:
57be6e6707SDavid Howells 			_net("Rx ICMP6 on v4 sock");
58494337c9SDavid Howells 			memcpy(&srx->transport.sin.sin_addr,
59be6e6707SDavid Howells 			       skb_network_header(skb) + serr->addr_offset + 12,
60be6e6707SDavid Howells 			       sizeof(struct in_addr));
61be6e6707SDavid Howells 			break;
62be6e6707SDavid Howells 		default:
63494337c9SDavid Howells 			memcpy(&srx->transport.sin.sin_addr, &ip_hdr(skb)->saddr,
64be6e6707SDavid Howells 			       sizeof(struct in_addr));
65be6e6707SDavid Howells 			break;
66be6e6707SDavid Howells 		}
67be6e6707SDavid Howells 		break;
68be6e6707SDavid Howells 
69d1912747SDavid Howells #ifdef CONFIG_AF_RXRPC_IPV6
7075b54cb5SDavid Howells 	case AF_INET6:
7175b54cb5SDavid Howells 		switch (serr->ee.ee_origin) {
7275b54cb5SDavid Howells 		case SO_EE_ORIGIN_ICMP6:
7375b54cb5SDavid Howells 			_net("Rx ICMP6");
7446894a13SDavid Howells 			srx->transport.sin6.sin6_port = serr->port;
75494337c9SDavid Howells 			memcpy(&srx->transport.sin6.sin6_addr,
7675b54cb5SDavid Howells 			       skb_network_header(skb) + serr->addr_offset,
7775b54cb5SDavid Howells 			       sizeof(struct in6_addr));
7875b54cb5SDavid Howells 			break;
7975b54cb5SDavid Howells 		case SO_EE_ORIGIN_ICMP:
8075b54cb5SDavid Howells 			_net("Rx ICMP on v6 sock");
8146894a13SDavid Howells 			srx->transport_len = sizeof(srx->transport.sin);
8246894a13SDavid Howells 			srx->transport.family = AF_INET;
8346894a13SDavid Howells 			srx->transport.sin.sin_port = serr->port;
8446894a13SDavid Howells 			memcpy(&srx->transport.sin.sin_addr,
8575b54cb5SDavid Howells 			       skb_network_header(skb) + serr->addr_offset,
8675b54cb5SDavid Howells 			       sizeof(struct in_addr));
8775b54cb5SDavid Howells 			break;
8875b54cb5SDavid Howells 		default:
89494337c9SDavid Howells 			memcpy(&srx->transport.sin6.sin6_addr,
9075b54cb5SDavid Howells 			       &ipv6_hdr(skb)->saddr,
9175b54cb5SDavid Howells 			       sizeof(struct in6_addr));
9275b54cb5SDavid Howells 			break;
9375b54cb5SDavid Howells 		}
9475b54cb5SDavid Howells 		break;
95d1912747SDavid Howells #endif
9675b54cb5SDavid Howells 
97be6e6707SDavid Howells 	default:
98be6e6707SDavid Howells 		BUG();
99be6e6707SDavid Howells 	}
100be6e6707SDavid Howells 
101494337c9SDavid Howells 	return rxrpc_lookup_peer_rcu(local, srx);
102be6e6707SDavid Howells }
103be6e6707SDavid Howells 
104be6e6707SDavid Howells /*
1051a70c05bSDavid Howells  * Handle an MTU/fragmentation problem.
1061a70c05bSDavid Howells  */
107ac56a0b4SDavid Howells static void rxrpc_adjust_mtu(struct rxrpc_peer *peer, unsigned int mtu)
1081a70c05bSDavid Howells {
1091a70c05bSDavid Howells 	_net("Rx ICMP Fragmentation Needed (%d)", mtu);
1101a70c05bSDavid Howells 
1111a70c05bSDavid Howells 	/* wind down the local interface MTU */
1121a70c05bSDavid Howells 	if (mtu > 0 && peer->if_mtu == 65535 && mtu < peer->if_mtu) {
1131a70c05bSDavid Howells 		peer->if_mtu = mtu;
1141a70c05bSDavid Howells 		_net("I/F MTU %u", mtu);
1151a70c05bSDavid Howells 	}
1161a70c05bSDavid Howells 
1171a70c05bSDavid Howells 	if (mtu == 0) {
1181a70c05bSDavid Howells 		/* they didn't give us a size, estimate one */
1191a70c05bSDavid Howells 		mtu = peer->if_mtu;
1201a70c05bSDavid Howells 		if (mtu > 1500) {
1211a70c05bSDavid Howells 			mtu >>= 1;
1221a70c05bSDavid Howells 			if (mtu < 1500)
1231a70c05bSDavid Howells 				mtu = 1500;
1241a70c05bSDavid Howells 		} else {
1251a70c05bSDavid Howells 			mtu -= 100;
1261a70c05bSDavid Howells 			if (mtu < peer->hdrsize)
1271a70c05bSDavid Howells 				mtu = peer->hdrsize + 4;
1281a70c05bSDavid Howells 		}
1291a70c05bSDavid Howells 	}
1301a70c05bSDavid Howells 
1311a70c05bSDavid Howells 	if (mtu < peer->mtu) {
1321a70c05bSDavid Howells 		spin_lock_bh(&peer->lock);
1331a70c05bSDavid Howells 		peer->mtu = mtu;
1341a70c05bSDavid Howells 		peer->maxdata = peer->mtu - peer->hdrsize;
1351a70c05bSDavid Howells 		spin_unlock_bh(&peer->lock);
1361a70c05bSDavid Howells 		_net("Net MTU %u (maxdata %u)",
1371a70c05bSDavid Howells 		     peer->mtu, peer->maxdata);
1381a70c05bSDavid Howells 	}
1391a70c05bSDavid Howells }
1401a70c05bSDavid Howells 
1411a70c05bSDavid Howells /*
142f66d7490SDavid Howells  * Handle an error received on the local endpoint.
1438c3e34a4SDavid Howells  */
144abe89ef0SDavid Howells void rxrpc_error_report(struct sock *sk)
1458c3e34a4SDavid Howells {
1468c3e34a4SDavid Howells 	struct sock_exterr_skb *serr;
147494337c9SDavid Howells 	struct sockaddr_rxrpc srx;
1482ca4f6caSEric Dumazet 	struct rxrpc_local *local;
149ac56a0b4SDavid Howells 	struct rxrpc_peer *peer = NULL;
1508c3e34a4SDavid Howells 	struct sk_buff *skb;
1518c3e34a4SDavid Howells 
1522ca4f6caSEric Dumazet 	rcu_read_lock();
1532ca4f6caSEric Dumazet 	local = rcu_dereference_sk_user_data(sk);
1542ca4f6caSEric Dumazet 	if (unlikely(!local)) {
1552ca4f6caSEric Dumazet 		rcu_read_unlock();
156f0308fb0SDavid Howells 		return;
1572ca4f6caSEric Dumazet 	}
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) {
1672ca4f6caSEric Dumazet 		rcu_read_unlock();
1688c3e34a4SDavid Howells 		_leave("UDP socket errqueue empty");
1698c3e34a4SDavid Howells 		return;
1708c3e34a4SDavid Howells 	}
171987db9f7SDavid Howells 	rxrpc_new_skb(skb, rxrpc_skb_received);
1728c3e34a4SDavid Howells 	serr = SKB_EXT_ERR(skb);
173*b6c66c43SDavid Howells 	if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) {
174*b6c66c43SDavid Howells 		_leave("UDP empty message");
175*b6c66c43SDavid Howells 		rcu_read_unlock();
176*b6c66c43SDavid Howells 		rxrpc_free_skb(skb, rxrpc_skb_freed);
177*b6c66c43SDavid Howells 		return;
178*b6c66c43SDavid Howells 	}
1798c3e34a4SDavid Howells 
180ac56a0b4SDavid Howells 	peer = rxrpc_lookup_peer_local_rcu(local, skb, &srx);
181be6e6707SDavid Howells 	if (peer && !rxrpc_get_peer_maybe(peer))
182be6e6707SDavid Howells 		peer = NULL;
183*b6c66c43SDavid Howells 	if (!peer) {
184*b6c66c43SDavid Howells 		rcu_read_unlock();
185*b6c66c43SDavid Howells 		rxrpc_free_skb(skb, rxrpc_skb_freed);
186*b6c66c43SDavid Howells 		_leave(" [no peer]");
187*b6c66c43SDavid Howells 		return;
1881a70c05bSDavid Howells 	}
189f66d7490SDavid Howells 
190*b6c66c43SDavid Howells 	trace_rxrpc_rx_icmp(peer, &serr->ee, &srx);
191*b6c66c43SDavid Howells 
192*b6c66c43SDavid Howells 	if ((serr->ee.ee_origin == SO_EE_ORIGIN_ICMP &&
193*b6c66c43SDavid Howells 	     serr->ee.ee_type == ICMP_DEST_UNREACH &&
194*b6c66c43SDavid Howells 	     serr->ee.ee_code == ICMP_FRAG_NEEDED)) {
195*b6c66c43SDavid Howells 		rxrpc_adjust_mtu(peer, serr->ee.ee_info);
196*b6c66c43SDavid Howells 		goto out;
197*b6c66c43SDavid Howells 	}
198*b6c66c43SDavid Howells 
199*b6c66c43SDavid Howells 	rxrpc_store_error(peer, serr);
200*b6c66c43SDavid Howells out:
201f66d7490SDavid Howells 	rcu_read_unlock();
202987db9f7SDavid Howells 	rxrpc_free_skb(skb, rxrpc_skb_freed);
2031890fea7SDavid Howells 	rxrpc_put_peer(peer);
204*b6c66c43SDavid Howells 
2058c3e34a4SDavid Howells 	_leave("");
2068c3e34a4SDavid Howells }
2078c3e34a4SDavid Howells 
2088c3e34a4SDavid Howells /*
209f66d7490SDavid Howells  * Map an error report to error codes on the peer record.
2108c3e34a4SDavid Howells  */
211f66d7490SDavid Howells static void rxrpc_store_error(struct rxrpc_peer *peer,
212f66d7490SDavid Howells 			      struct sock_exterr_skb *serr)
2138c3e34a4SDavid Howells {
214f3344303SDavid Howells 	enum rxrpc_call_completion compl = RXRPC_CALL_NETWORK_ERROR;
2158c3e34a4SDavid Howells 	struct sock_extended_err *ee;
2168c3e34a4SDavid Howells 	int err;
2178c3e34a4SDavid Howells 
2188c3e34a4SDavid Howells 	_enter("");
2198c3e34a4SDavid Howells 
2208c3e34a4SDavid Howells 	ee = &serr->ee;
2218c3e34a4SDavid Howells 
2228c3e34a4SDavid Howells 	err = ee->ee_errno;
2238c3e34a4SDavid Howells 
2248c3e34a4SDavid Howells 	switch (ee->ee_origin) {
2258c3e34a4SDavid Howells 	case SO_EE_ORIGIN_ICMP:
2268c3e34a4SDavid Howells 		switch (ee->ee_type) {
2278c3e34a4SDavid Howells 		case ICMP_DEST_UNREACH:
2288c3e34a4SDavid Howells 			switch (ee->ee_code) {
2298c3e34a4SDavid Howells 			case ICMP_NET_UNREACH:
2308c3e34a4SDavid Howells 				_net("Rx Received ICMP Network Unreachable");
2318c3e34a4SDavid Howells 				break;
2328c3e34a4SDavid Howells 			case ICMP_HOST_UNREACH:
2338c3e34a4SDavid Howells 				_net("Rx Received ICMP Host Unreachable");
2348c3e34a4SDavid Howells 				break;
2358c3e34a4SDavid Howells 			case ICMP_PORT_UNREACH:
2368c3e34a4SDavid Howells 				_net("Rx Received ICMP Port Unreachable");
2378c3e34a4SDavid Howells 				break;
2388c3e34a4SDavid Howells 			case ICMP_NET_UNKNOWN:
2398c3e34a4SDavid Howells 				_net("Rx Received ICMP Unknown Network");
2408c3e34a4SDavid Howells 				break;
2418c3e34a4SDavid Howells 			case ICMP_HOST_UNKNOWN:
2428c3e34a4SDavid Howells 				_net("Rx Received ICMP Unknown Host");
2438c3e34a4SDavid Howells 				break;
2448c3e34a4SDavid Howells 			default:
2458c3e34a4SDavid Howells 				_net("Rx Received ICMP DestUnreach code=%u",
2468c3e34a4SDavid Howells 				     ee->ee_code);
2478c3e34a4SDavid Howells 				break;
2488c3e34a4SDavid Howells 			}
2498c3e34a4SDavid Howells 			break;
2508c3e34a4SDavid Howells 
2518c3e34a4SDavid Howells 		case ICMP_TIME_EXCEEDED:
2528c3e34a4SDavid Howells 			_net("Rx Received ICMP TTL Exceeded");
2538c3e34a4SDavid Howells 			break;
2548c3e34a4SDavid Howells 
2558c3e34a4SDavid Howells 		default:
2568c3e34a4SDavid Howells 			_proto("Rx Received ICMP error { type=%u code=%u }",
2578c3e34a4SDavid Howells 			       ee->ee_type, ee->ee_code);
2588c3e34a4SDavid Howells 			break;
2598c3e34a4SDavid Howells 		}
2608c3e34a4SDavid Howells 		break;
2618c3e34a4SDavid Howells 
262f66d7490SDavid Howells 	case SO_EE_ORIGIN_NONE:
2638c3e34a4SDavid Howells 	case SO_EE_ORIGIN_LOCAL:
264fe77d5fcSDavid Howells 		_proto("Rx Received local error { error=%d }", err);
265f3344303SDavid Howells 		compl = RXRPC_CALL_LOCAL_ERROR;
2668c3e34a4SDavid Howells 		break;
2678c3e34a4SDavid Howells 
2688c3e34a4SDavid Howells 	case SO_EE_ORIGIN_ICMP6:
26923e2db31SDavid Howells 		if (err == EACCES)
27023e2db31SDavid Howells 			err = EHOSTUNREACH;
271df561f66SGustavo A. R. Silva 		fallthrough;
2728c3e34a4SDavid Howells 	default:
273fe77d5fcSDavid Howells 		_proto("Rx Received error report { orig=%u }", ee->ee_origin);
2748c3e34a4SDavid Howells 		break;
2758c3e34a4SDavid Howells 	}
2768c3e34a4SDavid Howells 
277f3344303SDavid Howells 	rxrpc_distribute_error(peer, err, compl);
278f66d7490SDavid Howells }
2798c3e34a4SDavid Howells 
280f66d7490SDavid Howells /*
281f3344303SDavid Howells  * Distribute an error that occurred on a peer.
282f66d7490SDavid Howells  */
283f3344303SDavid Howells static void rxrpc_distribute_error(struct rxrpc_peer *peer, int error,
284f3344303SDavid Howells 				   enum rxrpc_call_completion compl)
285f66d7490SDavid Howells {
286f66d7490SDavid Howells 	struct rxrpc_call *call;
2878c3e34a4SDavid Howells 
288f3344303SDavid Howells 	hlist_for_each_entry_rcu(call, &peer->error_targets, error_link) {
289e34d4234SDavid Howells 		rxrpc_see_call(call);
2905ac0d622SDavid Howells 		rxrpc_set_call_completion(call, compl, 0, -error);
2918c3e34a4SDavid Howells 	}
2928c3e34a4SDavid Howells }
293cf1a6474SDavid Howells 
294cf1a6474SDavid Howells /*
295330bdcfaSDavid Howells  * Perform keep-alive pings.
296330bdcfaSDavid Howells  */
297330bdcfaSDavid Howells static void rxrpc_peer_keepalive_dispatch(struct rxrpc_net *rxnet,
298330bdcfaSDavid Howells 					  struct list_head *collector,
299330bdcfaSDavid Howells 					  time64_t base,
300330bdcfaSDavid Howells 					  u8 cursor)
301330bdcfaSDavid Howells {
302330bdcfaSDavid Howells 	struct rxrpc_peer *peer;
303330bdcfaSDavid Howells 	const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1;
304330bdcfaSDavid Howells 	time64_t keepalive_at;
305330bdcfaSDavid Howells 	int slot;
306330bdcfaSDavid Howells 
307330bdcfaSDavid Howells 	spin_lock_bh(&rxnet->peer_hash_lock);
308330bdcfaSDavid Howells 
309330bdcfaSDavid Howells 	while (!list_empty(collector)) {
310330bdcfaSDavid Howells 		peer = list_entry(collector->next,
311330bdcfaSDavid Howells 				  struct rxrpc_peer, keepalive_link);
312330bdcfaSDavid Howells 
313330bdcfaSDavid Howells 		list_del_init(&peer->keepalive_link);
314330bdcfaSDavid Howells 		if (!rxrpc_get_peer_maybe(peer))
315330bdcfaSDavid Howells 			continue;
316330bdcfaSDavid Howells 
31704d36d74SDavid Howells 		if (__rxrpc_use_local(peer->local)) {
318330bdcfaSDavid Howells 			spin_unlock_bh(&rxnet->peer_hash_lock);
319330bdcfaSDavid Howells 
320330bdcfaSDavid Howells 			keepalive_at = peer->last_tx_at + RXRPC_KEEPALIVE_TIME;
321330bdcfaSDavid Howells 			slot = keepalive_at - base;
322330bdcfaSDavid Howells 			_debug("%02x peer %u t=%d {%pISp}",
323330bdcfaSDavid Howells 			       cursor, peer->debug_id, slot, &peer->srx.transport);
324330bdcfaSDavid Howells 
325330bdcfaSDavid Howells 			if (keepalive_at <= base ||
326330bdcfaSDavid Howells 			    keepalive_at > base + RXRPC_KEEPALIVE_TIME) {
327330bdcfaSDavid Howells 				rxrpc_send_keepalive(peer);
328330bdcfaSDavid Howells 				slot = RXRPC_KEEPALIVE_TIME;
329330bdcfaSDavid Howells 			}
330330bdcfaSDavid Howells 
33104d36d74SDavid Howells 			/* A transmission to this peer occurred since last we
33204d36d74SDavid Howells 			 * examined it so put it into the appropriate future
33304d36d74SDavid Howells 			 * bucket.
334330bdcfaSDavid Howells 			 */
335330bdcfaSDavid Howells 			slot += cursor;
336330bdcfaSDavid Howells 			slot &= mask;
337330bdcfaSDavid Howells 			spin_lock_bh(&rxnet->peer_hash_lock);
338330bdcfaSDavid Howells 			list_add_tail(&peer->keepalive_link,
339330bdcfaSDavid Howells 				      &rxnet->peer_keepalive[slot & mask]);
34004d36d74SDavid Howells 			rxrpc_unuse_local(peer->local);
34104d36d74SDavid Howells 		}
34260034d3dSDavid Howells 		rxrpc_put_peer_locked(peer);
343330bdcfaSDavid Howells 	}
344330bdcfaSDavid Howells 
345330bdcfaSDavid Howells 	spin_unlock_bh(&rxnet->peer_hash_lock);
346330bdcfaSDavid Howells }
347330bdcfaSDavid Howells 
348330bdcfaSDavid Howells /*
349ace45becSDavid Howells  * Perform keep-alive pings with VERSION packets to keep any NAT alive.
350ace45becSDavid Howells  */
351ace45becSDavid Howells void rxrpc_peer_keepalive_worker(struct work_struct *work)
352ace45becSDavid Howells {
353ace45becSDavid Howells 	struct rxrpc_net *rxnet =
354ace45becSDavid Howells 		container_of(work, struct rxrpc_net, peer_keepalive_work);
355330bdcfaSDavid Howells 	const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1;
356330bdcfaSDavid Howells 	time64_t base, now, delay;
357330bdcfaSDavid Howells 	u8 cursor, stop;
358330bdcfaSDavid Howells 	LIST_HEAD(collector);
359ace45becSDavid Howells 
360330bdcfaSDavid Howells 	now = ktime_get_seconds();
361ace45becSDavid Howells 	base = rxnet->peer_keepalive_base;
362ace45becSDavid Howells 	cursor = rxnet->peer_keepalive_cursor;
363330bdcfaSDavid Howells 	_enter("%lld,%u", base - now, cursor);
364ace45becSDavid Howells 
365330bdcfaSDavid Howells 	if (!rxnet->live)
366330bdcfaSDavid Howells 		return;
367ace45becSDavid Howells 
368330bdcfaSDavid Howells 	/* Remove to a temporary list all the peers that are currently lodged
369330bdcfaSDavid Howells 	 * in expired buckets plus all new peers.
370330bdcfaSDavid Howells 	 *
371330bdcfaSDavid Howells 	 * Everything in the bucket at the cursor is processed this
372330bdcfaSDavid Howells 	 * second; the bucket at cursor + 1 goes at now + 1s and so
373330bdcfaSDavid Howells 	 * on...
374ace45becSDavid Howells 	 */
375ace45becSDavid Howells 	spin_lock_bh(&rxnet->peer_hash_lock);
376330bdcfaSDavid Howells 	list_splice_init(&rxnet->peer_keepalive_new, &collector);
377ace45becSDavid Howells 
378330bdcfaSDavid Howells 	stop = cursor + ARRAY_SIZE(rxnet->peer_keepalive);
379330bdcfaSDavid Howells 	while (base <= now && (s8)(cursor - stop) < 0) {
380330bdcfaSDavid Howells 		list_splice_tail_init(&rxnet->peer_keepalive[cursor & mask],
381330bdcfaSDavid Howells 				      &collector);
382330bdcfaSDavid Howells 		base++;
383ace45becSDavid Howells 		cursor++;
384330bdcfaSDavid Howells 	}
385ace45becSDavid Howells 
386330bdcfaSDavid Howells 	base = now;
387330bdcfaSDavid Howells 	spin_unlock_bh(&rxnet->peer_hash_lock);
388330bdcfaSDavid Howells 
389ace45becSDavid Howells 	rxnet->peer_keepalive_base = base;
390ace45becSDavid Howells 	rxnet->peer_keepalive_cursor = cursor;
391330bdcfaSDavid Howells 	rxrpc_peer_keepalive_dispatch(rxnet, &collector, base, cursor);
392330bdcfaSDavid Howells 	ASSERT(list_empty(&collector));
393330bdcfaSDavid Howells 
394330bdcfaSDavid Howells 	/* Schedule the timer for the next occupied timeslot. */
395330bdcfaSDavid Howells 	cursor = rxnet->peer_keepalive_cursor;
396330bdcfaSDavid Howells 	stop = cursor + RXRPC_KEEPALIVE_TIME - 1;
397330bdcfaSDavid Howells 	for (; (s8)(cursor - stop) < 0; cursor++) {
398330bdcfaSDavid Howells 		if (!list_empty(&rxnet->peer_keepalive[cursor & mask]))
399330bdcfaSDavid Howells 			break;
400330bdcfaSDavid Howells 		base++;
401330bdcfaSDavid Howells 	}
402330bdcfaSDavid Howells 
403330bdcfaSDavid Howells 	now = ktime_get_seconds();
404330bdcfaSDavid Howells 	delay = base - now;
405330bdcfaSDavid Howells 	if (delay < 1)
406330bdcfaSDavid Howells 		delay = 1;
407330bdcfaSDavid Howells 	delay *= HZ;
408330bdcfaSDavid Howells 	if (rxnet->live)
409ace45becSDavid Howells 		timer_reduce(&rxnet->peer_keepalive_timer, jiffies + delay);
410330bdcfaSDavid Howells 
411ace45becSDavid Howells 	_leave("");
412ace45becSDavid Howells }
413