Home | History | Annotate | Line # | Download | only in nfs
nfs_bootdhcp.c revision 1.30
      1  1.30  christos /*	$NetBSD: nfs_bootdhcp.c,v 1.30 2006/03/16 15:55:20 christos Exp $	*/
      2   1.1       gwr 
      3   1.1       gwr /*-
      4   1.1       gwr  * Copyright (c) 1995, 1997 The NetBSD Foundation, Inc.
      5   1.1       gwr  * All rights reserved.
      6   1.1       gwr  *
      7   1.1       gwr  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1       gwr  * by Adam Glass and Gordon W. Ross.
      9   1.1       gwr  *
     10   1.1       gwr  * Redistribution and use in source and binary forms, with or without
     11   1.1       gwr  * modification, are permitted provided that the following conditions
     12   1.1       gwr  * are met:
     13   1.1       gwr  * 1. Redistributions of source code must retain the above copyright
     14   1.1       gwr  *    notice, this list of conditions and the following disclaimer.
     15   1.1       gwr  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       gwr  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       gwr  *    documentation and/or other materials provided with the distribution.
     18   1.1       gwr  * 3. All advertising materials mentioning features or use of this software
     19   1.1       gwr  *    must display the following acknowledgement:
     20   1.9  christos  *	This product includes software developed by the NetBSD
     21   1.9  christos  *	Foundation, Inc. and its contributors.
     22   1.1       gwr  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1       gwr  *    contributors may be used to endorse or promote products derived
     24   1.1       gwr  *    from this software without specific prior written permission.
     25   1.1       gwr  *
     26   1.1       gwr  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1       gwr  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1       gwr  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1       gwr  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1       gwr  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1       gwr  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1       gwr  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1       gwr  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1       gwr  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1       gwr  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1       gwr  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1       gwr  */
     38   1.1       gwr 
     39   1.1       gwr /*
     40   1.1       gwr  * Support for NFS diskless booting with BOOTP (RFC951, RFC1048)
     41   1.1       gwr  *
     42   1.1       gwr  * History:
     43   1.1       gwr  *
     44   1.1       gwr  * Tor Egge developed the initial version of this code based on
     45   1.1       gwr  * the Sun RPC/bootparam sources nfs_boot.c and krpc_subr.c and
     46   1.1       gwr  * submitted that work to NetBSD as bugreport "kern/2351" on
     47   1.1       gwr  * 29 Apr 1996.
     48   1.1       gwr  *
     49   1.1       gwr  * Gordon Ross reorganized Tor's version into this form and
     50   1.1       gwr  * integrated it into the NetBSD sources during Aug 1997.
     51   1.1       gwr  */
     52  1.18     lukem 
     53  1.18     lukem #include <sys/cdefs.h>
     54  1.30  christos __KERNEL_RCSID(0, "$NetBSD: nfs_bootdhcp.c,v 1.30 2006/03/16 15:55:20 christos Exp $");
     55   1.1       gwr 
     56   1.5    scottr #include "opt_nfs_boot.h"
     57   1.5    scottr 
     58   1.1       gwr #include <sys/param.h>
     59   1.1       gwr #include <sys/systm.h>
     60   1.1       gwr #include <sys/kernel.h>
     61   1.1       gwr #include <sys/device.h>
     62   1.1       gwr #include <sys/ioctl.h>
     63   1.1       gwr #include <sys/proc.h>
     64   1.1       gwr #include <sys/mount.h>
     65   1.1       gwr #include <sys/mbuf.h>
     66   1.1       gwr #include <sys/reboot.h>
     67   1.1       gwr #include <sys/socket.h>
     68   1.1       gwr #include <sys/socketvar.h>
     69   1.1       gwr 
     70   1.1       gwr #include <net/if.h>
     71   1.3  drochner #include <net/if_types.h>
     72   1.1       gwr #include <net/if_arp.h> 	/* ARPHRD_ETHER, etc. */
     73   1.1       gwr #include <net/if_dl.h>
     74   1.1       gwr #include <net/if_ether.h>
     75   1.1       gwr #include <net/route.h>
     76   1.1       gwr 
     77   1.1       gwr #include <netinet/in.h>
     78   1.1       gwr #include <netinet/if_inarp.h>
     79   1.1       gwr 
     80   1.6      ross #include <nfs/rpcv2.h>
     81   1.6      ross 
     82   1.1       gwr #include <nfs/nfsproto.h>
     83   1.6      ross #include <nfs/nfs.h>
     84   1.6      ross #include <nfs/nfsmount.h>
     85   1.1       gwr #include <nfs/nfsdiskless.h>
     86   1.1       gwr 
     87   1.1       gwr /*
     88   1.1       gwr  * There are two implementations of NFS diskless boot.
     89   1.1       gwr  * This implementation uses BOOTP (RFC951, RFC1048), and
     90   1.1       gwr  * the other uses Sun RPC/bootparams (nfs_bootparam.c).
     91   1.1       gwr  *
     92   1.1       gwr  * This method gets everything it needs with one BOOTP
     93   1.1       gwr  * request and reply.  Note that this actually uses only
     94   1.1       gwr  * the old BOOTP functionality subset of DHCP.  It is not
     95   1.1       gwr  * clear that DHCP provides any advantage over BOOTP for
     96   1.1       gwr  * diskless boot.  DHCP allows the server to assign an IP
     97   1.1       gwr  * address without any a-priori knowledge of the client,
     98   1.1       gwr  * but we require that the server has a-priori knowledge
     99   1.1       gwr  * of the client so it can export our (unique) NFS root.
    100   1.1       gwr  * Given that the server needs a-priori knowledge about
    101   1.1       gwr  * the client anyway, it might as well assign a fixed IP
    102   1.1       gwr  * address for the client and support BOOTP.
    103  1.28     perry  *
    104   1.1       gwr  * On the other hand, disk-FULL clients may use DHCP, but
    105   1.1       gwr  * in that case the DHCP client should be user-mode code,
    106   1.1       gwr  * and has no bearing on the code below. -gwr
    107   1.1       gwr  */
    108   1.1       gwr 
    109   1.1       gwr /* Begin stuff from bootp.h */
    110   1.1       gwr /* Definitions from RFC951 */
    111   1.1       gwr #define BP_CHADDR_LEN	 16
    112   1.1       gwr #define BP_SNAME_LEN	 64
    113   1.1       gwr #define BP_FILE_LEN	128
    114   1.1       gwr #define BP_VEND_LEN	 64
    115   1.1       gwr struct bootp {
    116   1.1       gwr 	u_int8_t	bp_op;		/* packet opcode type */
    117   1.1       gwr 	u_int8_t	bp_htype;	/* hardware addr type */
    118   1.1       gwr 	u_int8_t	bp_hlen;	/* hardware addr length */
    119   1.1       gwr 	u_int8_t	bp_hops;	/* gateway hops */
    120   1.1       gwr 	u_int32_t	bp_xid;		/* transaction ID */
    121   1.1       gwr 	u_int16_t	bp_secs;	/* seconds since boot began */
    122   1.1       gwr 	u_int16_t	bp_flags;	/* RFC1532 broadcast, etc. */
    123   1.1       gwr 	struct in_addr	bp_ciaddr;	/* client IP address */
    124   1.1       gwr 	struct in_addr	bp_yiaddr;	/* 'your' IP address */
    125   1.1       gwr 	struct in_addr	bp_siaddr;	/* server IP address */
    126   1.1       gwr 	struct in_addr	bp_giaddr;	/* gateway IP address */
    127   1.1       gwr 	u_int8_t bp_chaddr[BP_CHADDR_LEN]; /* client hardware address */
    128   1.1       gwr 	char	bp_sname[BP_SNAME_LEN]; /* server host name */
    129   1.1       gwr 	char	bp_file[BP_FILE_LEN];	/* boot file name */
    130   1.1       gwr 	u_int8_t bp_vend[BP_VEND_LEN];	/* RFC1048 options */
    131   1.1       gwr 	/*
    132   1.1       gwr 	 * Note that BOOTP packets are allowed to be longer
    133   1.1       gwr 	 * (see RFC 1532 sect. 2.1) and common practice is to
    134   1.1       gwr 	 * allow the option data in bp_vend to extend into the
    135   1.1       gwr 	 * additional space provided in longer packets.
    136   1.1       gwr 	 */
    137   1.1       gwr };
    138   1.1       gwr 
    139   1.1       gwr #define IPPORT_BOOTPS 67
    140   1.1       gwr #define IPPORT_BOOTPC 68
    141   1.1       gwr 
    142   1.1       gwr #define BOOTREQUEST		1
    143   1.1       gwr #define BOOTREPLY		2
    144   1.1       gwr 
    145   1.1       gwr /*
    146   1.1       gwr  * Is this available from the sockaddr_dl somehow?
    147   1.1       gwr  * Perhaps (struct arphdr)->ar_hrd = ARPHRD_ETHER?
    148   1.1       gwr  * The interface has ->if_type but not the ARP fmt.
    149   1.1       gwr  */
    150  1.15  gmcgarry #define HTYPE_ETHERNET		1
    151  1.15  gmcgarry #define HTYPE_IEEE802		6
    152   1.1       gwr 
    153   1.1       gwr /*
    154   1.1       gwr  * Vendor magic cookie (v_magic) for RFC1048
    155   1.1       gwr  */
    156   1.1       gwr static const u_int8_t vm_rfc1048[4] = { 99, 130, 83, 99 };
    157   1.1       gwr 
    158   1.1       gwr /*
    159   1.1       gwr  * Tag values used to specify what information is being supplied in
    160   1.1       gwr  * the vendor (options) data area of the packet.
    161   1.1       gwr  */
    162   1.1       gwr /* RFC 1048 */
    163   1.1       gwr #define TAG_END			((unsigned char) 255)
    164   1.1       gwr #define TAG_PAD			((unsigned char)   0)
    165   1.1       gwr #define TAG_SUBNET_MASK		((unsigned char)   1)
    166   1.1       gwr #define TAG_TIME_OFFSET		((unsigned char)   2)
    167   1.1       gwr #define TAG_GATEWAY		((unsigned char)   3)
    168   1.1       gwr #define TAG_TIME_SERVER		((unsigned char)   4)
    169   1.1       gwr #define TAG_NAME_SERVER		((unsigned char)   5)
    170   1.1       gwr #define TAG_DOMAIN_SERVER	((unsigned char)   6)
    171   1.1       gwr #define TAG_LOG_SERVER		((unsigned char)   7)
    172   1.1       gwr #define TAG_COOKIE_SERVER	((unsigned char)   8)
    173   1.1       gwr #define TAG_LPR_SERVER		((unsigned char)   9)
    174   1.1       gwr #define TAG_IMPRESS_SERVER	((unsigned char)  10)
    175   1.1       gwr #define TAG_RLP_SERVER		((unsigned char)  11)
    176   1.1       gwr #define TAG_HOST_NAME		((unsigned char)  12)
    177   1.1       gwr #define TAG_BOOT_SIZE		((unsigned char)  13)
    178   1.1       gwr /* RFC 1395 */
    179   1.1       gwr #define TAG_DUMP_FILE		((unsigned char)  14)
    180   1.1       gwr #define TAG_DOMAIN_NAME		((unsigned char)  15)
    181   1.1       gwr #define TAG_SWAP_SERVER		((unsigned char)  16)
    182   1.1       gwr #define TAG_ROOT_PATH		((unsigned char)  17)
    183   1.1       gwr /* End of stuff from bootp.h */
    184   1.1       gwr 
    185   1.2  drochner #ifdef NFS_BOOT_DHCP
    186   1.2  drochner #define TAG_REQ_ADDR		((unsigned char)  50)
    187   1.2  drochner #define TAG_LEASETIME		((unsigned char)  51)
    188   1.2  drochner #define TAG_OVERLOAD		((unsigned char)  52)
    189   1.2  drochner #define TAG_DHCP_MSGTYPE	((unsigned char)  53)
    190   1.2  drochner #define TAG_SERVERID		((unsigned char)  54)
    191   1.2  drochner #define TAG_PARAM_REQ		((unsigned char)  55)
    192   1.2  drochner #define TAG_MSG			((unsigned char)  56)
    193   1.2  drochner #define TAG_MAXSIZE		((unsigned char)  57)
    194   1.2  drochner #define TAG_T1			((unsigned char)  58)
    195   1.2  drochner #define TAG_T2			((unsigned char)  59)
    196   1.2  drochner #define TAG_CLASSID		((unsigned char)  60)
    197   1.2  drochner #define TAG_CLIENTID		((unsigned char)  61)
    198   1.2  drochner #endif
    199   1.2  drochner 
    200   1.2  drochner #ifdef NFS_BOOT_DHCP
    201   1.2  drochner #define DHCPDISCOVER 1
    202   1.2  drochner #define DHCPOFFER 2
    203   1.2  drochner #define DHCPREQUEST 3
    204   1.2  drochner #define DHCPDECLINE 4
    205   1.2  drochner #define DHCPACK 5
    206   1.2  drochner #define DHCPNAK 6
    207   1.2  drochner #define DHCPRELEASE 7
    208   1.2  drochner #endif
    209   1.1       gwr 
    210   1.2  drochner #ifdef NFS_BOOT_DHCP
    211   1.2  drochner #define BOOTP_SIZE_MAX	(sizeof(struct bootp)+312-64)
    212   1.2  drochner #else
    213   1.1       gwr /*
    214   1.1       gwr  * The "extended" size is somewhat arbitrary, but is
    215   1.1       gwr  * constrained by the maximum message size specified
    216   1.1       gwr  * by RFC1533 (567 total).  This value increases the
    217   1.1       gwr  * space for options from 64 bytes to 256 bytes.
    218   1.1       gwr  */
    219   1.2  drochner #define BOOTP_SIZE_MAX	(sizeof(struct bootp)+256-64)
    220   1.2  drochner #endif
    221   1.1       gwr #define BOOTP_SIZE_MIN	(sizeof(struct bootp))
    222   1.1       gwr 
    223   1.1       gwr /* Convenience macro */
    224   1.1       gwr #define INTOHL(ina) ((u_int32_t)ntohl((ina).s_addr))
    225   1.1       gwr 
    226  1.29  christos static int bootpc_call __P((struct nfs_diskless *, struct lwp *));
    227   1.2  drochner static void bootp_extract __P((struct bootp *, int, struct nfs_diskless *));
    228   1.1       gwr 
    229  1.30  christos #ifdef	DEBUG_NFS_BOOT_DHCP
    230  1.30  christos #define DPRINTF(s)  printf s
    231   1.1       gwr #else
    232  1.30  christos #define DPRINTF(s)
    233   1.1       gwr #endif
    234   1.1       gwr 
    235   1.1       gwr 
    236   1.1       gwr /*
    237   1.1       gwr  * Get our boot parameters using BOOTP.
    238   1.1       gwr  */
    239   1.1       gwr int
    240  1.29  christos nfs_bootdhcp(nd, lwp)
    241   1.1       gwr 	struct nfs_diskless *nd;
    242  1.29  christos 	struct lwp *lwp;
    243   1.1       gwr {
    244  1.11  drochner 	struct ifnet *ifp = nd->nd_ifp;
    245   1.1       gwr 	int error;
    246   1.1       gwr 
    247   1.1       gwr 	/*
    248   1.1       gwr 	 * Do enough of ifconfig(8) so that the chosen interface
    249   1.1       gwr 	 * can talk to the servers.  Use address zero for now.
    250   1.1       gwr 	 */
    251  1.29  christos 	error = nfs_boot_setaddress(ifp, lwp, INADDR_ANY, INADDR_ANY,
    252  1.11  drochner 				    INADDR_BROADCAST);
    253   1.1       gwr 	if (error) {
    254   1.1       gwr 		printf("nfs_boot: set ifaddr zero, error=%d\n", error);
    255  1.11  drochner 		return (error);
    256   1.1       gwr 	}
    257   1.1       gwr 
    258   1.1       gwr 	/* This function call does the real send/recv work. */
    259  1.29  christos 	error = bootpc_call(nd, lwp);
    260  1.11  drochner 
    261   1.1       gwr 	/* Get rid of the temporary (zero) IP address. */
    262  1.29  christos 	(void) nfs_boot_deladdress(ifp, lwp, INADDR_ANY);
    263  1.11  drochner 
    264   1.1       gwr 	/* NOW we can test the error from bootpc_call. */
    265   1.1       gwr 	if (error)
    266   1.1       gwr 		goto out;
    267   1.1       gwr 
    268   1.1       gwr 	/*
    269   1.1       gwr 	 * Do ifconfig with our real IP address and mask.
    270   1.1       gwr 	 */
    271  1.29  christos 	error = nfs_boot_setaddress(ifp, lwp, nd->nd_myip.s_addr,
    272  1.11  drochner 				    nd->nd_mask.s_addr, INADDR_ANY);
    273   1.1       gwr 	if (error) {
    274   1.1       gwr 		printf("nfs_boot: set ifaddr real, error=%d\n", error);
    275   1.1       gwr 		goto out;
    276   1.1       gwr 	}
    277   1.1       gwr 
    278   1.1       gwr out:
    279  1.11  drochner 	if (error) {
    280  1.29  christos 		(void) nfs_boot_ifupdown(ifp, lwp, 0);
    281  1.11  drochner 		nfs_boot_flushrt(ifp);
    282  1.11  drochner 	}
    283   1.1       gwr 	return (error);
    284   1.1       gwr }
    285   1.1       gwr 
    286   1.2  drochner struct bootpcontext {
    287   1.2  drochner 	int xid;
    288   1.2  drochner 	u_char *haddr;
    289   1.2  drochner 	u_char halen;
    290   1.2  drochner 	struct bootp *replybuf;
    291   1.2  drochner 	int replylen;
    292   1.2  drochner #ifdef NFS_BOOT_DHCP
    293   1.2  drochner 	char expected_dhcpmsgtype, dhcp_ok;
    294   1.2  drochner 	struct in_addr dhcp_serverip;
    295   1.2  drochner #endif
    296   1.2  drochner };
    297   1.2  drochner 
    298   1.2  drochner static int bootpset __P((struct mbuf*, void*, int));
    299   1.2  drochner static int bootpcheck __P((struct mbuf*, void*));
    300   1.2  drochner 
    301   1.7  drochner static int
    302   1.7  drochner bootpset(m, context, waited)
    303   1.7  drochner 	struct mbuf *m;
    304   1.7  drochner 	void *context;
    305   1.7  drochner 	int waited;
    306   1.2  drochner {
    307   1.2  drochner 	struct bootp *bootp;
    308   1.2  drochner 
    309   1.2  drochner 	/* we know it's contigous (in 1 mbuf cluster) */
    310   1.2  drochner 	bootp = mtod(m, struct bootp*);
    311   1.2  drochner 
    312   1.2  drochner 	bootp->bp_secs = htons(waited);
    313   1.2  drochner 
    314   1.7  drochner 	return (0);
    315   1.2  drochner }
    316   1.2  drochner 
    317   1.7  drochner static int
    318   1.7  drochner bootpcheck(m, context)
    319   1.7  drochner 	struct mbuf *m;
    320   1.7  drochner 	void *context;
    321   1.2  drochner {
    322   1.2  drochner 	struct bootp *bootp;
    323   1.2  drochner 	struct bootpcontext *bpc = context;
    324   1.2  drochner 	u_int tag, len;
    325   1.2  drochner 	u_char *p, *limit;
    326   1.2  drochner 
    327   1.2  drochner 	/*
    328   1.2  drochner 	 * Is this a valid reply?
    329   1.2  drochner 	 */
    330   1.2  drochner 	if (m->m_pkthdr.len < BOOTP_SIZE_MIN) {
    331  1.30  christos 		DPRINTF(("bootpcheck: short packet %d < %d\n", m->m_pkthdr.len,
    332  1.30  christos 		    BOOTP_SIZE_MIN));
    333   1.7  drochner 		return (-1);
    334   1.2  drochner 	}
    335   1.2  drochner 	if (m->m_pkthdr.len > BOOTP_SIZE_MAX) {
    336  1.30  christos 		DPRINTF(("bootpcheck: long packet %d > %d\n", m->m_pkthdr.len,
    337  1.30  christos 		    BOOTP_SIZE_MAX));
    338   1.7  drochner 		return (-1);
    339   1.2  drochner 	}
    340   1.2  drochner 
    341   1.2  drochner 	/*
    342   1.2  drochner 	 * don't make first checks more expensive than necessary
    343   1.2  drochner 	 */
    344  1.20    simonb 	if (m->m_len < offsetof(struct bootp, bp_sname)) {
    345  1.20    simonb 		m = m_pullup(m, offsetof(struct bootp, bp_sname));
    346  1.30  christos 		if (m == NULL) {
    347  1.30  christos 			DPRINTF(("bootpcheck: m_pullup failed\n"));
    348   1.7  drochner 			return (-1);
    349  1.30  christos 		}
    350   1.2  drochner 	}
    351   1.2  drochner 	bootp = mtod(m, struct bootp*);
    352   1.2  drochner 
    353   1.2  drochner 	if (bootp->bp_op != BOOTREPLY) {
    354  1.30  christos 		DPRINTF(("bootpcheck: op %d is not reply\n", bootp->bp_op));
    355   1.7  drochner 		return (-1);
    356   1.2  drochner 	}
    357   1.2  drochner 	if (bootp->bp_hlen != bpc->halen) {
    358  1.30  christos 		DPRINTF(("bootpcheck: hlen %d != %d\n", bootp->bp_hlen,
    359  1.30  christos 		    bpc->halen));
    360   1.7  drochner 		return (-1);
    361   1.2  drochner 	}
    362   1.8     perry 	if (memcmp(bootp->bp_chaddr, bpc->haddr, bpc->halen)) {
    363  1.30  christos #ifdef DEBUG_NFS_BOOT_DHCP
    364  1.30  christos 		char bp_chaddr[3 * bpc->halen], haddr[3 * bpc->halen];
    365  1.30  christos #endif
    366  1.30  christos 		DPRINTF(("bootpcheck: incorrect hwaddr %s != %s\n",
    367  1.30  christos 		    ether_snprintf(bp_chaddr, sizeof(bp_chaddr),
    368  1.30  christos 		    bootp->bp_chaddr),
    369  1.30  christos 		    ether_snprintf(haddr, sizeof(haddr), bpc->haddr)));
    370   1.7  drochner 		return (-1);
    371   1.2  drochner 	}
    372   1.2  drochner 	if (bootp->bp_xid != bpc->xid) {
    373  1.30  christos 		DPRINTF(("bootpcheck: xid %d != %d\n", bootp->bp_xid,
    374  1.30  christos 		    bpc->xid));
    375   1.7  drochner 		return (-1);
    376   1.2  drochner 	}
    377   1.2  drochner 
    378   1.2  drochner 	/*
    379   1.2  drochner 	 * OK, it's worth to look deeper.
    380   1.2  drochner 	 * We copy the mbuf into a flat buffer here because
    381   1.2  drochner 	 * m_pullup() is a bit limited for this purpose
    382   1.2  drochner 	 * (doesn't allocate a cluster if necessary).
    383   1.2  drochner 	 */
    384   1.2  drochner 	bpc->replylen = m->m_pkthdr.len;
    385   1.2  drochner 	m_copydata(m, 0, bpc->replylen, (caddr_t)bpc->replybuf);
    386   1.2  drochner 	bootp = bpc->replybuf;
    387   1.2  drochner 
    388   1.2  drochner 	/*
    389   1.7  drochner 	 * Check if the IP address we get looks correct.
    390   1.7  drochner 	 * (DHCP servers can send junk to unknown clients.)
    391   1.7  drochner 	 * XXX more checks might be needed
    392   1.7  drochner 	 */
    393   1.7  drochner 	if (bootp->bp_yiaddr.s_addr == INADDR_ANY ||
    394   1.7  drochner 	    bootp->bp_yiaddr.s_addr == INADDR_BROADCAST) {
    395  1.12  drochner 		printf("nfs_boot: wrong IP addr %s",
    396  1.12  drochner 		       inet_ntoa(bootp->bp_yiaddr));
    397   1.7  drochner 		goto warn;
    398   1.7  drochner 	}
    399   1.7  drochner 
    400   1.7  drochner 	/*
    401   1.2  drochner 	 * Check the vendor data.
    402   1.2  drochner 	 */
    403   1.8     perry 	if (memcmp(bootp->bp_vend, vm_rfc1048, 4)) {
    404   1.7  drochner 		printf("nfs_boot: reply missing options");
    405   1.7  drochner 		goto warn;
    406   1.2  drochner 	}
    407   1.2  drochner 	p = &bootp->bp_vend[4];
    408   1.2  drochner 	limit = ((char*)bootp) + bpc->replylen;
    409   1.2  drochner 	while (p < limit) {
    410   1.2  drochner 		tag = *p++;
    411   1.2  drochner 		if (tag == TAG_END)
    412   1.2  drochner 			break;
    413   1.2  drochner 		if (tag == TAG_PAD)
    414   1.2  drochner 			continue;
    415   1.2  drochner 		len = *p++;
    416   1.2  drochner 		if ((p + len) > limit) {
    417   1.7  drochner 			printf("nfs_boot: option %d too long", tag);
    418   1.7  drochner 			goto warn;
    419   1.2  drochner 		}
    420   1.2  drochner 		switch (tag) {
    421   1.2  drochner #ifdef NFS_BOOT_DHCP
    422  1.19   thorpej 		case TAG_DHCP_MSGTYPE:
    423   1.2  drochner 			if (*p != bpc->expected_dhcpmsgtype)
    424   1.7  drochner 				return (-1);
    425   1.2  drochner 			bpc->dhcp_ok = 1;
    426   1.2  drochner 			break;
    427  1.19   thorpej 		case TAG_SERVERID:
    428   1.8     perry 			memcpy(&bpc->dhcp_serverip.s_addr, p,
    429   1.2  drochner 			      sizeof(bpc->dhcp_serverip.s_addr));
    430   1.2  drochner 			break;
    431   1.2  drochner #endif
    432  1.19   thorpej 		default:
    433   1.2  drochner 			break;
    434   1.2  drochner 		}
    435   1.2  drochner 		p += len;
    436   1.2  drochner 	}
    437   1.7  drochner 	return (0);
    438   1.7  drochner 
    439   1.7  drochner warn:
    440  1.12  drochner 	printf(" (bad reply from %s)\n", inet_ntoa(bootp->bp_siaddr));
    441   1.7  drochner 	return (-1);
    442   1.2  drochner }
    443   1.2  drochner 
    444   1.1       gwr static int
    445  1.29  christos bootpc_call(nd, lwp)
    446   1.1       gwr 	struct nfs_diskless *nd;
    447  1.29  christos 	struct lwp *lwp;
    448   1.1       gwr {
    449  1.11  drochner 	struct socket *so;
    450  1.11  drochner 	struct ifnet *ifp = nd->nd_ifp;
    451   1.1       gwr 	static u_int32_t xid = ~0xFF;
    452   1.2  drochner 	struct bootp *bootp;	/* request */
    453   1.1       gwr 	struct mbuf *m, *nam;
    454   1.1       gwr 	struct sockaddr_in *sin;
    455   1.2  drochner 	int error;
    456   1.1       gwr 	u_char *haddr;
    457   1.1       gwr 	u_char hafmt, halen;
    458   1.2  drochner 	struct bootpcontext bpc;
    459  1.19   thorpej #ifdef NFS_BOOT_DHCP
    460  1.19   thorpej 	char vci[64];
    461  1.19   thorpej 	int vcilen;
    462  1.19   thorpej #endif
    463   1.1       gwr 
    464  1.29  christos 	error = socreate(AF_INET, &so, SOCK_DGRAM, 0, lwp);
    465  1.11  drochner 	if (error) {
    466  1.11  drochner 		printf("bootp: socreate, error=%d\n", error);
    467  1.11  drochner 		return (error);
    468  1.11  drochner 	}
    469  1.11  drochner 
    470   1.1       gwr 	/*
    471   1.1       gwr 	 * Initialize to NULL anything that will hold an allocation,
    472   1.1       gwr 	 * and free each at the end if not null.
    473   1.1       gwr 	 */
    474   1.2  drochner 	bpc.replybuf = NULL;
    475   1.1       gwr 	m = nam = NULL;
    476   1.1       gwr 
    477   1.1       gwr 	/* Record our H/W (Ethernet) address. */
    478   1.1       gwr 	{	struct sockaddr_dl *sdl = ifp->if_sadl;
    479   1.3  drochner 		switch (sdl->sdl_type) {
    480  1.15  gmcgarry 		    case IFT_ISO88025:
    481  1.15  gmcgarry 			hafmt = HTYPE_IEEE802;
    482  1.15  gmcgarry 			break;
    483   1.3  drochner 		    case IFT_ETHER:
    484   1.3  drochner 		    case IFT_FDDI:
    485   1.3  drochner 			hafmt = HTYPE_ETHERNET;
    486   1.3  drochner 			break;
    487   1.3  drochner 		    default:
    488   1.3  drochner 			printf("bootp: unsupported interface type %d\n",
    489   1.3  drochner 			       sdl->sdl_type);
    490   1.3  drochner 			error = EINVAL;
    491   1.3  drochner 			goto out;
    492   1.3  drochner 		}
    493   1.1       gwr 		halen = sdl->sdl_alen;
    494   1.1       gwr 		haddr = (unsigned char *)LLADDR(sdl);
    495   1.1       gwr 	}
    496   1.1       gwr 
    497   1.1       gwr 	/*
    498   1.1       gwr 	 * Skip the route table when sending on this socket.
    499   1.1       gwr 	 * If this is not done, ip_output finds the loopback
    500   1.1       gwr 	 * interface (why?) and then fails because broadcast
    501   1.1       gwr 	 * is not supported on that interface...
    502   1.1       gwr 	 */
    503   1.1       gwr 	{	int32_t *opt;
    504   1.1       gwr 		m = m_get(M_WAIT, MT_SOOPTS);
    505   1.1       gwr 		opt = mtod(m, int32_t *);
    506   1.1       gwr 		m->m_len = sizeof(*opt);
    507   1.1       gwr 		*opt = 1;
    508   1.1       gwr 		error = sosetopt(so, SOL_SOCKET, SO_DONTROUTE, m);
    509   1.1       gwr 		m = NULL;	/* was consumed */
    510   1.1       gwr 	}
    511   1.1       gwr 	if (error) {
    512  1.30  christos 		DPRINTF(("bootpc_call: SO_DONTROUTE failed %d\n", error));
    513   1.1       gwr 		goto out;
    514   1.1       gwr 	}
    515   1.1       gwr 
    516   1.1       gwr 	/* Enable broadcast. */
    517   1.2  drochner 	if ((error = nfs_boot_enbroadcast(so))) {
    518  1.30  christos 		DPRINTF(("bootpc_call: SO_BROADCAST failed %d\n", error));
    519  1.21  drochner 		goto out;
    520  1.21  drochner 	}
    521  1.21  drochner 
    522  1.21  drochner 	/*
    523  1.21  drochner 	 * Set some TTL so we can boot through routers.
    524  1.21  drochner 	 * Real BOOTP forwarding agents don't need this; they obey "bp_hops"
    525  1.21  drochner 	 * and set "bp_giaddr", thus rewrite the packet anyway.
    526  1.21  drochner 	 * The "helper-address" feature of some popular router vendor seems
    527  1.21  drochner 	 * to do simple IP forwarding and drops packets with (ip_ttl == 1).
    528  1.21  drochner 	 */
    529  1.21  drochner 	{	u_char *opt;
    530  1.21  drochner 		m = m_get(M_WAIT, MT_SOOPTS);
    531  1.21  drochner 		opt = mtod(m, u_char *);
    532  1.21  drochner 		m->m_len = sizeof(*opt);
    533  1.21  drochner 		*opt = 7;
    534  1.21  drochner 		error = sosetopt(so, IPPROTO_IP, IP_MULTICAST_TTL, m);
    535  1.21  drochner 		m = NULL;	/* was consumed */
    536  1.21  drochner 	}
    537  1.21  drochner 	if (error) {
    538  1.30  christos 		DPRINTF(("bootpc_call: IP_MULTICAST_TTL failed %d\n", error));
    539   1.1       gwr 		goto out;
    540   1.1       gwr 	}
    541   1.1       gwr 
    542   1.1       gwr 	/* Set the receive timeout for the socket. */
    543   1.2  drochner 	if ((error = nfs_boot_setrecvtimo(so))) {
    544  1.30  christos 		DPRINTF(("bootpc_call: SO_RCVTIMEO failed %d\n", error));
    545   1.1       gwr 		goto out;
    546   1.1       gwr 	}
    547   1.1       gwr 
    548   1.1       gwr 	/*
    549   1.1       gwr 	 * Bind the local endpoint to a bootp client port.
    550   1.1       gwr 	 */
    551  1.29  christos 	if ((error = nfs_boot_sobind_ipport(so, IPPORT_BOOTPC, lwp))) {
    552  1.30  christos 		DPRINTF(("bootpc_call: bind failed %d\n", error));
    553   1.1       gwr 		goto out;
    554   1.1       gwr 	}
    555   1.1       gwr 
    556   1.1       gwr 	/*
    557   1.1       gwr 	 * Setup socket address for the server.
    558   1.1       gwr 	 */
    559   1.1       gwr 	nam = m_get(M_WAIT, MT_SONAME);
    560   1.1       gwr 	sin = mtod(nam, struct sockaddr_in *);
    561   1.1       gwr 	sin->sin_len = nam->m_len = sizeof(*sin);
    562   1.1       gwr 	sin->sin_family = AF_INET;
    563   1.1       gwr 	sin->sin_addr.s_addr = INADDR_BROADCAST;
    564   1.1       gwr 	sin->sin_port = htons(IPPORT_BOOTPS);
    565   1.1       gwr 
    566   1.1       gwr 	/*
    567   1.2  drochner 	 * Allocate buffer used for request
    568   1.1       gwr 	 */
    569   1.2  drochner 	m = m_gethdr(M_WAIT, MT_DATA);
    570  1.22      matt 	m_clget(m, M_WAIT);
    571   1.2  drochner 	bootp = mtod(m, struct bootp*);
    572   1.2  drochner 	m->m_pkthdr.len = m->m_len = BOOTP_SIZE_MAX;
    573   1.2  drochner 	m->m_pkthdr.rcvif = NULL;
    574   1.1       gwr 
    575   1.1       gwr 	/*
    576   1.2  drochner 	 * Build the BOOTP reqest message.
    577   1.1       gwr 	 * Note: xid is host order! (opaque to server)
    578   1.1       gwr 	 */
    579   1.8     perry 	memset((caddr_t)bootp, 0, BOOTP_SIZE_MAX);
    580   1.1       gwr 	bootp->bp_op    = BOOTREQUEST;
    581   1.1       gwr 	bootp->bp_htype = hafmt;
    582   1.1       gwr 	bootp->bp_hlen  = halen;	/* Hardware address length */
    583   1.1       gwr 	bootp->bp_xid = ++xid;
    584   1.8     perry 	memcpy(bootp->bp_chaddr, haddr, halen);
    585  1.16  drochner #ifdef NFS_BOOT_BOOTP_REQFILE
    586  1.16  drochner 	strncpy(bootp->bp_file, NFS_BOOT_BOOTP_REQFILE, sizeof(bootp->bp_file));
    587  1.16  drochner #endif
    588   1.1       gwr 	/* Fill-in the vendor data. */
    589   1.8     perry 	memcpy(bootp->bp_vend, vm_rfc1048, 4);
    590   1.2  drochner #ifdef NFS_BOOT_DHCP
    591   1.2  drochner 	bootp->bp_vend[4] = TAG_DHCP_MSGTYPE;
    592   1.2  drochner 	bootp->bp_vend[5] = 1;
    593   1.2  drochner 	bootp->bp_vend[6] = DHCPDISCOVER;
    594  1.19   thorpej 	/*
    595  1.19   thorpej 	 * Insert a NetBSD Vendor Class Identifier option.
    596  1.19   thorpej 	 */
    597  1.25    itojun 	snprintf(vci, sizeof(vci), "%s:%s:kernel:%s", ostype, MACHINE,
    598  1.25    itojun 	    osrelease);
    599  1.19   thorpej 	vcilen = strlen(vci);
    600  1.19   thorpej 	bootp->bp_vend[7] = TAG_CLASSID;
    601  1.19   thorpej 	bootp->bp_vend[8] = vcilen;
    602  1.19   thorpej 	memcpy(&bootp->bp_vend[9], vci, vcilen);
    603  1.19   thorpej 	bootp->bp_vend[9 + vcilen] = TAG_END;
    604   1.2  drochner #else
    605   1.1       gwr 	bootp->bp_vend[4] = TAG_END;
    606   1.2  drochner #endif
    607   1.2  drochner 
    608   1.2  drochner 	bpc.xid = xid;
    609   1.2  drochner 	bpc.haddr = haddr;
    610   1.2  drochner 	bpc.halen = halen;
    611   1.2  drochner 	bpc.replybuf = malloc(BOOTP_SIZE_MAX, M_DEVBUF, M_WAITOK);
    612   1.2  drochner 	if (bpc.replybuf == NULL)
    613   1.2  drochner 		panic("nfs_boot: malloc reply buf");
    614   1.2  drochner #ifdef NFS_BOOT_DHCP
    615   1.2  drochner 	bpc.expected_dhcpmsgtype = DHCPOFFER;
    616   1.2  drochner 	bpc.dhcp_ok = 0;
    617   1.2  drochner #endif
    618   1.1       gwr 
    619   1.2  drochner 	error = nfs_boot_sendrecv(so, nam, bootpset, m,
    620  1.29  christos 				  bootpcheck, 0, 0, &bpc, lwp);
    621   1.2  drochner 	if (error)
    622   1.1       gwr 		goto out;
    623   1.2  drochner 
    624   1.2  drochner #ifdef NFS_BOOT_DHCP
    625   1.2  drochner 	if (bpc.dhcp_ok) {
    626   1.2  drochner 		u_int32_t leasetime;
    627   1.2  drochner 		bootp->bp_vend[6] = DHCPREQUEST;
    628   1.2  drochner 		bootp->bp_vend[7] = TAG_REQ_ADDR;
    629   1.2  drochner 		bootp->bp_vend[8] = 4;
    630   1.8     perry 		memcpy(&bootp->bp_vend[9], &bpc.replybuf->bp_yiaddr, 4);
    631   1.2  drochner 		bootp->bp_vend[13] = TAG_SERVERID;
    632   1.2  drochner 		bootp->bp_vend[14] = 4;
    633   1.8     perry 		memcpy(&bootp->bp_vend[15], &bpc.dhcp_serverip.s_addr, 4);
    634   1.2  drochner 		bootp->bp_vend[19] = TAG_LEASETIME;
    635   1.2  drochner 		bootp->bp_vend[20] = 4;
    636   1.2  drochner 		leasetime = htonl(300);
    637   1.8     perry 		memcpy(&bootp->bp_vend[21], &leasetime, 4);
    638  1.19   thorpej 		bootp->bp_vend[25] = TAG_CLASSID;
    639  1.19   thorpej 		bootp->bp_vend[26] = vcilen;
    640  1.19   thorpej 		memcpy(&bootp->bp_vend[27], vci, vcilen);
    641  1.19   thorpej 		bootp->bp_vend[27 + vcilen] = TAG_END;
    642   1.2  drochner 
    643   1.2  drochner 		bpc.expected_dhcpmsgtype = DHCPACK;
    644   1.2  drochner 
    645   1.2  drochner 		error = nfs_boot_sendrecv(so, nam, bootpset, m,
    646  1.29  christos 					  bootpcheck, 0, 0, &bpc, lwp);
    647   1.2  drochner 		if (error)
    648   1.2  drochner 			goto out;
    649   1.1       gwr 	}
    650   1.2  drochner #endif
    651   1.1       gwr 
    652   1.1       gwr 	/*
    653   1.2  drochner 	 * bootpcheck() has copied the receive mbuf into
    654   1.2  drochner 	 * the buffer at bpc.replybuf.
    655   1.1       gwr 	 */
    656   1.7  drochner #ifdef NFS_BOOT_DHCP
    657  1.17       kim 	printf("nfs_boot: %s next-server: %s\n",
    658   1.7  drochner 	       (bpc.dhcp_ok ? "DHCP" : "BOOTP"),
    659   1.7  drochner #else
    660  1.17       kim 	printf("nfs_boot: BOOTP next-server: %s\n",
    661   1.7  drochner #endif
    662  1.12  drochner 	       inet_ntoa(bpc.replybuf->bp_siaddr));
    663   1.7  drochner 
    664   1.2  drochner 	bootp_extract(bpc.replybuf, bpc.replylen, nd);
    665   1.2  drochner 
    666   1.2  drochner out:
    667   1.2  drochner 	if (bpc.replybuf)
    668   1.2  drochner 		free(bpc.replybuf, M_DEVBUF);
    669   1.2  drochner 	if (m)
    670   1.2  drochner 		m_freem(m);
    671   1.2  drochner 	if (nam)
    672   1.2  drochner 		m_freem(nam);
    673  1.11  drochner 	soclose(so);
    674   1.2  drochner 	return (error);
    675   1.2  drochner }
    676   1.1       gwr 
    677   1.7  drochner static void
    678   1.7  drochner bootp_extract(bootp, replylen, nd)
    679   1.2  drochner 	struct bootp *bootp;
    680   1.2  drochner 	int replylen;
    681   1.2  drochner 	struct nfs_diskless *nd;
    682   1.2  drochner {
    683   1.2  drochner 	struct sockaddr_in *sin;
    684   1.2  drochner 	struct in_addr netmask;
    685   1.2  drochner 	struct in_addr gateway;
    686   1.2  drochner 	struct in_addr rootserver;
    687   1.2  drochner 	char *myname;	/* my hostname */
    688   1.2  drochner 	char *mydomain;	/* my domainname */
    689   1.2  drochner 	char *rootpath;
    690   1.2  drochner 	int mynamelen;
    691   1.2  drochner 	int mydomainlen;
    692   1.2  drochner 	int rootpathlen;
    693  1.13     enami 	int overloaded;
    694   1.2  drochner 	u_int tag, len;
    695   1.2  drochner 	u_char *p, *limit;
    696   1.1       gwr 
    697   1.2  drochner 	/* Default these to "unspecified". */
    698   1.2  drochner 	netmask.s_addr = 0;
    699   1.2  drochner 	gateway.s_addr = 0;
    700   1.2  drochner 	mydomain    = myname    = rootpath = NULL;
    701   1.2  drochner 	mydomainlen = mynamelen = rootpathlen = 0;
    702   1.2  drochner 	/* default root server to bootp next-server */
    703   1.2  drochner 	rootserver = bootp->bp_siaddr;
    704  1.13     enami 	/* assume that server name field is not overloaded by default */
    705  1.13     enami 	overloaded = 0;
    706   1.2  drochner 
    707   1.2  drochner 	p = &bootp->bp_vend[4];
    708   1.2  drochner 	limit = ((char*)bootp) + replylen;
    709   1.2  drochner 	while (p < limit) {
    710   1.2  drochner 		tag = *p++;
    711   1.2  drochner 		if (tag == TAG_END)
    712   1.2  drochner 			break;
    713   1.2  drochner 		if (tag == TAG_PAD)
    714   1.1       gwr 			continue;
    715   1.2  drochner 		len = *p++;
    716   1.7  drochner #if 0 /* already done in bootpcheck() */
    717   1.2  drochner 		if ((p + len) > limit) {
    718   1.2  drochner 			printf("nfs_boot: option %d too long\n", tag);
    719   1.2  drochner 			break;
    720   1.2  drochner 		}
    721   1.7  drochner #endif
    722   1.2  drochner 		switch (tag) {
    723   1.2  drochner 		    case TAG_SUBNET_MASK:
    724   1.8     perry 			memcpy(&netmask, p, 4);
    725   1.2  drochner 			break;
    726   1.2  drochner 		    case TAG_GATEWAY:
    727   1.2  drochner 			/* Routers */
    728   1.8     perry 			memcpy(&gateway, p, 4);
    729   1.2  drochner 			break;
    730   1.2  drochner 		    case TAG_HOST_NAME:
    731   1.2  drochner 			if (len >= sizeof(hostname)) {
    732  1.10   thorpej 				printf("nfs_boot: host name >= %lu bytes",
    733  1.10   thorpej 				       (u_long)sizeof(hostname));
    734   1.1       gwr 				break;
    735   1.2  drochner 			}
    736   1.2  drochner 			myname = p;
    737   1.2  drochner 			mynamelen = len;
    738   1.2  drochner 			break;
    739   1.2  drochner 		    case TAG_DOMAIN_NAME:
    740   1.2  drochner 			if (len >= sizeof(domainname)) {
    741  1.10   thorpej 				printf("nfs_boot: domain name >= %lu bytes",
    742  1.10   thorpej 				       (u_long)sizeof(domainname));
    743   1.1       gwr 				break;
    744   1.1       gwr 			}
    745   1.2  drochner 			mydomain = p;
    746   1.2  drochner 			mydomainlen = len;
    747   1.2  drochner 			break;
    748   1.2  drochner 		    case TAG_ROOT_PATH:
    749   1.2  drochner 			/* Leave some room for the server name. */
    750   1.2  drochner 			if (len >= (MNAMELEN-10)) {
    751   1.2  drochner 				printf("nfs_boot: rootpath >=%d bytes",
    752   1.2  drochner 				       (MNAMELEN-10));
    753   1.1       gwr 				break;
    754   1.1       gwr 			}
    755   1.2  drochner 			rootpath = p;
    756   1.2  drochner 			rootpathlen = len;
    757   1.2  drochner 			break;
    758   1.2  drochner 		    case TAG_SWAP_SERVER:
    759   1.2  drochner 			/* override NFS server address */
    760   1.8     perry 			memcpy(&rootserver, p, 4);
    761   1.2  drochner 			break;
    762  1.13     enami #ifdef NFS_BOOT_DHCP
    763  1.13     enami 		    case TAG_OVERLOAD:
    764  1.13     enami 			if (len > 0 && ((*p & 0x02) != 0))
    765  1.13     enami 				/*
    766  1.13     enami 				 * The server name field in the dhcp packet
    767  1.13     enami 				 * is overloaded and we can't find server
    768  1.13     enami 				 * name there.
    769  1.13     enami 				 */
    770  1.13     enami 				overloaded = 1;
    771  1.13     enami 			break;
    772  1.13     enami #endif
    773   1.2  drochner 		    default:
    774   1.2  drochner 			break;
    775   1.1       gwr 		}
    776   1.2  drochner 		p += len;
    777   1.1       gwr 	}
    778   1.1       gwr 
    779   1.1       gwr 	/*
    780   1.1       gwr 	 * Store and print network config info.
    781   1.1       gwr 	 */
    782   1.1       gwr 	if (myname) {
    783   1.1       gwr 		myname[mynamelen] = '\0';
    784   1.1       gwr 		strncpy(hostname, myname, sizeof(hostname));
    785   1.1       gwr 		hostnamelen = mynamelen;
    786   1.1       gwr 		printf("nfs_boot: my_name=%s\n", hostname);
    787   1.1       gwr 	}
    788   1.1       gwr 	if (mydomain) {
    789   1.1       gwr 		mydomain[mydomainlen] = '\0';
    790   1.1       gwr 		strncpy(domainname, mydomain, sizeof(domainname));
    791   1.1       gwr 		domainnamelen = mydomainlen;
    792   1.1       gwr 		printf("nfs_boot: my_domain=%s\n", domainname);
    793   1.1       gwr 	}
    794   1.1       gwr 	nd->nd_myip = bootp->bp_yiaddr;
    795   1.1       gwr 	if (nd->nd_myip.s_addr)
    796  1.12  drochner 		printf("nfs_boot: my_addr=%s\n", inet_ntoa(nd->nd_myip));
    797   1.1       gwr 	nd->nd_mask = netmask;
    798   1.1       gwr 	if (nd->nd_mask.s_addr)
    799  1.12  drochner 		printf("nfs_boot: my_mask=%s\n", inet_ntoa(nd->nd_mask));
    800   1.1       gwr 	nd->nd_gwip = gateway;
    801   1.1       gwr 	if (nd->nd_gwip.s_addr)
    802  1.12  drochner 		printf("nfs_boot: gateway=%s\n", inet_ntoa(nd->nd_gwip));
    803   1.1       gwr 
    804   1.1       gwr 	/*
    805   1.1       gwr 	 * Store the information about our NFS root mount.
    806   1.1       gwr 	 * The caller will print it, so be silent here.
    807   1.1       gwr 	 */
    808   1.1       gwr 	{
    809   1.1       gwr 		struct nfs_dlmount *ndm = &nd->nd_root;
    810   1.1       gwr 
    811   1.1       gwr 		/* Server IP address. */
    812   1.1       gwr 		sin = (struct sockaddr_in *) &ndm->ndm_saddr;
    813   1.8     perry 		memset((caddr_t)sin, 0, sizeof(*sin));
    814   1.1       gwr 		sin->sin_len = sizeof(*sin);
    815   1.1       gwr 		sin->sin_family = AF_INET;
    816   1.2  drochner 		sin->sin_addr = rootserver;
    817   1.1       gwr 		/* Server name. */
    818  1.13     enami 		if (!overloaded && bootp->bp_sname[0] != 0 &&
    819  1.13     enami 		    !memcmp(&rootserver, &bootp->bp_siaddr,
    820   1.2  drochner 			  sizeof(struct in_addr))) {
    821   1.2  drochner 			/* standard root server, we have the name */
    822   1.2  drochner 			strncpy(ndm->ndm_host, bootp->bp_sname, BP_SNAME_LEN-1);
    823   1.2  drochner 		} else {
    824   1.1       gwr 			/* Show the server IP address numerically. */
    825  1.12  drochner 			strncpy(ndm->ndm_host, inet_ntoa(rootserver),
    826  1.12  drochner 				BP_SNAME_LEN-1);
    827   1.1       gwr 		}
    828   1.2  drochner 		len = strlen(ndm->ndm_host);
    829   1.2  drochner 		if (rootpath &&
    830   1.2  drochner 		    len + 1 + rootpathlen + 1 <= sizeof(ndm->ndm_host)) {
    831   1.2  drochner 			ndm->ndm_host[len++] = ':';
    832   1.2  drochner 			strncpy(ndm->ndm_host + len,
    833   1.2  drochner 				rootpath, rootpathlen);
    834   1.2  drochner 			ndm->ndm_host[len + rootpathlen] = '\0';
    835   1.2  drochner 		} /* else: upper layer will handle error */
    836   1.1       gwr 	}
    837   1.1       gwr }
    838