Home | History | Annotate | Line # | Download | only in nfs
nfs_boot.c revision 1.52.2.3
      1  1.52.2.3    bouyer /*	$NetBSD: nfs_boot.c,v 1.52.2.3 2001/02/11 19:17:34 bouyer Exp $	*/
      2       1.5       cgd 
      3      1.35       gwr /*-
      4      1.35       gwr  * Copyright (c) 1995, 1997 The NetBSD Foundation, Inc.
      5       1.1     glass  * All rights reserved.
      6       1.1     glass  *
      7      1.35       gwr  * This code is derived from software contributed to The NetBSD Foundation
      8      1.35       gwr  * by Adam Glass and Gordon W. Ross.
      9      1.35       gwr  *
     10       1.1     glass  * Redistribution and use in source and binary forms, with or without
     11       1.1     glass  * modification, are permitted provided that the following conditions
     12       1.1     glass  * are met:
     13       1.1     glass  * 1. Redistributions of source code must retain the above copyright
     14       1.1     glass  *    notice, this list of conditions and the following disclaimer.
     15       1.1     glass  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1     glass  *    notice, this list of conditions and the following disclaimer in the
     17       1.1     glass  *    documentation and/or other materials provided with the distribution.
     18      1.35       gwr  * 3. All advertising materials mentioning features or use of this software
     19      1.35       gwr  *    must display the following acknowledgement:
     20      1.49  christos  *	This product includes software developed by the NetBSD
     21      1.49  christos  *	Foundation, Inc. and its contributors.
     22      1.35       gwr  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.35       gwr  *    contributors may be used to endorse or promote products derived
     24      1.35       gwr  *    from this software without specific prior written permission.
     25       1.1     glass  *
     26      1.35       gwr  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.35       gwr  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.35       gwr  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.35       gwr  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.35       gwr  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.35       gwr  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.35       gwr  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.35       gwr  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.35       gwr  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.35       gwr  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.35       gwr  * POSSIBILITY OF SUCH DAMAGE.
     37      1.35       gwr  */
     38      1.35       gwr 
     39      1.35       gwr /*
     40      1.35       gwr  * Support for NFS diskless booting, specifically getting information
     41      1.35       gwr  * about where to mount root from, what pathnames, etc.
     42       1.1     glass  */
     43       1.1     glass 
     44  1.52.2.1    bouyer #include "opt_nfs.h"
     45      1.42    scottr #include "opt_nfs_boot.h"
     46      1.42    scottr 
     47       1.2       cgd #include <sys/param.h>
     48       1.1     glass #include <sys/systm.h>
     49       1.1     glass #include <sys/kernel.h>
     50      1.30   thorpej #include <sys/device.h>
     51       1.1     glass #include <sys/ioctl.h>
     52       1.1     glass #include <sys/proc.h>
     53       1.1     glass #include <sys/mount.h>
     54       1.1     glass #include <sys/mbuf.h>
     55      1.16       gwr #include <sys/reboot.h>
     56       1.1     glass #include <sys/socket.h>
     57      1.16       gwr #include <sys/socketvar.h>
     58       1.1     glass 
     59       1.1     glass #include <net/if.h>
     60       1.3       gwr #include <net/route.h>
     61      1.31        is #include <net/if_ether.h>
     62      1.35       gwr #include <net/if_types.h>
     63      1.31        is 
     64       1.1     glass #include <netinet/in.h>
     65      1.31        is #include <netinet/if_inarp.h>
     66       1.1     glass 
     67       1.1     glass #include <nfs/rpcv2.h>
     68      1.37       gwr #include <nfs/krpc.h>
     69      1.37       gwr #include <nfs/xdr_subs.h>
     70      1.37       gwr 
     71      1.25      fvdl #include <nfs/nfsproto.h>
     72      1.44      fvdl #include <nfs/nfs.h>
     73      1.44      fvdl #include <nfs/nfsmount.h>
     74       1.1     glass #include <nfs/nfsdiskless.h>
     75       1.7    paulus 
     76       1.1     glass /*
     77      1.35       gwr  * There are two implementations of NFS diskless boot.
     78      1.35       gwr  * One implementation uses BOOTP (RFC951, RFC1048),
     79      1.35       gwr  * the other uses Sun RPC/bootparams.  See the files:
     80      1.35       gwr  *    nfs_bootp.c:   BOOTP (RFC951, RFC1048)
     81      1.35       gwr  *    nfs_bootsun.c: Sun RPC/bootparams
     82       1.1     glass  */
     83      1.41    scottr #if defined(NFS_BOOT_BOOTP) || defined(NFS_BOOT_DHCP)
     84      1.40  drochner int nfs_boot_rfc951 = 1; /* BOOTP enabled (default) */
     85      1.40  drochner #endif
     86      1.40  drochner #ifdef NFS_BOOT_BOOTPARAM
     87      1.40  drochner int nfs_boot_bootparam = 1; /* BOOTPARAM enabled (default) */
     88      1.40  drochner #endif
     89       1.3       gwr 
     90       1.3       gwr /* mountd RPC */
     91       1.6   deraadt static int md_mount __P((struct sockaddr_in *mdsin, char *path,
     92      1.29      fvdl 	struct nfs_args *argp));
     93       1.3       gwr 
     94      1.35       gwr static void nfs_boot_defrt __P((struct in_addr *));
     95      1.37       gwr static  int nfs_boot_getfh __P((struct nfs_dlmount *ndm));
     96      1.35       gwr 
     97      1.35       gwr 
     98       1.1     glass /*
     99       1.3       gwr  * Called with an empty nfs_diskless struct to be filled in.
    100      1.35       gwr  * Find an interface, determine its ip address (etc.) and
    101      1.35       gwr  * save all the boot parameters in the nfs_diskless struct.
    102       1.1     glass  */
    103       1.6   deraadt int
    104       1.6   deraadt nfs_boot_init(nd, procp)
    105       1.3       gwr 	struct nfs_diskless *nd;
    106       1.3       gwr 	struct proc *procp;
    107       1.1     glass {
    108       1.3       gwr 	struct ifnet *ifp;
    109      1.16       gwr 	int error;
    110       1.3       gwr 
    111       1.3       gwr 	/*
    112      1.35       gwr 	 * Find the network interface.
    113       1.3       gwr 	 */
    114      1.30   thorpej 	ifp = ifunit(root_device->dv_xname);
    115      1.30   thorpej 	if (ifp == NULL) {
    116      1.35       gwr 		printf("nfs_boot: '%s' not found\n",
    117      1.35       gwr 		       root_device->dv_xname);
    118      1.30   thorpej 		return (ENXIO);
    119      1.30   thorpej 	}
    120      1.50  drochner 	nd->nd_ifp = ifp;
    121       1.3       gwr 
    122      1.40  drochner 	error = EADDRNOTAVAIL; /* ??? */
    123      1.41    scottr #if defined(NFS_BOOT_BOOTP) || defined(NFS_BOOT_DHCP)
    124      1.47  drochner 	if (error && nfs_boot_rfc951) {
    125      1.43       cgd #if defined(NFS_BOOT_DHCP)
    126      1.43       cgd 		printf("nfs_boot: trying DHCP/BOOTP\n");
    127      1.43       cgd #else
    128      1.43       cgd 		printf("nfs_boot: trying BOOTP\n");
    129      1.43       cgd #endif
    130      1.50  drochner 		error = nfs_bootdhcp(nd, procp);
    131      1.40  drochner 	}
    132      1.40  drochner #endif
    133      1.40  drochner #ifdef NFS_BOOT_BOOTPARAM
    134      1.47  drochner 	if (error && nfs_boot_bootparam) {
    135      1.35       gwr 		printf("nfs_boot: trying RARP (and RPC/bootparam)\n");
    136      1.50  drochner 		error = nfs_bootparam(nd, procp);
    137      1.30   thorpej 	}
    138      1.40  drochner #endif
    139      1.46        tv 	if (error)
    140      1.46        tv 		return (error);
    141       1.3       gwr 
    142       1.3       gwr 	/*
    143       1.3       gwr 	 * If the gateway address is set, add a default route.
    144       1.3       gwr 	 * (The mountd RPCs may go across a gateway.)
    145       1.1     glass 	 */
    146      1.35       gwr 	if (nd->nd_gwip.s_addr)
    147      1.35       gwr 		nfs_boot_defrt(&nd->nd_gwip);
    148       1.3       gwr 
    149      1.37       gwr 	/*
    150      1.37       gwr 	 * Now fetch the NFS file handles as appropriate.
    151      1.37       gwr 	 */
    152      1.37       gwr 	error = nfs_boot_getfh(&nd->nd_root);
    153      1.37       gwr 
    154      1.50  drochner 	if (error)
    155      1.50  drochner 		nfs_boot_cleanup(nd, procp);
    156      1.50  drochner 
    157      1.37       gwr 	return (error);
    158      1.39  drochner }
    159      1.39  drochner 
    160      1.50  drochner void
    161      1.50  drochner nfs_boot_cleanup(nd, procp)
    162      1.50  drochner 	struct nfs_diskless *nd;
    163      1.50  drochner 	struct proc *procp;
    164      1.50  drochner {
    165      1.50  drochner 
    166      1.50  drochner 	nfs_boot_deladdress(nd->nd_ifp, procp, nd->nd_myip.s_addr);
    167      1.50  drochner 	nfs_boot_ifupdown(nd->nd_ifp, procp, 0);
    168      1.50  drochner 	nfs_boot_flushrt(nd->nd_ifp);
    169      1.50  drochner }
    170      1.50  drochner 
    171      1.50  drochner int
    172      1.50  drochner nfs_boot_ifupdown(ifp, procp, up)
    173      1.50  drochner 	struct ifnet *ifp;
    174      1.50  drochner 	struct proc *procp;
    175      1.50  drochner 	int up;
    176      1.50  drochner {
    177      1.50  drochner 	struct socket *so;
    178      1.50  drochner 	struct ifreq ireq;
    179      1.50  drochner 	int error;
    180      1.50  drochner 
    181      1.50  drochner 	memset(&ireq, 0, sizeof(ireq));
    182      1.50  drochner 	memcpy(ireq.ifr_name, ifp->if_xname, IFNAMSIZ);
    183      1.50  drochner 
    184      1.50  drochner 	/*
    185      1.50  drochner 	 * Get a socket to use for various things in here.
    186      1.50  drochner 	 * After this, use "goto out" to cleanup and return.
    187      1.50  drochner 	 */
    188      1.50  drochner 	error = socreate(AF_INET, &so, SOCK_DGRAM, 0);
    189      1.50  drochner 	if (error) {
    190      1.50  drochner 		printf("ifupdown: socreate, error=%d\n", error);
    191      1.50  drochner 		return (error);
    192      1.50  drochner 	}
    193      1.50  drochner 
    194      1.50  drochner 	/*
    195      1.50  drochner 	 * Bring up the interface. (just set the "up" flag)
    196      1.50  drochner 	 * Get the old interface flags and or IFF_UP into them so
    197      1.50  drochner 	 * things like media selection flags are not clobbered.
    198      1.50  drochner 	 */
    199      1.50  drochner 	error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)&ireq, procp);
    200      1.50  drochner 	if (error) {
    201      1.50  drochner 		printf("ifupdown: GIFFLAGS, error=%d\n", error);
    202      1.50  drochner 		goto out;
    203      1.50  drochner 	}
    204      1.50  drochner 	if (up)
    205      1.50  drochner 		ireq.ifr_flags |= IFF_UP;
    206      1.50  drochner 	else
    207      1.50  drochner 		ireq.ifr_flags &= ~IFF_UP;
    208      1.50  drochner 	error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)&ireq, procp);
    209      1.50  drochner 	if (error) {
    210      1.50  drochner 		printf("ifupdown: SIFFLAGS, error=%d\n", error);
    211      1.50  drochner 		goto out;
    212      1.50  drochner 	}
    213      1.50  drochner 
    214      1.52  drochner 	if (up)
    215  1.52.2.3    bouyer 		/* give the link some time to get up */
    216  1.52.2.3    bouyer 		tsleep(nfs_boot_ifupdown, PZERO, "nfsbif", 3 * hz);
    217      1.50  drochner out:
    218      1.50  drochner 	soclose(so);
    219      1.50  drochner 	return (error);
    220      1.50  drochner }
    221      1.50  drochner 
    222      1.50  drochner int
    223      1.50  drochner nfs_boot_setaddress(ifp, procp, addr, netmask, braddr)
    224      1.50  drochner 	struct ifnet *ifp;
    225      1.50  drochner 	struct proc *procp;
    226      1.50  drochner 	u_int32_t addr, netmask, braddr;
    227      1.50  drochner {
    228      1.50  drochner 	struct socket *so;
    229      1.50  drochner 	struct ifaliasreq iareq;
    230      1.50  drochner 	struct sockaddr_in *sin;
    231      1.50  drochner 	int error;
    232      1.50  drochner 
    233      1.50  drochner 	/*
    234      1.50  drochner 	 * Get a socket to use for various things in here.
    235      1.50  drochner 	 * After this, use "goto out" to cleanup and return.
    236      1.50  drochner 	 */
    237      1.50  drochner 	error = socreate(AF_INET, &so, SOCK_DGRAM, 0);
    238      1.50  drochner 	if (error) {
    239      1.50  drochner 		printf("setaddress: socreate, error=%d\n", error);
    240      1.50  drochner 		return (error);
    241      1.50  drochner 	}
    242      1.50  drochner 
    243      1.50  drochner 	memset(&iareq, 0, sizeof(iareq));
    244      1.50  drochner 	memcpy(iareq.ifra_name, ifp->if_xname, IFNAMSIZ);
    245      1.50  drochner 
    246      1.50  drochner 	/* Set the I/F address */
    247      1.50  drochner 	sin = (struct sockaddr_in *)&iareq.ifra_addr;
    248      1.50  drochner 	sin->sin_len = sizeof(*sin);
    249      1.50  drochner 	sin->sin_family = AF_INET;
    250      1.50  drochner 	sin->sin_addr.s_addr = addr;
    251      1.50  drochner 
    252      1.50  drochner 	/* Set the netmask */
    253      1.50  drochner 	if (netmask != INADDR_ANY) {
    254      1.50  drochner 		sin = (struct sockaddr_in *)&iareq.ifra_mask;
    255      1.50  drochner 		sin->sin_len = sizeof(*sin);
    256      1.50  drochner 		sin->sin_family = AF_INET;
    257      1.50  drochner 		sin->sin_addr.s_addr = netmask;
    258      1.50  drochner 	} /* else leave subnetmask unspecified (len=0) */
    259      1.50  drochner 
    260      1.50  drochner 	/* Set the broadcast addr. */
    261      1.50  drochner 	if (braddr != INADDR_ANY) {
    262      1.50  drochner 		sin = (struct sockaddr_in *)&iareq.ifra_broadaddr;
    263      1.50  drochner 		sin->sin_len = sizeof(*sin);
    264      1.50  drochner 		sin->sin_family = AF_INET;
    265      1.50  drochner 		sin->sin_addr.s_addr = braddr;
    266      1.50  drochner 	} /* else leave broadcast addr unspecified (len=0) */
    267      1.50  drochner 
    268      1.50  drochner 	error = ifioctl(so, SIOCAIFADDR, (caddr_t)&iareq, procp);
    269      1.50  drochner 	if (error) {
    270      1.50  drochner 		printf("setaddress, error=%d\n", error);
    271      1.50  drochner 		goto out;
    272      1.50  drochner 	}
    273      1.50  drochner 
    274  1.52.2.3    bouyer 	/* give the link some time to get up */
    275  1.52.2.3    bouyer 	tsleep(nfs_boot_setaddress, PZERO, "nfsbtd", 3 * hz);
    276      1.50  drochner out:
    277      1.50  drochner 	soclose(so);
    278      1.50  drochner 	return (error);
    279      1.50  drochner }
    280      1.50  drochner 
    281      1.50  drochner int
    282      1.50  drochner nfs_boot_deladdress(ifp, procp, addr)
    283      1.50  drochner 	struct ifnet *ifp;
    284      1.50  drochner 	struct proc *procp;
    285      1.50  drochner 	u_int32_t addr;
    286      1.50  drochner {
    287      1.50  drochner 	struct socket *so;
    288      1.50  drochner 	struct ifreq ireq;
    289      1.50  drochner 	struct sockaddr_in *sin;
    290      1.50  drochner 	int error;
    291      1.50  drochner 
    292      1.50  drochner 	/*
    293      1.50  drochner 	 * Get a socket to use for various things in here.
    294      1.50  drochner 	 * After this, use "goto out" to cleanup and return.
    295      1.50  drochner 	 */
    296      1.50  drochner 	error = socreate(AF_INET, &so, SOCK_DGRAM, 0);
    297      1.50  drochner 	if (error) {
    298      1.50  drochner 		printf("deladdress: socreate, error=%d\n", error);
    299      1.50  drochner 		return (error);
    300      1.50  drochner 	}
    301      1.50  drochner 
    302      1.50  drochner 	memset(&ireq, 0, sizeof(ireq));
    303      1.50  drochner 	memcpy(ireq.ifr_name, ifp->if_xname, IFNAMSIZ);
    304      1.50  drochner 
    305      1.50  drochner 	sin = (struct sockaddr_in *)&ireq.ifr_addr;
    306      1.50  drochner 	sin->sin_len = sizeof(*sin);
    307      1.50  drochner 	sin->sin_family = AF_INET;
    308      1.50  drochner 	sin->sin_addr.s_addr = addr;
    309      1.50  drochner 
    310      1.50  drochner 	error = ifioctl(so, SIOCDIFADDR, (caddr_t)&ireq, procp);
    311      1.50  drochner 	if (error) {
    312      1.50  drochner 		printf("deladdress, error=%d\n", error);
    313      1.50  drochner 		goto out;
    314      1.50  drochner 	}
    315      1.50  drochner 
    316      1.50  drochner out:
    317      1.50  drochner 	soclose(so);
    318      1.50  drochner 	return (error);
    319      1.50  drochner }
    320      1.50  drochner 
    321      1.50  drochner int
    322      1.50  drochner nfs_boot_setrecvtimo(so)
    323      1.50  drochner 	struct socket *so;
    324      1.39  drochner {
    325      1.39  drochner 	struct mbuf *m;
    326      1.39  drochner 	struct timeval *tv;
    327      1.39  drochner 
    328      1.39  drochner 	m = m_get(M_WAIT, MT_SOOPTS);
    329      1.39  drochner 	tv = mtod(m, struct timeval *);
    330      1.39  drochner 	m->m_len = sizeof(*tv);
    331      1.39  drochner 	tv->tv_sec = 1;
    332      1.39  drochner 	tv->tv_usec = 0;
    333      1.50  drochner 	return (sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m));
    334      1.39  drochner }
    335      1.39  drochner 
    336      1.50  drochner int
    337      1.50  drochner nfs_boot_enbroadcast(so)
    338      1.50  drochner 	struct socket *so;
    339      1.39  drochner {
    340      1.39  drochner 	struct mbuf *m;
    341      1.39  drochner 	int32_t *on;
    342      1.39  drochner 
    343      1.39  drochner 	m = m_get(M_WAIT, MT_SOOPTS);
    344      1.39  drochner 	on = mtod(m, int32_t *);
    345      1.39  drochner 	m->m_len = sizeof(*on);
    346      1.39  drochner 	*on = 1;
    347      1.50  drochner 	return (sosetopt(so, SOL_SOCKET, SO_BROADCAST, m));
    348      1.39  drochner }
    349      1.39  drochner 
    350      1.50  drochner int
    351      1.50  drochner nfs_boot_sobind_ipport(so, port)
    352      1.50  drochner 	struct socket *so;
    353      1.50  drochner 	u_int16_t port;
    354      1.39  drochner {
    355      1.39  drochner 	struct mbuf *m;
    356      1.39  drochner 	struct sockaddr_in *sin;
    357      1.39  drochner 	int error;
    358      1.39  drochner 
    359      1.39  drochner 	m = m_getclr(M_WAIT, MT_SONAME);
    360      1.39  drochner 	sin = mtod(m, struct sockaddr_in *);
    361      1.39  drochner 	sin->sin_len = m->m_len = sizeof(*sin);
    362      1.39  drochner 	sin->sin_family = AF_INET;
    363      1.39  drochner 	sin->sin_addr.s_addr = INADDR_ANY;
    364      1.39  drochner 	sin->sin_port = htons(port);
    365  1.52.2.2    bouyer 	error = sobind(so, m, curproc);
    366      1.39  drochner 	m_freem(m);
    367      1.50  drochner 	return (error);
    368      1.39  drochner }
    369      1.39  drochner 
    370      1.39  drochner /*
    371      1.39  drochner  * What is the longest we will wait before re-sending a request?
    372      1.39  drochner  * Note this is also the frequency of "timeout" messages.
    373      1.39  drochner  * The re-send loop counts up linearly to this maximum, so the
    374      1.39  drochner  * first complaint will happen after (1+2+3+4+5)=15 seconds.
    375      1.39  drochner  */
    376      1.39  drochner #define	MAX_RESEND_DELAY 5	/* seconds */
    377      1.39  drochner #define TOTAL_TIMEOUT   30	/* seconds */
    378      1.39  drochner 
    379      1.50  drochner int
    380      1.50  drochner nfs_boot_sendrecv(so, nam, sndproc, snd, rcvproc, rcv, from_p, context)
    381      1.50  drochner 	struct socket *so;
    382      1.50  drochner 	struct mbuf *nam;
    383      1.50  drochner 	int (*sndproc) __P((struct mbuf*, void*, int));
    384      1.50  drochner 	struct mbuf *snd;
    385      1.50  drochner 	int (*rcvproc) __P((struct mbuf*, void*));
    386      1.50  drochner 	struct mbuf **rcv, **from_p;
    387      1.50  drochner 	void *context;
    388      1.39  drochner {
    389      1.39  drochner 	int error, rcvflg, timo, secs, waited;
    390      1.39  drochner 	struct mbuf *m, *from;
    391      1.39  drochner 	struct uio uio;
    392      1.39  drochner 
    393      1.39  drochner 	/* Free at end if not null. */
    394      1.39  drochner 	from = NULL;
    395      1.39  drochner 
    396      1.39  drochner 	/*
    397      1.39  drochner 	 * Send it, repeatedly, until a reply is received,
    398      1.39  drochner 	 * but delay each re-send by an increasing amount.
    399      1.39  drochner 	 * If the delay hits the maximum, start complaining.
    400      1.39  drochner 	 */
    401      1.39  drochner 	waited = timo = 0;
    402      1.39  drochner send_again:
    403      1.39  drochner 	waited += timo;
    404      1.39  drochner 	if (waited >= TOTAL_TIMEOUT)
    405      1.50  drochner 		return (ETIMEDOUT);
    406      1.39  drochner 
    407      1.39  drochner 	/* Determine new timeout. */
    408      1.39  drochner 	if (timo < MAX_RESEND_DELAY)
    409      1.39  drochner 		timo++;
    410      1.39  drochner 	else
    411      1.39  drochner 		printf("nfs_boot: timeout...\n");
    412      1.39  drochner 
    413      1.39  drochner 	if (sndproc) {
    414      1.39  drochner 		error = (*sndproc)(snd, context, waited);
    415      1.39  drochner 		if (error)
    416      1.39  drochner 			goto out;
    417      1.39  drochner 	}
    418      1.39  drochner 
    419      1.39  drochner 	/* Send request (or re-send). */
    420      1.39  drochner 	m = m_copypacket(snd, M_WAIT);
    421      1.39  drochner 	if (m == NULL) {
    422      1.39  drochner 		error = ENOBUFS;
    423      1.39  drochner 		goto out;
    424      1.39  drochner 	}
    425      1.45      matt 	error = (*so->so_send)(so, nam, NULL, m, NULL, 0);
    426      1.39  drochner 	if (error) {
    427      1.39  drochner 		printf("nfs_boot: sosend: %d\n", error);
    428      1.39  drochner 		goto out;
    429      1.39  drochner 	}
    430      1.39  drochner 	m = NULL;
    431      1.39  drochner 
    432      1.39  drochner 	/*
    433      1.39  drochner 	 * Wait for up to timo seconds for a reply.
    434      1.39  drochner 	 * The socket receive timeout was set to 1 second.
    435      1.39  drochner 	 */
    436      1.39  drochner 
    437      1.39  drochner 	secs = timo;
    438      1.39  drochner 	for (;;) {
    439      1.39  drochner 		if (from) {
    440      1.39  drochner 			m_freem(from);
    441      1.39  drochner 			from = NULL;
    442      1.39  drochner 		}
    443      1.39  drochner 		if (m) {
    444      1.39  drochner 			m_freem(m);
    445      1.39  drochner 			m = NULL;
    446      1.39  drochner 		}
    447      1.39  drochner 		uio.uio_resid = 1 << 16; /* ??? */
    448      1.39  drochner 		rcvflg = 0;
    449      1.45      matt 		error = (*so->so_receive)(so, &from, &uio, &m, NULL, &rcvflg);
    450      1.39  drochner 		if (error == EWOULDBLOCK) {
    451      1.39  drochner 			if (--secs <= 0)
    452      1.39  drochner 				goto send_again;
    453      1.39  drochner 			continue;
    454      1.39  drochner 		}
    455      1.39  drochner 		if (error)
    456      1.39  drochner 			goto out;
    457      1.39  drochner #ifdef DIAGNOSTIC
    458      1.39  drochner 		if (!m || !(m->m_flags & M_PKTHDR)
    459      1.39  drochner 		    || (1 << 16) - uio.uio_resid != m->m_pkthdr.len)
    460      1.39  drochner 			panic("nfs_boot_sendrecv: return size");
    461      1.39  drochner #endif
    462      1.39  drochner 
    463      1.39  drochner 		if ((*rcvproc)(m, context))
    464      1.39  drochner 			continue;
    465      1.39  drochner 
    466      1.39  drochner 		if (rcv)
    467      1.39  drochner 			*rcv = m;
    468      1.39  drochner 		else
    469      1.39  drochner 			m_freem(m);
    470      1.39  drochner 		if (from_p) {
    471      1.39  drochner 			*from_p = from;
    472      1.39  drochner 			from = NULL;
    473      1.39  drochner 		}
    474      1.39  drochner 		break;
    475      1.39  drochner 	}
    476      1.39  drochner out:
    477      1.39  drochner 	if (from) m_freem(from);
    478      1.50  drochner 	return (error);
    479      1.35       gwr }
    480       1.1     glass 
    481      1.35       gwr /*
    482      1.35       gwr  * Install a default route to the passed IP address.
    483      1.35       gwr  */
    484      1.35       gwr static void
    485      1.35       gwr nfs_boot_defrt(gw_ip)
    486      1.35       gwr 	struct in_addr *gw_ip;
    487      1.35       gwr {
    488      1.35       gwr 	struct sockaddr dst, gw, mask;
    489      1.35       gwr 	struct sockaddr_in *sin;
    490      1.35       gwr 	int error;
    491      1.33       gwr 
    492      1.35       gwr 	/* Destination: (default) */
    493      1.48     perry 	memset((caddr_t)&dst, 0, sizeof(dst));
    494      1.35       gwr 	dst.sa_len = sizeof(dst);
    495      1.35       gwr 	dst.sa_family = AF_INET;
    496      1.35       gwr 	/* Gateway: */
    497      1.48     perry 	memset((caddr_t)&gw, 0, sizeof(gw));
    498      1.35       gwr 	sin = (struct sockaddr_in *)&gw;
    499      1.35       gwr 	sin->sin_len = sizeof(*sin);
    500      1.35       gwr 	sin->sin_family = AF_INET;
    501      1.35       gwr 	sin->sin_addr.s_addr = gw_ip->s_addr;
    502      1.35       gwr 	/* Mask: (zero length) */
    503      1.35       gwr 	/* XXX - Just pass a null pointer? */
    504      1.48     perry 	memset(&mask, 0, sizeof(mask));
    505      1.35       gwr 
    506      1.35       gwr 	/* add, dest, gw, mask, flags, 0 */
    507      1.35       gwr 	error = rtrequest(RTM_ADD, &dst, &gw, &mask,
    508      1.50  drochner 			  (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
    509      1.33       gwr 	if (error) {
    510      1.35       gwr 		printf("nfs_boot: add route, error=%d\n", error);
    511      1.33       gwr 		error = 0;
    512      1.33       gwr 	}
    513      1.50  drochner }
    514      1.50  drochner 
    515      1.50  drochner static int nfs_boot_delroute __P((struct radix_node *, void *));
    516      1.50  drochner static int
    517      1.50  drochner nfs_boot_delroute(rn, w)
    518      1.50  drochner 	struct radix_node *rn;
    519      1.50  drochner 	void *w;
    520      1.50  drochner {
    521      1.50  drochner 	struct rtentry *rt = (struct rtentry *)rn;
    522      1.50  drochner 	int error;
    523      1.50  drochner 
    524      1.50  drochner 	if (rt->rt_ifp != (struct ifnet *)w)
    525      1.50  drochner 		return (0);
    526      1.50  drochner 
    527      1.50  drochner 	error = rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL);
    528      1.50  drochner 	if (error)
    529      1.50  drochner 		printf("nfs_boot: del route, error=%d\n", error);
    530      1.50  drochner 
    531      1.50  drochner 	return (0);
    532      1.50  drochner }
    533      1.50  drochner 
    534      1.50  drochner void
    535      1.50  drochner nfs_boot_flushrt(ifp)
    536      1.50  drochner 	struct ifnet *ifp;
    537      1.50  drochner {
    538      1.50  drochner 
    539      1.50  drochner 	rn_walktree(rt_tables[AF_INET], nfs_boot_delroute, ifp);
    540       1.3       gwr }
    541       1.1     glass 
    542      1.35       gwr /*
    543      1.35       gwr  * Get an initial NFS file handle using Sun RPC/mountd.
    544      1.35       gwr  * Separate function because we used to call it twice.
    545      1.35       gwr  * (once for root and once for swap)
    546      1.35       gwr  */
    547      1.37       gwr static int
    548      1.33       gwr nfs_boot_getfh(ndm)
    549      1.33       gwr 	struct nfs_dlmount *ndm;	/* output */
    550       1.3       gwr {
    551      1.29      fvdl 	struct nfs_args *args;
    552      1.23       gwr 	struct sockaddr_in *sin;
    553      1.33       gwr 	char *pathname;
    554       1.3       gwr 	int error;
    555      1.35       gwr 	u_int16_t port;
    556       1.1     glass 
    557      1.33       gwr 	args = &ndm->ndm_args;
    558      1.29      fvdl 
    559      1.29      fvdl 	/* Initialize mount args. */
    560      1.48     perry 	memset((caddr_t) args, 0, sizeof(*args));
    561      1.33       gwr 	args->addr     = &ndm->ndm_saddr;
    562      1.29      fvdl 	args->addrlen  = args->addr->sa_len;
    563      1.29      fvdl #ifdef NFS_BOOT_TCP
    564      1.29      fvdl 	args->sotype   = SOCK_STREAM;
    565      1.29      fvdl #else
    566      1.29      fvdl 	args->sotype   = SOCK_DGRAM;
    567      1.29      fvdl #endif
    568      1.33       gwr 	args->fh       = ndm->ndm_fh;
    569      1.33       gwr 	args->hostname = ndm->ndm_host;
    570  1.52.2.1    bouyer 	args->flags    = NFSMNT_NOCONN | NFSMNT_RESVPORT;
    571      1.29      fvdl 
    572  1.52.2.1    bouyer #ifndef NFS_V2_ONLY
    573  1.52.2.1    bouyer 	args->flags    |= NFSMNT_NFSV3;
    574  1.52.2.1    bouyer #endif
    575      1.29      fvdl #ifdef	NFS_BOOT_OPTIONS
    576      1.29      fvdl 	args->flags    |= NFS_BOOT_OPTIONS;
    577      1.29      fvdl #endif
    578      1.29      fvdl #ifdef	NFS_BOOT_RWSIZE
    579      1.29      fvdl 	/*
    580      1.29      fvdl 	 * Reduce rsize,wsize for interfaces that consistently
    581      1.29      fvdl 	 * drop fragments of long UDP messages.  (i.e. wd8003).
    582      1.29      fvdl 	 * You can always change these later via remount.
    583      1.29      fvdl 	 */
    584      1.29      fvdl 	args->flags   |= NFSMNT_WSIZE | NFSMNT_RSIZE;
    585      1.29      fvdl 	args->wsize    = NFS_BOOT_RWSIZE;
    586      1.29      fvdl 	args->rsize    = NFS_BOOT_RWSIZE;
    587      1.29      fvdl #endif
    588      1.29      fvdl 
    589       1.3       gwr 	/*
    590      1.33       gwr 	 * Find the pathname part of the "server:pathname"
    591      1.33       gwr 	 * string left in ndm->ndm_host by nfs_boot_init.
    592       1.3       gwr 	 */
    593      1.33       gwr 	pathname = strchr(ndm->ndm_host, ':');
    594      1.33       gwr 	if (pathname == 0) {
    595      1.33       gwr 		printf("nfs_boot: getfh - no pathname\n");
    596      1.33       gwr 		return (EIO);
    597      1.33       gwr 	}
    598      1.33       gwr 	pathname++;
    599       1.1     glass 
    600       1.3       gwr 	/*
    601      1.33       gwr 	 * Get file handle using RPC to mountd/mount
    602       1.3       gwr 	 */
    603      1.33       gwr 	sin = (struct sockaddr_in *)&ndm->ndm_saddr;
    604      1.29      fvdl 	error = md_mount(sin, pathname, args);
    605      1.33       gwr 	if (error) {
    606      1.33       gwr 		printf("nfs_boot: mountd `%s', error=%d\n",
    607      1.35       gwr 		       ndm->ndm_host, error);
    608      1.33       gwr 		return (error);
    609      1.33       gwr 	}
    610       1.4        pk 
    611      1.23       gwr 	/* Set port number for NFS use. */
    612      1.23       gwr 	/* XXX: NFS port is always 2049, right? */
    613      1.29      fvdl #ifdef NFS_BOOT_TCP
    614      1.29      fvdl retry:
    615      1.29      fvdl #endif
    616      1.29      fvdl 	error = krpc_portmap(sin, NFS_PROG,
    617      1.29      fvdl 		    (args->flags & NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
    618      1.29      fvdl 		    (args->sotype == SOCK_STREAM) ? IPPROTO_TCP : IPPROTO_UDP,
    619      1.35       gwr 		    &port);
    620      1.35       gwr 	if (port == htons(0))
    621      1.33       gwr 		error = EIO;
    622      1.35       gwr 	if (error) {
    623      1.29      fvdl #ifdef NFS_BOOT_TCP
    624      1.29      fvdl 		if (args->sotype == SOCK_STREAM) {
    625      1.29      fvdl 			args->sotype = SOCK_DGRAM;
    626      1.29      fvdl 			goto retry;
    627      1.29      fvdl 		}
    628      1.29      fvdl #endif
    629      1.33       gwr 		printf("nfs_boot: portmap NFS, error=%d\n", error);
    630      1.35       gwr 		return (error);
    631      1.29      fvdl 	}
    632      1.35       gwr 	sin->sin_port = port;
    633      1.35       gwr 	return (0);
    634       1.3       gwr }
    635       1.1     glass 
    636       1.1     glass 
    637       1.1     glass /*
    638       1.3       gwr  * RPC: mountd/mount
    639       1.3       gwr  * Given a server pathname, get an NFS file handle.
    640       1.3       gwr  * Also, sets sin->sin_port to the NFS service port.
    641       1.1     glass  */
    642       1.3       gwr static int
    643      1.29      fvdl md_mount(mdsin, path, argp)
    644       1.3       gwr 	struct sockaddr_in *mdsin;		/* mountd server address */
    645       1.3       gwr 	char *path;
    646      1.29      fvdl 	struct nfs_args *argp;
    647       1.1     glass {
    648       1.3       gwr 	/* The RPC structures */
    649       1.3       gwr 	struct rdata {
    650      1.20       cgd 		u_int32_t errno;
    651      1.29      fvdl 		union {
    652      1.29      fvdl 			u_int8_t  v2fh[NFSX_V2FH];
    653      1.29      fvdl 			struct {
    654      1.29      fvdl 				u_int32_t fhlen;
    655      1.29      fvdl 				u_int8_t  fh[1];
    656      1.29      fvdl 			} v3fh;
    657      1.29      fvdl 		} fh;
    658       1.3       gwr 	} *rdata;
    659       1.3       gwr 	struct mbuf *m;
    660      1.29      fvdl 	u_int8_t *fh;
    661      1.29      fvdl 	int minlen, error;
    662      1.35       gwr 	int mntver;
    663       1.3       gwr 
    664      1.35       gwr 	mntver = (argp->flags & NFSMNT_NFSV3) ? 3 : 2;
    665      1.35       gwr 	do {
    666      1.35       gwr 		/*
    667      1.35       gwr 		 * Get port number for MOUNTD.
    668      1.35       gwr 		 */
    669      1.35       gwr 		error = krpc_portmap(mdsin, RPCPROG_MNT, mntver,
    670      1.35       gwr 		                    IPPROTO_UDP, &mdsin->sin_port);
    671      1.35       gwr 		if (error)
    672      1.35       gwr 			continue;
    673      1.35       gwr 
    674      1.35       gwr 		/* This mbuf is consumed by krpc_call. */
    675      1.35       gwr 		m = xdr_string_encode(path, strlen(path));
    676      1.35       gwr 		if (m == NULL)
    677      1.35       gwr 			return ENOMEM;
    678      1.11       gwr 
    679      1.35       gwr 		/* Do RPC to mountd. */
    680      1.35       gwr 		error = krpc_call(mdsin, RPCPROG_MNT, mntver,
    681      1.35       gwr 		                  RPCMNT_MOUNT, &m, NULL);
    682      1.35       gwr 		if (error != EPROGMISMATCH)
    683      1.35       gwr 			break;
    684      1.35       gwr 		/* Try lower version of mountd. */
    685      1.35       gwr 	} while (--mntver >= 1);
    686      1.29      fvdl 	if (error) {
    687      1.35       gwr 		printf("nfs_boot: mountd error=%d\n", error);
    688      1.35       gwr 		return error;
    689      1.29      fvdl 	}
    690      1.35       gwr 	if (mntver != 3)
    691      1.35       gwr 		argp->flags &= ~NFSMNT_NFSV3;
    692       1.1     glass 
    693      1.23       gwr 	/* The reply might have only the errno. */
    694      1.23       gwr 	if (m->m_len < 4)
    695      1.23       gwr 		goto bad;
    696      1.23       gwr 	/* Have at least errno, so check that. */
    697      1.23       gwr 	rdata = mtod(m, struct rdata *);
    698      1.23       gwr 	error = fxdr_unsigned(u_int32_t, rdata->errno);
    699      1.23       gwr 	if (error)
    700      1.23       gwr 		goto out;
    701      1.23       gwr 
    702      1.35       gwr 	/* Have errno==0, so the fh must be there. */
    703      1.35       gwr 	if (mntver == 3) {
    704      1.29      fvdl 		argp->fhsize   = fxdr_unsigned(u_int32_t, rdata->fh.v3fh.fhlen);
    705      1.29      fvdl 		if (argp->fhsize > NFSX_V3FHMAX)
    706      1.29      fvdl 			goto bad;
    707      1.29      fvdl 		minlen = 2 * sizeof(u_int32_t) + argp->fhsize;
    708      1.29      fvdl 	} else {
    709      1.29      fvdl 		argp->fhsize   = NFSX_V2FH;
    710      1.29      fvdl 		minlen = sizeof(u_int32_t) + argp->fhsize;
    711      1.29      fvdl 	}
    712      1.29      fvdl 
    713      1.29      fvdl 	if (m->m_len < minlen) {
    714      1.29      fvdl 		m = m_pullup(m, minlen);
    715      1.16       gwr 		if (m == NULL)
    716      1.29      fvdl 			return(EBADRPC);
    717      1.23       gwr 		rdata = mtod(m, struct rdata *);
    718      1.16       gwr 	}
    719      1.29      fvdl 
    720      1.35       gwr 	fh = (mntver == 3) ?
    721      1.35       gwr 		rdata->fh.v3fh.fh : rdata->fh.v2fh;
    722      1.48     perry 	memcpy(argp->fh, fh, argp->fhsize);
    723      1.29      fvdl 
    724       1.3       gwr 	goto out;
    725       1.1     glass 
    726       1.6   deraadt bad:
    727       1.3       gwr 	error = EBADRPC;
    728       1.1     glass 
    729       1.6   deraadt out:
    730       1.3       gwr 	m_freem(m);
    731       1.3       gwr 	return error;
    732       1.7    paulus }
    733