BSD 4_3_Net_2 release
[unix-history] / usr / src / sys / netns / ns_pcb.c
index e2e0312..0d3bd8e 100644 (file)
@@ -1,21 +1,48 @@
 /*
 /*
- * Copyright (c) 1984, 1985, 1986 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    7.2 (Berkeley) %G%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. 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 BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *     @(#)ns_pcb.c    7.11 (Berkeley) 6/27/91
  */
 
 #include "param.h"
 #include "systm.h"
  */
 
 #include "param.h"
 #include "systm.h"
-#include "dir.h"
-#include "user.h"
 #include "mbuf.h"
 #include "mbuf.h"
+#include "errno.h"
 #include "socket.h"
 #include "socketvar.h"
 #include "socket.h"
 #include "socketvar.h"
+#include "protosw.h"
+
 #include "../net/if.h"
 #include "../net/route.h"
 #include "../net/if.h"
 #include "../net/route.h"
-#include "protosw.h"
 
 #include "ns.h"
 #include "ns_if.h"
 
 #include "ns.h"
 #include "ns_if.h"
@@ -47,7 +74,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;
@@ -66,7 +93,8 @@ ns_pcbbind(nsp, nam)
        if (lport) {
                u_short aport = ntohs(lport);
 
        if (lport) {
                u_short aport = ntohs(lport);
 
-               if (aport < NSPORT_RESERVED && u.u_uid != 0)
+               if (aport < NSPORT_RESERVED &&
+                   (nsp->nsp_socket->so_state & SS_PRIV) == 0)
                        return (EACCES);
                if (ns_pcblookup(&zerons_addr, lport, 0))
                        return (EADDRINUSE);
                        return (EACCES);
                if (ns_pcblookup(&zerons_addr, lport, 0))
                        return (EADDRINUSE);
@@ -95,8 +123,9 @@ ns_pcbconnect(nsp, nam)
 {
        struct ns_ifaddr *ia;
        register struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
 {
        struct ns_ifaddr *ia;
        register struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
-       struct sockaddr_ns *ifaddr;
        register struct ns_addr *dst;
        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);
@@ -104,36 +133,54 @@ 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)) {
-               register struct route *ro;
-               struct ifnet *ifp;
+       /*
+        * 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 (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 && ! (ro->ro_rt->rt_flags & RTF_HOST)) {
+                       /* can patch route to avoid rtalloc */
+                       *dst = sns->sns_addr;
+               } else {
+       flush:
+                       if (ro->ro_rt)
+                               RTFREE(ro->ro_rt);
+                       ro->ro_rt = (struct rtentry *)0;
+                       nsp->nsp_laddr.x_net = ns_zeronet;
+               }
+       }/* 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.
                 */
                /* 
                 * If route is known or can be allocated now,
                 * our src addr is taken from the i/f, else punt.
                 */
-               ro = &nsp->nsp_route;
-               dst = &satons_addr(ro->ro_dst);
 
                ia = (struct ns_ifaddr *)0;
 
                ia = (struct ns_ifaddr *)0;
-               if (ro->ro_rt) {
-                   if ((!ns_neteq(nsp->nsp_lastdst, sns->sns_addr)) ||
-                       ((ifp = ro->ro_rt->rt_ifp) &&
-                        (ifp->if_flags & IFF_POINTOPOINT) &&
-                        (!ns_hosteq(nsp->nsp_lastdst, sns->sns_addr))) ||
-                       (nsp->nsp_socket->so_options & SO_DONTROUTE)) {
-                               RTFREE(ro->ro_rt);
-                               ro->ro_rt = (struct rtentry *)0;
-                       }
-               }
-               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;
-                           *dst = sns->sns_addr;
-                           dst->x_port = 0;
-                           rtalloc(ro);
-               }
                /*
                 * If we found a route, use the address
                 * corresponding to the outgoing interface
                /*
                 * If we found a route, use the address
                 * corresponding to the outgoing interface
@@ -156,7 +203,6 @@ ns_pcbconnect(nsp, nam)
                                return (EADDRNOTAVAIL);
                }
                nsp->nsp_laddr.x_net = satons_addr(ia->ia_addr).x_net;
                                return (EADDRNOTAVAIL);
                }
                nsp->nsp_laddr.x_net = satons_addr(ia->ia_addr).x_net;
-               nsp->nsp_lastdst = sns->sns_addr;
        }
        if (ns_pcblookup(&sns->sns_addr, nsp->nsp_lport, 0))
                return (EADDRINUSE);
        }
        if (ns_pcblookup(&sns->sns_addr, nsp->nsp_lport, 0))
                return (EADDRINUSE);
@@ -201,6 +247,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;
 }
@@ -214,6 +261,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;
 }
@@ -294,8 +342,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++;