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

Cache object: b4599adcd9f841b9c65d25a58d324c04


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