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 *); 26f66d7490SDavid Howells 278c3e34a4SDavid Howells /* 28be6e6707SDavid Howells * Find the peer associated with an ICMP packet. 29be6e6707SDavid Howells */ 30be6e6707SDavid Howells static struct rxrpc_peer *rxrpc_lookup_peer_icmp_rcu(struct rxrpc_local *local, 31494337c9SDavid Howells const struct sk_buff *skb, 32494337c9SDavid Howells struct sockaddr_rxrpc *srx) 33be6e6707SDavid Howells { 34be6e6707SDavid Howells struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); 35be6e6707SDavid Howells 36be6e6707SDavid Howells _enter(""); 37be6e6707SDavid Howells 38494337c9SDavid Howells memset(srx, 0, sizeof(*srx)); 39494337c9SDavid Howells srx->transport_type = local->srx.transport_type; 40494337c9SDavid Howells srx->transport_len = local->srx.transport_len; 41494337c9SDavid Howells srx->transport.family = local->srx.transport.family; 42be6e6707SDavid Howells 43be6e6707SDavid Howells /* Can we see an ICMP4 packet on an ICMP6 listening socket? and vice 44be6e6707SDavid Howells * versa? 45be6e6707SDavid Howells */ 46494337c9SDavid Howells switch (srx->transport.family) { 47be6e6707SDavid Howells case 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: 71494337c9SDavid Howells srx->transport.sin6.sin6_port = serr->port; 7275b54cb5SDavid Howells switch (serr->ee.ee_origin) { 7375b54cb5SDavid Howells case SO_EE_ORIGIN_ICMP6: 7475b54cb5SDavid Howells _net("Rx ICMP6"); 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"); 81494337c9SDavid Howells srx->transport.sin6.sin6_addr.s6_addr32[0] = 0; 82494337c9SDavid Howells srx->transport.sin6.sin6_addr.s6_addr32[1] = 0; 83494337c9SDavid Howells srx->transport.sin6.sin6_addr.s6_addr32[2] = htonl(0xffff); 84494337c9SDavid Howells memcpy(srx->transport.sin6.sin6_addr.s6_addr + 12, 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 */ 1071a70c05bSDavid Howells static void rxrpc_adjust_mtu(struct rxrpc_peer *peer, struct sock_exterr_skb *serr) 1081a70c05bSDavid Howells { 1091a70c05bSDavid Howells u32 mtu = serr->ee.ee_info; 1101a70c05bSDavid Howells 1111a70c05bSDavid Howells _net("Rx ICMP Fragmentation Needed (%d)", mtu); 1121a70c05bSDavid Howells 1131a70c05bSDavid Howells /* wind down the local interface MTU */ 1141a70c05bSDavid Howells if (mtu > 0 && peer->if_mtu == 65535 && mtu < peer->if_mtu) { 1151a70c05bSDavid Howells peer->if_mtu = mtu; 1161a70c05bSDavid Howells _net("I/F MTU %u", mtu); 1171a70c05bSDavid Howells } 1181a70c05bSDavid Howells 1191a70c05bSDavid Howells if (mtu == 0) { 1201a70c05bSDavid Howells /* they didn't give us a size, estimate one */ 1211a70c05bSDavid Howells mtu = peer->if_mtu; 1221a70c05bSDavid Howells if (mtu > 1500) { 1231a70c05bSDavid Howells mtu >>= 1; 1241a70c05bSDavid Howells if (mtu < 1500) 1251a70c05bSDavid Howells mtu = 1500; 1261a70c05bSDavid Howells } else { 1271a70c05bSDavid Howells mtu -= 100; 1281a70c05bSDavid Howells if (mtu < peer->hdrsize) 1291a70c05bSDavid Howells mtu = peer->hdrsize + 4; 1301a70c05bSDavid Howells } 1311a70c05bSDavid Howells } 1321a70c05bSDavid Howells 1331a70c05bSDavid Howells if (mtu < peer->mtu) { 1341a70c05bSDavid Howells spin_lock_bh(&peer->lock); 1351a70c05bSDavid Howells peer->mtu = mtu; 1361a70c05bSDavid Howells peer->maxdata = peer->mtu - peer->hdrsize; 1371a70c05bSDavid Howells spin_unlock_bh(&peer->lock); 1381a70c05bSDavid Howells _net("Net MTU %u (maxdata %u)", 1391a70c05bSDavid Howells peer->mtu, peer->maxdata); 1401a70c05bSDavid Howells } 1411a70c05bSDavid Howells } 1421a70c05bSDavid Howells 1431a70c05bSDavid Howells /* 144f66d7490SDavid Howells * Handle an error received on the local endpoint. 1458c3e34a4SDavid Howells */ 146abe89ef0SDavid Howells void rxrpc_error_report(struct sock *sk) 1478c3e34a4SDavid Howells { 1488c3e34a4SDavid Howells struct sock_exterr_skb *serr; 149494337c9SDavid Howells struct sockaddr_rxrpc srx; 1508c3e34a4SDavid Howells struct rxrpc_local *local = sk->sk_user_data; 1518c3e34a4SDavid Howells struct rxrpc_peer *peer; 1528c3e34a4SDavid Howells struct sk_buff *skb; 1538c3e34a4SDavid Howells 1548c3e34a4SDavid Howells _enter("%p{%d}", sk, local->debug_id); 1558c3e34a4SDavid Howells 1568c3e34a4SDavid Howells skb = sock_dequeue_err_skb(sk); 1578c3e34a4SDavid Howells if (!skb) { 1588c3e34a4SDavid Howells _leave("UDP socket errqueue empty"); 1598c3e34a4SDavid Howells return; 1608c3e34a4SDavid Howells } 16171f3ca40SDavid Howells rxrpc_new_skb(skb, rxrpc_skb_rx_received); 1628c3e34a4SDavid Howells serr = SKB_EXT_ERR(skb); 1638c3e34a4SDavid Howells if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) { 1648c3e34a4SDavid Howells _leave("UDP empty message"); 16571f3ca40SDavid Howells rxrpc_free_skb(skb, rxrpc_skb_rx_freed); 1668c3e34a4SDavid Howells return; 1678c3e34a4SDavid Howells } 1688c3e34a4SDavid Howells 169be6e6707SDavid Howells rcu_read_lock(); 170494337c9SDavid Howells peer = rxrpc_lookup_peer_icmp_rcu(local, skb, &srx); 171be6e6707SDavid Howells if (peer && !rxrpc_get_peer_maybe(peer)) 172be6e6707SDavid Howells peer = NULL; 173be6e6707SDavid Howells if (!peer) { 174be6e6707SDavid Howells rcu_read_unlock(); 17571f3ca40SDavid Howells rxrpc_free_skb(skb, rxrpc_skb_rx_freed); 1768c3e34a4SDavid Howells _leave(" [no peer]"); 1778c3e34a4SDavid Howells return; 1788c3e34a4SDavid Howells } 1798c3e34a4SDavid Howells 180494337c9SDavid Howells trace_rxrpc_rx_icmp(peer, &serr->ee, &srx); 181494337c9SDavid Howells 1821a70c05bSDavid Howells if ((serr->ee.ee_origin == SO_EE_ORIGIN_ICMP && 1838c3e34a4SDavid Howells serr->ee.ee_type == ICMP_DEST_UNREACH && 1841a70c05bSDavid Howells serr->ee.ee_code == ICMP_FRAG_NEEDED)) { 1851a70c05bSDavid Howells rxrpc_adjust_mtu(peer, serr); 186be6e6707SDavid Howells rcu_read_unlock(); 18771f3ca40SDavid Howells rxrpc_free_skb(skb, rxrpc_skb_rx_freed); 1888c3e34a4SDavid Howells rxrpc_put_peer(peer); 189f66d7490SDavid Howells _leave(" [MTU update]"); 190f66d7490SDavid Howells return; 1911a70c05bSDavid Howells } 192f66d7490SDavid Howells 193f66d7490SDavid Howells rxrpc_store_error(peer, serr); 194f66d7490SDavid Howells rcu_read_unlock(); 19571f3ca40SDavid Howells rxrpc_free_skb(skb, rxrpc_skb_rx_freed); 196f66d7490SDavid Howells 197f66d7490SDavid Howells /* The ref we obtained is passed off to the work item */ 1981159d4b4SDavid Howells __rxrpc_queue_peer_error(peer); 1998c3e34a4SDavid Howells _leave(""); 2008c3e34a4SDavid Howells } 2018c3e34a4SDavid Howells 2028c3e34a4SDavid Howells /* 203f66d7490SDavid Howells * Map an error report to error codes on the peer record. 2048c3e34a4SDavid Howells */ 205f66d7490SDavid Howells static void rxrpc_store_error(struct rxrpc_peer *peer, 206f66d7490SDavid Howells struct sock_exterr_skb *serr) 2078c3e34a4SDavid Howells { 2088c3e34a4SDavid Howells struct sock_extended_err *ee; 2098c3e34a4SDavid Howells int err; 2108c3e34a4SDavid Howells 2118c3e34a4SDavid Howells _enter(""); 2128c3e34a4SDavid Howells 2138c3e34a4SDavid Howells ee = &serr->ee; 2148c3e34a4SDavid Howells 2158c3e34a4SDavid Howells err = ee->ee_errno; 2168c3e34a4SDavid Howells 2178c3e34a4SDavid Howells switch (ee->ee_origin) { 2188c3e34a4SDavid Howells case SO_EE_ORIGIN_ICMP: 2198c3e34a4SDavid Howells switch (ee->ee_type) { 2208c3e34a4SDavid Howells case ICMP_DEST_UNREACH: 2218c3e34a4SDavid Howells switch (ee->ee_code) { 2228c3e34a4SDavid Howells case ICMP_NET_UNREACH: 2238c3e34a4SDavid Howells _net("Rx Received ICMP Network Unreachable"); 2248c3e34a4SDavid Howells break; 2258c3e34a4SDavid Howells case ICMP_HOST_UNREACH: 2268c3e34a4SDavid Howells _net("Rx Received ICMP Host Unreachable"); 2278c3e34a4SDavid Howells break; 2288c3e34a4SDavid Howells case ICMP_PORT_UNREACH: 2298c3e34a4SDavid Howells _net("Rx Received ICMP Port Unreachable"); 2308c3e34a4SDavid Howells break; 2318c3e34a4SDavid Howells case ICMP_NET_UNKNOWN: 2328c3e34a4SDavid Howells _net("Rx Received ICMP Unknown Network"); 2338c3e34a4SDavid Howells break; 2348c3e34a4SDavid Howells case ICMP_HOST_UNKNOWN: 2358c3e34a4SDavid Howells _net("Rx Received ICMP Unknown Host"); 2368c3e34a4SDavid Howells break; 2378c3e34a4SDavid Howells default: 2388c3e34a4SDavid Howells _net("Rx Received ICMP DestUnreach code=%u", 2398c3e34a4SDavid Howells ee->ee_code); 2408c3e34a4SDavid Howells break; 2418c3e34a4SDavid Howells } 2428c3e34a4SDavid Howells break; 2438c3e34a4SDavid Howells 2448c3e34a4SDavid Howells case ICMP_TIME_EXCEEDED: 2458c3e34a4SDavid Howells _net("Rx Received ICMP TTL Exceeded"); 2468c3e34a4SDavid Howells break; 2478c3e34a4SDavid Howells 2488c3e34a4SDavid Howells default: 2498c3e34a4SDavid Howells _proto("Rx Received ICMP error { type=%u code=%u }", 2508c3e34a4SDavid Howells ee->ee_type, ee->ee_code); 2518c3e34a4SDavid Howells break; 2528c3e34a4SDavid Howells } 2538c3e34a4SDavid Howells break; 2548c3e34a4SDavid Howells 255f66d7490SDavid Howells case SO_EE_ORIGIN_NONE: 2568c3e34a4SDavid Howells case SO_EE_ORIGIN_LOCAL: 257fe77d5fcSDavid Howells _proto("Rx Received local error { error=%d }", err); 258f66d7490SDavid Howells err += RXRPC_LOCAL_ERROR_OFFSET; 2598c3e34a4SDavid Howells break; 2608c3e34a4SDavid Howells 2618c3e34a4SDavid Howells case SO_EE_ORIGIN_ICMP6: 2628c3e34a4SDavid Howells default: 263fe77d5fcSDavid Howells _proto("Rx Received error report { orig=%u }", ee->ee_origin); 2648c3e34a4SDavid Howells break; 2658c3e34a4SDavid Howells } 2668c3e34a4SDavid Howells 267f66d7490SDavid Howells peer->error_report = err; 268f66d7490SDavid Howells } 2698c3e34a4SDavid Howells 270f66d7490SDavid Howells /* 271f66d7490SDavid Howells * Distribute an error that occurred on a peer 272f66d7490SDavid Howells */ 273f66d7490SDavid Howells void rxrpc_peer_error_distributor(struct work_struct *work) 274f66d7490SDavid Howells { 275f66d7490SDavid Howells struct rxrpc_peer *peer = 276f66d7490SDavid Howells container_of(work, struct rxrpc_peer, error_distributor); 277f66d7490SDavid Howells struct rxrpc_call *call; 278f5c17aaeSDavid Howells enum rxrpc_call_completion compl; 279f5c17aaeSDavid Howells int error; 2808c3e34a4SDavid Howells 281f66d7490SDavid Howells _enter(""); 2828c3e34a4SDavid Howells 283f5c17aaeSDavid Howells error = READ_ONCE(peer->error_report); 284f5c17aaeSDavid Howells if (error < RXRPC_LOCAL_ERROR_OFFSET) { 285f5c17aaeSDavid Howells compl = RXRPC_CALL_NETWORK_ERROR; 286f5c17aaeSDavid Howells } else { 287f5c17aaeSDavid Howells compl = RXRPC_CALL_LOCAL_ERROR; 288f5c17aaeSDavid Howells error -= RXRPC_LOCAL_ERROR_OFFSET; 289f5c17aaeSDavid Howells } 290f66d7490SDavid Howells 291f5c17aaeSDavid Howells _debug("ISSUE ERROR %s %d", rxrpc_call_completions[compl], error); 292f66d7490SDavid Howells 293f66d7490SDavid Howells spin_lock_bh(&peer->lock); 294f66d7490SDavid Howells 295f66d7490SDavid Howells while (!hlist_empty(&peer->error_targets)) { 296f66d7490SDavid Howells call = hlist_entry(peer->error_targets.first, 297f66d7490SDavid Howells struct rxrpc_call, error_link); 298f66d7490SDavid Howells hlist_del_init(&call->error_link); 299e34d4234SDavid Howells rxrpc_see_call(call); 300f66d7490SDavid Howells 3013a92789aSDavid Howells if (rxrpc_set_call_completion(call, compl, 0, -error)) 302248f219cSDavid Howells rxrpc_notify_socket(call); 3038c3e34a4SDavid Howells } 3048c3e34a4SDavid Howells 305f66d7490SDavid Howells spin_unlock_bh(&peer->lock); 3068c3e34a4SDavid Howells 307f66d7490SDavid Howells rxrpc_put_peer(peer); 3088c3e34a4SDavid Howells _leave(""); 3098c3e34a4SDavid Howells } 310cf1a6474SDavid Howells 311cf1a6474SDavid Howells /* 312cf1a6474SDavid Howells * Add RTT information to cache. This is called in softirq mode and has 313cf1a6474SDavid Howells * exclusive access to the peer RTT data. 314cf1a6474SDavid Howells */ 315cf1a6474SDavid Howells void rxrpc_peer_add_rtt(struct rxrpc_call *call, enum rxrpc_rtt_rx_trace why, 316cf1a6474SDavid Howells rxrpc_serial_t send_serial, rxrpc_serial_t resp_serial, 317cf1a6474SDavid Howells ktime_t send_time, ktime_t resp_time) 318cf1a6474SDavid Howells { 319cf1a6474SDavid Howells struct rxrpc_peer *peer = call->peer; 320cf1a6474SDavid Howells s64 rtt; 321cf1a6474SDavid Howells u64 sum = peer->rtt_sum, avg; 322cf1a6474SDavid Howells u8 cursor = peer->rtt_cursor, usage = peer->rtt_usage; 323cf1a6474SDavid Howells 324cf1a6474SDavid Howells rtt = ktime_to_ns(ktime_sub(resp_time, send_time)); 325cf1a6474SDavid Howells if (rtt < 0) 326cf1a6474SDavid Howells return; 327cf1a6474SDavid Howells 328cf1a6474SDavid Howells /* Replace the oldest datum in the RTT buffer */ 329cf1a6474SDavid Howells sum -= peer->rtt_cache[cursor]; 330cf1a6474SDavid Howells sum += rtt; 331cf1a6474SDavid Howells peer->rtt_cache[cursor] = rtt; 332cf1a6474SDavid Howells peer->rtt_cursor = (cursor + 1) & (RXRPC_RTT_CACHE_SIZE - 1); 333cf1a6474SDavid Howells peer->rtt_sum = sum; 334cf1a6474SDavid Howells if (usage < RXRPC_RTT_CACHE_SIZE) { 335cf1a6474SDavid Howells usage++; 336cf1a6474SDavid Howells peer->rtt_usage = usage; 337cf1a6474SDavid Howells } 338cf1a6474SDavid Howells 339cf1a6474SDavid Howells /* Now recalculate the average */ 340cf1a6474SDavid Howells if (usage == RXRPC_RTT_CACHE_SIZE) { 341cf1a6474SDavid Howells avg = sum / RXRPC_RTT_CACHE_SIZE; 342cf1a6474SDavid Howells } else { 343cf1a6474SDavid Howells avg = sum; 344cf1a6474SDavid Howells do_div(avg, usage); 345cf1a6474SDavid Howells } 346cf1a6474SDavid Howells 347cf1a6474SDavid Howells peer->rtt = avg; 348cf1a6474SDavid Howells trace_rxrpc_rtt_rx(call, why, send_serial, resp_serial, rtt, 349cf1a6474SDavid Howells usage, avg); 350cf1a6474SDavid Howells } 351ace45becSDavid Howells 352ace45becSDavid Howells /* 353ace45becSDavid Howells * Perform keep-alive pings with VERSION packets to keep any NAT alive. 354ace45becSDavid Howells */ 355ace45becSDavid Howells void rxrpc_peer_keepalive_worker(struct work_struct *work) 356ace45becSDavid Howells { 357ace45becSDavid Howells struct rxrpc_net *rxnet = 358ace45becSDavid Howells container_of(work, struct rxrpc_net, peer_keepalive_work); 359ace45becSDavid Howells struct rxrpc_peer *peer; 360ace45becSDavid Howells unsigned long delay; 361ace45becSDavid Howells ktime_t base, now = ktime_get_real(); 362ace45becSDavid Howells s64 diff; 363ace45becSDavid Howells u8 cursor, slot; 364ace45becSDavid Howells 365ace45becSDavid Howells base = rxnet->peer_keepalive_base; 366ace45becSDavid Howells cursor = rxnet->peer_keepalive_cursor; 367ace45becSDavid Howells 368ace45becSDavid Howells _enter("%u,%lld", cursor, ktime_sub(now, base)); 369ace45becSDavid Howells 370ace45becSDavid Howells next_bucket: 371ace45becSDavid Howells diff = ktime_to_ns(ktime_sub(now, base)); 372ace45becSDavid Howells if (diff < 0) 373ace45becSDavid Howells goto resched; 374ace45becSDavid Howells 375ace45becSDavid Howells _debug("at %u", cursor); 376ace45becSDavid Howells spin_lock_bh(&rxnet->peer_hash_lock); 377ace45becSDavid Howells next_peer: 378ace45becSDavid Howells if (!rxnet->live) { 379ace45becSDavid Howells spin_unlock_bh(&rxnet->peer_hash_lock); 380ace45becSDavid Howells goto out; 381ace45becSDavid Howells } 382ace45becSDavid Howells 383ace45becSDavid Howells /* Everything in the bucket at the cursor is processed this second; the 384ace45becSDavid Howells * bucket at cursor + 1 goes now + 1s and so on... 385ace45becSDavid Howells */ 386ace45becSDavid Howells if (hlist_empty(&rxnet->peer_keepalive[cursor])) { 387ace45becSDavid Howells if (hlist_empty(&rxnet->peer_keepalive_new)) { 388ace45becSDavid Howells spin_unlock_bh(&rxnet->peer_hash_lock); 389ace45becSDavid Howells goto emptied_bucket; 390ace45becSDavid Howells } 391ace45becSDavid Howells 392ace45becSDavid Howells hlist_move_list(&rxnet->peer_keepalive_new, 393ace45becSDavid Howells &rxnet->peer_keepalive[cursor]); 394ace45becSDavid Howells } 395ace45becSDavid Howells 396ace45becSDavid Howells peer = hlist_entry(rxnet->peer_keepalive[cursor].first, 397ace45becSDavid Howells struct rxrpc_peer, keepalive_link); 398ace45becSDavid Howells hlist_del_init(&peer->keepalive_link); 399ace45becSDavid Howells if (!rxrpc_get_peer_maybe(peer)) 400ace45becSDavid Howells goto next_peer; 401ace45becSDavid Howells 402ace45becSDavid Howells spin_unlock_bh(&rxnet->peer_hash_lock); 403ace45becSDavid Howells 404ace45becSDavid Howells _debug("peer %u {%pISp}", peer->debug_id, &peer->srx.transport); 405ace45becSDavid Howells 406ace45becSDavid Howells recalc: 407ace45becSDavid Howells diff = ktime_divns(ktime_sub(peer->last_tx_at, base), NSEC_PER_SEC); 408ace45becSDavid Howells if (diff < -30 || diff > 30) 409ace45becSDavid Howells goto send; /* LSW of 64-bit time probably wrapped on 32-bit */ 410ace45becSDavid Howells diff += RXRPC_KEEPALIVE_TIME - 1; 411ace45becSDavid Howells if (diff < 0) 412ace45becSDavid Howells goto send; 413ace45becSDavid Howells 414ace45becSDavid Howells slot = (diff > RXRPC_KEEPALIVE_TIME - 1) ? RXRPC_KEEPALIVE_TIME - 1 : diff; 415ace45becSDavid Howells if (slot == 0) 416ace45becSDavid Howells goto send; 417ace45becSDavid Howells 418ace45becSDavid Howells /* A transmission to this peer occurred since last we examined it so 419ace45becSDavid Howells * put it into the appropriate future bucket. 420ace45becSDavid Howells */ 421ace45becSDavid Howells slot = (slot + cursor) % ARRAY_SIZE(rxnet->peer_keepalive); 422ace45becSDavid Howells spin_lock_bh(&rxnet->peer_hash_lock); 423ace45becSDavid Howells hlist_add_head(&peer->keepalive_link, &rxnet->peer_keepalive[slot]); 424ace45becSDavid Howells rxrpc_put_peer(peer); 425ace45becSDavid Howells goto next_peer; 426ace45becSDavid Howells 427ace45becSDavid Howells send: 428ace45becSDavid Howells rxrpc_send_keepalive(peer); 429ace45becSDavid Howells now = ktime_get_real(); 430ace45becSDavid Howells goto recalc; 431ace45becSDavid Howells 432ace45becSDavid Howells emptied_bucket: 433ace45becSDavid Howells cursor++; 434ace45becSDavid Howells if (cursor >= ARRAY_SIZE(rxnet->peer_keepalive)) 435ace45becSDavid Howells cursor = 0; 436ace45becSDavid Howells base = ktime_add_ns(base, NSEC_PER_SEC); 437ace45becSDavid Howells goto next_bucket; 438ace45becSDavid Howells 439ace45becSDavid Howells resched: 440ace45becSDavid Howells rxnet->peer_keepalive_base = base; 441ace45becSDavid Howells rxnet->peer_keepalive_cursor = cursor; 442ace45becSDavid Howells delay = nsecs_to_jiffies(-diff) + 1; 443ace45becSDavid Howells timer_reduce(&rxnet->peer_keepalive_timer, jiffies + delay); 444ace45becSDavid Howells out: 445ace45becSDavid Howells _leave(""); 446ace45becSDavid Howells } 447