The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netns/ns_pcb.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright (c) 1984, 1985, 1986, 1987, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *      This product includes software developed by the University of
   16  *      California, Berkeley and its contributors.
   17  * 4. Neither the name of the University nor the names of its contributors
   18  *    may be used to endorse or promote products derived from this software
   19  *    without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  *
   33  *      @(#)ns_pcb.c    8.1 (Berkeley) 6/10/93
   34  * $FreeBSD$
   35  */
   36 
   37 #include <sys/param.h>
   38 #include <sys/systm.h>
   39 #include <sys/mbuf.h>
   40 #include <sys/errno.h>
   41 #include <sys/socket.h>
   42 #include <sys/socketvar.h>
   43 #include <sys/protosw.h>
   44 
   45 #include <net/if.h>
   46 #include <net/route.h>
   47 
   48 #include <netns/ns.h>
   49 #include <netns/ns_if.h>
   50 #include <netns/ns_pcb.h>
   51 
   52 struct  ns_addr zerons_addr;
   53 
   54 ns_pcballoc(so, head)
   55         struct socket *so;
   56         struct nspcb *head;
   57 {
   58         struct mbuf *m;
   59         register struct nspcb *nsp;
   60 
   61         m = m_getclr(M_DONTWAIT, MT_PCB);
   62         if (m == NULL)
   63                 return (ENOBUFS);
   64         nsp = mtod(m, struct nspcb *);
   65         nsp->nsp_socket = so;
   66         insque(nsp, head);
   67         so->so_pcb = (caddr_t)nsp;
   68         return (0);
   69 }
   70 
   71 ns_pcbbind(nsp, nam)
   72         register struct nspcb *nsp;
   73         struct mbuf *nam;
   74 {
   75         register struct sockaddr_ns *sns;
   76         u_short lport = 0;
   77 
   78         if (nsp->nsp_lport || !ns_nullhost(nsp->nsp_laddr))
   79                 return (EINVAL);
   80         if (nam == 0)
   81                 goto noname;
   82         sns = mtod(nam, struct sockaddr_ns *);
   83         if (nam->m_len != sizeof (*sns))
   84                 return (EINVAL);
   85         if (!ns_nullhost(sns->sns_addr)) {
   86                 int tport = sns->sns_port;
   87 
   88                 sns->sns_port = 0;              /* yech... */
   89                 if (ifa_ifwithaddr((struct sockaddr *)sns) == 0)
   90                         return (EADDRNOTAVAIL);
   91                 sns->sns_port = tport;
   92         }
   93         lport = sns->sns_port;
   94         if (lport) {
   95                 u_short aport = ntohs(lport);
   96 
   97                 if (aport < NSPORT_RESERVED &&
   98                     (nsp->nsp_socket->so_state & SS_PRIV) == 0)
   99                         return (EACCES);
  100                 if (ns_pcblookup(&zerons_addr, lport, 0))
  101                         return (EADDRINUSE);
  102         }
  103         nsp->nsp_laddr = sns->sns_addr;
  104 noname:
  105         if (lport == 0)
  106                 do {
  107                         if (nspcb.nsp_lport++ < NSPORT_RESERVED)
  108                                 nspcb.nsp_lport = NSPORT_RESERVED;
  109                         lport = htons(nspcb.nsp_lport);
  110                 } while (ns_pcblookup(&zerons_addr, lport, 0));
  111         nsp->nsp_lport = lport;
  112         return (0);
  113 }
  114 
  115 /*
  116  * Connect from a socket to a specified address.
  117  * Both address and port must be specified in argument sns.
  118  * If don't have a local address for this socket yet,
  119  * then pick one.
  120  */
  121 ns_pcbconnect(nsp, nam)
  122         struct nspcb *nsp;
  123         struct mbuf *nam;
  124 {
  125         struct ns_ifaddr *ia;
  126         register struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
  127         register struct ns_addr *dst;
  128         register struct route *ro;
  129         struct ifnet *ifp;
  130 
  131         if (nam->m_len != sizeof (*sns))
  132                 return (EINVAL);
  133         if (sns->sns_family != AF_NS)
  134                 return (EAFNOSUPPORT);
  135         if (sns->sns_port==0 || ns_nullhost(sns->sns_addr))
  136                 return (EADDRNOTAVAIL);
  137         /*
  138          * If we haven't bound which network number to use as ours,
  139          * we will use the number of the outgoing interface.
  140          * This depends on having done a routing lookup, which
  141          * we will probably have to do anyway, so we might
  142          * as well do it now.  On the other hand if we are
  143          * sending to multiple destinations we may have already
  144          * done the lookup, so see if we can use the route
  145          * from before.  In any case, we only
  146          * chose a port number once, even if sending to multiple
  147          * destinations.
  148          */
  149         ro = &nsp->nsp_route;
  150         dst = &satons_addr(ro->ro_dst);
  151         if (nsp->nsp_socket->so_options & SO_DONTROUTE)
  152                 goto flush;
  153         if (!ns_neteq(nsp->nsp_lastdst, sns->sns_addr))
  154                 goto flush;
  155         if (!ns_hosteq(nsp->nsp_lastdst, sns->sns_addr)) {
  156                 if (ro->ro_rt && ! (ro->ro_rt->rt_flags & RTF_HOST)) {
  157                         /* can patch route to avoid rtalloc */
  158                         *dst = sns->sns_addr;
  159                 } else {
  160         flush:
  161                         if (ro->ro_rt)
  162                                 RTFREE(ro->ro_rt);
  163                         ro->ro_rt = (struct rtentry *)0;
  164                         nsp->nsp_laddr.x_net = ns_zeronet;
  165                 }
  166         }/* else cached route is ok; do nothing */
  167         nsp->nsp_lastdst = sns->sns_addr;
  168         if ((nsp->nsp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
  169             (ro->ro_rt == (struct rtentry *)0 ||
  170              ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
  171                     /* No route yet, so try to acquire one */
  172                     ro->ro_dst.sa_family = AF_NS;
  173                     ro->ro_dst.sa_len = sizeof(ro->ro_dst);
  174                     *dst = sns->sns_addr;
  175                     dst->x_port = 0;
  176                     rtalloc(ro);
  177         }
  178         if (ns_neteqnn(nsp->nsp_laddr.x_net, ns_zeronet)) {
  179                 /*
  180                  * If route is known or can be allocated now,
  181                  * our src addr is taken from the i/f, else punt.
  182                  */
  183 
  184                 ia = (struct ns_ifaddr *)0;
  185                 /*
  186                  * If we found a route, use the address
  187                  * corresponding to the outgoing interface
  188                  */
  189                 if (ro->ro_rt && (ifp = ro->ro_rt->rt_ifp))
  190                         for (ia = ns_ifaddr; ia; ia = ia->ia_next)
  191                                 if (ia->ia_ifp == ifp)
  192                                         break;
  193                 if (ia == 0) {
  194                         u_short fport = sns->sns_addr.x_port;
  195                         sns->sns_addr.x_port = 0;
  196                         ia = (struct ns_ifaddr *)
  197                                 ifa_ifwithdstaddr((struct sockaddr *)sns);
  198                         sns->sns_addr.x_port = fport;
  199                         if (ia == 0)
  200                                 ia = ns_iaonnetof(&sns->sns_addr);
  201                         if (ia == 0)
  202                                 ia = ns_ifaddr;
  203                         if (ia == 0)
  204                                 return (EADDRNOTAVAIL);
  205                 }
  206                 nsp->nsp_laddr.x_net = satons_addr(ia->ia_addr).x_net;
  207         }
  208         if (ns_pcblookup(&sns->sns_addr, nsp->nsp_lport, 0))
  209                 return (EADDRINUSE);
  210         if (ns_nullhost(nsp->nsp_laddr)) {
  211                 if (nsp->nsp_lport == 0)
  212                         (void) ns_pcbbind(nsp, (struct mbuf *)0);
  213                 nsp->nsp_laddr.x_host = ns_thishost;
  214         }
  215         nsp->nsp_faddr = sns->sns_addr;
  216         /* Includes nsp->nsp_fport = sns->sns_port; */
  217         return (0);
  218 }
  219 
  220 ns_pcbdisconnect(nsp)
  221         struct nspcb *nsp;
  222 {
  223 
  224         nsp->nsp_faddr = zerons_addr;
  225         if (nsp->nsp_socket->so_state & SS_NOFDREF)
  226                 ns_pcbdetach(nsp);
  227 }
  228 
  229 ns_pcbdetach(nsp)
  230         struct nspcb *nsp;
  231 {
  232         struct socket *so = nsp->nsp_socket;
  233 
  234         so->so_pcb = 0;
  235         sofree(so);
  236         if (nsp->nsp_route.ro_rt)
  237                 rtfree(nsp->nsp_route.ro_rt);
  238         remque(nsp);
  239         (void) m_free(dtom(nsp));
  240 }
  241 
  242 ns_setsockaddr(nsp, nam)
  243         register struct nspcb *nsp;
  244         struct mbuf *nam;
  245 {
  246         register struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
  247 
  248         nam->m_len = sizeof (*sns);
  249         sns = mtod(nam, struct sockaddr_ns *);
  250         bzero((caddr_t)sns, sizeof (*sns));
  251         sns->sns_len = sizeof(*sns);
  252         sns->sns_family = AF_NS;
  253         sns->sns_addr = nsp->nsp_laddr;
  254 }
  255 
  256 ns_setpeeraddr(nsp, nam)
  257         register struct nspcb *nsp;
  258         struct mbuf *nam;
  259 {
  260         register struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
  261 
  262         nam->m_len = sizeof (*sns);
  263         sns = mtod(nam, struct sockaddr_ns *);
  264         bzero((caddr_t)sns, sizeof (*sns));
  265         sns->sns_len = sizeof(*sns);
  266         sns->sns_family = AF_NS;
  267         sns->sns_addr  = nsp->nsp_faddr;
  268 }
  269 
  270 /*
  271  * Pass some notification to all connections of a protocol
  272  * associated with address dst.  Call the
  273  * protocol specific routine to handle each connection.
  274  * Also pass an extra paramter via the nspcb. (which may in fact
  275  * be a parameter list!)
  276  */
  277 ns_pcbnotify(dst, errno, notify, param)
  278         register struct ns_addr *dst;
  279         long param;
  280         int errno, (*notify)();
  281 {
  282         register struct nspcb *nsp, *oinp;
  283         int s = splimp();
  284 
  285         for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb);) {
  286                 if (!ns_hosteq(*dst,nsp->nsp_faddr)) {
  287         next:
  288                         nsp = nsp->nsp_next;
  289                         continue;
  290                 }
  291                 if (nsp->nsp_socket == 0)
  292                         goto next;
  293                 if (errno)
  294                         nsp->nsp_socket->so_error = errno;
  295                 oinp = nsp;
  296                 nsp = nsp->nsp_next;
  297                 oinp->nsp_notify_param = param;
  298                 (*notify)(oinp);
  299         }
  300         splx(s);
  301 }
  302 
  303 #ifdef notdef
  304 /*
  305  * After a routing change, flush old routing
  306  * and allocate a (hopefully) better one.
  307  */
  308 ns_rtchange(nsp)
  309         struct nspcb *nsp;
  310 {
  311         if (nsp->nsp_route.ro_rt) {
  312                 rtfree(nsp->nsp_route.ro_rt);
  313                 nsp->nsp_route.ro_rt = 0;
  314                 /*
  315                  * A new route can be allocated the next time
  316                  * output is attempted.
  317                  */
  318         }
  319         /* SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
  320 }
  321 #endif
  322 
  323 struct nspcb *
  324 ns_pcblookup(faddr, lport, wildp)
  325         struct ns_addr *faddr;
  326         u_short lport;
  327 {
  328         register struct nspcb *nsp, *match = 0;
  329         int matchwild = 3, wildcard;
  330         u_short fport;
  331 
  332         fport = faddr->x_port;
  333         for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb); nsp = nsp->nsp_next) {
  334                 if (nsp->nsp_lport != lport)
  335                         continue;
  336                 wildcard = 0;
  337                 if (ns_nullhost(nsp->nsp_faddr)) {
  338                         if (!ns_nullhost(*faddr))
  339                                 wildcard++;
  340                 } else {
  341                         if (ns_nullhost(*faddr))
  342                                 wildcard++;
  343                         else {
  344                                 if (!ns_hosteq(nsp->nsp_faddr, *faddr))
  345                                         continue;
  346                                 if (nsp->nsp_fport != fport) {
  347                                         if (nsp->nsp_fport != 0)
  348                                                 continue;
  349                                         else
  350                                                 wildcard++;
  351                                 }
  352                         }
  353                 }
  354                 if (wildcard && wildp==0)
  355                         continue;
  356                 if (wildcard < matchwild) {
  357                         match = nsp;
  358                         matchwild = wildcard;
  359                         if (wildcard == 0)
  360                                 break;
  361                 }
  362         }
  363         return (match);
  364 }

Cache object: 8ff92c83e5dddb94bd4f9a6dc7fc01ea


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.