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

Cache object: 9934a2225fc344d60dfffefe7d97629e


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