blob: 7f749505e699a0ac5d8e04f832f21629643a55d0 [file] [log] [blame]
David Howellsf66d7492016-04-04 14:00:34 +01001/* Peer event handling, typically ICMP messages.
David Howells17926a72007-04-26 15:48:28 -07002 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
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>
17#include <linux/in.h>
18#include <linux/in6.h>
19#include <linux/icmp.h>
20#include <net/sock.h>
21#include <net/af_rxrpc.h>
22#include <net/ip.h>
23#include "ar-internal.h"
24
David Howellsf66d7492016-04-04 14:00:34 +010025static void rxrpc_store_error(struct rxrpc_peer *, struct sock_exterr_skb *);
26
David Howells17926a72007-04-26 15:48:28 -070027/*
David Howellsbe6e6702016-04-04 14:00:32 +010028 * Find the peer associated with an ICMP packet.
29 */
30static struct rxrpc_peer *rxrpc_lookup_peer_icmp_rcu(struct rxrpc_local *local,
31 const struct sk_buff *skb)
32{
33 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
34 struct sockaddr_rxrpc srx;
35
36 _enter("");
37
38 memset(&srx, 0, sizeof(srx));
39 srx.transport_type = local->srx.transport_type;
David Howells7b674e32017-08-29 10:18:37 +010040 srx.transport_len = local->srx.transport_len;
David Howellsbe6e6702016-04-04 14:00:32 +010041 srx.transport.family = local->srx.transport.family;
42
43 /* Can we see an ICMP4 packet on an ICMP6 listening socket? and vice
44 * versa?
45 */
46 switch (srx.transport.family) {
47 case AF_INET:
48 srx.transport.sin.sin_port = serr->port;
David Howellsbe6e6702016-04-04 14:00:32 +010049 switch (serr->ee.ee_origin) {
50 case SO_EE_ORIGIN_ICMP:
51 _net("Rx ICMP");
52 memcpy(&srx.transport.sin.sin_addr,
53 skb_network_header(skb) + serr->addr_offset,
54 sizeof(struct in_addr));
55 break;
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));
61 break;
62 default:
63 memcpy(&srx.transport.sin.sin_addr, &ip_hdr(skb)->saddr,
64 sizeof(struct in_addr));
65 break;
66 }
67 break;
68
David Howellsd1912742016-09-17 07:26:01 +010069#ifdef CONFIG_AF_RXRPC_IPV6
David Howells75b54cb2016-09-13 08:49:05 +010070 case AF_INET6:
71 srx.transport.sin6.sin6_port = serr->port;
David Howells75b54cb2016-09-13 08:49:05 +010072 switch (serr->ee.ee_origin) {
73 case SO_EE_ORIGIN_ICMP6:
74 _net("Rx ICMP6");
75 memcpy(&srx.transport.sin6.sin6_addr,
76 skb_network_header(skb) + serr->addr_offset,
77 sizeof(struct in6_addr));
78 break;
79 case SO_EE_ORIGIN_ICMP:
80 _net("Rx ICMP on v6 sock");
David Howells7b674e32017-08-29 10:18:37 +010081 srx.transport.sin6.sin6_addr.s6_addr32[0] = 0;
82 srx.transport.sin6.sin6_addr.s6_addr32[1] = 0;
83 srx.transport.sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
David Howellsd1912742016-09-17 07:26:01 +010084 memcpy(srx.transport.sin6.sin6_addr.s6_addr + 12,
David Howells75b54cb2016-09-13 08:49:05 +010085 skb_network_header(skb) + serr->addr_offset,
86 sizeof(struct in_addr));
87 break;
88 default:
89 memcpy(&srx.transport.sin6.sin6_addr,
90 &ipv6_hdr(skb)->saddr,
91 sizeof(struct in6_addr));
92 break;
93 }
94 break;
David Howellsd1912742016-09-17 07:26:01 +010095#endif
David Howells75b54cb2016-09-13 08:49:05 +010096
David Howellsbe6e6702016-04-04 14:00:32 +010097 default:
98 BUG();
99 }
100
101 return rxrpc_lookup_peer_rcu(local, &srx);
102}
103
104/*
David Howells1a70c052016-04-04 14:00:33 +0100105 * Handle an MTU/fragmentation problem.
106 */
107static void rxrpc_adjust_mtu(struct rxrpc_peer *peer, struct sock_exterr_skb *serr)
108{
109 u32 mtu = serr->ee.ee_info;
110
111 _net("Rx ICMP Fragmentation Needed (%d)", mtu);
112
113 /* wind down the local interface MTU */
114 if (mtu > 0 && peer->if_mtu == 65535 && mtu < peer->if_mtu) {
115 peer->if_mtu = mtu;
116 _net("I/F MTU %u", mtu);
117 }
118
119 if (mtu == 0) {
120 /* they didn't give us a size, estimate one */
121 mtu = peer->if_mtu;
122 if (mtu > 1500) {
123 mtu >>= 1;
124 if (mtu < 1500)
125 mtu = 1500;
126 } else {
127 mtu -= 100;
128 if (mtu < peer->hdrsize)
129 mtu = peer->hdrsize + 4;
130 }
131 }
132
133 if (mtu < peer->mtu) {
134 spin_lock_bh(&peer->lock);
135 peer->mtu = mtu;
136 peer->maxdata = peer->mtu - peer->hdrsize;
137 spin_unlock_bh(&peer->lock);
138 _net("Net MTU %u (maxdata %u)",
139 peer->mtu, peer->maxdata);
140 }
141}
142
143/*
David Howellsf66d7492016-04-04 14:00:34 +0100144 * Handle an error received on the local endpoint.
David Howells17926a72007-04-26 15:48:28 -0700145 */
David Howellsabe89ef2016-04-04 14:00:32 +0100146void rxrpc_error_report(struct sock *sk)
David Howells17926a72007-04-26 15:48:28 -0700147{
148 struct sock_exterr_skb *serr;
David Howells17926a72007-04-26 15:48:28 -0700149 struct rxrpc_local *local = sk->sk_user_data;
150 struct rxrpc_peer *peer;
151 struct sk_buff *skb;
David Howells17926a72007-04-26 15:48:28 -0700152
153 _enter("%p{%d}", sk, local->debug_id);
154
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400155 skb = sock_dequeue_err_skb(sk);
David Howells17926a72007-04-26 15:48:28 -0700156 if (!skb) {
157 _leave("UDP socket errqueue empty");
158 return;
159 }
David Howells71f3ca42016-09-17 10:49:14 +0100160 rxrpc_new_skb(skb, rxrpc_skb_rx_received);
Willem de Bruijnc247f052015-03-07 20:33:22 -0500161 serr = SKB_EXT_ERR(skb);
162 if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) {
Willem de Bruijn49ca0d82015-01-30 13:29:31 -0500163 _leave("UDP empty message");
David Howells71f3ca42016-09-17 10:49:14 +0100164 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
Willem de Bruijn49ca0d82015-01-30 13:29:31 -0500165 return;
166 }
David Howells17926a72007-04-26 15:48:28 -0700167
David Howellsbe6e6702016-04-04 14:00:32 +0100168 rcu_read_lock();
169 peer = rxrpc_lookup_peer_icmp_rcu(local, skb);
170 if (peer && !rxrpc_get_peer_maybe(peer))
171 peer = NULL;
172 if (!peer) {
173 rcu_read_unlock();
David Howells71f3ca42016-09-17 10:49:14 +0100174 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
David Howells17926a72007-04-26 15:48:28 -0700175 _leave(" [no peer]");
176 return;
177 }
178
David Howells1a70c052016-04-04 14:00:33 +0100179 if ((serr->ee.ee_origin == SO_EE_ORIGIN_ICMP &&
180 serr->ee.ee_type == ICMP_DEST_UNREACH &&
181 serr->ee.ee_code == ICMP_FRAG_NEEDED)) {
182 rxrpc_adjust_mtu(peer, serr);
David Howellsf66d7492016-04-04 14:00:34 +0100183 rcu_read_unlock();
David Howells71f3ca42016-09-17 10:49:14 +0100184 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
David Howellsf66d7492016-04-04 14:00:34 +0100185 rxrpc_put_peer(peer);
186 _leave(" [MTU update]");
187 return;
David Howells17926a72007-04-26 15:48:28 -0700188 }
189
David Howellsf66d7492016-04-04 14:00:34 +0100190 rxrpc_store_error(peer, serr);
David Howellsbe6e6702016-04-04 14:00:32 +0100191 rcu_read_unlock();
David Howells71f3ca42016-09-17 10:49:14 +0100192 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
David Howells17926a72007-04-26 15:48:28 -0700193
David Howellsf66d7492016-04-04 14:00:34 +0100194 /* The ref we obtained is passed off to the work item */
195 rxrpc_queue_work(&peer->error_distributor);
David Howells17926a72007-04-26 15:48:28 -0700196 _leave("");
197}
198
199/*
David Howellsf66d7492016-04-04 14:00:34 +0100200 * Map an error report to error codes on the peer record.
David Howells17926a72007-04-26 15:48:28 -0700201 */
David Howellsf66d7492016-04-04 14:00:34 +0100202static void rxrpc_store_error(struct rxrpc_peer *peer,
203 struct sock_exterr_skb *serr)
David Howells17926a72007-04-26 15:48:28 -0700204{
205 struct sock_extended_err *ee;
David S. Millerc9d10c42011-05-19 18:37:11 -0400206 int err;
David Howells17926a72007-04-26 15:48:28 -0700207
208 _enter("");
209
David Howells17926a72007-04-26 15:48:28 -0700210 ee = &serr->ee;
211
212 _net("Rx Error o=%d t=%d c=%d e=%d",
213 ee->ee_origin, ee->ee_type, ee->ee_code, ee->ee_errno);
214
215 err = ee->ee_errno;
216
217 switch (ee->ee_origin) {
218 case SO_EE_ORIGIN_ICMP:
David Howells17926a72007-04-26 15:48:28 -0700219 switch (ee->ee_type) {
220 case ICMP_DEST_UNREACH:
221 switch (ee->ee_code) {
222 case ICMP_NET_UNREACH:
223 _net("Rx Received ICMP Network Unreachable");
David Howells17926a72007-04-26 15:48:28 -0700224 break;
225 case ICMP_HOST_UNREACH:
226 _net("Rx Received ICMP Host Unreachable");
David Howells17926a72007-04-26 15:48:28 -0700227 break;
228 case ICMP_PORT_UNREACH:
229 _net("Rx Received ICMP Port Unreachable");
David Howells17926a72007-04-26 15:48:28 -0700230 break;
231 case ICMP_NET_UNKNOWN:
232 _net("Rx Received ICMP Unknown Network");
David Howells17926a72007-04-26 15:48:28 -0700233 break;
234 case ICMP_HOST_UNKNOWN:
235 _net("Rx Received ICMP Unknown Host");
David Howells17926a72007-04-26 15:48:28 -0700236 break;
237 default:
238 _net("Rx Received ICMP DestUnreach code=%u",
239 ee->ee_code);
240 break;
241 }
242 break;
243
244 case ICMP_TIME_EXCEEDED:
245 _net("Rx Received ICMP TTL Exceeded");
246 break;
247
248 default:
249 _proto("Rx Received ICMP error { type=%u code=%u }",
250 ee->ee_type, ee->ee_code);
251 break;
252 }
253 break;
254
David Howellsf66d7492016-04-04 14:00:34 +0100255 case SO_EE_ORIGIN_NONE:
David Howells17926a72007-04-26 15:48:28 -0700256 case SO_EE_ORIGIN_LOCAL:
David Howellsfe77d5f2016-04-04 14:00:34 +0100257 _proto("Rx Received local error { error=%d }", err);
David Howellsf66d7492016-04-04 14:00:34 +0100258 err += RXRPC_LOCAL_ERROR_OFFSET;
David Howells17926a72007-04-26 15:48:28 -0700259 break;
260
David Howells17926a72007-04-26 15:48:28 -0700261 case SO_EE_ORIGIN_ICMP6:
262 default:
David Howellsfe77d5f2016-04-04 14:00:34 +0100263 _proto("Rx Received error report { orig=%u }", ee->ee_origin);
David Howells17926a72007-04-26 15:48:28 -0700264 break;
265 }
266
David Howellsf66d7492016-04-04 14:00:34 +0100267 peer->error_report = err;
268}
David Howells17926a72007-04-26 15:48:28 -0700269
David Howellsf66d7492016-04-04 14:00:34 +0100270/*
271 * Distribute an error that occurred on a peer
272 */
273void rxrpc_peer_error_distributor(struct work_struct *work)
274{
275 struct rxrpc_peer *peer =
276 container_of(work, struct rxrpc_peer, error_distributor);
277 struct rxrpc_call *call;
David Howellsf5c17aa2016-08-30 09:49:28 +0100278 enum rxrpc_call_completion compl;
David Howellsf5c17aa2016-08-30 09:49:28 +0100279 int error;
David Howells17926a72007-04-26 15:48:28 -0700280
David Howellsf66d7492016-04-04 14:00:34 +0100281 _enter("");
David Howells17926a72007-04-26 15:48:28 -0700282
David Howellsf5c17aa2016-08-30 09:49:28 +0100283 error = READ_ONCE(peer->error_report);
284 if (error < RXRPC_LOCAL_ERROR_OFFSET) {
285 compl = RXRPC_CALL_NETWORK_ERROR;
286 } else {
287 compl = RXRPC_CALL_LOCAL_ERROR;
288 error -= RXRPC_LOCAL_ERROR_OFFSET;
289 }
David Howellsf66d7492016-04-04 14:00:34 +0100290
David Howellsf5c17aa2016-08-30 09:49:28 +0100291 _debug("ISSUE ERROR %s %d", rxrpc_call_completions[compl], error);
David Howellsf66d7492016-04-04 14:00:34 +0100292
293 spin_lock_bh(&peer->lock);
294
295 while (!hlist_empty(&peer->error_targets)) {
296 call = hlist_entry(peer->error_targets.first,
297 struct rxrpc_call, error_link);
298 hlist_del_init(&call->error_link);
David Howellse34d4232016-08-30 09:49:29 +0100299 rxrpc_see_call(call);
David Howellsf66d7492016-04-04 14:00:34 +0100300
David Howells3a927892017-04-06 10:11:56 +0100301 if (rxrpc_set_call_completion(call, compl, 0, -error))
David Howells248f2192016-09-08 11:10:12 +0100302 rxrpc_notify_socket(call);
David Howells17926a72007-04-26 15:48:28 -0700303 }
304
David Howellsf66d7492016-04-04 14:00:34 +0100305 spin_unlock_bh(&peer->lock);
David Howells17926a72007-04-26 15:48:28 -0700306
David Howellsf66d7492016-04-04 14:00:34 +0100307 rxrpc_put_peer(peer);
David Howells17926a72007-04-26 15:48:28 -0700308 _leave("");
309}
David Howellscf1a6472016-09-22 00:41:53 +0100310
311/*
312 * Add RTT information to cache. This is called in softirq mode and has
313 * exclusive access to the peer RTT data.
314 */
315void rxrpc_peer_add_rtt(struct rxrpc_call *call, enum rxrpc_rtt_rx_trace why,
316 rxrpc_serial_t send_serial, rxrpc_serial_t resp_serial,
317 ktime_t send_time, ktime_t resp_time)
318{
319 struct rxrpc_peer *peer = call->peer;
320 s64 rtt;
321 u64 sum = peer->rtt_sum, avg;
322 u8 cursor = peer->rtt_cursor, usage = peer->rtt_usage;
323
324 rtt = ktime_to_ns(ktime_sub(resp_time, send_time));
325 if (rtt < 0)
326 return;
327
328 /* Replace the oldest datum in the RTT buffer */
329 sum -= peer->rtt_cache[cursor];
330 sum += rtt;
331 peer->rtt_cache[cursor] = rtt;
332 peer->rtt_cursor = (cursor + 1) & (RXRPC_RTT_CACHE_SIZE - 1);
333 peer->rtt_sum = sum;
334 if (usage < RXRPC_RTT_CACHE_SIZE) {
335 usage++;
336 peer->rtt_usage = usage;
337 }
338
339 /* Now recalculate the average */
340 if (usage == RXRPC_RTT_CACHE_SIZE) {
341 avg = sum / RXRPC_RTT_CACHE_SIZE;
342 } else {
343 avg = sum;
344 do_div(avg, usage);
345 }
346
347 peer->rtt = avg;
348 trace_rxrpc_rtt_rx(call, why, send_serial, resp_serial, rtt,
349 usage, avg);
350}