Home | History | Annotate | Line # | Download | only in nfs
nfs_bootstatic.c revision 1.5
      1 /*	$NetBSD: nfs_bootstatic.c,v 1.5 2007/07/08 21:08:09 bouyer Exp $	*/
      2 
      3 /*
      4  *
      5  * Copyright (c) 2004 Christian Limpach.
      6  * All rights reserved.
      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 Christian Limpach.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: nfs_bootstatic.c,v 1.5 2007/07/08 21:08:09 bouyer Exp $");
     37 
     38 #include "opt_nfs_boot.h"
     39 #include "opt_inet.h"
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/ioctl.h>
     45 #include <sys/proc.h>
     46 #include <sys/mount.h>
     47 #include <sys/mbuf.h>
     48 #include <sys/socket.h>
     49 #include <sys/socketvar.h>
     50 
     51 #include <net/if.h>
     52 #include <net/if_types.h>
     53 #include <net/if_media.h>
     54 #include <net/route.h>
     55 #include <net/if_ether.h>
     56 
     57 #include <netinet/in.h>
     58 #include <netinet/if_inarp.h>
     59 
     60 #include <nfs/rpcv2.h>
     61 
     62 #include <nfs/nfsproto.h>
     63 #include <nfs/nfs.h>
     64 #include <nfs/nfsmount.h>
     65 #include <nfs/nfsdiskless.h>
     66 
     67 int (*nfs_bootstatic_callback)(struct nfs_diskless *) = NULL;
     68 
     69 int
     70 nfs_bootstatic(struct nfs_diskless *nd, struct lwp *lwp)
     71 {
     72 	struct ifnet *ifp = nd->nd_ifp;
     73 	struct sockaddr_in *sin;
     74 	int error, flags;
     75 
     76 	if (nfs_bootstatic_callback)
     77 		flags = (*nfs_bootstatic_callback)(nd);
     78 	else
     79 		flags = 0;
     80 
     81 	if (flags & NFS_BOOTSTATIC_NOSTATIC)
     82 		return EOPNOTSUPP;
     83 
     84 	if (flags == 0) {
     85 #ifdef NFS_BOOTSTATIC_MYIP
     86 		nd->nd_myip.s_addr = inet_addr(NFS_BOOTSTATIC_MYIP);
     87 		flags |= NFS_BOOTSTATIC_HAS_MYIP;
     88 #endif
     89 #ifdef NFS_BOOTSTATIC_GWIP
     90 		nd->nd_gwip.s_addr = inet_addr(NFS_BOOTSTATIC_GWIP);
     91 		flags |= NFS_BOOTSTATIC_HAS_GWIP;
     92 #endif
     93 #ifdef NFS_BOOTSTATIC_MASK
     94 		nd->nd_mask.s_addr = inet_addr(NFS_BOOTSTATIC_MASK);
     95 		flags |= NFS_BOOTSTATIC_HAS_MASK;
     96 #endif
     97 #ifdef NFS_BOOTSTATIC_SERVADDR
     98 		sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
     99 		memset((void *)sin, 0, sizeof(*sin));
    100 		sin->sin_len = sizeof(*sin);
    101 		sin->sin_family = AF_INET;
    102 		sin->sin_addr.s_addr = inet_addr(NFS_BOOTSTATIC_SERVADDR);
    103 		flags |= NFS_BOOTSTATIC_HAS_SERVADDR;
    104 #endif
    105 #ifdef NFS_BOOTSTATIC_SERVER
    106 		strncpy(nd->nd_root.ndm_host, NFS_BOOTSTATIC_SERVER, MNAMELEN);
    107 		flags |= NFS_BOOTSTATIC_HAS_SERVER;
    108 #endif
    109 	}
    110 
    111 	if (flags & NFS_BOOTSTATIC_HAS_MYIP)
    112 		aprint_normal("nfs_boot: client_addr=%s\n",
    113 		    inet_ntoa(nd->nd_myip));
    114 
    115 	if (flags & NFS_BOOTSTATIC_HAS_GWIP)
    116 		aprint_normal("nfs_boot: gateway=%s\n",
    117 		    inet_ntoa(nd->nd_gwip));
    118 
    119 	if (flags & NFS_BOOTSTATIC_HAS_MASK)
    120 		aprint_normal("nfs_boot: netmask=%s\n",
    121 		    inet_ntoa(nd->nd_mask));
    122 
    123 	if (flags & NFS_BOOTSTATIC_HAS_SERVADDR) {
    124 		sin = (struct sockaddr_in *) &nd->nd_root.ndm_saddr;
    125 		aprint_normal("nfs_boot: server=%s\n",
    126 		    inet_ntoa(sin->sin_addr));
    127 	}
    128 
    129 	if (flags & NFS_BOOTSTATIC_HAS_SERVER)
    130 		aprint_normal("nfs_boot: root=%.*s\n", MNAMELEN,
    131 		    nd->nd_root.ndm_host);
    132 
    133 	/*
    134 	 * Do enough of ifconfig(8) so that the chosen interface
    135 	 * can talk to the servers.
    136 	 */
    137 	error = nfs_boot_setaddress(ifp, lwp,
    138 	    flags & NFS_BOOTSTATIC_HAS_MYIP ? nd->nd_myip.s_addr : INADDR_ANY,
    139 	    flags & NFS_BOOTSTATIC_HAS_MASK ? nd->nd_mask.s_addr : INADDR_ANY,
    140 	    INADDR_ANY);
    141 	if (error) {
    142 		aprint_error("nfs_boot: set ifaddr, error=%d\n", error);
    143 		goto out;
    144 	}
    145 
    146 	error = 0;
    147 
    148  out:
    149 
    150 	return error;
    151 }
    152