BSD 4_3_Reno release
[unix-history] / usr / src / sys / net / route.c
index 22b2be4..676a6e6 100644 (file)
@@ -2,19 +2,22 @@
  * Copyright (c) 1980, 1986 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1980, 1986 Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * Redistribution is only permitted until one year after the first shipment
+ * of 4.4BSD by the Regents.  Otherwise, redistribution and use in source and
+ * binary forms are permitted provided that: (1) source distributions retain
+ * this entire copyright notice and comment, and (2) distributions including
+ * binaries display the following acknowledgement:  This product includes
+ * software developed by the University of California, Berkeley and its
+ * contributors'' in the documentation or other materials provided with the
+ * distribution and in all advertising materials mentioning features or use
+ * of this software.  Neither the name of the University nor the names of
+ * its contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  *
  *
- *     @(#)route.c     7.14 (Berkeley) %G%
+ *     @(#)route.c     7.17 (Berkeley) 6/28/90
  */
 #include "machine/reg.h"
  
  */
 #include "machine/reg.h"
  
@@ -40,6 +43,7 @@
 #include "../netns/ns.h"
 #include "machine/mtpr.h"
 #include "netisr.h"
 #include "../netns/ns.h"
 #include "machine/mtpr.h"
 #include "netisr.h"
+#define        SA(p) ((struct sockaddr *)(p))
 
 int    rttrash;                /* routes not in table but not freed */
 struct sockaddr wildcard;      /* zero valued cookie for wildcard searches */
 
 int    rttrash;                /* routes not in table but not freed */
 struct sockaddr wildcard;      /* zero valued cookie for wildcard searches */
@@ -69,41 +73,34 @@ rtalloc(ro)
 
 struct rtentry *
 rtalloc1(dst, report)
 
 struct rtentry *
 rtalloc1(dst, report)
-       struct sockaddr *dst;
+       register struct sockaddr *dst;
        int  report;
 {
        register struct radix_node_head *rnh;
        int  report;
 {
        register struct radix_node_head *rnh;
+       register struct rtentry *rt;
        register struct radix_node *rn;
        register struct radix_node *rn;
-       register struct rtentry *rt = 0;
-       u_char af = dst->sa_family;
-       int  s = splnet();
+       struct rtentry *newrt = 0;
+       int  s = splnet(), err = 0;
 
 
-       for (rnh = radix_node_head; rnh && (af != rnh->rnh_af); )
+       for (rnh = radix_node_head; rnh && (dst->sa_family != rnh->rnh_af); )
                rnh = rnh->rnh_next;
        if (rnh && rnh->rnh_treetop &&
            (rn = rn_match((caddr_t)dst, rnh->rnh_treetop)) &&
            ((rn->rn_flags & RNF_ROOT) == 0)) {
                rnh = rnh->rnh_next;
        if (rnh && rnh->rnh_treetop &&
            (rn = rn_match((caddr_t)dst, rnh->rnh_treetop)) &&
            ((rn->rn_flags & RNF_ROOT) == 0)) {
-               rt = (struct rtentry *)rn;
+               newrt = rt = (struct rtentry *)rn;
                if (report && (rt->rt_flags & RTF_CLONING)) {
                if (report && (rt->rt_flags & RTF_CLONING)) {
-                       struct rtentry *newrt = 0;
-                       int flags = rt->rt_flags & ~RTF_CLONING;
-
-                       if (rt->rt_genmask == 0) flags |= RTF_HOST;
-                       (void) rtrequest(RTM_ADD, dst, rt->rt_gateway,
-                             rt->rt_genmask, flags, &newrt);
-                       rt = newrt;
+                       if (err = rtrequest(RTM_RESOLVE, dst, SA(0),
+                                             SA(0), 0, &newrt))
+                               goto miss;
                } else
                        rt->rt_refcnt++;
                } else
                        rt->rt_refcnt++;
-               if (rt == 0)
-                       goto miss;
        } else {
                rtstat.rts_unreach++;
        miss:   if (report)
        } else {
                rtstat.rts_unreach++;
        miss:   if (report)
-                       rt_missmsg(RTM_MISS, dst, (struct sockaddr *)0,
-                          (struct sockaddr *)0, (struct sockaddr *)0, 0, 0);
+                       rt_missmsg(RTM_MISS, dst, SA(0), SA(0), SA(0), 0, err);
        }
        splx(s);
        }
        splx(s);
-       return (rt);
+       return (newrt);
 }
 
 rtfree(rt)
 }
 
 rtfree(rt)
@@ -179,7 +176,7 @@ rtredirect(dst, gateway, netmask, flags, src, rtp)
                create:
                        flags |=  RTF_GATEWAY | RTF_DYNAMIC;
                        error = rtrequest((int)RTM_ADD, dst, gateway,
                create:
                        flags |=  RTF_GATEWAY | RTF_DYNAMIC;
                        error = rtrequest((int)RTM_ADD, dst, gateway,
-                                   (struct sockaddr *)0, flags,
+                                   SA(0), flags,
                                    (struct rtentry **)0);
                        stat = &rtstat.rts_dynamic;
                } else {
                                    (struct rtentry **)0);
                        stat = &rtstat.rts_dynamic;
                } else {
@@ -275,10 +272,43 @@ rtioctl(req, data)
                                entry->rt_flags, (struct rtentry **)0);
        rt_missmsg((req == RTM_ADD ? RTM_OLDADD : RTM_OLDDEL),
                   &(entry->rt_dst), &(entry->rt_gateway),
                                entry->rt_flags, (struct rtentry **)0);
        rt_missmsg((req == RTM_ADD ? RTM_OLDADD : RTM_OLDDEL),
                   &(entry->rt_dst), &(entry->rt_gateway),
-                  netmask, (struct sockaddr *)0, entry->rt_flags, error);
+                  netmask, SA(0), entry->rt_flags, error);
        return (error);
 #endif
 }
        return (error);
 #endif
 }
+
+struct ifaddr *
+ifa_ifwithroute(flags, dst, gateway)
+int    flags;
+struct sockaddr        *dst, *gateway;
+{
+       struct ifaddr *ifa;
+       if ((flags & RTF_GATEWAY) == 0) {
+               /*
+                * If we are adding a route to an interface,
+                * and the interface is a pt to pt link
+                * we should search for the destination
+                * as our clue to the interface.  Otherwise
+                * we can use the local address.
+                */
+               ifa = 0;
+               if (flags & RTF_HOST) 
+                       ifa = ifa_ifwithdstaddr(dst);
+               if (ifa == 0)
+                       ifa = ifa_ifwithaddr(gateway);
+       } else {
+               /*
+                * If we are adding a route to a remote net
+                * or host, the gateway may still be on the
+                * other end of a pt to pt link.
+                */
+               ifa = ifa_ifwithdstaddr(gateway);
+       }
+       if (ifa == 0)
+               ifa = ifa_ifwithnet(gateway);
+       return (ifa);
+}
+
 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
 
 rtrequest(req, dst, gateway, netmask, flags, ret_nrt)
 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
 
 rtrequest(req, dst, gateway, netmask, flags, ret_nrt)
@@ -317,41 +347,28 @@ rtrequest(req, dst, gateway, netmask, flags, ret_nrt)
                rt = (struct rtentry *)rn;
                rt->rt_flags &= ~RTF_UP;
                if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
                rt = (struct rtentry *)rn;
                rt->rt_flags &= ~RTF_UP;
                if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
-                       ifa->ifa_rtrequest(RTM_DELETE, rt, (struct sockaddr *)0);
+                       ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0));
                rttrash++;
                if (rt->rt_refcnt <= 0)
                        rtfree(rt);
                break;
 
                rttrash++;
                if (rt->rt_refcnt <= 0)
                        rtfree(rt);
                break;
 
+       case RTM_RESOLVE:
+               if (ret_nrt == 0 || (rt = *ret_nrt) == 0)
+                       senderr(EINVAL);
+               ifa = rt->rt_ifa;
+               flags = rt->rt_flags & ~RTF_CLONING;
+               gateway = rt->rt_gateway;
+               if ((netmask = rt->rt_genmask) == 0)
+                       flags |= RTF_HOST;
+               goto makeroute;
+
        case RTM_ADD:
        case RTM_ADD:
-               if ((flags & RTF_GATEWAY) == 0) {
-                       /*
-                        * If we are adding a route to an interface,
-                        * and the interface is a pt to pt link
-                        * we should search for the destination
-                        * as our clue to the interface.  Otherwise
-                        * we can use the local address.
-                        */
-                       ifa = 0;
-                       if (flags & RTF_HOST) 
-                               ifa = ifa_ifwithdstaddr(dst);
-                       if (ifa == 0)
-                               ifa = ifa_ifwithaddr(gateway);
-               } else {
-                       /*
-                        * If we are adding a route to a remote net
-                        * or host, the gateway may still be on the
-                        * other end of a pt to pt link.
-                        */
-                       ifa = ifa_ifwithdstaddr(gateway);
-               }
-               if (ifa == 0) {
-                       ifa = ifa_ifwithnet(gateway);
-                       if (ifa == 0 && req == RTM_ADD)
-                               senderr(ENETUNREACH);
-               }
+               if ((ifa = ifa_ifwithroute(flags, dst, gateway)) == 0)
+                       senderr(ENETUNREACH);
+       makeroute:
                len = sizeof (*rt) + ROUNDUP(gateway->sa_len)
                len = sizeof (*rt) + ROUNDUP(gateway->sa_len)
-                   + ROUNDUP(dst->sa_len) + ROUNDUP(ifa->ifa_llinfolen);
+                   + ROUNDUP(dst->sa_len);
                R_Malloc(rt, struct rtentry *, len);
                if (rt == 0)
                        senderr(ENOBUFS);
                R_Malloc(rt, struct rtentry *, len);
                if (rt == 0)
                        senderr(ENOBUFS);
@@ -367,21 +384,21 @@ rtrequest(req, dst, gateway, netmask, flags, ret_nrt)
                        free((caddr_t)rt, M_RTABLE);
                        senderr(EEXIST);
                }
                        free((caddr_t)rt, M_RTABLE);
                        senderr(EEXIST);
                }
-               rt->rt_ifp = ifa->ifa_ifp;
                rt->rt_ifa = ifa;
                rt->rt_ifa = ifa;
-               if (ret_nrt) {
-                       *ret_nrt = rt;
-                       rt->rt_refcnt++;
-               }
+               rt->rt_ifp = ifa->ifa_ifp;
                rt->rt_flags = RTF_UP | flags;
                rn->rn_key = (caddr_t) ndst; /* == rt_dst */
                rt->rt_gateway = (struct sockaddr *)
                                        (rn->rn_key + ROUNDUP(dst->sa_len));
                Bcopy(gateway, rt->rt_gateway, gateway->sa_len);
                rt->rt_flags = RTF_UP | flags;
                rn->rn_key = (caddr_t) ndst; /* == rt_dst */
                rt->rt_gateway = (struct sockaddr *)
                                        (rn->rn_key + ROUNDUP(dst->sa_len));
                Bcopy(gateway, rt->rt_gateway, gateway->sa_len);
-               rt->rt_llinfo = ROUNDUP(gateway->sa_len)
-                                       + (caddr_t)rt->rt_gateway;
+               if (req == RTM_RESOLVE)
+                       rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
                if (ifa->ifa_rtrequest)
                if (ifa->ifa_rtrequest)
-                       ifa->ifa_rtrequest(RTM_ADD, rt, (struct sockaddr *)0);
+                       ifa->ifa_rtrequest(req, rt, SA(ret_nrt ? *ret_nrt : 0));
+               if (ret_nrt) {
+                       *ret_nrt = rt;
+                       rt->rt_refcnt++;
+               }
                break;
        }
 bad:
                break;
        }
 bad: