BSD 4_3_Reno release
[unix-history] / usr / src / sys / netns / ns_pcb.c
index 4e2f030..06ef150 100644 (file)
@@ -1,14 +1,27 @@
 /*
 /*
- * Copyright (c) 1982 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1984, 1985, 1986, 1987 Regents of the University of California.
+ * All rights reserved.
  *
  *
- *     @(#)ns_pcb.c    6.2 (Berkeley) %G%
+ * 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.
+ *
+ *     @(#)ns_pcb.c    7.9 (Berkeley) 6/28/90
  */
 
 #include "param.h"
 #include "systm.h"
  */
 
 #include "param.h"
 #include "systm.h"
-#include "dir.h"
 #include "user.h"
 #include "mbuf.h"
 #include "socket.h"
 #include "user.h"
 #include "mbuf.h"
 #include "socket.h"
@@ -25,7 +38,7 @@ struct        ns_addr zerons_addr;
 
 ns_pcballoc(so, head)
        struct socket *so;
 
 ns_pcballoc(so, head)
        struct socket *so;
-       struct nsp *head;
+       struct nspcb *head;
 {
        struct mbuf *m;
        register struct nspcb *nsp;
 {
        struct mbuf *m;
        register struct nspcb *nsp;
@@ -47,7 +60,7 @@ ns_pcbbind(nsp, nam)
        register struct sockaddr_ns *sns;
        u_short lport = 0;
 
        register struct sockaddr_ns *sns;
        u_short lport = 0;
 
-       if(nsp->nsp_lport || !ns_nullhost(nsp->nsp_laddr))
+       if (nsp->nsp_lport || !ns_nullhost(nsp->nsp_laddr))
                return (EINVAL);
        if (nam == 0)
                goto noname;
                return (EINVAL);
        if (nam == 0)
                goto noname;
@@ -94,8 +107,10 @@ ns_pcbconnect(nsp, nam)
        struct mbuf *nam;
 {
        struct ns_ifaddr *ia;
        struct mbuf *nam;
 {
        struct ns_ifaddr *ia;
-       struct sockaddr_ns *ifaddr;
        register struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
        register struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
+       register struct ns_addr *dst;
+       register struct route *ro;
+       struct ifnet *ifp;
 
        if (nam->m_len != sizeof (*sns))
                return (EINVAL);
 
        if (nam->m_len != sizeof (*sns))
                return (EINVAL);
@@ -103,53 +118,85 @@ ns_pcbconnect(nsp, nam)
                return (EAFNOSUPPORT);
        if (sns->sns_port==0 || ns_nullhost(sns->sns_addr))
                return (EADDRNOTAVAIL);
                return (EAFNOSUPPORT);
        if (sns->sns_port==0 || ns_nullhost(sns->sns_addr))
                return (EADDRNOTAVAIL);
-       if (ns_nullhost(nsp->nsp_laddr) &&
-           ((*(long *)&nsp->nsp_lastnet)!=ns_netof(sns->sns_addr))) {
-               ia = (struct ns_ifaddr *)ifa_ifwithnet((struct sockaddr *)sns);
-               if (ia == 0) {
-                       register struct route *ro;
-                       struct ifnet *ifp;
-
-                       /* 
-                        * If route is known or can be allocated now,
-                        * our src addr is taken from the i/f, else punt.
-                        */
-                       ro = &nsp->nsp_route;
-#define        satosns(sa)     ((struct sockaddr_ns *)(sa))
-                       if (ro->ro_rt &&
-                               !ns_hosteq(satosns(&ro->ro_dst)->sns_addr,
-                                       sns->sns_addr)) {
+       /*
+        * If we haven't bound which network number to use as ours,
+        * we will use the number of the outgoing interface.
+        * This depends on having done a routing lookup, which
+        * we will probably have to do anyway, so we might
+        * as well do it now.  On the other hand if we are
+        * sending to multiple destinations we may have already
+        * done the lookup, so see if we can use the route
+        * from before.  In any case, we only
+        * chose a port number once, even if sending to multiple
+        * destinations.
+        */
+       ro = &nsp->nsp_route;
+       dst = &satons_addr(ro->ro_dst);
+       if (ro->ro_rt) {
+               if (nsp->nsp_socket->so_options & SO_DONTROUTE)
+                       goto flush;
+               if (!ns_neteq(nsp->nsp_lastdst, sns->sns_addr))
+                       goto flush;
+               if (!ns_hosteq(nsp->nsp_lastdst, sns->sns_addr)) {
+                       if (((ro->ro_rt->rt_flags & (RTF_GATEWAY|RTF_HOST))
+                            == RTF_GATEWAY)
+                           || ((ifp = ro->ro_rt->rt_ifp) &&
+                                !(ifp->if_flags & IFF_POINTOPOINT))) {
+                               /* can patch route to avoid rtalloc */
+                               *dst = sns->sns_addr;
+                       } else {
+               flush:
                                RTFREE(ro->ro_rt);
                                ro->ro_rt = (struct rtentry *)0;
                        }
                                RTFREE(ro->ro_rt);
                                ro->ro_rt = (struct rtentry *)0;
                        }
-                       if ((ro->ro_rt == (struct rtentry *)0) ||
-                           (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) {
-                               /* No route yet, so try to acquire one */
-                               ro->ro_dst.sa_family = AF_NS;
-                               ((struct sockaddr_ns *) &ro->ro_dst)->sns_addr =
-                                       sns->sns_addr;
-                               rtalloc(ro);
-                               if (ro->ro_rt == 0)
-                                       ia = (struct ns_ifaddr *) 0;
-                               else
-                                   for (ia = ns_ifaddr; ia; ia = ia->ia_next)
-                                       if (ia->ia_ifp == ifp)
-                                           break;
-                       }
+               }/* else cached route is ok; do nothing */
+       }
+       nsp->nsp_lastdst = sns->sns_addr;
+       if ((nsp->nsp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
+           (ro->ro_rt == (struct rtentry *)0 ||
+            ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
+                   /* No route yet, so try to acquire one */
+                   ro->ro_dst.sa_family = AF_NS;
+                   ro->ro_dst.sa_len = sizeof(ro->ro_dst);
+                   *dst = sns->sns_addr;
+                   dst->x_port = 0;
+                   rtalloc(ro);
+       }
+       if (ns_neteqnn(nsp->nsp_laddr.x_net, ns_zeronet)) {
+               /* 
+                * If route is known or can be allocated now,
+                * our src addr is taken from the i/f, else punt.
+                */
+
+               ia = (struct ns_ifaddr *)0;
+               /*
+                * If we found a route, use the address
+                * corresponding to the outgoing interface
+                */
+               if (ro->ro_rt && (ifp = ro->ro_rt->rt_ifp))
+                       for (ia = ns_ifaddr; ia; ia = ia->ia_next)
+                               if (ia->ia_ifp == ifp)
+                                       break;
+               if (ia == 0) {
+                       u_short fport = sns->sns_addr.x_port;
+                       sns->sns_addr.x_port = 0;
+                       ia = (struct ns_ifaddr *)
+                               ifa_ifwithdstaddr((struct sockaddr *)sns);
+                       sns->sns_addr.x_port = fport;
+                       if (ia == 0)
+                               ia = ns_iaonnetof(&sns->sns_addr);
                        if (ia == 0)
                                ia = ns_ifaddr;
                        if (ia == 0)
                                return (EADDRNOTAVAIL);
                }
                        if (ia == 0)
                                ia = ns_ifaddr;
                        if (ia == 0)
                                return (EADDRNOTAVAIL);
                }
-               ifaddr = (struct sockaddr_ns *)&ia->ia_addr;
-               nsp->nsp_laddr.x_net = ifaddr->sns_addr.x_net;
-               nsp->nsp_lastnet = sns->sns_addr.x_net;
+               nsp->nsp_laddr.x_net = satons_addr(ia->ia_addr).x_net;
        }
        if (ns_pcblookup(&sns->sns_addr, nsp->nsp_lport, 0))
                return (EADDRINUSE);
        if (ns_nullhost(nsp->nsp_laddr)) {
                if (nsp->nsp_lport == 0)
        }
        if (ns_pcblookup(&sns->sns_addr, nsp->nsp_lport, 0))
                return (EADDRINUSE);
        if (ns_nullhost(nsp->nsp_laddr)) {
                if (nsp->nsp_lport == 0)
-                       ns_pcbbind(nsp, (struct mbuf *)0);
+                       (void) ns_pcbbind(nsp, (struct mbuf *)0);
                nsp->nsp_laddr.x_host = ns_thishost;
        }
        nsp->nsp_faddr = sns->sns_addr;
                nsp->nsp_laddr.x_host = ns_thishost;
        }
        nsp->nsp_faddr = sns->sns_addr;
@@ -188,6 +235,7 @@ ns_setsockaddr(nsp, nam)
        nam->m_len = sizeof (*sns);
        sns = mtod(nam, struct sockaddr_ns *);
        bzero((caddr_t)sns, sizeof (*sns));
        nam->m_len = sizeof (*sns);
        sns = mtod(nam, struct sockaddr_ns *);
        bzero((caddr_t)sns, sizeof (*sns));
+       sns->sns_len = sizeof(*sns);
        sns->sns_family = AF_NS;
        sns->sns_addr = nsp->nsp_laddr;
 }
        sns->sns_family = AF_NS;
        sns->sns_addr = nsp->nsp_laddr;
 }
@@ -201,6 +249,7 @@ ns_setpeeraddr(nsp, nam)
        nam->m_len = sizeof (*sns);
        sns = mtod(nam, struct sockaddr_ns *);
        bzero((caddr_t)sns, sizeof (*sns));
        nam->m_len = sizeof (*sns);
        sns = mtod(nam, struct sockaddr_ns *);
        bzero((caddr_t)sns, sizeof (*sns));
+       sns->sns_len = sizeof(*sns);
        sns->sns_family = AF_NS;
        sns->sns_addr  = nsp->nsp_faddr;
 }
        sns->sns_family = AF_NS;
        sns->sns_addr  = nsp->nsp_faddr;
 }
@@ -238,6 +287,7 @@ ns_pcbnotify(dst, errno, notify, param)
        splx(s);
 }
 
        splx(s);
 }
 
+#ifdef notdef
 /*
  * After a routing change, flush old routing
  * and allocate a (hopefully) better one.
 /*
  * After a routing change, flush old routing
  * and allocate a (hopefully) better one.
@@ -255,6 +305,7 @@ ns_rtchange(nsp)
        }
        /* SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
 }
        }
        /* SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
 }
+#endif
 
 struct nspcb *
 ns_pcblookup(faddr, lport, wildp)
 
 struct nspcb *
 ns_pcblookup(faddr, lport, wildp)
@@ -279,8 +330,8 @@ ns_pcblookup(faddr, lport, wildp)
                        else {
                                if (!ns_hosteq(nsp->nsp_faddr, *faddr))
                                        continue;
                        else {
                                if (!ns_hosteq(nsp->nsp_faddr, *faddr))
                                        continue;
-                               ifnsp->nsp_fport != fport) {
-                                       if(nsp->nsp_fport != 0)
+                               if (nsp->nsp_fport != fport) {
+                                       if (nsp->nsp_fport != 0)
                                                continue;
                                        else
                                                wildcard++;
                                                continue;
                                        else
                                                wildcard++;