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/nfs/krpc_subr.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 /*      $NetBSD: krpc_subr.c,v 1.27 2003/02/26 06:31:18 matt Exp $      */
    2 
    3 /*
    4  * Copyright (c) 1995 Gordon Ross, Adam Glass
    5  * Copyright (c) 1992 Regents of the University of California.
    6  * All rights reserved.
    7  *
    8  * This software was developed by the Computer Systems Engineering group
    9  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
   10  * contributed to Berkeley.
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  * 1. Redistributions of source code must retain the above copyright
   16  *    notice, this list of conditions and the following disclaimer.
   17  * 2. Redistributions in binary form must reproduce the above copyright
   18  *    notice, this list of conditions and the following disclaimer in the
   19  *    documentation and/or other materials provided with the distribution.
   20  * 3. All advertising materials mentioning features or use of this software
   21  *    must display the following acknowledgement:
   22  *      This product includes software developed by the University of
   23  *      California, Lawrence Berkeley Laboratory and its contributors.
   24  * 4. Neither the name of the University nor the names of its contributors
   25  *    may be used to endorse or promote products derived from this software
   26  *    without specific prior written permission.
   27  *
   28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   38  * SUCH DAMAGE.
   39  *
   40  * partially based on:
   41  *      libnetboot/rpc.c
   42  *               @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp  (LBL)
   43  */
   44 
   45 #include <sys/cdefs.h>
   46 __KERNEL_RCSID(0, "$NetBSD: krpc_subr.c,v 1.27 2003/02/26 06:31:18 matt Exp $");
   47 
   48 #include <sys/param.h>
   49 #include <sys/systm.h>
   50 #include <sys/ioctl.h>
   51 #include <sys/proc.h>
   52 #include <sys/mount.h>
   53 #include <sys/mbuf.h>
   54 #include <sys/reboot.h>
   55 #include <sys/socket.h>
   56 #include <sys/socketvar.h>
   57 
   58 #include <netinet/in.h>
   59 
   60 #include <nfs/rpcv2.h>
   61 #include <nfs/krpc.h>
   62 #include <nfs/xdr_subs.h>
   63 #include <nfs/nfsproto.h> /* XXX NFSX_V3FHMAX for next */
   64 #include <nfs/nfs.h>
   65 #include <nfs/nfsmount.h>
   66 #include <nfs/nfsdiskless.h> /* XXX decl nfs_boot_sendrecv */
   67 
   68 /*
   69  * Kernel support for Sun RPC
   70  *
   71  * Used currently for bootstrapping in nfs diskless configurations.
   72  */
   73 
   74 /*
   75  * Generic RPC headers
   76  */
   77 
   78 struct auth_info {
   79         u_int32_t       authtype;       /* auth type */
   80         u_int32_t       authlen;        /* auth length */
   81 };
   82 
   83 struct auth_unix {
   84         int32_t   ua_time;
   85         int32_t   ua_hostname;  /* null */
   86         int32_t   ua_uid;
   87         int32_t   ua_gid;
   88         int32_t   ua_gidlist;   /* null */
   89 };
   90 
   91 struct rpc_call {
   92         u_int32_t       rp_xid;         /* request transaction id */
   93         int32_t         rp_direction;   /* call direction (0) */
   94         u_int32_t       rp_rpcvers;     /* rpc version (2) */
   95         u_int32_t       rp_prog;        /* program */
   96         u_int32_t       rp_vers;        /* version */
   97         u_int32_t       rp_proc;        /* procedure */
   98         struct  auth_info rpc_auth;
   99         struct  auth_unix rpc_unix;
  100         struct  auth_info rpc_verf;
  101 };
  102 
  103 struct rpc_reply {
  104         u_int32_t rp_xid;               /* request transaction id */
  105         int32_t  rp_direction;          /* call direction (1) */
  106         int32_t  rp_astatus;            /* accept status (0: accepted) */
  107         union {
  108                 /* rejected */
  109                 struct {
  110                         u_int32_t rej_stat;
  111                         u_int32_t rej_val1;
  112                         u_int32_t rej_val2;
  113                 } rpu_rej;
  114                 /* accepted */
  115                 struct {
  116                         struct auth_info rok_auth;
  117                         u_int32_t       rok_status;
  118                 } rpu_rok;
  119         } rp_u;
  120 };
  121 #define rp_rstat  rp_u.rpu_rej.rej_stat
  122 #define rp_auth   rp_u.rpu_rok.rok_auth
  123 #define rp_status rp_u.rpu_rok.rok_status
  124 
  125 #define MIN_REPLY_HDR 16        /* xid, dir, astat, errno */
  126 
  127 static int krpccheck __P((struct mbuf*, void*));
  128 
  129 /*
  130  * Call portmap to lookup a port number for a particular rpc program
  131  * Returns non-zero error on failure.
  132  */
  133 int
  134 krpc_portmap(sin,  prog, vers, proto, portp)
  135         struct sockaddr_in *sin;                /* server address */
  136         u_int prog, vers, proto;        /* host order */
  137         u_int16_t *portp;       /* network order */
  138 {
  139         struct sdata {
  140                 u_int32_t prog;         /* call program */
  141                 u_int32_t vers;         /* call version */
  142                 u_int32_t proto;        /* call protocol */
  143                 u_int32_t port;         /* call port (unused) */
  144         } *sdata;
  145         struct rdata {
  146                 u_int16_t pad;
  147                 u_int16_t port;
  148         } *rdata;
  149         struct mbuf *m;
  150         int error;
  151 
  152         /* The portmapper port is fixed. */
  153         if (prog == PMAPPROG) {
  154                 *portp = htons(PMAPPORT);
  155                 return 0;
  156         }
  157 
  158         m = m_get(M_WAIT, MT_DATA);
  159         sdata = mtod(m, struct sdata *);
  160         m->m_len = sizeof(*sdata);
  161 
  162         /* Do the RPC to get it. */
  163         sdata->prog = txdr_unsigned(prog);
  164         sdata->vers = txdr_unsigned(vers);
  165         sdata->proto = txdr_unsigned(proto);
  166         sdata->port = 0;
  167 
  168         sin->sin_port = htons(PMAPPORT);
  169         error = krpc_call(sin, PMAPPROG, PMAPVERS,
  170                                           PMAPPROC_GETPORT, &m, NULL);
  171         if (error) 
  172                 return error;
  173 
  174         if (m->m_len < sizeof(*rdata)) {
  175                 m = m_pullup(m, sizeof(*rdata));
  176                 if (m == NULL)
  177                         return ENOBUFS;
  178         }
  179         rdata = mtod(m, struct rdata *);
  180         *portp = rdata->port;
  181 
  182         m_freem(m);
  183         return 0;
  184 }
  185 
  186 static int krpccheck(m, context)
  187 struct mbuf *m;
  188 void *context;
  189 {
  190         struct rpc_reply *reply;
  191 
  192         /* Does the reply contain at least a header? */
  193         if (m->m_pkthdr.len < MIN_REPLY_HDR)
  194                 return(-1);
  195         if (m->m_len < sizeof(struct rpc_reply)) {
  196                 m = m_pullup(m, sizeof(struct rpc_reply));
  197                 if (m == NULL)
  198                         return(-1);
  199         }
  200         reply = mtod(m, struct rpc_reply *);
  201 
  202         /* Is it the right reply? */
  203         if (reply->rp_direction != txdr_unsigned(RPC_REPLY))
  204                 return(-1);
  205 
  206         if (reply->rp_xid != txdr_unsigned(*(u_int32_t*)context))
  207                 return(-1);
  208 
  209         return(0);
  210 }
  211 
  212 /*
  213  * Do a remote procedure call (RPC) and wait for its reply.
  214  * If from_p is non-null, then we are doing broadcast, and
  215  * the address from whence the response came is saved there.
  216  */
  217 int
  218 krpc_call(sa, prog, vers, func, data, from_p)
  219         struct sockaddr_in *sa;
  220         u_int prog, vers, func;
  221         struct mbuf **data;     /* input/output */
  222         struct mbuf **from_p;   /* output */
  223 {
  224         struct socket *so;
  225         struct sockaddr_in *sin;
  226         struct mbuf *m, *nam, *mhead, *from;
  227         struct rpc_call *call;
  228         struct rpc_reply *reply;
  229         int error, len;
  230         static u_int32_t xid = ~0xFF;
  231         u_int16_t tport;
  232 
  233         /*
  234          * Validate address family.
  235          * Sorry, this is INET specific...
  236          */
  237         if (sa->sin_family != AF_INET)
  238                 return (EAFNOSUPPORT);
  239 
  240         /* Free at end if not null. */
  241         nam = mhead = NULL;
  242         from = NULL;
  243 
  244         /*
  245          * Create socket and set its receive timeout.
  246          */
  247         if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)))
  248                 goto out;
  249 
  250         if ((error = nfs_boot_setrecvtimo(so)))
  251                 goto out;
  252 
  253         /*
  254          * Enable broadcast if necessary.
  255          */
  256         if (from_p) {
  257                 if ((error = nfs_boot_enbroadcast(so)))
  258                         goto out;
  259         }
  260 
  261         /*
  262          * Bind the local endpoint to a reserved port,
  263          * because some NFS servers refuse requests from
  264          * non-reserved (non-privileged) ports.
  265          */
  266         tport = IPPORT_RESERVED;
  267         do {
  268                 tport--;
  269                 error = nfs_boot_sobind_ipport(so, tport);
  270         } while (error == EADDRINUSE &&
  271                          tport > IPPORT_RESERVED / 2);
  272         if (error) {
  273                 printf("bind failed\n");
  274                 goto out;
  275         }
  276 
  277         /*
  278          * Setup socket address for the server.
  279          */
  280         nam = m_get(M_WAIT, MT_SONAME);
  281         sin = mtod(nam, struct sockaddr_in *);
  282         memcpy((caddr_t)sin, (caddr_t)sa,
  283                   (nam->m_len = sa->sin_len));
  284 
  285         /*
  286          * Prepend RPC message header.
  287          */
  288         mhead = m_gethdr(M_WAIT, MT_DATA);
  289         mhead->m_next = *data;
  290         call = mtod(mhead, struct rpc_call *);
  291         mhead->m_len = sizeof(*call);
  292         memset((caddr_t)call, 0, sizeof(*call));
  293         /* rpc_call part */
  294         xid++;
  295         call->rp_xid = txdr_unsigned(xid);
  296         /* call->rp_direction = 0; */
  297         call->rp_rpcvers = txdr_unsigned(2);
  298         call->rp_prog = txdr_unsigned(prog);
  299         call->rp_vers = txdr_unsigned(vers);
  300         call->rp_proc = txdr_unsigned(func);
  301         /* rpc_auth part (auth_unix as root) */
  302         call->rpc_auth.authtype = txdr_unsigned(RPCAUTH_UNIX);
  303         call->rpc_auth.authlen  = txdr_unsigned(sizeof(struct auth_unix));
  304         /* rpc_verf part (auth_null) */
  305         call->rpc_verf.authtype = 0;
  306         call->rpc_verf.authlen  = 0;
  307 
  308         /*
  309          * Setup packet header
  310          */
  311         len = 0;
  312         m = mhead;
  313         while (m) {
  314                 len += m->m_len;
  315                 m = m->m_next;
  316         }
  317         mhead->m_pkthdr.len = len;
  318         mhead->m_pkthdr.rcvif = NULL;
  319 
  320         error = nfs_boot_sendrecv(so, nam, 0, mhead, krpccheck, &m, &from, &xid);
  321         if (error)
  322                 goto out;
  323 
  324         /* m_pullup() was done in krpccheck() */
  325         reply = mtod(m, struct rpc_reply *);
  326 
  327         /* Was RPC accepted? (authorization OK) */
  328         if (reply->rp_astatus != 0) {
  329                 /* Note: This is NOT an error code! */
  330                 error = fxdr_unsigned(u_int32_t, reply->rp_rstat);
  331                 switch (error) {
  332                     case RPC_MISMATCH:
  333                         /* .re_status = RPC_VERSMISMATCH; */
  334                         error = ERPCMISMATCH;
  335                         break;
  336                     case RPC_AUTHERR:
  337                         /* .re_status = RPC_AUTHERROR; */
  338                         error = EAUTH;
  339                         break;
  340                     default:
  341                         /* unexpected */
  342                         error = EBADRPC;
  343                         break;
  344                 }
  345                 goto out;
  346         }
  347 
  348         /* Did the call succeed? */
  349         if (reply->rp_status != 0) {
  350                 /* Note: This is NOT an error code! */
  351                 error = fxdr_unsigned(u_int32_t, reply->rp_status);
  352                 switch (error) {
  353                     case RPC_PROGUNAVAIL:
  354                         error = EPROGUNAVAIL;
  355                         break;
  356                     case RPC_PROGMISMATCH:
  357                         error = EPROGMISMATCH;
  358                         break;
  359                     case RPC_PROCUNAVAIL:
  360                         error = EPROCUNAVAIL;
  361                         break;
  362                     case RPC_GARBAGE:
  363                     default:
  364                         error = EBADRPC;
  365                 }
  366                 goto out;
  367         }
  368 
  369         /*
  370          * OK, we have received a good reply!
  371          * Get its length, then strip it off.
  372          */
  373         len = sizeof(*reply);
  374         if (reply->rp_auth.authtype != 0) {
  375                 len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
  376                 len = (len + 3) & ~3; /* XXX? */
  377         }
  378         m_adj(m, len);
  379 
  380         /* result */
  381         *data = m;
  382         if (from_p) {
  383                 *from_p = from;
  384                 from = NULL;
  385         }
  386 
  387  out:
  388         if (nam) m_freem(nam);
  389         if (mhead) m_freem(mhead);
  390         if (from) m_freem(from);
  391         soclose(so);
  392         return error;
  393 }
  394 
  395 /*
  396  * eXternal Data Representation routines.
  397  * (but with non-standard args...)
  398  */
  399 
  400 /*
  401  * String representation for RPC.
  402  */
  403 struct xdr_string {
  404         u_int32_t len;          /* length without null or padding */
  405         char data[4];   /* data (longer, of course) */
  406     /* data is padded to a long-word boundary */
  407 };
  408 
  409 struct mbuf *
  410 xdr_string_encode(str, len)
  411         char *str;
  412         int len;
  413 {
  414         struct mbuf *m;
  415         struct xdr_string *xs;
  416         int dlen;       /* padded string length */
  417         int mlen;       /* message length */
  418 
  419         dlen = (len + 3) & ~3;
  420         mlen = dlen + 4;
  421 
  422         if (mlen > MCLBYTES)            /* If too big, we just can't do it. */
  423                 return (NULL);
  424 
  425         m = m_get(M_WAIT, MT_DATA);
  426         if (mlen > MLEN) {
  427                 m_clget(m, M_WAIT);
  428                 if ((m->m_flags & M_EXT) == 0) {
  429                         (void) m_free(m);       /* There can be only one. */
  430                         return (NULL);
  431                 }
  432         }
  433         xs = mtod(m, struct xdr_string *);
  434         m->m_len = mlen;
  435         xs->len = txdr_unsigned(len);
  436         memcpy(xs->data, str, len);
  437         return (m);
  438 }
  439 
  440 struct mbuf *
  441 xdr_string_decode(m, str, len_p)
  442         struct mbuf *m;
  443         char *str;
  444         int *len_p;             /* bufsize - 1 */
  445 {
  446         struct xdr_string *xs;
  447         int mlen;       /* message length */
  448         int slen;       /* string length */
  449 
  450         if (m->m_len < 4) {
  451                 m = m_pullup(m, 4);
  452                 if (m == NULL)
  453                         return (NULL);
  454         }
  455         xs = mtod(m, struct xdr_string *);
  456         slen = fxdr_unsigned(u_int32_t, xs->len);
  457         mlen = 4 + ((slen + 3) & ~3);
  458 
  459         if (slen > *len_p)
  460                 slen = *len_p;
  461         m_copydata(m, 4, slen, str);
  462         m_adj(m, mlen);
  463 
  464         str[slen] = '\0';
  465         *len_p = slen;
  466 
  467         return (m);
  468 }
  469 
  470 
  471 /*
  472  * Inet address in RPC messages
  473  * (Note, really four ints, NOT chars.  Blech.)
  474  */
  475 struct xdr_inaddr {
  476         u_int32_t atype;
  477         u_int32_t addr[4];
  478 };
  479 
  480 struct mbuf *
  481 xdr_inaddr_encode(ia)
  482         struct in_addr *ia;             /* already in network order */
  483 {
  484         struct mbuf *m;
  485         struct xdr_inaddr *xi;
  486         u_int8_t *cp;
  487         u_int32_t *ip;
  488 
  489         m = m_get(M_WAIT, MT_DATA);
  490         xi = mtod(m, struct xdr_inaddr *);
  491         m->m_len = sizeof(*xi);
  492         xi->atype = txdr_unsigned(1);
  493         ip = xi->addr;
  494         cp = (u_int8_t *)&ia->s_addr;
  495         *ip++ = txdr_unsigned(*cp++);
  496         *ip++ = txdr_unsigned(*cp++);
  497         *ip++ = txdr_unsigned(*cp++);
  498         *ip++ = txdr_unsigned(*cp++);
  499 
  500         return (m);
  501 }
  502 
  503 struct mbuf *
  504 xdr_inaddr_decode(m, ia)
  505         struct mbuf *m;
  506         struct in_addr *ia;             /* already in network order */
  507 {
  508         struct xdr_inaddr *xi;
  509         u_int8_t *cp;
  510         u_int32_t *ip;
  511 
  512         if (m->m_len < sizeof(*xi)) {
  513                 m = m_pullup(m, sizeof(*xi));
  514                 if (m == NULL)
  515                         return (NULL);
  516         }
  517         xi = mtod(m, struct xdr_inaddr *);
  518         if (xi->atype != txdr_unsigned(1)) {
  519                 ia->s_addr = INADDR_ANY;
  520                 goto out;
  521         }
  522         ip = xi->addr;
  523         cp = (u_int8_t *)&ia->s_addr;
  524         *cp++ = fxdr_unsigned(u_int8_t, *ip++);
  525         *cp++ = fxdr_unsigned(u_int8_t, *ip++);
  526         *cp++ = fxdr_unsigned(u_int8_t, *ip++);
  527         *cp++ = fxdr_unsigned(u_int8_t, *ip++);
  528 
  529 out:
  530         m_adj(m, sizeof(*xi));
  531         return (m);
  532 }

Cache object: 64bfe48573c85217e77ce65551a0d58c


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