Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Add RTF_BROADCAST to mark routes used for the broadcast addr...



details:   https://anonhg.NetBSD.org/src/rev/b2b56e3d9785
branches:  trunk
changeset: 336859:b2b56e3d9785
user:      roy <roy%NetBSD.org@localhost>
date:      Mon Mar 23 18:33:17 2015 +0000

description:
Add RTF_BROADCAST to mark routes used for the broadcast address when
they are created on the fly. This makes it clear what the route is for
and allows an optimisation in ip_output() by avoiding a call to
in_broadcast() because most of the time we do talk to a host.
It also avoids a needless allocation for the storage of llinfo_arp and
thus vanishes from arp(8) - it showed as incomplete anyway so this
is a nice side effect.

Guard against this and routes marked with RTF_BLACKHOLE in
ip_fastforward().
While here, guard against routes marked with RTF_BLACKHOLE in
ip6_fastforward().
RTF_BROADCAST is IPv4 only, so don't bother checking that here.

diffstat:

 sbin/route/route.8        |   5 +++--
 sbin/route/route.c        |   6 +++---
 sbin/route/rtutil.c       |   3 ++-
 sbin/route/show.c         |   6 +++---
 share/man/man4/route.4    |   5 +++--
 sys/net/route.h           |   3 ++-
 sys/netinet/if_arp.c      |  18 ++++++++++++++++--
 sys/netinet/ip_flow.c     |   7 ++++---
 sys/netinet/ip_output.c   |  13 ++++++++++---
 sys/netinet6/ip6_flow.c   |   9 ++++-----
 usr.bin/netstat/netstat.1 |   5 +++--
 11 files changed, 53 insertions(+), 27 deletions(-)

diffs (truncated from 329 to 300 lines):

diff -r 80d96e9b645c -r b2b56e3d9785 sbin/route/route.8
--- a/sbin/route/route.8        Mon Mar 23 17:42:02 2015 +0000
+++ b/sbin/route/route.8        Mon Mar 23 18:33:17 2015 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: route.8,v 1.54 2015/02/26 09:56:11 roy Exp $
+.\"    $NetBSD: route.8,v 1.55 2015/03/23 18:33:17 roy Exp $
 .\"
 .\" Copyright (c) 1983, 1991, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"     @(#)route.8    8.4 (Berkeley) 6/1/94
 .\"
-.Dd February 26, 2015
+.Dd March 19, 2015
 .Dt ROUTE 8
 .Os
 .Sh NAME
@@ -295,6 +295,7 @@
 .It Li 1 Ta -proto1 Ta " RTF_PROTO1" Ta set protocol specific flag #1
 .It Li 2 Ta -proto2 Ta " RTF_PROTO2" Ta set protocol specific flag #2
 .It Li B Ta -blackhole Ta " RTF_BLACKHOLE" Ta discard pkts (during updates)
+.It Li b Ta "" Ta " RTF_BROADCAST" Ta Route represents a broadcast address
 .It Li " " Ta -noblackhole Ta ~RTF_BLACKHOLE Ta clear blackhole flag
 .It Li C Ta -cloning Ta " RTF_CLONING" Ta  generates a new route on use
 .It Li " " Ta -nocloning Ta ~RTF_CLONING Ta stop generating new routes on use
diff -r 80d96e9b645c -r b2b56e3d9785 sbin/route/route.c
--- a/sbin/route/route.c        Mon Mar 23 17:42:02 2015 +0000
+++ b/sbin/route/route.c        Mon Mar 23 18:33:17 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: route.c,v 1.150 2015/02/26 09:56:11 roy Exp $  */
+/*     $NetBSD: route.c,v 1.151 2015/03/23 18:33:17 roy Exp $  */
 
 /*
  * Copyright (c) 1983, 1989, 1991, 1993
@@ -39,7 +39,7 @@
 #if 0
 static char sccsid[] = "@(#)route.c    8.6 (Berkeley) 4/28/95";
 #else
-__RCSID("$NetBSD: route.c,v 1.150 2015/02/26 09:56:11 roy Exp $");
+__RCSID("$NetBSD: route.c,v 1.151 2015/03/23 18:33:17 roy Exp $");
 #endif
 #endif /* not lint */
 
@@ -1292,7 +1292,7 @@
 const char metricnames[] =
 "\011pksent\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire\2hopcount\1mtu";
 const char routeflags[] =
-"\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE\010MASK_PRESENT\011CLONING\012XRESOLVE\013LLINFO\014STATIC\015BLACKHOLE\016CLONED\017PROTO2\020PROTO1\023LOCAL";
+"\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE\010MASK_PRESENT\011CLONING\012XRESOLVE\013LLINFO\014STATIC\015BLACKHOLE\016CLONED\017PROTO2\020PROTO1\023LOCAL\024BROADCAST";
 const char ifnetflags[] =
 "\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5PTP\6NOTRAILERS\7RUNNING\010NOARP\011PPROMISC\012ALLMULTI\013OACTIVE\014SIMPLEX\015LINK0\016LINK1\017LINK2\020MULTICAST";
 const char addrnames[] =
diff -r 80d96e9b645c -r b2b56e3d9785 sbin/route/rtutil.c
--- a/sbin/route/rtutil.c       Mon Mar 23 17:42:02 2015 +0000
+++ b/sbin/route/rtutil.c       Mon Mar 23 18:33:17 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rtutil.c,v 1.5 2015/02/26 09:56:11 roy Exp $   */
+/*     $NetBSD: rtutil.c,v 1.6 2015/03/23 18:33:17 roy Exp $   */
 /*     $OpenBSD: show.c,v 1.1 2006/05/27 19:16:37 claudio Exp $        */
 
 /*
@@ -93,6 +93,7 @@
        /* { RTF_JUMBO, 'J' }, */
        { RTF_ANNOUNCE, 'p' },
        { RTF_LOCAL, 'l'},
+       { RTF_BROADCAST, 'b'},
        { 0, 0 }
 };
 
diff -r 80d96e9b645c -r b2b56e3d9785 sbin/route/show.c
--- a/sbin/route/show.c Mon Mar 23 17:42:02 2015 +0000
+++ b/sbin/route/show.c Mon Mar 23 18:33:17 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: show.c,v 1.47 2015/02/26 09:56:11 roy Exp $    */
+/*     $NetBSD: show.c,v 1.48 2015/03/23 18:33:17 roy Exp $    */
 
 /*
  * Copyright (c) 1983, 1988, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "from: @(#)route.c      8.3 (Berkeley) 3/9/94";
 #else
-__RCSID("$NetBSD: show.c,v 1.47 2015/02/26 09:56:11 roy Exp $");
+__RCSID("$NetBSD: show.c,v 1.48 2015/03/23 18:33:17 roy Exp $");
 #endif
 #endif /* not lint */
 
@@ -137,7 +137,7 @@
 {
        int af, rflags;
        static int interesting = RTF_UP | RTF_GATEWAY | RTF_HOST |
-           RTF_REJECT | RTF_LLINFO | RTF_LOCAL;
+           RTF_REJECT | RTF_LLINFO | RTF_LOCAL | RTF_BROADCAST;
 
        parse_show_opts(argc, argv, &af, &rflags, NULL, true);
        p_rttables(af, flags, rflags, interesting);
diff -r 80d96e9b645c -r b2b56e3d9785 share/man/man4/route.4
--- a/share/man/man4/route.4    Mon Mar 23 17:42:02 2015 +0000
+++ b/share/man/man4/route.4    Mon Mar 23 18:33:17 2015 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: route.4,v 1.24 2015/02/26 09:58:39 roy Exp $
+.\"    $NetBSD: route.4,v 1.25 2015/03/23 18:33:17 roy Exp $
 .\"
 .\" Copyright (c) 1990, 1991, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"     @(#)route.4    8.6 (Berkeley) 4/19/94
 .\"
-.Dd February 26, 2015
+.Dd March 19, 2015
 .Dt ROUTE 4
 .Os
 .Sh NAME
@@ -311,6 +311,7 @@
 #define        RTF_SRC       0x10000   /* route has fixed source address */
 #define        RTF_ANNOUNCE  0x20000   /* announce new ARP or NDP entry */
 #define        RTF_LOCAL     0x40000   /* route represents a local address */
+#define        RTF_BROADCAST 0x80000   /* route represents a bcast address */
 .Ed
 .Pp
 Specifiers for metric values in rmx_locks and rtm_inits are:
diff -r 80d96e9b645c -r b2b56e3d9785 sys/net/route.h
--- a/sys/net/route.h   Mon Mar 23 17:42:02 2015 +0000
+++ b/sys/net/route.h   Mon Mar 23 18:33:17 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: route.h,v 1.87 2015/02/26 09:54:46 roy Exp $   */
+/*     $NetBSD: route.h,v 1.88 2015/03/23 18:33:17 roy Exp $   */
 
 /*
  * Copyright (c) 1980, 1986, 1993
@@ -156,6 +156,7 @@
 #define RTF_SRC                0x10000         /* route has fixed source address */
 #define RTF_ANNOUNCE   0x20000         /* announce new ARP or NDP entry */
 #define RTF_LOCAL      0x40000         /* route represents a local address */
+#define RTF_BROADCAST  0x80000         /* route represents a bcast address */
 
 /*
  * Routing statistics.
diff -r 80d96e9b645c -r b2b56e3d9785 sys/netinet/if_arp.c
--- a/sys/netinet/if_arp.c      Mon Mar 23 17:42:02 2015 +0000
+++ b/sys/netinet/if_arp.c      Mon Mar 23 18:33:17 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_arp.c,v 1.161 2015/02/26 09:54:46 roy Exp $ */
+/*     $NetBSD: if_arp.c,v 1.162 2015/03/23 18:33:17 roy Exp $ */
 
 /*-
  * Copyright (c) 1998, 2000, 2008 The NetBSD Foundation, Inc.
@@ -68,7 +68,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.161 2015/02/26 09:54:46 roy Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.162 2015/03/23 18:33:17 roy Exp $");
 
 #include "opt_ddb.h"
 #include "opt_inet.h"
@@ -613,10 +613,24 @@
                        log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
                        break;
                }
+
                satosdl(gate)->sdl_type = ifp->if_type;
                satosdl(gate)->sdl_index = ifp->if_index;
                if (la != NULL)
                        break; /* This happens on a route change */
+
+               /* If the route is for a broadcast address mark it as such.
+                * This way we can avoid an expensive call to in_broadcast()
+                * in ip_output() most of the time (because the route passed
+                * to ip_output() is almost always a host route). */
+               if (rt->rt_flags & RTF_HOST &&
+                   !(rt->rt_flags & RTF_BROADCAST) &&
+                   in_broadcast(satocsin(rt_getkey(rt))->sin_addr, rt->rt_ifp))
+                       rt->rt_flags |= RTF_BROADCAST;
+               /* There is little point in resolving the broadcast address */
+               if (rt->rt_flags & RTF_BROADCAST)
+                       break;
+
                /*
                 * Case 2:  This route may come from cloning, or a manual route
                 * add with a LL address.
diff -r 80d96e9b645c -r b2b56e3d9785 sys/netinet/ip_flow.c
--- a/sys/netinet/ip_flow.c     Mon Mar 23 17:42:02 2015 +0000
+++ b/sys/netinet/ip_flow.c     Mon Mar 23 18:33:17 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ip_flow.c,v 1.65 2014/10/18 08:33:29 snj Exp $ */
+/*     $NetBSD: ip_flow.c,v 1.66 2015/03/23 18:33:17 roy Exp $ */
 
 /*-
  * Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ip_flow.c,v 1.65 2014/10/18 08:33:29 snj Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ip_flow.c,v 1.66 2015/03/23 18:33:17 roy Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -234,7 +234,8 @@
         * Route and interface still up?
         */
        if ((rt = rtcache_validate(&ipf->ipf_ro)) == NULL ||
-           (rt->rt_ifp->if_flags & IFF_UP) == 0)
+           (rt->rt_ifp->if_flags & IFF_UP) == 0 ||
+           (rt->rt_flags & (RTF_BLACKHOLE | RTF_BROADCAST)) != 0)
                return 0;
 
        /*
diff -r 80d96e9b645c -r b2b56e3d9785 sys/netinet/ip_output.c
--- a/sys/netinet/ip_output.c   Mon Mar 23 17:42:02 2015 +0000
+++ b/sys/netinet/ip_output.c   Mon Mar 23 18:33:17 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ip_output.c,v 1.233 2014/11/26 10:18:37 ozaki-r Exp $  */
+/*     $NetBSD: ip_output.c,v 1.234 2015/03/23 18:33:17 roy Exp $      */
 
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -91,7 +91,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ip_output.c,v 1.233 2014/11/26 10:18:37 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ip_output.c,v 1.234 2015/03/23 18:33:17 roy Exp $");
 
 #include "opt_inet.h"
 #include "opt_ipsec.h"
@@ -163,6 +163,7 @@
        struct route iproute;
        const struct sockaddr_in *dst;
        struct in_ifaddr *ia;
+       int isbroadcast;
        struct mbuf *opt;
        struct route *ro;
        int flags, sw_csum;
@@ -257,12 +258,14 @@
                ifp = ia->ia_ifp;
                mtu = ifp->if_mtu;
                ip->ip_ttl = 1;
+               isbroadcast = in_broadcast(dst->sin_addr, ifp);
        } else if ((IN_MULTICAST(ip->ip_dst.s_addr) ||
            ip->ip_dst.s_addr == INADDR_BROADCAST) &&
            imo != NULL && imo->imo_multicast_ifp != NULL) {
                ifp = imo->imo_multicast_ifp;
                mtu = ifp->if_mtu;
                IFP_TO_IA(ifp, ia);
+               isbroadcast = 0;
        } else {
                if (rt == NULL)
                        rt = rtcache_init(ro);
@@ -278,6 +281,10 @@
                rt->rt_use++;
                if (rt->rt_flags & RTF_GATEWAY)
                        dst = satosin(rt->rt_gateway);
+               if (rt->rt_flags & RTF_HOST)
+                       isbroadcast = rt->rt_flags & RTF_BROADCAST;
+               else
+                       isbroadcast = in_broadcast(dst->sin_addr, ifp);
        }
        rtmtu_nolock = rt && (rt->rt_rmx.rmx_locks & RTV_MTU) == 0;
 
@@ -412,7 +419,7 @@
         * Look for broadcast address and and verify user is allowed to
         * send such a packet.
         */
-       if (in_broadcast(dst->sin_addr, ifp)) {
+       if (isbroadcast) {
                if ((ifp->if_flags & IFF_BROADCAST) == 0) {
                        error = EADDRNOTAVAIL;
                        goto bad;
diff -r 80d96e9b645c -r b2b56e3d9785 sys/netinet6/ip6_flow.c
--- a/sys/netinet6/ip6_flow.c   Mon Mar 23 17:42:02 2015 +0000
+++ b/sys/netinet6/ip6_flow.c   Mon Mar 23 18:33:17 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ip6_flow.c,v 1.23 2014/05/20 20:23:56 bouyer Exp $     */
+/*     $NetBSD: ip6_flow.c,v 1.24 2015/03/23 18:33:17 roy Exp $        */
 
 /*-
  * Copyright (c) 2007 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ip6_flow.c,v 1.23 2014/05/20 20:23:56 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ip6_flow.c,v 1.24 2015/03/23 18:33:17 roy Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -275,10 +275,9 @@
         * Route and interface still up?
         */
        if ((rt = rtcache_validate(&ip6f->ip6f_ro)) == NULL ||
-           (rt->rt_ifp->if_flags & IFF_UP) == 0) {
-               /* Route or interface is down */
+           (rt->rt_ifp->if_flags & IFF_UP) == 0 ||
+           (rt->rt_flags & RTF_BLACKHOLE) != 0)
                return 0;
-       }



Home | Main Index | Thread Index | Old Index