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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

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

Cache object: 0fb680016beb011c584de5272786e3dd


[ 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.