~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/rxrpc/peer_event.c

Version: ~ [ linux-5.15-rc5 ] ~ [ linux-5.14.11 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.72 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.152 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.210 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.250 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.286 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.288 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* Peer event handling, typically ICMP messages.
  2  *
  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 
 25 static void rxrpc_store_error(struct rxrpc_peer *, struct sock_exterr_skb *);
 26 static void rxrpc_distribute_error(struct rxrpc_peer *, int,
 27                                    enum rxrpc_call_completion);
 28 
 29 /*
 30  * Find the peer associated with an ICMP packet.
 31  */
 32 static struct rxrpc_peer *rxrpc_lookup_peer_icmp_rcu(struct rxrpc_local *local,
 33                                                      const struct sk_buff *skb,
 34                                                      struct sockaddr_rxrpc *srx)
 35 {
 36         struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
 37 
 38         _enter("");
 39 
 40         memset(srx, 0, sizeof(*srx));
 41         srx->transport_type = local->srx.transport_type;
 42         srx->transport_len = local->srx.transport_len;
 43         srx->transport.family = local->srx.transport.family;
 44 
 45         /* Can we see an ICMP4 packet on an ICMP6 listening socket?  and vice
 46          * versa?
 47          */
 48         switch (srx->transport.family) {
 49         case AF_INET:
 50                 srx->transport.sin.sin_port = serr->port;
 51                 switch (serr->ee.ee_origin) {
 52                 case SO_EE_ORIGIN_ICMP:
 53                         _net("Rx ICMP");
 54                         memcpy(&srx->transport.sin.sin_addr,
 55                                skb_network_header(skb) + serr->addr_offset,
 56                                sizeof(struct in_addr));
 57                         break;
 58                 case SO_EE_ORIGIN_ICMP6:
 59                         _net("Rx ICMP6 on v4 sock");
 60                         memcpy(&srx->transport.sin.sin_addr,
 61                                skb_network_header(skb) + serr->addr_offset + 12,
 62                                sizeof(struct in_addr));
 63                         break;
 64                 default:
 65                         memcpy(&srx->transport.sin.sin_addr, &ip_hdr(skb)->saddr,
 66                                sizeof(struct in_addr));
 67                         break;
 68                 }
 69                 break;
 70 
 71 #ifdef CONFIG_AF_RXRPC_IPV6
 72         case AF_INET6:
 73                 srx->transport.sin6.sin6_port = serr->port;
 74                 switch (serr->ee.ee_origin) {
 75                 case SO_EE_ORIGIN_ICMP6:
 76                         _net("Rx ICMP6");
 77                         memcpy(&srx->transport.sin6.sin6_addr,
 78                                skb_network_header(skb) + serr->addr_offset,
 79                                sizeof(struct in6_addr));
 80                         break;
 81                 case SO_EE_ORIGIN_ICMP:
 82                         _net("Rx ICMP on v6 sock");
 83                         srx->transport.sin6.sin6_addr.s6_addr32[0] = 0;
 84                         srx->transport.sin6.sin6_addr.s6_addr32[1] = 0;
 85                         srx->transport.sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
 86                         memcpy(srx->transport.sin6.sin6_addr.s6_addr + 12,
 87                                skb_network_header(skb) + serr->addr_offset,
 88                                sizeof(struct in_addr));
 89                         break;
 90                 default:
 91                         memcpy(&srx->transport.sin6.sin6_addr,
 92                                &ipv6_hdr(skb)->saddr,
 93                                sizeof(struct in6_addr));
 94                         break;
 95                 }
 96                 break;
 97 #endif
 98 
 99         default:
100                 BUG();
101         }
102 
103         return rxrpc_lookup_peer_rcu(local, srx);
104 }
105 
106 /*
107  * Handle an MTU/fragmentation problem.
108  */
109 static void rxrpc_adjust_mtu(struct rxrpc_peer *peer, struct sock_exterr_skb *serr)
110 {
111         u32 mtu = serr->ee.ee_info;
112 
113         _net("Rx ICMP Fragmentation Needed (%d)", mtu);
114 
115         /* wind down the local interface MTU */
116         if (mtu > 0 && peer->if_mtu == 65535 && mtu < peer->if_mtu) {
117                 peer->if_mtu = mtu;
118                 _net("I/F MTU %u", mtu);
119         }
120 
121         if (mtu == 0) {
122                 /* they didn't give us a size, estimate one */
123                 mtu = peer->if_mtu;
124                 if (mtu > 1500) {
125                         mtu >>= 1;
126                         if (mtu < 1500)
127                                 mtu = 1500;
128                 } else {
129                         mtu -= 100;
130                         if (mtu < peer->hdrsize)
131                                 mtu = peer->hdrsize + 4;
132                 }
133         }
134 
135         if (mtu < peer->mtu) {
136                 spin_lock_bh(&peer->lock);
137                 peer->mtu = mtu;
138                 peer->maxdata = peer->mtu - peer->hdrsize;
139                 spin_unlock_bh(&peer->lock);
140                 _net("Net MTU %u (maxdata %u)",
141                      peer->mtu, peer->maxdata);
142         }
143 }
144 
145 /*
146  * Handle an error received on the local endpoint.
147  */
148 void rxrpc_error_report(struct sock *sk)
149 {
150         struct sock_exterr_skb *serr;
151         struct sockaddr_rxrpc srx;
152         struct rxrpc_local *local = sk->sk_user_data;
153         struct rxrpc_peer *peer;
154         struct sk_buff *skb;
155 
156         _enter("%p{%d}", sk, local->debug_id);
157 
158         skb = sock_dequeue_err_skb(sk);
159         if (!skb) {
160                 _leave("UDP socket errqueue empty");
161                 return;
162         }
163         rxrpc_new_skb(skb, rxrpc_skb_rx_received);
164         serr = SKB_EXT_ERR(skb);
165         if (!skb->len && serr->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING) {
166                 _leave("UDP empty message");
167                 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
168                 return;
169         }
170 
171         rcu_read_lock();
172         peer = rxrpc_lookup_peer_icmp_rcu(local, skb, &srx);
173         if (peer && !rxrpc_get_peer_maybe(peer))
174                 peer = NULL;
175         if (!peer) {
176                 rcu_read_unlock();
177                 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
178                 _leave(" [no peer]");
179                 return;
180         }
181 
182         trace_rxrpc_rx_icmp(peer, &serr->ee, &srx);
183 
184         if ((serr->ee.ee_origin == SO_EE_ORIGIN_ICMP &&
185              serr->ee.ee_type == ICMP_DEST_UNREACH &&
186              serr->ee.ee_code == ICMP_FRAG_NEEDED)) {
187                 rxrpc_adjust_mtu(peer, serr);
188                 rcu_read_unlock();
189                 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
190                 rxrpc_put_peer(peer);
191                 _leave(" [MTU update]");
192                 return;
193         }
194 
195         rxrpc_store_error(peer, serr);
196         rcu_read_unlock();
197         rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
198 
199         _leave("");
200 }
201 
202 /*
203  * Map an error report to error codes on the peer record.
204  */
205 static void rxrpc_store_error(struct rxrpc_peer *peer,
206                               struct sock_exterr_skb *serr)
207 {
208         enum rxrpc_call_completion compl = RXRPC_CALL_NETWORK_ERROR;
209         struct sock_extended_err *ee;
210         int err;
211 
212         _enter("");
213 
214         ee = &serr->ee;
215 
216         err = ee->ee_errno;
217 
218         switch (ee->ee_origin) {
219         case SO_EE_ORIGIN_ICMP:
220                 switch (ee->ee_type) {
221                 case ICMP_DEST_UNREACH:
222                         switch (ee->ee_code) {
223                         case ICMP_NET_UNREACH:
224                                 _net("Rx Received ICMP Network Unreachable");
225                                 break;
226                         case ICMP_HOST_UNREACH:
227                                 _net("Rx Received ICMP Host Unreachable");
228                                 break;
229                         case ICMP_PORT_UNREACH:
230                                 _net("Rx Received ICMP Port Unreachable");
231                                 break;
232                         case ICMP_NET_UNKNOWN:
233                                 _net("Rx Received ICMP Unknown Network");
234                                 break;
235                         case ICMP_HOST_UNKNOWN:
236                                 _net("Rx Received ICMP Unknown Host");
237                                 break;
238                         default:
239                                 _net("Rx Received ICMP DestUnreach code=%u",
240                                      ee->ee_code);
241                                 break;
242                         }
243                         break;
244 
245                 case ICMP_TIME_EXCEEDED:
246                         _net("Rx Received ICMP TTL Exceeded");
247                         break;
248 
249                 default:
250                         _proto("Rx Received ICMP error { type=%u code=%u }",
251                                ee->ee_type, ee->ee_code);
252                         break;
253                 }
254                 break;
255 
256         case SO_EE_ORIGIN_NONE:
257         case SO_EE_ORIGIN_LOCAL:
258                 _proto("Rx Received local error { error=%d }", err);
259                 compl = RXRPC_CALL_LOCAL_ERROR;
260                 break;
261 
262         case SO_EE_ORIGIN_ICMP6:
263         default:
264                 _proto("Rx Received error report { orig=%u }", ee->ee_origin);
265                 break;
266         }
267 
268         rxrpc_distribute_error(peer, err, compl);
269 }
270 
271 /*
272  * Distribute an error that occurred on a peer.
273  */
274 static void rxrpc_distribute_error(struct rxrpc_peer *peer, int error,
275                                    enum rxrpc_call_completion compl)
276 {
277         struct rxrpc_call *call;
278 
279         hlist_for_each_entry_rcu(call, &peer->error_targets, error_link) {
280                 rxrpc_see_call(call);
281                 if (call->state < RXRPC_CALL_COMPLETE &&
282                     rxrpc_set_call_completion(call, compl, 0, -error))
283                         rxrpc_notify_socket(call);
284         }
285 }
286 
287 /*
288  * Add RTT information to cache.  This is called in softirq mode and has
289  * exclusive access to the peer RTT data.
290  */
291 void rxrpc_peer_add_rtt(struct rxrpc_call *call, enum rxrpc_rtt_rx_trace why,
292                         rxrpc_serial_t send_serial, rxrpc_serial_t resp_serial,
293                         ktime_t send_time, ktime_t resp_time)
294 {
295         struct rxrpc_peer *peer = call->peer;
296         s64 rtt;
297         u64 sum = peer->rtt_sum, avg;
298         u8 cursor = peer->rtt_cursor, usage = peer->rtt_usage;
299 
300         rtt = ktime_to_ns(ktime_sub(resp_time, send_time));
301         if (rtt < 0)
302                 return;
303 
304         /* Replace the oldest datum in the RTT buffer */
305         sum -= peer->rtt_cache[cursor];
306         sum += rtt;
307         peer->rtt_cache[cursor] = rtt;
308         peer->rtt_cursor = (cursor + 1) & (RXRPC_RTT_CACHE_SIZE - 1);
309         peer->rtt_sum = sum;
310         if (usage < RXRPC_RTT_CACHE_SIZE) {
311                 usage++;
312                 peer->rtt_usage = usage;
313         }
314 
315         /* Now recalculate the average */
316         if (usage == RXRPC_RTT_CACHE_SIZE) {
317                 avg = sum / RXRPC_RTT_CACHE_SIZE;
318         } else {
319                 avg = sum;
320                 do_div(avg, usage);
321         }
322 
323         peer->rtt = avg;
324         trace_rxrpc_rtt_rx(call, why, send_serial, resp_serial, rtt,
325                            usage, avg);
326 }
327 
328 /*
329  * Perform keep-alive pings.
330  */
331 static void rxrpc_peer_keepalive_dispatch(struct rxrpc_net *rxnet,
332                                           struct list_head *collector,
333                                           time64_t base,
334                                           u8 cursor)
335 {
336         struct rxrpc_peer *peer;
337         const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1;
338         time64_t keepalive_at;
339         int slot;
340 
341         spin_lock_bh(&rxnet->peer_hash_lock);
342 
343         while (!list_empty(collector)) {
344                 peer = list_entry(collector->next,
345                                   struct rxrpc_peer, keepalive_link);
346 
347                 list_del_init(&peer->keepalive_link);
348                 if (!rxrpc_get_peer_maybe(peer))
349                         continue;
350 
351                 spin_unlock_bh(&rxnet->peer_hash_lock);
352 
353                 keepalive_at = peer->last_tx_at + RXRPC_KEEPALIVE_TIME;
354                 slot = keepalive_at - base;
355                 _debug("%02x peer %u t=%d {%pISp}",
356                        cursor, peer->debug_id, slot, &peer->srx.transport);
357 
358                 if (keepalive_at <= base ||
359                     keepalive_at > base + RXRPC_KEEPALIVE_TIME) {
360                         rxrpc_send_keepalive(peer);
361                         slot = RXRPC_KEEPALIVE_TIME;
362                 }
363 
364                 /* A transmission to this peer occurred since last we examined
365                  * it so put it into the appropriate future bucket.
366                  */
367                 slot += cursor;
368                 slot &= mask;
369                 spin_lock_bh(&rxnet->peer_hash_lock);
370                 list_add_tail(&peer->keepalive_link,
371                               &rxnet->peer_keepalive[slot & mask]);
372                 rxrpc_put_peer(peer);
373         }
374 
375         spin_unlock_bh(&rxnet->peer_hash_lock);
376 }
377 
378 /*
379  * Perform keep-alive pings with VERSION packets to keep any NAT alive.
380  */
381 void rxrpc_peer_keepalive_worker(struct work_struct *work)
382 {
383         struct rxrpc_net *rxnet =
384                 container_of(work, struct rxrpc_net, peer_keepalive_work);
385         const u8 mask = ARRAY_SIZE(rxnet->peer_keepalive) - 1;
386         time64_t base, now, delay;
387         u8 cursor, stop;
388         LIST_HEAD(collector);
389 
390         now = ktime_get_seconds();
391         base = rxnet->peer_keepalive_base;
392         cursor = rxnet->peer_keepalive_cursor;
393         _enter("%lld,%u", base - now, cursor);
394 
395         if (!rxnet->live)
396                 return;
397 
398         /* Remove to a temporary list all the peers that are currently lodged
399          * in expired buckets plus all new peers.
400          *
401          * Everything in the bucket at the cursor is processed this
402          * second; the bucket at cursor + 1 goes at now + 1s and so
403          * on...
404          */
405         spin_lock_bh(&rxnet->peer_hash_lock);
406         list_splice_init(&rxnet->peer_keepalive_new, &collector);
407 
408         stop = cursor + ARRAY_SIZE(rxnet->peer_keepalive);
409         while (base <= now && (s8)(cursor - stop) < 0) {
410                 list_splice_tail_init(&rxnet->peer_keepalive[cursor & mask],
411                                       &collector);
412                 base++;
413                 cursor++;
414         }
415 
416         base = now;
417         spin_unlock_bh(&rxnet->peer_hash_lock);
418 
419         rxnet->peer_keepalive_base = base;
420         rxnet->peer_keepalive_cursor = cursor;
421         rxrpc_peer_keepalive_dispatch(rxnet, &collector, base, cursor);
422         ASSERT(list_empty(&collector));
423 
424         /* Schedule the timer for the next occupied timeslot. */
425         cursor = rxnet->peer_keepalive_cursor;
426         stop = cursor + RXRPC_KEEPALIVE_TIME - 1;
427         for (; (s8)(cursor - stop) < 0; cursor++) {
428                 if (!list_empty(&rxnet->peer_keepalive[cursor & mask]))
429                         break;
430                 base++;
431         }
432 
433         now = ktime_get_seconds();
434         delay = base - now;
435         if (delay < 1)
436                 delay = 1;
437         delay *= HZ;
438         if (rxnet->live)
439                 timer_reduce(&rxnet->peer_keepalive_timer, jiffies + delay);
440 
441         _leave("");
442 }
443 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp