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/nfsclient/nfs_diskless.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) 1990 The Regents of the University of California.
    3  * All rights reserved.
    4  *
    5  * This code is derived from software contributed to Berkeley by
    6  * William Jolitz.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 4. Neither the name of the University nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  *
   32  *      from: @(#)autoconf.c    7.1 (Berkeley) 5/9/91
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 __FBSDID("$FreeBSD: releng/5.3/sys/nfsclient/nfs_diskless.c 132808 2004-07-28 21:54:57Z phk $");
   37 
   38 #include <sys/param.h>
   39 #include <sys/systm.h>
   40 #include <sys/kernel.h>
   41 #include <sys/malloc.h>
   42 #include <sys/mount.h>
   43 
   44 #include <sys/socket.h>
   45 #include <net/if.h>
   46 #include <net/if_dl.h>
   47 #include <net/if_types.h>
   48 #include <net/if_var.h>
   49 #include <net/ethernet.h>
   50 #include <netinet/in.h>
   51 #include <rpc/rpcclnt.h>
   52 #include <nfs/rpcv2.h>
   53 #include <nfs/nfsproto.h>
   54 #include <nfsclient/nfs.h>
   55 #include <nfsclient/nfsdiskless.h>
   56 
   57 static int inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa);
   58 static int hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa);
   59 static int decode_nfshandle(char *ev, u_char *fh);
   60 
   61 /*
   62  * Populate the essential fields in the nfsv3_diskless structure.
   63  *
   64  * The loader is expected to export the following environment variables:
   65  *
   66  * boot.netif.ip                IP address on boot interface
   67  * boot.netif.netmask           netmask on boot interface
   68  * boot.netif.gateway           default gateway (optional)
   69  * boot.netif.hwaddr            hardware address of boot interface
   70  * boot.nfsroot.server          IP address of root filesystem server
   71  * boot.nfsroot.path            path of the root filesystem on server
   72  * boot.nfsroot.nfshandle       NFS handle for root filesystem on server
   73  */
   74 void
   75 nfs_setup_diskless(void)
   76 {
   77         struct nfs_diskless *nd = &nfs_diskless;
   78         struct ifnet *ifp;
   79         struct ifaddr *ifa;
   80         struct sockaddr_dl *sdl, ourdl;
   81         struct sockaddr_in myaddr, netmask;
   82         char *cp;
   83 
   84         if (nfs_diskless_valid)
   85                 return;
   86         /* set up interface */
   87         if (inaddr_to_sockaddr("boot.netif.ip", &myaddr))
   88                 return;
   89         if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) {
   90                 printf("nfs_diskless: no netmask\n");
   91                 return;
   92         }
   93         bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr));
   94         bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr));
   95         ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
   96                 myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
   97         bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask));
   98 
   99         if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) {
  100                 printf("nfs_diskless: no hardware address\n");
  101                 return;
  102         }
  103         ifa = NULL;
  104         IFNET_RLOCK();
  105         TAILQ_FOREACH(ifp, &ifnet, if_link) {
  106                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  107                         if ((ifa->ifa_addr->sa_family == AF_LINK) &&
  108                             (sdl = ((struct sockaddr_dl *)ifa->ifa_addr))) {
  109                                 if ((sdl->sdl_type == ourdl.sdl_type) &&
  110                                     (sdl->sdl_alen == ourdl.sdl_alen) &&
  111                                     !bcmp(sdl->sdl_data + sdl->sdl_nlen,
  112                                           ourdl.sdl_data + ourdl.sdl_nlen, 
  113                                           sdl->sdl_alen)) {
  114                                     IFNET_RUNLOCK();
  115                                     goto match_done;
  116                                 }
  117                         }
  118                 }
  119         }
  120         IFNET_RUNLOCK();
  121         printf("nfs_diskless: no interface\n");
  122         return; /* no matching interface */
  123 match_done:
  124         strlcpy(nd->myif.ifra_name, ifp->if_xname, sizeof(nd->myif.ifra_name));
  125         
  126         /* set up gateway */
  127         inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway);
  128 
  129         /* set up root mount */
  130         nd->root_args.rsize = 8192;             /* XXX tunable? */
  131         nd->root_args.wsize = 8192;
  132         nd->root_args.sotype = SOCK_DGRAM;
  133         nd->root_args.flags = (NFSMNT_WSIZE | NFSMNT_RSIZE | NFSMNT_RESVPORT);
  134         if (inaddr_to_sockaddr("boot.nfsroot.server", &nd->root_saddr)) {
  135                 printf("nfs_diskless: no server\n");
  136                 return;
  137         }
  138         nd->root_saddr.sin_port = htons(NFS_PORT);
  139         if (decode_nfshandle("boot.nfsroot.nfshandle", &nd->root_fh[0]) == 0) {
  140                 printf("nfs_diskless: no NFS handle\n");
  141                 return;
  142         }
  143         if ((cp = getenv("boot.nfsroot.path")) != NULL) {
  144                 strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
  145                 freeenv(cp);
  146         }
  147 
  148         nfs_diskless_valid = 1;
  149 }
  150 
  151 static int
  152 inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
  153 {
  154         u_int32_t a[4];
  155         char *cp;
  156         int count;
  157 
  158         bzero(sa, sizeof(*sa));
  159         sa->sin_len = sizeof(*sa);
  160         sa->sin_family = AF_INET;
  161 
  162         if ((cp = getenv(ev)) == NULL)
  163                 return (1);
  164         count = sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
  165         freeenv(cp);
  166         if (count != 4)
  167                 return (1);
  168         sa->sin_addr.s_addr =
  169             htonl((a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3]);
  170         return (0);
  171 }
  172 
  173 static int
  174 hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
  175 {
  176         char *cp;
  177         u_int32_t a[6];
  178         int count;
  179 
  180         bzero(sa, sizeof(*sa));
  181         sa->sdl_len = sizeof(*sa);
  182         sa->sdl_family = AF_LINK;
  183         sa->sdl_type = IFT_ETHER;
  184         sa->sdl_alen = ETHER_ADDR_LEN;
  185         if ((cp = getenv(ev)) == NULL)
  186                 return (1);
  187         count = sscanf(cp, "%x:%x:%x:%x:%x:%x",
  188             &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
  189         freeenv(cp);
  190         if (count != 6)
  191                 return (1);
  192         sa->sdl_data[0] = a[0];
  193         sa->sdl_data[1] = a[1];
  194         sa->sdl_data[2] = a[2];
  195         sa->sdl_data[3] = a[3];
  196         sa->sdl_data[4] = a[4];
  197         sa->sdl_data[5] = a[5];
  198         return (0);
  199 }
  200 
  201 static int
  202 decode_nfshandle(char *ev, u_char *fh) 
  203 {
  204         u_char *cp, *ep;
  205         int len, val;
  206 
  207         ep = cp = getenv(ev);
  208         if (cp == NULL)
  209                 return (0);
  210         if ((strlen(cp) < 2) || (*cp != 'X')) {
  211                 freeenv(ep);
  212                 return (0);
  213         }
  214         len = 0;
  215         cp++;
  216         for (;;) {
  217                 if (*cp == 'X') {
  218                         freeenv(ep);
  219                         return (len);
  220                 }
  221                 if ((sscanf(cp, "%2x", &val) != 1) || (val > 0xff)) {
  222                         freeenv(ep);
  223                         return (0);
  224                 }
  225                 *(fh++) = val;
  226                 len++;
  227                 cp += 2;
  228                 if (len > NFSX_V2FH) {
  229                     freeenv(ep);
  230                     return (0);
  231                 }
  232         }
  233 }
  234 
  235 #if !defined(BOOTP_NFSROOT)
  236 static void
  237 nfs_rootconf(void)
  238 {
  239 
  240         nfs_setup_diskless();
  241         if (nfs_diskless_valid)
  242                 rootdevnames[0] = "nfs:";
  243 }
  244 
  245 SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, nfs_rootconf, NULL)
  246 #endif
  247 

Cache object: c0fd105bf7df55f2fa01279cdbf6aabc


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