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  * 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/6.1/sys/nfsclient/nfs_diskless.c 158179 2006-04-30 16:44:43Z cvs2svn $");
   37 
   38 #include "opt_bootp.h"
   39 
   40 #include <sys/param.h>
   41 #include <sys/systm.h>
   42 #include <sys/kernel.h>
   43 #include <sys/malloc.h>
   44 #include <sys/mount.h>
   45 
   46 #include <sys/socket.h>
   47 #include <net/if.h>
   48 #include <net/if_dl.h>
   49 #include <net/if_types.h>
   50 #include <net/if_var.h>
   51 #include <net/ethernet.h>
   52 #include <netinet/in.h>
   53 #include <rpc/rpcclnt.h>
   54 #include <nfs/rpcv2.h>
   55 #include <nfs/nfsproto.h>
   56 #include <nfsclient/nfs.h>
   57 #include <nfsclient/nfsdiskless.h>
   58 
   59 static int inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa);
   60 static int hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa);
   61 static int decode_nfshandle(char *ev, u_char *fh);
   62 
   63 static void
   64 nfs_parse_options(const char *envopts, struct nfs_diskless *nd)
   65 {
   66         char *opts, *o, *otmp;
   67 
   68         opts = strdup(envopts, M_TEMP);
   69         otmp = opts;
   70         while ((o = strsep(&otmp, ":;, ")) != NULL) {
   71                 if (*o == '\0')
   72                         ; /* Skip empty options. */
   73                 else if (strcmp(o, "soft") == 0)
   74                         nd->root_args.flags |= NFSMNT_SOFT;
   75                 else if (strcmp(o, "intr") == 0)
   76                         nd->root_args.flags |= NFSMNT_INT;
   77                 else if (strcmp(o, "conn") == 0)
   78                         nd->root_args.flags |= NFSMNT_NOCONN;
   79                 else if (strcmp(o, "nolockd") == 0)
   80                         nd->root_args.flags |= NFSMNT_NOLOCKD;
   81                 else
   82                         printf("nfs_diskless: unknown option: %s\n", o);
   83         }
   84         free(opts, M_TEMP);
   85 }
   86 
   87 /*
   88  * Populate the essential fields in the nfsv3_diskless structure.
   89  *
   90  * The loader is expected to export the following environment variables:
   91  *
   92  * boot.netif.name              name of boot interface
   93  * boot.netif.ip                IP address on boot interface
   94  * boot.netif.netmask           netmask on boot interface
   95  * boot.netif.gateway           default gateway (optional)
   96  * boot.netif.hwaddr            hardware address of boot interface
   97  * boot.nfsroot.server          IP address of root filesystem server
   98  * boot.nfsroot.path            path of the root filesystem on server
   99  * boot.nfsroot.nfshandle       NFS handle for root filesystem on server
  100  * boot.nfsroot.options         NFS options for the root filesystem
  101  */
  102 void
  103 nfs_setup_diskless(void)
  104 {
  105         struct nfs_diskless *nd = &nfs_diskless;
  106         struct ifnet *ifp;
  107         struct ifaddr *ifa;
  108         struct sockaddr_dl *sdl, ourdl;
  109         struct sockaddr_in myaddr, netmask;
  110         char *cp;
  111 
  112         if (nfs_diskless_valid)
  113                 return;
  114         /* set up interface */
  115         if (inaddr_to_sockaddr("boot.netif.ip", &myaddr))
  116                 return;
  117         if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) {
  118                 printf("nfs_diskless: no netmask\n");
  119                 return;
  120         }
  121         bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr));
  122         bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr));
  123         ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
  124                 myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
  125         bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask));
  126 
  127         if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) {
  128                 printf("nfs_diskless: no hardware address\n");
  129                 return;
  130         }
  131         ifa = NULL;
  132         IFNET_RLOCK();
  133         TAILQ_FOREACH(ifp, &ifnet, if_link) {
  134                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  135                         if ((ifa->ifa_addr->sa_family == AF_LINK) &&
  136                             (sdl = ((struct sockaddr_dl *)ifa->ifa_addr))) {
  137                                 if ((sdl->sdl_type == ourdl.sdl_type) &&
  138                                     (sdl->sdl_alen == ourdl.sdl_alen) &&
  139                                     !bcmp(sdl->sdl_data + sdl->sdl_nlen,
  140                                           ourdl.sdl_data + ourdl.sdl_nlen, 
  141                                           sdl->sdl_alen)) {
  142                                     IFNET_RUNLOCK();
  143                                     goto match_done;
  144                                 }
  145                         }
  146                 }
  147         }
  148         IFNET_RUNLOCK();
  149         printf("nfs_diskless: no interface\n");
  150         return; /* no matching interface */
  151 match_done:
  152         setenv("boot.netif.name", ifp->if_xname);
  153         strlcpy(nd->myif.ifra_name, ifp->if_xname, sizeof(nd->myif.ifra_name));
  154         
  155         /* set up gateway */
  156         inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway);
  157 
  158         /* set up root mount */
  159         nd->root_args.rsize = 8192;             /* XXX tunable? */
  160         nd->root_args.wsize = 8192;
  161         nd->root_args.sotype = SOCK_DGRAM;
  162         nd->root_args.flags = (NFSMNT_WSIZE | NFSMNT_RSIZE | NFSMNT_RESVPORT);
  163         if (inaddr_to_sockaddr("boot.nfsroot.server", &nd->root_saddr)) {
  164                 printf("nfs_diskless: no server\n");
  165                 return;
  166         }
  167         nd->root_saddr.sin_port = htons(NFS_PORT);
  168         if (decode_nfshandle("boot.nfsroot.nfshandle", &nd->root_fh[0]) == 0) {
  169                 printf("nfs_diskless: no NFS handle\n");
  170                 return;
  171         }
  172         if ((cp = getenv("boot.nfsroot.path")) != NULL) {
  173                 strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
  174                 freeenv(cp);
  175         }
  176         if ((cp = getenv("boot.nfsroot.options")) != NULL) {
  177                 nfs_parse_options(cp, nd);
  178                 freeenv(cp);
  179         }
  180 
  181         nfs_diskless_valid = 1;
  182 }
  183 
  184 static int
  185 inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
  186 {
  187         u_int32_t a[4];
  188         char *cp;
  189         int count;
  190 
  191         bzero(sa, sizeof(*sa));
  192         sa->sin_len = sizeof(*sa);
  193         sa->sin_family = AF_INET;
  194 
  195         if ((cp = getenv(ev)) == NULL)
  196                 return (1);
  197         count = sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
  198         freeenv(cp);
  199         if (count != 4)
  200                 return (1);
  201         sa->sin_addr.s_addr =
  202             htonl((a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3]);
  203         return (0);
  204 }
  205 
  206 static int
  207 hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
  208 {
  209         char *cp;
  210         u_int32_t a[6];
  211         int count;
  212 
  213         bzero(sa, sizeof(*sa));
  214         sa->sdl_len = sizeof(*sa);
  215         sa->sdl_family = AF_LINK;
  216         sa->sdl_type = IFT_ETHER;
  217         sa->sdl_alen = ETHER_ADDR_LEN;
  218         if ((cp = getenv(ev)) == NULL)
  219                 return (1);
  220         count = sscanf(cp, "%x:%x:%x:%x:%x:%x",
  221             &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]);
  222         freeenv(cp);
  223         if (count != 6)
  224                 return (1);
  225         sa->sdl_data[0] = a[0];
  226         sa->sdl_data[1] = a[1];
  227         sa->sdl_data[2] = a[2];
  228         sa->sdl_data[3] = a[3];
  229         sa->sdl_data[4] = a[4];
  230         sa->sdl_data[5] = a[5];
  231         return (0);
  232 }
  233 
  234 static int
  235 decode_nfshandle(char *ev, u_char *fh) 
  236 {
  237         u_char *cp, *ep;
  238         int len, val;
  239 
  240         ep = cp = getenv(ev);
  241         if (cp == NULL)
  242                 return (0);
  243         if ((strlen(cp) < 2) || (*cp != 'X')) {
  244                 freeenv(ep);
  245                 return (0);
  246         }
  247         len = 0;
  248         cp++;
  249         for (;;) {
  250                 if (*cp == 'X') {
  251                         freeenv(ep);
  252                         return (len);
  253                 }
  254                 if ((sscanf(cp, "%2x", &val) != 1) || (val > 0xff)) {
  255                         freeenv(ep);
  256                         return (0);
  257                 }
  258                 *(fh++) = val;
  259                 len++;
  260                 cp += 2;
  261                 if (len > NFSX_V2FH) {
  262                     freeenv(ep);
  263                     return (0);
  264                 }
  265         }
  266 }
  267 
  268 #if !defined(BOOTP_NFSROOT)
  269 static void
  270 nfs_rootconf(void)
  271 {
  272 
  273         nfs_setup_diskless();
  274         if (nfs_diskless_valid)
  275                 rootdevnames[0] = "nfs:";
  276 }
  277 
  278 SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, nfs_rootconf, NULL)
  279 #endif
  280 

Cache object: 4af4cc72ab5273a7c82bd4fa771785b1


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