Merge "do not rely on ALLOW_ERROR_INJECTION for fmod_ret" into bpf-next
[platform/kernel/linux-starfive.git] / net / rxrpc / peer_event.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Peer event handling, typically ICMP messages.
3  *
4  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
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
21 static void rxrpc_store_error(struct rxrpc_peer *, struct sock_exterr_skb *);
22 static void rxrpc_distribute_error(struct rxrpc_peer *, int,
23                                    enum rxrpc_call_completion);
24
25 /*
26  * Find the peer associated with a local error.
27  */
28 static struct rxrpc_peer *rxrpc_lookup_peer_local_rcu(struct rxrpc_local *local,
29                                                       const struct sk_buff *skb,
30                                                       struct sockaddr_rxrpc *srx)
31 {
32         struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
33
34         _enter("");
35
36         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;
40
41         /* Can we see an ICMP4 packet on an ICMP6 listening socket?  and vice
42          * versa?
43          */
44         switch (srx->transport.family) {
45         case AF_INET:
46                 srx->transport_len = sizeof(srx->transport.sin);
47                 srx->transport.family = AF_INET;
48                 srx->transport.sin.sin_port = serr->port;
49                 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
69 #ifdef CONFIG_AF_RXRPC_IPV6
70         case AF_INET6:
71                 switch (serr->ee.ee_origin) {
72                 case SO_EE_ORIGIN_ICMP6:
73                         _net("Rx ICMP6");
74                         srx->transport.sin6.sin6_port = serr->port;
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");
81                         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,
85                                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;
95 #endif
96
97         default:
98                 BUG();
99         }
100
101         return rxrpc_lookup_peer_rcu(local, srx);
102 }
103
104 /*
105  * Handle an MTU/fragmentation problem.
106  */
107 static void rxrpc_adjust_mtu(struct rxrpc_peer *peer, unsigned int mtu)
108 {
109         _net("Rx ICMP Fragmentation Needed (%d)", mtu);
110
111         /* wind down the local interface MTU */
112         if (mtu > 0 && peer->if_mtu == 65535 && mtu < peer->if_mtu) {
113                 peer->if_mtu = mtu;
114                 _net("I/F MTU %u", mtu);
115         }
116
117         if (mtu == 0) {
118                 /* they didn't give us a size, estimate one */
119                 mtu = peer->if_mtu;
120                 if (mtu > 1500) {
121                         mtu >>= 1;
122                         if (mtu < 1500)
123                                 mtu = 1500;
124                 } else {
125                         mtu -= 100;
126                         if (mtu < peer->hdrsize)
127                                 mtu = peer->hdrsize + 4;
128                 }
129         }
130
131         if (mtu < peer->mtu) {
132                 spin_lock_bh(&peer->lock);
133                 peer->mtu = mtu;
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);
138         }
139 }
140
141 /*
142  * Handle an error received on the local endpoint.
143  */
144 void rxrpc_error_report(struct sock *sk)
145 {
146         struct sock_exterr_skb *serr;
147         struct sockaddr_rxrpc srx;
148         struct rxrpc_local *local;
149         struct rxrpc_peer *peer = NULL;
150         struct sk_buff *skb;
151
152         rcu_read_lock();
153         local = rcu_dereference_sk_user_data(sk);
154         if (unlikely(!local)) {
155                 rcu_read_unlock();
156                 return;
157         }
158         _enter("%p{%d}", sk, local->debug_id);
159
160         /* Clear the outstanding error value on the socket so that it doesn't
161          * cause kernel_sendmsg() to return it later.
162          */
163         sock_error(sk);
164
165         skb = sock_dequeue_err_skb(sk);
166         if (!skb) {
167                 rcu_read_unlock();
168                 _leave("UDP socket errqueue empty");
169                 return;
170         }
171         rxrpc_new_skb(skb, rxrpc_skb_received);
172         serr = SKB_EXT_ERR(skb);
173         if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) {
174                 _leave("UDP empty message");
175                 rcu_read_unlock();
176                 rxrpc_free_skb(skb, rxrpc_skb_freed);
177                 return;
178         }
179
180         peer = rxrpc_lookup_peer_local_rcu(local, skb, &srx);
181         if (peer && !rxrpc_get_peer_maybe(peer))
182                 peer = NULL;
183         if (!peer) {
184                 rcu_read_unlock();
185                 rxrpc_free_skb(skb, rxrpc_skb_freed);
186                 _leave(" [no peer]");
187                 return;
188         }
189
190         trace_rxrpc_rx_icmp(peer, &serr->ee, &srx);
191
192         if ((serr->ee.ee_origin == SO_EE_ORIGIN_ICMP &&
193              serr->ee.ee_type == ICMP_DEST_UNREACH &&
194              serr->ee.ee_code == ICMP_FRAG_NEEDED)) {
195                 rxrpc_adjust_mtu(peer, serr->ee.ee_info);
196                 goto out;
197         }
198
199         rxrpc_store_error(peer, serr);
200 out:
201         rcu_read_unlock();
202         rxrpc_free_skb(skb, rxrpc_skb_freed);
203         rxrpc_put_peer(peer);
204
205         _leave("");
206 }
207
208 /*
209  * Map an error report to error codes on the peer record.
210  */
211 static void rxrpc_store_error(struct rxrpc_peer *peer,
212                               struct sock_exterr_skb *serr)
213 {
214         enum rxrpc_call_completion compl = RXRPC_CALL_NETWORK_ERROR;
215         struct sock_extended_err *ee;
216         int err;
217
218         _enter("");
219
220         ee = &serr->ee;
221
222         err = ee->ee_errno;
223
224         switch (ee->ee_origin) {
225         case SO_EE_ORIGIN_ICMP:
226                 switch (ee->ee_type) {
227                 case ICMP_DEST_UNREACH:
228                         switch (ee->ee_code) {
229                         case ICMP_NET_UNREACH:
230                                 _net("Rx Received ICMP Network Unreachable");
231                                 break;
232                         case ICMP_HOST_UNREACH:
233                                 _net("Rx Received ICMP Host Unreachable");
234                                 break;
235                         case ICMP_PORT_UNREACH:
236                                 _net("Rx Received ICMP Port Unreachable");
237                                 break;
238                         case ICMP_NET_UNKNOWN:
239                                 _net("Rx Received ICMP Unknown Network");
240                                 break;
241                         case ICMP_HOST_UNKNOWN:
242                                 _net("Rx Received ICMP Unknown Host");
243                                 break;
244                         default:
245                                 _net("Rx Received ICMP DestUnreach code=%u",
246                                      ee->ee_code);
247                                 break;
248                         }
249                         break;
250
251                 case ICMP_TIME_EXCEEDED:
252                         _net("Rx Received ICMP TTL Exceeded");
253                         break;
254
255                 default:
256                         _proto("Rx Received ICMP error { type=%u code=%u }",
257                                ee->ee_type, ee->ee_code);
258                         break;
259                 }
260                 break;
261
262         case SO_EE_ORIGIN_NONE:
263         case SO_EE_ORIGIN_LOCAL:
264                 _proto("Rx Received local error { error=%d }", err);
265                 compl = RXRPC_CALL_LOCAL_ERROR;
266                 break;
267
268         case SO_EE_ORIGIN_ICMP6:
269                 if (err == EACCES)
270                         err = EHOSTUNREACH;
271                 fallthrough;
272         default:
273                 _proto("Rx Received error report { orig=%u }", ee->ee_origin);
274                 break;
275         }
276
277         rxrpc_distribute_error(peer, err, compl);
278 }
279
280 /*
281  * Distribute an error that occurred on a peer.
282  */
283 static void rxrpc_distribute_error(struct rxrpc_peer *peer, int error,
284                                    enum rxrpc_call_completion compl)
285 {
286         struct rxrpc_call *call;
287
288         hlist_for_each_entry_rcu(call, &peer->error_targets, error_link) {
289                 rxrpc_see_call(call);
290                 rxrpc_set_call_completion(call, compl, 0, -error);
291         }
292 }
293
294 /*
295  * Perform keep-alive pings.
296  */
297 static void rxrpc_peer_keepalive_dispatch(struct rxrpc_net *rxnet,
298                                           struct list_head *collector,
299                                           time64_t base,
300                                           u8 cursor)
301 {
302         struct rxrpc_peer *peer;
303         const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1;
304         time64_t keepalive_at;
305         int slot;
306
307         spin_lock_bh(&rxnet->peer_hash_lock);
308
309         while (!list_empty(collector)) {
310                 peer = list_entry(collector->next,
311                                   struct rxrpc_peer, keepalive_link);
312
313                 list_del_init(&peer->keepalive_link);
314                 if (!rxrpc_get_peer_maybe(peer))
315                         continue;
316
317                 if (__rxrpc_use_local(peer->local)) {
318                         spin_unlock_bh(&rxnet->peer_hash_lock);
319
320                         keepalive_at = peer->last_tx_at + RXRPC_KEEPALIVE_TIME;
321                         slot = keepalive_at - base;
322                         _debug("%02x peer %u t=%d {%pISp}",
323                                cursor, peer->debug_id, slot, &peer->srx.transport);
324
325                         if (keepalive_at <= base ||
326                             keepalive_at > base + RXRPC_KEEPALIVE_TIME) {
327                                 rxrpc_send_keepalive(peer);
328                                 slot = RXRPC_KEEPALIVE_TIME;
329                         }
330
331                         /* A transmission to this peer occurred since last we
332                          * examined it so put it into the appropriate future
333                          * bucket.
334                          */
335                         slot += cursor;
336                         slot &= mask;
337                         spin_lock_bh(&rxnet->peer_hash_lock);
338                         list_add_tail(&peer->keepalive_link,
339                                       &rxnet->peer_keepalive[slot & mask]);
340                         rxrpc_unuse_local(peer->local);
341                 }
342                 rxrpc_put_peer_locked(peer);
343         }
344
345         spin_unlock_bh(&rxnet->peer_hash_lock);
346 }
347
348 /*
349  * Perform keep-alive pings with VERSION packets to keep any NAT alive.
350  */
351 void rxrpc_peer_keepalive_worker(struct work_struct *work)
352 {
353         struct rxrpc_net *rxnet =
354                 container_of(work, struct rxrpc_net, peer_keepalive_work);
355         const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1;
356         time64_t base, now, delay;
357         u8 cursor, stop;
358         LIST_HEAD(collector);
359
360         now = ktime_get_seconds();
361         base = rxnet->peer_keepalive_base;
362         cursor = rxnet->peer_keepalive_cursor;
363         _enter("%lld,%u", base - now, cursor);
364
365         if (!rxnet->live)
366                 return;
367
368         /* Remove to a temporary list all the peers that are currently lodged
369          * in expired buckets plus all new peers.
370          *
371          * Everything in the bucket at the cursor is processed this
372          * second; the bucket at cursor + 1 goes at now + 1s and so
373          * on...
374          */
375         spin_lock_bh(&rxnet->peer_hash_lock);
376         list_splice_init(&rxnet->peer_keepalive_new, &collector);
377
378         stop = cursor + ARRAY_SIZE(rxnet->peer_keepalive);
379         while (base <= now && (s8)(cursor - stop) < 0) {
380                 list_splice_tail_init(&rxnet->peer_keepalive[cursor & mask],
381                                       &collector);
382                 base++;
383                 cursor++;
384         }
385
386         base = now;
387         spin_unlock_bh(&rxnet->peer_hash_lock);
388
389         rxnet->peer_keepalive_base = base;
390         rxnet->peer_keepalive_cursor = cursor;
391         rxrpc_peer_keepalive_dispatch(rxnet, &collector, base, cursor);
392         ASSERT(list_empty(&collector));
393
394         /* Schedule the timer for the next occupied timeslot. */
395         cursor = rxnet->peer_keepalive_cursor;
396         stop = cursor + RXRPC_KEEPALIVE_TIME - 1;
397         for (; (s8)(cursor - stop) < 0; cursor++) {
398                 if (!list_empty(&rxnet->peer_keepalive[cursor & mask]))
399                         break;
400                 base++;
401         }
402
403         now = ktime_get_seconds();
404         delay = base - now;
405         if (delay < 1)
406                 delay = 1;
407         delay *= HZ;
408         if (rxnet->live)
409                 timer_reduce(&rxnet->peer_keepalive_timer, jiffies + delay);
410
411         _leave("");
412 }