rxrpc: trace: Don't use __builtin_return_address for rxrpc_local tracing
[platform/kernel/linux-rpi.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                         memcpy(&srx->transport.sin.sin_addr,
52                                skb_network_header(skb) + serr->addr_offset,
53                                sizeof(struct in_addr));
54                         break;
55                 case SO_EE_ORIGIN_ICMP6:
56                         memcpy(&srx->transport.sin.sin_addr,
57                                skb_network_header(skb) + serr->addr_offset + 12,
58                                sizeof(struct in_addr));
59                         break;
60                 default:
61                         memcpy(&srx->transport.sin.sin_addr, &ip_hdr(skb)->saddr,
62                                sizeof(struct in_addr));
63                         break;
64                 }
65                 break;
66
67 #ifdef CONFIG_AF_RXRPC_IPV6
68         case AF_INET6:
69                 switch (serr->ee.ee_origin) {
70                 case SO_EE_ORIGIN_ICMP6:
71                         srx->transport.sin6.sin6_port = serr->port;
72                         memcpy(&srx->transport.sin6.sin6_addr,
73                                skb_network_header(skb) + serr->addr_offset,
74                                sizeof(struct in6_addr));
75                         break;
76                 case SO_EE_ORIGIN_ICMP:
77                         srx->transport_len = sizeof(srx->transport.sin);
78                         srx->transport.family = AF_INET;
79                         srx->transport.sin.sin_port = serr->port;
80                         memcpy(&srx->transport.sin.sin_addr,
81                                skb_network_header(skb) + serr->addr_offset,
82                                sizeof(struct in_addr));
83                         break;
84                 default:
85                         memcpy(&srx->transport.sin6.sin6_addr,
86                                &ipv6_hdr(skb)->saddr,
87                                sizeof(struct in6_addr));
88                         break;
89                 }
90                 break;
91 #endif
92
93         default:
94                 BUG();
95         }
96
97         return rxrpc_lookup_peer_rcu(local, srx);
98 }
99
100 /*
101  * Handle an MTU/fragmentation problem.
102  */
103 static void rxrpc_adjust_mtu(struct rxrpc_peer *peer, unsigned int mtu)
104 {
105         /* wind down the local interface MTU */
106         if (mtu > 0 && peer->if_mtu == 65535 && mtu < peer->if_mtu)
107                 peer->if_mtu = mtu;
108
109         if (mtu == 0) {
110                 /* they didn't give us a size, estimate one */
111                 mtu = peer->if_mtu;
112                 if (mtu > 1500) {
113                         mtu >>= 1;
114                         if (mtu < 1500)
115                                 mtu = 1500;
116                 } else {
117                         mtu -= 100;
118                         if (mtu < peer->hdrsize)
119                                 mtu = peer->hdrsize + 4;
120                 }
121         }
122
123         if (mtu < peer->mtu) {
124                 spin_lock_bh(&peer->lock);
125                 peer->mtu = mtu;
126                 peer->maxdata = peer->mtu - peer->hdrsize;
127                 spin_unlock_bh(&peer->lock);
128         }
129 }
130
131 /*
132  * Handle an error received on the local endpoint.
133  */
134 void rxrpc_error_report(struct sock *sk)
135 {
136         struct sock_exterr_skb *serr;
137         struct sockaddr_rxrpc srx;
138         struct rxrpc_local *local;
139         struct rxrpc_peer *peer = NULL;
140         struct sk_buff *skb;
141
142         rcu_read_lock();
143         local = rcu_dereference_sk_user_data(sk);
144         if (unlikely(!local)) {
145                 rcu_read_unlock();
146                 return;
147         }
148         _enter("%p{%d}", sk, local->debug_id);
149
150         /* Clear the outstanding error value on the socket so that it doesn't
151          * cause kernel_sendmsg() to return it later.
152          */
153         sock_error(sk);
154
155         skb = sock_dequeue_err_skb(sk);
156         if (!skb) {
157                 rcu_read_unlock();
158                 _leave("UDP socket errqueue empty");
159                 return;
160         }
161         rxrpc_new_skb(skb, rxrpc_skb_received);
162         serr = SKB_EXT_ERR(skb);
163         if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) {
164                 _leave("UDP empty message");
165                 rcu_read_unlock();
166                 rxrpc_free_skb(skb, rxrpc_skb_freed);
167                 return;
168         }
169
170         peer = rxrpc_lookup_peer_local_rcu(local, skb, &srx);
171         if (peer && !rxrpc_get_peer_maybe(peer))
172                 peer = NULL;
173         if (!peer) {
174                 rcu_read_unlock();
175                 rxrpc_free_skb(skb, rxrpc_skb_freed);
176                 _leave(" [no peer]");
177                 return;
178         }
179
180         trace_rxrpc_rx_icmp(peer, &serr->ee, &srx);
181
182         if ((serr->ee.ee_origin == SO_EE_ORIGIN_ICMP &&
183              serr->ee.ee_type == ICMP_DEST_UNREACH &&
184              serr->ee.ee_code == ICMP_FRAG_NEEDED)) {
185                 rxrpc_adjust_mtu(peer, serr->ee.ee_info);
186                 goto out;
187         }
188
189         rxrpc_store_error(peer, serr);
190 out:
191         rcu_read_unlock();
192         rxrpc_free_skb(skb, rxrpc_skb_freed);
193         rxrpc_put_peer(peer);
194
195         _leave("");
196 }
197
198 /*
199  * Map an error report to error codes on the peer record.
200  */
201 static void rxrpc_store_error(struct rxrpc_peer *peer,
202                               struct sock_exterr_skb *serr)
203 {
204         enum rxrpc_call_completion compl = RXRPC_CALL_NETWORK_ERROR;
205         struct sock_extended_err *ee;
206         int err;
207
208         _enter("");
209
210         ee = &serr->ee;
211
212         err = ee->ee_errno;
213
214         switch (ee->ee_origin) {
215         case SO_EE_ORIGIN_NONE:
216         case SO_EE_ORIGIN_LOCAL:
217                 compl = RXRPC_CALL_LOCAL_ERROR;
218                 break;
219
220         case SO_EE_ORIGIN_ICMP6:
221                 if (err == EACCES)
222                         err = EHOSTUNREACH;
223                 fallthrough;
224         case SO_EE_ORIGIN_ICMP:
225         default:
226                 break;
227         }
228
229         rxrpc_distribute_error(peer, err, compl);
230 }
231
232 /*
233  * Distribute an error that occurred on a peer.
234  */
235 static void rxrpc_distribute_error(struct rxrpc_peer *peer, int error,
236                                    enum rxrpc_call_completion compl)
237 {
238         struct rxrpc_call *call;
239
240         hlist_for_each_entry_rcu(call, &peer->error_targets, error_link) {
241                 rxrpc_see_call(call);
242                 rxrpc_set_call_completion(call, compl, 0, -error);
243         }
244 }
245
246 /*
247  * Perform keep-alive pings.
248  */
249 static void rxrpc_peer_keepalive_dispatch(struct rxrpc_net *rxnet,
250                                           struct list_head *collector,
251                                           time64_t base,
252                                           u8 cursor)
253 {
254         struct rxrpc_peer *peer;
255         const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1;
256         time64_t keepalive_at;
257         int slot;
258
259         spin_lock_bh(&rxnet->peer_hash_lock);
260
261         while (!list_empty(collector)) {
262                 peer = list_entry(collector->next,
263                                   struct rxrpc_peer, keepalive_link);
264
265                 list_del_init(&peer->keepalive_link);
266                 if (!rxrpc_get_peer_maybe(peer))
267                         continue;
268
269                 if (__rxrpc_use_local(peer->local, rxrpc_local_use_peer_keepalive)) {
270                         spin_unlock_bh(&rxnet->peer_hash_lock);
271
272                         keepalive_at = peer->last_tx_at + RXRPC_KEEPALIVE_TIME;
273                         slot = keepalive_at - base;
274                         _debug("%02x peer %u t=%d {%pISp}",
275                                cursor, peer->debug_id, slot, &peer->srx.transport);
276
277                         if (keepalive_at <= base ||
278                             keepalive_at > base + RXRPC_KEEPALIVE_TIME) {
279                                 rxrpc_send_keepalive(peer);
280                                 slot = RXRPC_KEEPALIVE_TIME;
281                         }
282
283                         /* A transmission to this peer occurred since last we
284                          * examined it so put it into the appropriate future
285                          * bucket.
286                          */
287                         slot += cursor;
288                         slot &= mask;
289                         spin_lock_bh(&rxnet->peer_hash_lock);
290                         list_add_tail(&peer->keepalive_link,
291                                       &rxnet->peer_keepalive[slot & mask]);
292                         rxrpc_unuse_local(peer->local, rxrpc_local_unuse_peer_keepalive);
293                 }
294                 rxrpc_put_peer_locked(peer);
295         }
296
297         spin_unlock_bh(&rxnet->peer_hash_lock);
298 }
299
300 /*
301  * Perform keep-alive pings with VERSION packets to keep any NAT alive.
302  */
303 void rxrpc_peer_keepalive_worker(struct work_struct *work)
304 {
305         struct rxrpc_net *rxnet =
306                 container_of(work, struct rxrpc_net, peer_keepalive_work);
307         const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1;
308         time64_t base, now, delay;
309         u8 cursor, stop;
310         LIST_HEAD(collector);
311
312         now = ktime_get_seconds();
313         base = rxnet->peer_keepalive_base;
314         cursor = rxnet->peer_keepalive_cursor;
315         _enter("%lld,%u", base - now, cursor);
316
317         if (!rxnet->live)
318                 return;
319
320         /* Remove to a temporary list all the peers that are currently lodged
321          * in expired buckets plus all new peers.
322          *
323          * Everything in the bucket at the cursor is processed this
324          * second; the bucket at cursor + 1 goes at now + 1s and so
325          * on...
326          */
327         spin_lock_bh(&rxnet->peer_hash_lock);
328         list_splice_init(&rxnet->peer_keepalive_new, &collector);
329
330         stop = cursor + ARRAY_SIZE(rxnet->peer_keepalive);
331         while (base <= now && (s8)(cursor - stop) < 0) {
332                 list_splice_tail_init(&rxnet->peer_keepalive[cursor & mask],
333                                       &collector);
334                 base++;
335                 cursor++;
336         }
337
338         base = now;
339         spin_unlock_bh(&rxnet->peer_hash_lock);
340
341         rxnet->peer_keepalive_base = base;
342         rxnet->peer_keepalive_cursor = cursor;
343         rxrpc_peer_keepalive_dispatch(rxnet, &collector, base, cursor);
344         ASSERT(list_empty(&collector));
345
346         /* Schedule the timer for the next occupied timeslot. */
347         cursor = rxnet->peer_keepalive_cursor;
348         stop = cursor + RXRPC_KEEPALIVE_TIME - 1;
349         for (; (s8)(cursor - stop) < 0; cursor++) {
350                 if (!list_empty(&rxnet->peer_keepalive[cursor & mask]))
351                         break;
352                 base++;
353         }
354
355         now = ktime_get_seconds();
356         delay = base - now;
357         if (delay < 1)
358                 delay = 1;
359         delay *= HZ;
360         if (rxnet->live)
361                 timer_reduce(&rxnet->peer_keepalive_timer, jiffies + delay);
362
363         _leave("");
364 }