1 /* Peer event handling, typically ICMP messages.
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/module.h>
13 #include <linux/net.h>
14 #include <linux/skbuff.h>
15 #include <linux/errqueue.h>
16 #include <linux/udp.h>
18 #include <linux/in6.h>
19 #include <linux/icmp.h>
21 #include <net/af_rxrpc.h>
23 #include "ar-internal.h"
25 static void rxrpc_store_error(struct rxrpc_peer *, struct sock_exterr_skb *);
28 * Find the peer associated with an ICMP packet.
30 static struct rxrpc_peer *rxrpc_lookup_peer_icmp_rcu(struct rxrpc_local *local,
31 const struct sk_buff *skb)
33 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
34 struct sockaddr_rxrpc srx;
38 memset(&srx, 0, sizeof(srx));
39 srx.transport_type = local->srx.transport_type;
40 srx.transport.family = local->srx.transport.family;
42 /* Can we see an ICMP4 packet on an ICMP6 listening socket? and vice
45 switch (srx.transport.family) {
47 srx.transport.sin.sin_port = serr->port;
48 srx.transport_len = sizeof(struct sockaddr_in);
49 switch (serr->ee.ee_origin) {
50 case SO_EE_ORIGIN_ICMP:
52 memcpy(&srx.transport.sin.sin_addr,
53 skb_network_header(skb) + serr->addr_offset,
54 sizeof(struct in_addr));
56 case SO_EE_ORIGIN_ICMP6:
57 _net("Rx ICMP6 on v4 sock");
58 memcpy(&srx.transport.sin.sin_addr,
59 skb_network_header(skb) + serr->addr_offset + 12,
60 sizeof(struct in_addr));
63 memcpy(&srx.transport.sin.sin_addr, &ip_hdr(skb)->saddr,
64 sizeof(struct in_addr));
69 #ifdef CONFIG_AF_RXRPC_IPV6
71 srx.transport.sin6.sin6_port = serr->port;
72 srx.transport_len = sizeof(struct sockaddr_in6);
73 switch (serr->ee.ee_origin) {
74 case SO_EE_ORIGIN_ICMP6:
76 memcpy(&srx.transport.sin6.sin6_addr,
77 skb_network_header(skb) + serr->addr_offset,
78 sizeof(struct in6_addr));
80 case SO_EE_ORIGIN_ICMP:
81 _net("Rx ICMP on v6 sock");
82 memcpy(srx.transport.sin6.sin6_addr.s6_addr + 12,
83 skb_network_header(skb) + serr->addr_offset,
84 sizeof(struct in_addr));
87 memcpy(&srx.transport.sin6.sin6_addr,
88 &ipv6_hdr(skb)->saddr,
89 sizeof(struct in6_addr));
99 return rxrpc_lookup_peer_rcu(local, &srx);
103 * Handle an MTU/fragmentation problem.
105 static void rxrpc_adjust_mtu(struct rxrpc_peer *peer, struct sock_exterr_skb *serr)
107 u32 mtu = serr->ee.ee_info;
109 _net("Rx ICMP Fragmentation Needed (%d)", mtu);
111 /* wind down the local interface MTU */
112 if (mtu > 0 && peer->if_mtu == 65535 && mtu < peer->if_mtu) {
114 _net("I/F MTU %u", mtu);
118 /* they didn't give us a size, estimate one */
126 if (mtu < peer->hdrsize)
127 mtu = peer->hdrsize + 4;
131 if (mtu < peer->mtu) {
132 spin_lock_bh(&peer->lock);
134 peer->maxdata = peer->mtu - peer->hdrsize;
135 spin_unlock_bh(&peer->lock);
136 _net("Net MTU %u (maxdata %u)",
137 peer->mtu, peer->maxdata);
142 * Handle an error received on the local endpoint.
144 void rxrpc_error_report(struct sock *sk)
146 struct sock_exterr_skb *serr;
147 struct rxrpc_local *local = sk->sk_user_data;
148 struct rxrpc_peer *peer;
151 _enter("%p{%d}", sk, local->debug_id);
153 skb = sock_dequeue_err_skb(sk);
155 _leave("UDP socket errqueue empty");
158 rxrpc_new_skb(skb, rxrpc_skb_rx_received);
159 serr = SKB_EXT_ERR(skb);
160 if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) {
161 _leave("UDP empty message");
162 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
167 peer = rxrpc_lookup_peer_icmp_rcu(local, skb);
168 if (peer && !rxrpc_get_peer_maybe(peer))
172 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
173 _leave(" [no peer]");
177 if ((serr->ee.ee_origin == SO_EE_ORIGIN_ICMP &&
178 serr->ee.ee_type == ICMP_DEST_UNREACH &&
179 serr->ee.ee_code == ICMP_FRAG_NEEDED)) {
180 rxrpc_adjust_mtu(peer, serr);
182 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
183 rxrpc_put_peer(peer);
184 _leave(" [MTU update]");
188 rxrpc_store_error(peer, serr);
190 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
192 /* The ref we obtained is passed off to the work item */
193 rxrpc_queue_work(&peer->error_distributor);
198 * Map an error report to error codes on the peer record.
200 static void rxrpc_store_error(struct rxrpc_peer *peer,
201 struct sock_exterr_skb *serr)
203 struct sock_extended_err *ee;
210 _net("Rx Error o=%d t=%d c=%d e=%d",
211 ee->ee_origin, ee->ee_type, ee->ee_code, ee->ee_errno);
215 switch (ee->ee_origin) {
216 case SO_EE_ORIGIN_ICMP:
217 switch (ee->ee_type) {
218 case ICMP_DEST_UNREACH:
219 switch (ee->ee_code) {
220 case ICMP_NET_UNREACH:
221 _net("Rx Received ICMP Network Unreachable");
223 case ICMP_HOST_UNREACH:
224 _net("Rx Received ICMP Host Unreachable");
226 case ICMP_PORT_UNREACH:
227 _net("Rx Received ICMP Port Unreachable");
229 case ICMP_NET_UNKNOWN:
230 _net("Rx Received ICMP Unknown Network");
232 case ICMP_HOST_UNKNOWN:
233 _net("Rx Received ICMP Unknown Host");
236 _net("Rx Received ICMP DestUnreach code=%u",
242 case ICMP_TIME_EXCEEDED:
243 _net("Rx Received ICMP TTL Exceeded");
247 _proto("Rx Received ICMP error { type=%u code=%u }",
248 ee->ee_type, ee->ee_code);
253 case SO_EE_ORIGIN_NONE:
254 case SO_EE_ORIGIN_LOCAL:
255 _proto("Rx Received local error { error=%d }", err);
256 err += RXRPC_LOCAL_ERROR_OFFSET;
259 case SO_EE_ORIGIN_ICMP6:
261 _proto("Rx Received error report { orig=%u }", ee->ee_origin);
265 peer->error_report = err;
269 * Distribute an error that occurred on a peer
271 void rxrpc_peer_error_distributor(struct work_struct *work)
273 struct rxrpc_peer *peer =
274 container_of(work, struct rxrpc_peer, error_distributor);
275 struct rxrpc_call *call;
276 enum rxrpc_call_completion compl;
281 error = READ_ONCE(peer->error_report);
282 if (error < RXRPC_LOCAL_ERROR_OFFSET) {
283 compl = RXRPC_CALL_NETWORK_ERROR;
285 compl = RXRPC_CALL_LOCAL_ERROR;
286 error -= RXRPC_LOCAL_ERROR_OFFSET;
289 _debug("ISSUE ERROR %s %d", rxrpc_call_completions[compl], error);
291 spin_lock_bh(&peer->lock);
293 while (!hlist_empty(&peer->error_targets)) {
294 call = hlist_entry(peer->error_targets.first,
295 struct rxrpc_call, error_link);
296 hlist_del_init(&call->error_link);
297 rxrpc_see_call(call);
299 if (rxrpc_set_call_completion(call, compl, 0, error))
300 rxrpc_notify_socket(call);
303 spin_unlock_bh(&peer->lock);
305 rxrpc_put_peer(peer);
310 * Add RTT information to cache. This is called in softirq mode and has
311 * exclusive access to the peer RTT data.
313 void rxrpc_peer_add_rtt(struct rxrpc_call *call, enum rxrpc_rtt_rx_trace why,
314 rxrpc_serial_t send_serial, rxrpc_serial_t resp_serial,
315 ktime_t send_time, ktime_t resp_time)
317 struct rxrpc_peer *peer = call->peer;
319 u64 sum = peer->rtt_sum, avg;
320 u8 cursor = peer->rtt_cursor, usage = peer->rtt_usage;
322 rtt = ktime_to_ns(ktime_sub(resp_time, send_time));
326 /* Replace the oldest datum in the RTT buffer */
327 sum -= peer->rtt_cache[cursor];
329 peer->rtt_cache[cursor] = rtt;
330 peer->rtt_cursor = (cursor + 1) & (RXRPC_RTT_CACHE_SIZE - 1);
332 if (usage < RXRPC_RTT_CACHE_SIZE) {
334 peer->rtt_usage = usage;
337 /* Now recalculate the average */
338 if (usage == RXRPC_RTT_CACHE_SIZE) {
339 avg = sum / RXRPC_RTT_CACHE_SIZE;
346 trace_rxrpc_rtt_rx(call, why, send_serial, resp_serial, rtt,