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