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/nfs_bootparam.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: nfs_bootparam.c,v 1.39 2019/06/29 17:42:36 kamil Exp $ */
    2 
    3 /*-
    4  * Copyright (c) 1995, 1997 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to The NetBSD Foundation
    8  * by Adam Glass and Gordon W. Ross.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   29  * POSSIBILITY OF SUCH DAMAGE.
   30  */
   31 
   32 /*
   33  * Support for NFS diskless booting, Sun-style (RPC/bootparams)
   34  */
   35 
   36 #include <sys/cdefs.h>
   37 __KERNEL_RCSID(0, "$NetBSD: nfs_bootparam.c,v 1.39 2019/06/29 17:42:36 kamil Exp $");
   38 
   39 #ifdef _KERNEL_OPT
   40 #include "opt_nfs_boot.h"
   41 #include "arp.h"
   42 #endif
   43 
   44 #include <sys/param.h>
   45 #include <sys/systm.h>
   46 #include <sys/kernel.h>
   47 #include <sys/ioctl.h>
   48 #include <sys/proc.h>
   49 #include <sys/mount.h>
   50 #include <sys/mbuf.h>
   51 #include <sys/reboot.h>
   52 #include <sys/socket.h>
   53 #include <sys/socketvar.h>
   54 #include <sys/vnode.h>
   55 
   56 #include <net/if.h>
   57 #include <net/if_types.h>
   58 #include <net/route.h>
   59 #include <net/if_ether.h>
   60 
   61 #include <netinet/in.h>
   62 #include <netinet/if_inarp.h>
   63 
   64 #include <nfs/rpcv2.h>
   65 #include <nfs/krpc.h>
   66 #include <nfs/xdr_subs.h>
   67 
   68 #include <nfs/nfsproto.h>
   69 #include <nfs/nfs.h>
   70 #include <nfs/nfsmount.h>
   71 #include <nfs/nfsdiskless.h>
   72 #include <nfs/nfs_var.h>
   73 
   74 /*
   75  * There are two implementations of NFS diskless boot.
   76  * This implementation uses Sun RPC/bootparams, and the
   77  * the other uses BOOTP (RFC951 - see nfs_bootdhcp.c).
   78  *
   79  * The Sun-style boot sequence goes as follows:
   80  * (1) Use RARP to get our interface address
   81  * (2) Use RPC/bootparam/whoami to get our hostname,
   82  *     our IP address, and the server's IP address.
   83  * (3) Use RPC/bootparam/getfile to get the root path
   84  * (4) Use RPC/mountd to get the root file handle
   85  * (5) Use RPC/bootparam/getfile to get the swap path
   86  * (6) Use RPC/mountd to get the swap file handle
   87  */
   88 
   89 /* bootparam RPC */
   90 static int bp_whoami (struct sockaddr_in *bpsin,
   91         struct in_addr *my_ip, struct in_addr *gw_ip, struct lwp *l);
   92 static int bp_getfile (struct sockaddr_in *bpsin, const char *key,
   93         struct nfs_dlmount *ndm, struct lwp *l);
   94 
   95 
   96 /*
   97  * Get client name, gateway address, then
   98  * get root and swap server:pathname info.
   99  * RPCs: bootparam/whoami, bootparam/getfile
  100  *
  101  * Use the old broadcast address for the WHOAMI
  102  * call because we do not yet know our netmask.
  103  * The server address returned by the WHOAMI call
  104  * is used for all subsequent bootparam RPCs.
  105  */
  106 int
  107 nfs_bootparam(struct nfs_diskless *nd, struct lwp *lwp, int *flags)
  108 {
  109         struct ifnet *ifp = nd->nd_ifp;
  110         struct in_addr my_ip, arps_ip, gw_ip;
  111         struct sockaddr_in bp_sin;
  112         struct sockaddr_in *sin;
  113 #ifndef NFS_BOOTPARAM_NOGATEWAY
  114         struct nfs_dlmount *gw_ndm = 0;
  115         char *p;
  116         u_int32_t mask;
  117 #endif
  118         int error;
  119 
  120         /*
  121          * Bring up the interface. (just set the "up" flag)
  122          */
  123         error = nfs_boot_ifupdown(ifp, lwp, 1);
  124         if (error) {
  125                 printf("nfs_boot: SIFFLAGS, error=%d\n", error);
  126                 return (error);
  127         }
  128 
  129         error = EADDRNOTAVAIL;
  130         memset(&arps_ip, 0, sizeof(arps_ip)); /* GCC */
  131 #if NARP > 0
  132         if (ifp->if_type == IFT_ETHER || ifp->if_type == IFT_FDDI) {
  133                 /*
  134                  * Do RARP for the interface address.
  135                  */
  136                 error = revarpwhoarewe(ifp, &arps_ip, &my_ip);
  137         }
  138 #endif
  139         if (error) {
  140                 printf("revarp failed, error=%d\n", error);
  141                 goto out;
  142         }
  143 
  144         if (!(*flags & NFS_BOOT_HAS_MYIP)) {
  145                 nd->nd_myip.s_addr = my_ip.s_addr;
  146                 printf("nfs_boot: client_addr=%s", inet_ntoa(my_ip));
  147                 printf(" (RARP from %s)\n", inet_ntoa(arps_ip));
  148                 *flags |= NFS_BOOT_HAS_MYIP;
  149         }
  150 
  151         /*
  152          * Do enough of ifconfig(8) so that the chosen interface
  153          * can talk to the servers.  (just set the address)
  154          */
  155         error = nfs_boot_setaddress(ifp, lwp, my_ip.s_addr,
  156                                     INADDR_ANY, INADDR_ANY);
  157         if (error) {
  158                 printf("nfs_boot: set ifaddr, error=%d\n", error);
  159                 goto out;
  160         }
  161 
  162         /*
  163          * Get client name and gateway address.
  164          * RPC: bootparam/whoami
  165          * Use the old broadcast address for the WHOAMI
  166          * call because we do not yet know our netmask.
  167          * The server address returned by the WHOAMI call
  168          * is used for all subsequent booptaram RPCs.
  169          */
  170         sin = &bp_sin;
  171         memset((void *)sin, 0, sizeof(*sin));
  172         sin->sin_len = sizeof(*sin);
  173         sin->sin_family = AF_INET;
  174         sin->sin_addr.s_addr = INADDR_BROADCAST;
  175 
  176         /* Do the RPC/bootparam/whoami. */
  177         error = bp_whoami(sin, &my_ip, &gw_ip, lwp);
  178         if (error) {
  179                 printf("nfs_boot: bootparam whoami, error=%d\n", error);
  180                 goto delout;
  181         }
  182         *flags |= NFS_BOOT_HAS_SERVADDR | NFS_BOOT_HAS_SERVER;
  183         printf("nfs_boot: server_addr=%s\n", inet_ntoa(sin->sin_addr));
  184         printf("nfs_boot: hostname=%s\n", hostname);
  185 
  186         /*
  187          * Now fetch the server:pathname strings and server IP
  188          * for root and swap.  Missing swap is not fatal.
  189          */
  190         error = bp_getfile(sin, "root", &nd->nd_root, lwp);
  191         if (error) {
  192                 printf("nfs_boot: bootparam get root: %d\n", error);
  193                 goto delout;
  194         }
  195         *flags |= NFS_BOOT_HAS_ROOTPATH;
  196 
  197 #ifndef NFS_BOOTPARAM_NOGATEWAY
  198         gw_ndm = kmem_alloc(sizeof(*gw_ndm), KM_SLEEP);
  199         memset((void *)gw_ndm, 0, sizeof(*gw_ndm));
  200         error = bp_getfile(sin, "gateway", gw_ndm, lwp);
  201         if (error) {
  202                 /* No gateway supplied. No error, but try fallback. */
  203                 error = 0;
  204                 goto nogwrepl;
  205         }
  206         sin = (struct sockaddr_in *) &gw_ndm->ndm_saddr;
  207         if (sin->sin_addr.s_addr == 0)
  208                 goto out;       /* no gateway */
  209 
  210         /* OK, we have a gateway! */
  211         printf("nfs_boot: gateway=%s\n", inet_ntoa(sin->sin_addr));
  212         /* Just save it.  Caller adds the route. */
  213         nd->nd_gwip = sin->sin_addr;
  214         *flags |= NFS_BOOT_HAS_GWIP;
  215 
  216         /* Look for a mask string after the colon. */
  217         p = strchr(gw_ndm->ndm_host, ':');
  218         if (p == 0)
  219                 goto out;       /* no netmask */
  220         /* have pathname */
  221         p++;    /* skip ':' */
  222         mask = inet_addr(p);    /* libkern */
  223         if (mask == 0)
  224                 goto out;       /* no netmask */
  225 
  226         /* Have a netmask too!  Save it; update the I/F. */
  227         nd->nd_mask.s_addr = mask;
  228         *flags |= NFS_BOOT_HAS_MASK;
  229         printf("nfs_boot: my_mask=%s\n", inet_ntoa(nd->nd_mask));
  230         (void)  nfs_boot_deladdress(ifp, lwp, my_ip.s_addr);
  231         error = nfs_boot_setaddress(ifp, lwp, my_ip.s_addr,
  232                                     mask, INADDR_ANY);
  233         if (error) {
  234                 printf("nfs_boot: set ifmask, error=%d\n", error);
  235                 goto out;
  236         }
  237         goto gwok;
  238 nogwrepl:
  239 #endif
  240 #ifdef NFS_BOOT_GATEWAY
  241         /*
  242          * Note: we normally ignore the gateway address returned
  243          * by the "bootparam/whoami" RPC above, because many old
  244          * bootparam servers supply a bogus gateway value.
  245          *
  246          * These deficiencies in the bootparam RPC interface are
  247          * circumvented by using the bootparam/getfile RPC.  The
  248          * parameter "gateway" is requested, and if its returned,
  249          * we use the "server" part of the reply as the gateway,
  250          * and use the "pathname" part of the reply as the mask.
  251          * (The mask comes to us as a string.)
  252          */
  253         if (gw_ip.s_addr) {
  254                 /* Our caller will add the route. */
  255                 nd->nd_gwip = gw_ip;
  256                 *flags |= NFS_BOOT_HAS_GWIP;
  257         }
  258 #endif
  259 
  260 delout:
  261         if (error)
  262                 (void) nfs_boot_deladdress(ifp, lwp, my_ip.s_addr);
  263 out:
  264         if (error) {
  265                 (void) nfs_boot_ifupdown(ifp, lwp, 0);
  266                 nfs_boot_flushrt(ifp);
  267         }
  268 #ifndef NFS_BOOTPARAM_NOGATEWAY
  269 gwok:
  270         if (gw_ndm)
  271                 kmem_free(gw_ndm, sizeof(*gw_ndm));
  272 #endif
  273         if ((*flags & NFS_BOOT_ALLINFO) != NFS_BOOT_ALLINFO)
  274                 return error ? error : EADDRNOTAVAIL;
  275 
  276         return (error);
  277 }
  278 
  279 
  280 /*
  281  * RPC: bootparam/whoami
  282  * Given client IP address, get:
  283  *      client name     (hostname)
  284  *      domain name (domainname)
  285  *      gateway address
  286  *
  287  * The hostname and domainname are set here for convenience.
  288  *
  289  * Note - bpsin is initialized to the broadcast address,
  290  * and will be replaced with the bootparam server address
  291  * after this call is complete.  Have to use PMAP_PROC_CALL
  292  * to make sure we get responses only from a servers that
  293  * know about us (don't want to broadcast a getport call).
  294  */
  295 static int
  296 bp_whoami(struct sockaddr_in *bpsin, struct in_addr *my_ip,
  297         struct in_addr *gw_ip, struct lwp *l)
  298 {
  299         /* RPC structures for PMAPPROC_CALLIT */
  300         struct whoami_call {
  301                 u_int32_t call_prog;
  302                 u_int32_t call_vers;
  303                 u_int32_t call_proc;
  304                 u_int32_t call_arglen;
  305         } *call;
  306         struct callit_reply {
  307                 u_int32_t port;
  308                 u_int32_t encap_len;
  309                 /* encapsulated data here */
  310         } *reply;
  311 
  312         struct mbuf *m, *from;
  313         struct sockaddr_in *sin;
  314         int error;
  315         int16_t port;
  316 
  317         /*
  318          * Build request message for PMAPPROC_CALLIT.
  319          */
  320         m = m_get(M_WAIT, MT_DATA);
  321         call = mtod(m, struct whoami_call *);
  322         m->m_len = sizeof(*call);
  323         call->call_prog = txdr_unsigned(BOOTPARAM_PROG);
  324         call->call_vers = txdr_unsigned(BOOTPARAM_VERS);
  325         call->call_proc = txdr_unsigned(BOOTPARAM_WHOAMI);
  326 
  327         /*
  328          * append encapsulated data (client IP address)
  329          */
  330         m->m_next = xdr_inaddr_encode(my_ip);
  331         call->call_arglen = txdr_unsigned(m->m_next->m_len);
  332 
  333         /* RPC: portmap/callit */
  334         bpsin->sin_port = htons(PMAPPORT);
  335         error = krpc_call(bpsin, PMAPPROG, PMAPVERS,
  336                         PMAPPROC_CALLIT, &m, &from, l);
  337         if (error) {
  338                 m_freem(m);
  339                 return error;
  340         }
  341 
  342         /*
  343          * Parse result message.
  344          */
  345         if (m->m_len < sizeof(*reply)) {
  346                 m = m_pullup(m, sizeof(*reply));
  347                 if (m == NULL)
  348                         goto bad;
  349         }
  350         reply = mtod(m, struct callit_reply *);
  351         port = fxdr_unsigned(u_int32_t, reply->port);
  352         m_adj(m, sizeof(*reply));
  353 
  354         /*
  355          * Save bootparam server address
  356          */
  357         sin = mtod(from, struct sockaddr_in *);
  358         bpsin->sin_port = htons(port);
  359         bpsin->sin_addr.s_addr = sin->sin_addr.s_addr;
  360 
  361         /* client name */
  362         hostnamelen = MAXHOSTNAMELEN-1;
  363         m = xdr_string_decode(m, hostname, &hostnamelen);
  364         if (m == NULL)
  365                 goto bad;
  366 
  367         /* domain name */
  368         domainnamelen = MAXHOSTNAMELEN-1;
  369         m = xdr_string_decode(m, domainname, &domainnamelen);
  370         if (m == NULL)
  371                 goto bad;
  372 
  373         /* gateway address */
  374         m = xdr_inaddr_decode(m, gw_ip);
  375         if (m == NULL)
  376                 goto bad;
  377 
  378         /* success */
  379         goto out;
  380 
  381 bad:
  382         printf("nfs_boot: bootparam_whoami: bad reply\n");
  383         error = EBADRPC;
  384 
  385 out:
  386         m_freem(from);
  387         if (m)
  388                 m_freem(m);
  389         return(error);
  390 }
  391 
  392 
  393 /*
  394  * RPC: bootparam/getfile
  395  * Given client name and file "key", get:
  396  *      server name
  397  *      server IP address
  398  *      server pathname
  399  */
  400 static int
  401 bp_getfile(struct sockaddr_in *bpsin, const char *key,
  402         struct nfs_dlmount *ndm, struct lwp *l)
  403 {
  404         char pathname[MNAMELEN];
  405         struct in_addr inaddr;
  406         struct sockaddr_in *sin;
  407         struct mbuf *m;
  408         char *serv_name;
  409         int error, sn_len, path_len;
  410 
  411         /*
  412          * Build request message.
  413          */
  414 
  415         /* client name (hostname) */
  416         m  = xdr_string_encode(hostname, hostnamelen);
  417         if (m == NULL)
  418                 return (ENOMEM);
  419 
  420         /* key name (root or swap) */
  421         /*XXXUNCONST*/
  422         m->m_next = xdr_string_encode(__UNCONST(key), strlen(key));
  423         if (m->m_next == NULL)
  424                 return (ENOMEM);
  425 
  426         /* RPC: bootparam/getfile */
  427         error = krpc_call(bpsin, BOOTPARAM_PROG, BOOTPARAM_VERS,
  428                           BOOTPARAM_GETFILE, &m, NULL, l);
  429         if (error)
  430                 return error;
  431 
  432         /*
  433          * Parse result message.
  434          */
  435 
  436         /* server name */
  437         serv_name = &ndm->ndm_host[0];
  438         sn_len = sizeof(ndm->ndm_host) - 1;
  439         m = xdr_string_decode(m, serv_name, &sn_len);
  440         if (m == NULL)
  441                 goto bad;
  442 
  443         /* server IP address (mountd/NFS) */
  444         m = xdr_inaddr_decode(m, &inaddr);
  445         if (m == NULL)
  446                 goto bad;
  447 
  448         /* server pathname */
  449         path_len = sizeof(pathname) - 1;
  450         m = xdr_string_decode(m, pathname, &path_len);
  451         if (m == NULL)
  452                 goto bad;
  453 
  454         /*
  455          * Store the results in the nfs_dlmount.
  456          * The strings become "server:pathname"
  457          */
  458         sin = (struct sockaddr_in *) &ndm->ndm_saddr;
  459         memset((void *)sin, 0, sizeof(*sin));
  460         sin->sin_len = sizeof(*sin);
  461         sin->sin_family = AF_INET;
  462         sin->sin_addr = inaddr;
  463         if ((sn_len + 1 + path_len + 1) > sizeof(ndm->ndm_host)) {
  464                 printf("nfs_boot: getfile name too long\n");
  465                 error = EIO;
  466                 goto out;
  467         }
  468         ndm->ndm_host[sn_len] = ':';
  469         memcpy(ndm->ndm_host + sn_len + 1, pathname, path_len + 1);
  470 
  471         /* success */
  472         goto out;
  473 
  474 bad:
  475         printf("nfs_boot: bootparam_getfile: bad reply\n");
  476         error = EBADRPC;
  477 
  478 out:
  479         m_freem(m);
  480         return(0);
  481 }

Cache object: 835314a3ea00d193b9b4099ef8cc9238


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