blob: 9f2f45c09e58353d11e1b8abc9345209a92dc726 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howellsf66d7492016-04-04 14:00:34 +01002/* Peer event handling, typically ICMP messages.
David Howells17926a72007-04-26 15:48:28 -07003 *
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howells17926a72007-04-26 15:48:28 -07006 */
7
8#include <linux/module.h>
9#include <linux/net.h>
10#include <linux/skbuff.h>
11#include <linux/errqueue.h>
12#include <linux/udp.h>
13#include <linux/in.h>
14#include <linux/in6.h>
15#include <linux/icmp.h>
16#include <net/sock.h>
17#include <net/af_rxrpc.h>
18#include <net/ip.h>
19#include "ar-internal.h"
20
David Howellsf66d7492016-04-04 14:00:34 +010021static void rxrpc_store_error(struct rxrpc_peer *, struct sock_exterr_skb *);
David Howellsf3344302018-09-27 15:13:09 +010022static void rxrpc_distribute_error(struct rxrpc_peer *, int,
23 enum rxrpc_call_completion);
David Howellsf66d7492016-04-04 14:00:34 +010024
David Howells17926a72007-04-26 15:48:28 -070025/*
David Howellsbe6e6702016-04-04 14:00:32 +010026 * Find the peer associated with an ICMP packet.
27 */
28static struct rxrpc_peer *rxrpc_lookup_peer_icmp_rcu(struct rxrpc_local *local,
David Howells494337c2018-05-10 23:26:01 +010029 const struct sk_buff *skb,
30 struct sockaddr_rxrpc *srx)
David Howellsbe6e6702016-04-04 14:00:32 +010031{
32 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
David Howellsbe6e6702016-04-04 14:00:32 +010033
34 _enter("");
35
David Howells494337c2018-05-10 23:26:01 +010036 memset(srx, 0, sizeof(*srx));
37 srx->transport_type = local->srx.transport_type;
38 srx->transport_len = local->srx.transport_len;
39 srx->transport.family = local->srx.transport.family;
David Howellsbe6e6702016-04-04 14:00:32 +010040
41 /* Can we see an ICMP4 packet on an ICMP6 listening socket? and vice
42 * versa?
43 */
David Howells494337c2018-05-10 23:26:01 +010044 switch (srx->transport.family) {
David Howellsbe6e6702016-04-04 14:00:32 +010045 case AF_INET:
David Howells46894a12018-10-04 09:32:28 +010046 srx->transport_len = sizeof(srx->transport.sin);
47 srx->transport.family = AF_INET;
David Howells494337c2018-05-10 23:26:01 +010048 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");
David Howells494337c2018-05-10 23:26:01 +010052 memcpy(&srx->transport.sin.sin_addr,
David Howellsbe6e6702016-04-04 14:00:32 +010053 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");
David Howells494337c2018-05-10 23:26:01 +010058 memcpy(&srx->transport.sin.sin_addr,
David Howellsbe6e6702016-04-04 14:00:32 +010059 skb_network_header(skb) + serr->addr_offset + 12,
60 sizeof(struct in_addr));
61 break;
62 default:
David Howells494337c2018-05-10 23:26:01 +010063 memcpy(&srx->transport.sin.sin_addr, &ip_hdr(skb)->saddr,
David Howellsbe6e6702016-04-04 14:00:32 +010064 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:
David Howells75b54cb2016-09-13 08:49:05 +010071 switch (serr->ee.ee_origin) {
72 case SO_EE_ORIGIN_ICMP6:
73 _net("Rx ICMP6");
David Howells46894a12018-10-04 09:32:28 +010074 srx->transport.sin6.sin6_port = serr->port;
David Howells494337c2018-05-10 23:26:01 +010075 memcpy(&srx->transport.sin6.sin6_addr,
David Howells75b54cb2016-09-13 08:49:05 +010076 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 Howells46894a12018-10-04 09:32:28 +010081 srx->transport_len = sizeof(srx->transport.sin);
82 srx->transport.family = AF_INET;
83 srx->transport.sin.sin_port = serr->port;
84 memcpy(&srx->transport.sin.sin_addr,
David Howells75b54cb2016-09-13 08:49:05 +010085 skb_network_header(skb) + serr->addr_offset,
86 sizeof(struct in_addr));
87 break;
88 default:
David Howells494337c2018-05-10 23:26:01 +010089 memcpy(&srx->transport.sin6.sin6_addr,
David Howells75b54cb2016-09-13 08:49:05 +010090 &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
David Howells494337c2018-05-10 23:26:01 +0100101 return rxrpc_lookup_peer_rcu(local, srx);
David Howellsbe6e6702016-04-04 14:00:32 +0100102}
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 Howells494337c2018-05-10 23:26:01 +0100149 struct sockaddr_rxrpc srx;
David Howells17926a72007-04-26 15:48:28 -0700150 struct rxrpc_local *local = sk->sk_user_data;
151 struct rxrpc_peer *peer;
152 struct sk_buff *skb;
David Howells17926a72007-04-26 15:48:28 -0700153
154 _enter("%p{%d}", sk, local->debug_id);
155
Marc Dionne56d282d2019-04-12 16:33:40 +0100156 /* Clear the outstanding error value on the socket so that it doesn't
157 * cause kernel_sendmsg() to return it later.
158 */
159 sock_error(sk);
160
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400161 skb = sock_dequeue_err_skb(sk);
David Howells17926a72007-04-26 15:48:28 -0700162 if (!skb) {
163 _leave("UDP socket errqueue empty");
164 return;
165 }
David Howells71f3ca42016-09-17 10:49:14 +0100166 rxrpc_new_skb(skb, rxrpc_skb_rx_received);
Willem de Bruijnc247f052015-03-07 20:33:22 -0500167 serr = SKB_EXT_ERR(skb);
168 if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) {
Willem de Bruijn49ca0d82015-01-30 13:29:31 -0500169 _leave("UDP empty message");
David Howells71f3ca42016-09-17 10:49:14 +0100170 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
Willem de Bruijn49ca0d82015-01-30 13:29:31 -0500171 return;
172 }
David Howells17926a72007-04-26 15:48:28 -0700173
David Howellsbe6e6702016-04-04 14:00:32 +0100174 rcu_read_lock();
David Howells494337c2018-05-10 23:26:01 +0100175 peer = rxrpc_lookup_peer_icmp_rcu(local, skb, &srx);
David Howellsbe6e6702016-04-04 14:00:32 +0100176 if (peer && !rxrpc_get_peer_maybe(peer))
177 peer = NULL;
178 if (!peer) {
179 rcu_read_unlock();
David Howells71f3ca42016-09-17 10:49:14 +0100180 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
David Howells17926a72007-04-26 15:48:28 -0700181 _leave(" [no peer]");
182 return;
183 }
184
David Howells494337c2018-05-10 23:26:01 +0100185 trace_rxrpc_rx_icmp(peer, &serr->ee, &srx);
186
David Howells1a70c052016-04-04 14:00:33 +0100187 if ((serr->ee.ee_origin == SO_EE_ORIGIN_ICMP &&
188 serr->ee.ee_type == ICMP_DEST_UNREACH &&
189 serr->ee.ee_code == ICMP_FRAG_NEEDED)) {
190 rxrpc_adjust_mtu(peer, serr);
David Howellsf66d7492016-04-04 14:00:34 +0100191 rcu_read_unlock();
David Howells71f3ca42016-09-17 10:49:14 +0100192 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
David Howellsf66d7492016-04-04 14:00:34 +0100193 rxrpc_put_peer(peer);
194 _leave(" [MTU update]");
195 return;
David Howells17926a72007-04-26 15:48:28 -0700196 }
197
David Howellsf66d7492016-04-04 14:00:34 +0100198 rxrpc_store_error(peer, serr);
David Howellsbe6e6702016-04-04 14:00:32 +0100199 rcu_read_unlock();
David Howells71f3ca42016-09-17 10:49:14 +0100200 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
David Howells1890fea2018-10-15 22:37:21 +0100201 rxrpc_put_peer(peer);
David Howells17926a72007-04-26 15:48:28 -0700202
David Howells17926a72007-04-26 15:48:28 -0700203 _leave("");
204}
205
206/*
David Howellsf66d7492016-04-04 14:00:34 +0100207 * Map an error report to error codes on the peer record.
David Howells17926a72007-04-26 15:48:28 -0700208 */
David Howellsf66d7492016-04-04 14:00:34 +0100209static void rxrpc_store_error(struct rxrpc_peer *peer,
210 struct sock_exterr_skb *serr)
David Howells17926a72007-04-26 15:48:28 -0700211{
David Howellsf3344302018-09-27 15:13:09 +0100212 enum rxrpc_call_completion compl = RXRPC_CALL_NETWORK_ERROR;
David Howells17926a72007-04-26 15:48:28 -0700213 struct sock_extended_err *ee;
David S. Millerc9d10c42011-05-19 18:37:11 -0400214 int err;
David Howells17926a72007-04-26 15:48:28 -0700215
216 _enter("");
217
David Howells17926a72007-04-26 15:48:28 -0700218 ee = &serr->ee;
219
David Howells17926a72007-04-26 15:48:28 -0700220 err = ee->ee_errno;
221
222 switch (ee->ee_origin) {
223 case SO_EE_ORIGIN_ICMP:
David Howells17926a72007-04-26 15:48:28 -0700224 switch (ee->ee_type) {
225 case ICMP_DEST_UNREACH:
226 switch (ee->ee_code) {
227 case ICMP_NET_UNREACH:
228 _net("Rx Received ICMP Network Unreachable");
David Howells17926a72007-04-26 15:48:28 -0700229 break;
230 case ICMP_HOST_UNREACH:
231 _net("Rx Received ICMP Host Unreachable");
David Howells17926a72007-04-26 15:48:28 -0700232 break;
233 case ICMP_PORT_UNREACH:
234 _net("Rx Received ICMP Port Unreachable");
David Howells17926a72007-04-26 15:48:28 -0700235 break;
236 case ICMP_NET_UNKNOWN:
237 _net("Rx Received ICMP Unknown Network");
David Howells17926a72007-04-26 15:48:28 -0700238 break;
239 case ICMP_HOST_UNKNOWN:
240 _net("Rx Received ICMP Unknown Host");
David Howells17926a72007-04-26 15:48:28 -0700241 break;
242 default:
243 _net("Rx Received ICMP DestUnreach code=%u",
244 ee->ee_code);
245 break;
246 }
247 break;
248
249 case ICMP_TIME_EXCEEDED:
250 _net("Rx Received ICMP TTL Exceeded");
251 break;
252
253 default:
254 _proto("Rx Received ICMP error { type=%u code=%u }",
255 ee->ee_type, ee->ee_code);
256 break;
257 }
258 break;
259
David Howellsf66d7492016-04-04 14:00:34 +0100260 case SO_EE_ORIGIN_NONE:
David Howells17926a72007-04-26 15:48:28 -0700261 case SO_EE_ORIGIN_LOCAL:
David Howellsfe77d5f2016-04-04 14:00:34 +0100262 _proto("Rx Received local error { error=%d }", err);
David Howellsf3344302018-09-27 15:13:09 +0100263 compl = RXRPC_CALL_LOCAL_ERROR;
David Howells17926a72007-04-26 15:48:28 -0700264 break;
265
David Howells17926a72007-04-26 15:48:28 -0700266 case SO_EE_ORIGIN_ICMP6:
267 default:
David Howellsfe77d5f2016-04-04 14:00:34 +0100268 _proto("Rx Received error report { orig=%u }", ee->ee_origin);
David Howells17926a72007-04-26 15:48:28 -0700269 break;
270 }
271
David Howellsf3344302018-09-27 15:13:09 +0100272 rxrpc_distribute_error(peer, err, compl);
David Howellsf66d7492016-04-04 14:00:34 +0100273}
David Howells17926a72007-04-26 15:48:28 -0700274
David Howellsf66d7492016-04-04 14:00:34 +0100275/*
David Howellsf3344302018-09-27 15:13:09 +0100276 * Distribute an error that occurred on a peer.
David Howellsf66d7492016-04-04 14:00:34 +0100277 */
David Howellsf3344302018-09-27 15:13:09 +0100278static void rxrpc_distribute_error(struct rxrpc_peer *peer, int error,
279 enum rxrpc_call_completion compl)
David Howellsf66d7492016-04-04 14:00:34 +0100280{
David Howellsf66d7492016-04-04 14:00:34 +0100281 struct rxrpc_call *call;
David Howells17926a72007-04-26 15:48:28 -0700282
David Howellsf3344302018-09-27 15:13:09 +0100283 hlist_for_each_entry_rcu(call, &peer->error_targets, error_link) {
David Howellse34d4232016-08-30 09:49:29 +0100284 rxrpc_see_call(call);
David Howellsf3344302018-09-27 15:13:09 +0100285 if (call->state < RXRPC_CALL_COMPLETE &&
286 rxrpc_set_call_completion(call, compl, 0, -error))
David Howells248f2192016-09-08 11:10:12 +0100287 rxrpc_notify_socket(call);
David Howells17926a72007-04-26 15:48:28 -0700288 }
David Howells17926a72007-04-26 15:48:28 -0700289}
David Howellscf1a6472016-09-22 00:41:53 +0100290
291/*
292 * Add RTT information to cache. This is called in softirq mode and has
293 * exclusive access to the peer RTT data.
294 */
295void rxrpc_peer_add_rtt(struct rxrpc_call *call, enum rxrpc_rtt_rx_trace why,
296 rxrpc_serial_t send_serial, rxrpc_serial_t resp_serial,
297 ktime_t send_time, ktime_t resp_time)
298{
299 struct rxrpc_peer *peer = call->peer;
300 s64 rtt;
301 u64 sum = peer->rtt_sum, avg;
302 u8 cursor = peer->rtt_cursor, usage = peer->rtt_usage;
303
304 rtt = ktime_to_ns(ktime_sub(resp_time, send_time));
305 if (rtt < 0)
306 return;
307
David Howellsc1e15b42018-10-08 15:46:25 +0100308 spin_lock(&peer->rtt_input_lock);
309
David Howellscf1a6472016-09-22 00:41:53 +0100310 /* Replace the oldest datum in the RTT buffer */
311 sum -= peer->rtt_cache[cursor];
312 sum += rtt;
313 peer->rtt_cache[cursor] = rtt;
314 peer->rtt_cursor = (cursor + 1) & (RXRPC_RTT_CACHE_SIZE - 1);
315 peer->rtt_sum = sum;
316 if (usage < RXRPC_RTT_CACHE_SIZE) {
317 usage++;
318 peer->rtt_usage = usage;
319 }
320
David Howellsc1e15b42018-10-08 15:46:25 +0100321 spin_unlock(&peer->rtt_input_lock);
322
David Howellscf1a6472016-09-22 00:41:53 +0100323 /* Now recalculate the average */
324 if (usage == RXRPC_RTT_CACHE_SIZE) {
325 avg = sum / RXRPC_RTT_CACHE_SIZE;
326 } else {
327 avg = sum;
328 do_div(avg, usage);
329 }
330
David Howellsc1e15b42018-10-08 15:46:25 +0100331 /* Don't need to update this under lock */
David Howellscf1a6472016-09-22 00:41:53 +0100332 peer->rtt = avg;
333 trace_rxrpc_rtt_rx(call, why, send_serial, resp_serial, rtt,
334 usage, avg);
335}
David Howellsace45be2018-03-30 21:04:43 +0100336
337/*
David Howells330bdcf2018-08-08 11:30:02 +0100338 * Perform keep-alive pings.
339 */
340static void rxrpc_peer_keepalive_dispatch(struct rxrpc_net *rxnet,
341 struct list_head *collector,
342 time64_t base,
343 u8 cursor)
344{
345 struct rxrpc_peer *peer;
346 const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1;
347 time64_t keepalive_at;
348 int slot;
349
350 spin_lock_bh(&rxnet->peer_hash_lock);
351
352 while (!list_empty(collector)) {
353 peer = list_entry(collector->next,
354 struct rxrpc_peer, keepalive_link);
355
356 list_del_init(&peer->keepalive_link);
357 if (!rxrpc_get_peer_maybe(peer))
358 continue;
359
360 spin_unlock_bh(&rxnet->peer_hash_lock);
361
362 keepalive_at = peer->last_tx_at + RXRPC_KEEPALIVE_TIME;
363 slot = keepalive_at - base;
364 _debug("%02x peer %u t=%d {%pISp}",
365 cursor, peer->debug_id, slot, &peer->srx.transport);
366
367 if (keepalive_at <= base ||
368 keepalive_at > base + RXRPC_KEEPALIVE_TIME) {
369 rxrpc_send_keepalive(peer);
370 slot = RXRPC_KEEPALIVE_TIME;
371 }
372
373 /* A transmission to this peer occurred since last we examined
374 * it so put it into the appropriate future bucket.
375 */
376 slot += cursor;
377 slot &= mask;
378 spin_lock_bh(&rxnet->peer_hash_lock);
379 list_add_tail(&peer->keepalive_link,
380 &rxnet->peer_keepalive[slot & mask]);
381 rxrpc_put_peer(peer);
382 }
383
384 spin_unlock_bh(&rxnet->peer_hash_lock);
385}
386
387/*
David Howellsace45be2018-03-30 21:04:43 +0100388 * Perform keep-alive pings with VERSION packets to keep any NAT alive.
389 */
390void rxrpc_peer_keepalive_worker(struct work_struct *work)
391{
392 struct rxrpc_net *rxnet =
393 container_of(work, struct rxrpc_net, peer_keepalive_work);
David Howells330bdcf2018-08-08 11:30:02 +0100394 const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1;
395 time64_t base, now, delay;
396 u8 cursor, stop;
397 LIST_HEAD(collector);
David Howellsace45be2018-03-30 21:04:43 +0100398
David Howells330bdcf2018-08-08 11:30:02 +0100399 now = ktime_get_seconds();
David Howellsace45be2018-03-30 21:04:43 +0100400 base = rxnet->peer_keepalive_base;
401 cursor = rxnet->peer_keepalive_cursor;
David Howells330bdcf2018-08-08 11:30:02 +0100402 _enter("%lld,%u", base - now, cursor);
David Howellsace45be2018-03-30 21:04:43 +0100403
David Howells330bdcf2018-08-08 11:30:02 +0100404 if (!rxnet->live)
405 return;
David Howellsace45be2018-03-30 21:04:43 +0100406
David Howells330bdcf2018-08-08 11:30:02 +0100407 /* Remove to a temporary list all the peers that are currently lodged
408 * in expired buckets plus all new peers.
409 *
410 * Everything in the bucket at the cursor is processed this
411 * second; the bucket at cursor + 1 goes at now + 1s and so
412 * on...
David Howellsace45be2018-03-30 21:04:43 +0100413 */
David Howells330bdcf2018-08-08 11:30:02 +0100414 spin_lock_bh(&rxnet->peer_hash_lock);
415 list_splice_init(&rxnet->peer_keepalive_new, &collector);
David Howellsace45be2018-03-30 21:04:43 +0100416
David Howells330bdcf2018-08-08 11:30:02 +0100417 stop = cursor + ARRAY_SIZE(rxnet->peer_keepalive);
418 while (base <= now && (s8)(cursor - stop) < 0) {
419 list_splice_tail_init(&rxnet->peer_keepalive[cursor & mask],
420 &collector);
421 base++;
422 cursor++;
David Howellsace45be2018-03-30 21:04:43 +0100423 }
424
David Howells330bdcf2018-08-08 11:30:02 +0100425 base = now;
David Howellsace45be2018-03-30 21:04:43 +0100426 spin_unlock_bh(&rxnet->peer_hash_lock);
427
David Howellsace45be2018-03-30 21:04:43 +0100428 rxnet->peer_keepalive_base = base;
429 rxnet->peer_keepalive_cursor = cursor;
David Howells330bdcf2018-08-08 11:30:02 +0100430 rxrpc_peer_keepalive_dispatch(rxnet, &collector, base, cursor);
431 ASSERT(list_empty(&collector));
432
433 /* Schedule the timer for the next occupied timeslot. */
434 cursor = rxnet->peer_keepalive_cursor;
435 stop = cursor + RXRPC_KEEPALIVE_TIME - 1;
436 for (; (s8)(cursor - stop) < 0; cursor++) {
437 if (!list_empty(&rxnet->peer_keepalive[cursor & mask]))
438 break;
439 base++;
440 }
441
442 now = ktime_get_seconds();
443 delay = base - now;
444 if (delay < 1)
445 delay = 1;
446 delay *= HZ;
447 if (rxnet->live)
448 timer_reduce(&rxnet->peer_keepalive_timer, jiffies + delay);
449
David Howellsace45be2018-03-30 21:04:43 +0100450 _leave("");
451}