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