1*c94bf7cbSmlelstv /* $NetBSD: nfs_bootdhcp.c,v 1.60 2024/10/20 14:01:52 mlelstv Exp $ */ 279c1e7caSgwr 379c1e7caSgwr /*- 479c1e7caSgwr * Copyright (c) 1995, 1997 The NetBSD Foundation, Inc. 579c1e7caSgwr * All rights reserved. 679c1e7caSgwr * 779c1e7caSgwr * This code is derived from software contributed to The NetBSD Foundation 879c1e7caSgwr * by Adam Glass and Gordon W. Ross. 979c1e7caSgwr * 1079c1e7caSgwr * Redistribution and use in source and binary forms, with or without 1179c1e7caSgwr * modification, are permitted provided that the following conditions 1279c1e7caSgwr * are met: 1379c1e7caSgwr * 1. Redistributions of source code must retain the above copyright 1479c1e7caSgwr * notice, this list of conditions and the following disclaimer. 1579c1e7caSgwr * 2. Redistributions in binary form must reproduce the above copyright 1679c1e7caSgwr * notice, this list of conditions and the following disclaimer in the 1779c1e7caSgwr * documentation and/or other materials provided with the distribution. 1879c1e7caSgwr * 1979c1e7caSgwr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2079c1e7caSgwr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2179c1e7caSgwr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2279c1e7caSgwr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2379c1e7caSgwr * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2479c1e7caSgwr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2579c1e7caSgwr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2679c1e7caSgwr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2779c1e7caSgwr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2879c1e7caSgwr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2979c1e7caSgwr * POSSIBILITY OF SUCH DAMAGE. 3079c1e7caSgwr */ 3179c1e7caSgwr 3279c1e7caSgwr /* 3379c1e7caSgwr * Support for NFS diskless booting with BOOTP (RFC951, RFC1048) 3479c1e7caSgwr * 3579c1e7caSgwr * History: 3679c1e7caSgwr * 3779c1e7caSgwr * Tor Egge developed the initial version of this code based on 3879c1e7caSgwr * the Sun RPC/bootparam sources nfs_boot.c and krpc_subr.c and 3979c1e7caSgwr * submitted that work to NetBSD as bugreport "kern/2351" on 4079c1e7caSgwr * 29 Apr 1996. 4179c1e7caSgwr * 4279c1e7caSgwr * Gordon Ross reorganized Tor's version into this form and 4379c1e7caSgwr * integrated it into the NetBSD sources during Aug 1997. 4479c1e7caSgwr */ 4579c1e7caSgwr 460ffad693Slukem #include <sys/cdefs.h> 47*c94bf7cbSmlelstv __KERNEL_RCSID(0, "$NetBSD: nfs_bootdhcp.c,v 1.60 2024/10/20 14:01:52 mlelstv Exp $"); 480ffad693Slukem 4992ce8c6aSad #ifdef _KERNEL_OPT 50ecc55fb7Sscottr #include "opt_nfs_boot.h" 5131b57f40Smanu #include "opt_tftproot.h" 5292ce8c6aSad #endif 53ecc55fb7Sscottr 5479c1e7caSgwr #include <sys/param.h> 5579c1e7caSgwr #include <sys/systm.h> 5679c1e7caSgwr #include <sys/kernel.h> 5779c1e7caSgwr #include <sys/device.h> 5879c1e7caSgwr #include <sys/ioctl.h> 5979c1e7caSgwr #include <sys/proc.h> 6079c1e7caSgwr #include <sys/mount.h> 6179c1e7caSgwr #include <sys/mbuf.h> 6279c1e7caSgwr #include <sys/reboot.h> 6379c1e7caSgwr #include <sys/socket.h> 6479c1e7caSgwr #include <sys/socketvar.h> 6579c1e7caSgwr 6679c1e7caSgwr #include <net/if.h> 672179b648Sdrochner #include <net/if_types.h> 6879c1e7caSgwr #include <net/if_arp.h> /* ARPHRD_ETHER, etc. */ 6979c1e7caSgwr #include <net/if_dl.h> 7079c1e7caSgwr #include <net/if_ether.h> 7179c1e7caSgwr #include <net/route.h> 7279c1e7caSgwr 7379c1e7caSgwr #include <netinet/in.h> 7479c1e7caSgwr #include <netinet/if_inarp.h> 7579c1e7caSgwr 76331fee62Sross #include <nfs/rpcv2.h> 77331fee62Sross 7879c1e7caSgwr #include <nfs/nfsproto.h> 79331fee62Sross #include <nfs/nfs.h> 80331fee62Sross #include <nfs/nfsmount.h> 8179c1e7caSgwr #include <nfs/nfsdiskless.h> 8279c1e7caSgwr 8379c1e7caSgwr /* 8479c1e7caSgwr * There are two implementations of NFS diskless boot. 8579c1e7caSgwr * This implementation uses BOOTP (RFC951, RFC1048), and 8679c1e7caSgwr * the other uses Sun RPC/bootparams (nfs_bootparam.c). 8779c1e7caSgwr * 8879c1e7caSgwr * This method gets everything it needs with one BOOTP 8979c1e7caSgwr * request and reply. Note that this actually uses only 9079c1e7caSgwr * the old BOOTP functionality subset of DHCP. It is not 9179c1e7caSgwr * clear that DHCP provides any advantage over BOOTP for 9279c1e7caSgwr * diskless boot. DHCP allows the server to assign an IP 9379c1e7caSgwr * address without any a-priori knowledge of the client, 9479c1e7caSgwr * but we require that the server has a-priori knowledge 9579c1e7caSgwr * of the client so it can export our (unique) NFS root. 9679c1e7caSgwr * Given that the server needs a-priori knowledge about 9779c1e7caSgwr * the client anyway, it might as well assign a fixed IP 9879c1e7caSgwr * address for the client and support BOOTP. 9979c1e7caSgwr * 10079c1e7caSgwr * On the other hand, disk-FULL clients may use DHCP, but 10179c1e7caSgwr * in that case the DHCP client should be user-mode code, 10279c1e7caSgwr * and has no bearing on the code below. -gwr 10379c1e7caSgwr */ 10479c1e7caSgwr 10579c1e7caSgwr /* Begin stuff from bootp.h */ 10679c1e7caSgwr /* Definitions from RFC951 */ 10779c1e7caSgwr #define BP_CHADDR_LEN 16 10879c1e7caSgwr #define BP_SNAME_LEN 64 10979c1e7caSgwr #define BP_FILE_LEN 128 11079c1e7caSgwr #define BP_VEND_LEN 64 11179c1e7caSgwr struct bootp { 11279c1e7caSgwr u_int8_t bp_op; /* packet opcode type */ 11379c1e7caSgwr u_int8_t bp_htype; /* hardware addr type */ 11479c1e7caSgwr u_int8_t bp_hlen; /* hardware addr length */ 11579c1e7caSgwr u_int8_t bp_hops; /* gateway hops */ 11679c1e7caSgwr u_int32_t bp_xid; /* transaction ID */ 11779c1e7caSgwr u_int16_t bp_secs; /* seconds since boot began */ 11879c1e7caSgwr u_int16_t bp_flags; /* RFC1532 broadcast, etc. */ 11979c1e7caSgwr struct in_addr bp_ciaddr; /* client IP address */ 12079c1e7caSgwr struct in_addr bp_yiaddr; /* 'your' IP address */ 12179c1e7caSgwr struct in_addr bp_siaddr; /* server IP address */ 12279c1e7caSgwr struct in_addr bp_giaddr; /* gateway IP address */ 12379c1e7caSgwr u_int8_t bp_chaddr[BP_CHADDR_LEN]; /* client hardware address */ 12479c1e7caSgwr char bp_sname[BP_SNAME_LEN]; /* server host name */ 12579c1e7caSgwr char bp_file[BP_FILE_LEN]; /* boot file name */ 12679c1e7caSgwr u_int8_t bp_vend[BP_VEND_LEN]; /* RFC1048 options */ 12779c1e7caSgwr /* 12879c1e7caSgwr * Note that BOOTP packets are allowed to be longer 12979c1e7caSgwr * (see RFC 1532 sect. 2.1) and common practice is to 13079c1e7caSgwr * allow the option data in bp_vend to extend into the 13179c1e7caSgwr * additional space provided in longer packets. 13279c1e7caSgwr */ 13379c1e7caSgwr }; 13479c1e7caSgwr 13579c1e7caSgwr #define IPPORT_BOOTPS 67 13679c1e7caSgwr #define IPPORT_BOOTPC 68 13779c1e7caSgwr 13879c1e7caSgwr #define BOOTREQUEST 1 13979c1e7caSgwr #define BOOTREPLY 2 14079c1e7caSgwr 14179c1e7caSgwr /* 14279c1e7caSgwr * Is this available from the sockaddr_dl somehow? 14379c1e7caSgwr * Perhaps (struct arphdr)->ar_hrd = ARPHRD_ETHER? 14479c1e7caSgwr * The interface has ->if_type but not the ARP fmt. 14579c1e7caSgwr */ 14679c1e7caSgwr #define HTYPE_ETHERNET 1 1478d994b82Sgmcgarry #define HTYPE_IEEE802 6 14879c1e7caSgwr 14979c1e7caSgwr /* 15079c1e7caSgwr * Vendor magic cookie (v_magic) for RFC1048 15179c1e7caSgwr */ 15279c1e7caSgwr static const u_int8_t vm_rfc1048[4] = { 99, 130, 83, 99 }; 15379c1e7caSgwr 15479c1e7caSgwr /* 15579c1e7caSgwr * Tag values used to specify what information is being supplied in 15679c1e7caSgwr * the vendor (options) data area of the packet. 15779c1e7caSgwr */ 15879c1e7caSgwr /* RFC 1048 */ 15979c1e7caSgwr #define TAG_END ((unsigned char) 255) 16079c1e7caSgwr #define TAG_PAD ((unsigned char) 0) 16179c1e7caSgwr #define TAG_SUBNET_MASK ((unsigned char) 1) 16279c1e7caSgwr #define TAG_TIME_OFFSET ((unsigned char) 2) 16379c1e7caSgwr #define TAG_GATEWAY ((unsigned char) 3) 16479c1e7caSgwr #define TAG_TIME_SERVER ((unsigned char) 4) 16579c1e7caSgwr #define TAG_NAME_SERVER ((unsigned char) 5) 16679c1e7caSgwr #define TAG_DOMAIN_SERVER ((unsigned char) 6) 16779c1e7caSgwr #define TAG_LOG_SERVER ((unsigned char) 7) 16879c1e7caSgwr #define TAG_COOKIE_SERVER ((unsigned char) 8) 16979c1e7caSgwr #define TAG_LPR_SERVER ((unsigned char) 9) 17079c1e7caSgwr #define TAG_IMPRESS_SERVER ((unsigned char) 10) 17179c1e7caSgwr #define TAG_RLP_SERVER ((unsigned char) 11) 17279c1e7caSgwr #define TAG_HOST_NAME ((unsigned char) 12) 17379c1e7caSgwr #define TAG_BOOT_SIZE ((unsigned char) 13) 17479c1e7caSgwr /* RFC 1395 */ 17579c1e7caSgwr #define TAG_DUMP_FILE ((unsigned char) 14) 17679c1e7caSgwr #define TAG_DOMAIN_NAME ((unsigned char) 15) 17779c1e7caSgwr #define TAG_SWAP_SERVER ((unsigned char) 16) 17879c1e7caSgwr #define TAG_ROOT_PATH ((unsigned char) 17) 179753a25a9Scyber /* RFC 2132 */ 180753a25a9Scyber #define TAG_INTERFACE_MTU ((unsigned char) 26) 18179c1e7caSgwr /* End of stuff from bootp.h */ 18279c1e7caSgwr 1839a7e1c1dSdrochner #ifdef NFS_BOOT_DHCP 1849a7e1c1dSdrochner #define TAG_REQ_ADDR ((unsigned char) 50) 1859a7e1c1dSdrochner #define TAG_LEASETIME ((unsigned char) 51) 1869a7e1c1dSdrochner #define TAG_OVERLOAD ((unsigned char) 52) 1879a7e1c1dSdrochner #define TAG_DHCP_MSGTYPE ((unsigned char) 53) 1889a7e1c1dSdrochner #define TAG_SERVERID ((unsigned char) 54) 1899a7e1c1dSdrochner #define TAG_PARAM_REQ ((unsigned char) 55) 1909a7e1c1dSdrochner #define TAG_MSG ((unsigned char) 56) 1919a7e1c1dSdrochner #define TAG_MAXSIZE ((unsigned char) 57) 1929a7e1c1dSdrochner #define TAG_T1 ((unsigned char) 58) 1939a7e1c1dSdrochner #define TAG_T2 ((unsigned char) 59) 1949a7e1c1dSdrochner #define TAG_CLASSID ((unsigned char) 60) 1959a7e1c1dSdrochner #define TAG_CLIENTID ((unsigned char) 61) 1969a7e1c1dSdrochner #endif 19779c1e7caSgwr 1989a7e1c1dSdrochner #ifdef NFS_BOOT_DHCP 1999a7e1c1dSdrochner #define DHCPDISCOVER 1 2009a7e1c1dSdrochner #define DHCPOFFER 2 2019a7e1c1dSdrochner #define DHCPREQUEST 3 2029a7e1c1dSdrochner #define DHCPDECLINE 4 2039a7e1c1dSdrochner #define DHCPACK 5 2049a7e1c1dSdrochner #define DHCPNAK 6 2059a7e1c1dSdrochner #define DHCPRELEASE 7 2069a7e1c1dSdrochner #endif 2079a7e1c1dSdrochner 208753a25a9Scyber #define IP_MIN_MTU 576 209753a25a9Scyber 2109a7e1c1dSdrochner #ifdef NFS_BOOT_DHCP 2119a7e1c1dSdrochner #define BOOTP_SIZE_MAX (sizeof(struct bootp)+312-64) 2129a7e1c1dSdrochner #else 21379c1e7caSgwr /* 21479c1e7caSgwr * The "extended" size is somewhat arbitrary, but is 21579c1e7caSgwr * constrained by the maximum message size specified 21679c1e7caSgwr * by RFC1533 (567 total). This value increases the 21779c1e7caSgwr * space for options from 64 bytes to 256 bytes. 21879c1e7caSgwr */ 2199a7e1c1dSdrochner #define BOOTP_SIZE_MAX (sizeof(struct bootp)+256-64) 2209a7e1c1dSdrochner #endif 22179c1e7caSgwr #define BOOTP_SIZE_MIN (sizeof(struct bootp)) 22279c1e7caSgwr 22379c1e7caSgwr /* Convenience macro */ 22479c1e7caSgwr #define INTOHL(ina) ((u_int32_t)ntohl((ina).s_addr)) 22579c1e7caSgwr 226c4798840Scegger static int bootpc_call (struct nfs_diskless *, struct lwp *, int *); 227c4798840Scegger static void bootp_extract (struct bootp *, int, struct nfs_diskless *, int *); 22879c1e7caSgwr 229c92b23ffSchristos #ifdef DEBUG_NFS_BOOT_DHCP 230c92b23ffSchristos #define DPRINTF(s) printf s 23179c1e7caSgwr #else 232c92b23ffSchristos #define DPRINTF(s) 23379c1e7caSgwr #endif 23479c1e7caSgwr 23579c1e7caSgwr 23679c1e7caSgwr /* 23779c1e7caSgwr * Get our boot parameters using BOOTP. 23879c1e7caSgwr */ 23979c1e7caSgwr int 240c4798840Scegger nfs_bootdhcp(struct nfs_diskless *nd, struct lwp *lwp, int *flags) 24179c1e7caSgwr { 24227098b11Sdrochner struct ifnet *ifp = nd->nd_ifp; 24379c1e7caSgwr int error; 24479c1e7caSgwr 24579c1e7caSgwr /* 24679c1e7caSgwr * Do enough of ifconfig(8) so that the chosen interface 24779c1e7caSgwr * can talk to the servers. Use address zero for now. 24879c1e7caSgwr */ 249c4798840Scegger error = nfs_boot_setaddress(ifp, lwp, 250c4798840Scegger *flags & NFS_BOOT_HAS_MYIP ? nd->nd_myip.s_addr : INADDR_ANY, 251c4798840Scegger *flags & NFS_BOOT_HAS_MASK ? nd->nd_mask.s_addr : INADDR_ANY, 25227098b11Sdrochner INADDR_BROADCAST); 25379c1e7caSgwr if (error) { 25479c1e7caSgwr printf("nfs_boot: set ifaddr zero, error=%d\n", error); 25527098b11Sdrochner return (error); 25679c1e7caSgwr } 25779c1e7caSgwr 25879c1e7caSgwr /* This function call does the real send/recv work. */ 259c4798840Scegger error = bootpc_call(nd, lwp, flags); 26027098b11Sdrochner 26179c1e7caSgwr /* Get rid of the temporary (zero) IP address. */ 26295e1ffb1Schristos (void) nfs_boot_deladdress(ifp, lwp, INADDR_ANY); 26327098b11Sdrochner 26479c1e7caSgwr /* NOW we can test the error from bootpc_call. */ 26579c1e7caSgwr if (error) 26679c1e7caSgwr goto out; 26779c1e7caSgwr 26879c1e7caSgwr /* 26979c1e7caSgwr * Do ifconfig with our real IP address and mask. 27079c1e7caSgwr */ 27195e1ffb1Schristos error = nfs_boot_setaddress(ifp, lwp, nd->nd_myip.s_addr, 27227098b11Sdrochner nd->nd_mask.s_addr, INADDR_ANY); 27379c1e7caSgwr if (error) { 27479c1e7caSgwr printf("nfs_boot: set ifaddr real, error=%d\n", error); 27579c1e7caSgwr goto out; 27679c1e7caSgwr } 27779c1e7caSgwr 27820f56b30Smanu if ((*flags & NFS_BOOT_ALLINFO) != NFS_BOOT_ALLINFO) { 27920f56b30Smanu printf("nfs_boot: missing options (need IP, netmask, " 28020f56b30Smanu "gateway, next-server, root-path)\n"); 281c4798840Scegger return EADDRNOTAVAIL; 28220f56b30Smanu } 283c4798840Scegger 28479c1e7caSgwr out: 28527098b11Sdrochner if (error) { 28695e1ffb1Schristos (void) nfs_boot_ifupdown(ifp, lwp, 0); 28727098b11Sdrochner nfs_boot_flushrt(ifp); 28827098b11Sdrochner } 28979c1e7caSgwr return (error); 29079c1e7caSgwr } 29179c1e7caSgwr 2929a7e1c1dSdrochner struct bootpcontext { 2939a7e1c1dSdrochner int xid; 2941751c510Sdyoung const u_char *haddr; 2959a7e1c1dSdrochner u_char halen; 2969a7e1c1dSdrochner struct bootp *replybuf; 2979a7e1c1dSdrochner int replylen; 2989a7e1c1dSdrochner #ifdef NFS_BOOT_DHCP 2999a7e1c1dSdrochner char expected_dhcpmsgtype, dhcp_ok; 3009a7e1c1dSdrochner struct in_addr dhcp_serverip; 3019a7e1c1dSdrochner #endif 3029a7e1c1dSdrochner }; 3039a7e1c1dSdrochner 304cac68954Scegger static int bootpset (struct mbuf*, void*, int); 305638842f1Shikaru static int bootpcheck (struct mbuf**, void*); 3069a7e1c1dSdrochner 307bb2aeabeSdrochner static int 30889682b24Syamt bootpset(struct mbuf *m, void *context, int waited) 3099a7e1c1dSdrochner { 3109a7e1c1dSdrochner struct bootp *bootp; 3119a7e1c1dSdrochner 312a11cd6f1Smsaitoh /* we know it's contiguous (in 1 mbuf cluster) */ 3139a7e1c1dSdrochner bootp = mtod(m, struct bootp*); 3149a7e1c1dSdrochner 3159a7e1c1dSdrochner bootp->bp_secs = htons(waited); 3169a7e1c1dSdrochner 3179a7e1c1dSdrochner return (0); 3189a7e1c1dSdrochner } 3199a7e1c1dSdrochner 320bb2aeabeSdrochner static int 321638842f1Shikaru bootpcheck(struct mbuf **mp, void *context) 3229a7e1c1dSdrochner { 3239a7e1c1dSdrochner struct bootp *bootp; 3249a7e1c1dSdrochner struct bootpcontext *bpc = context; 325638842f1Shikaru struct mbuf *m = *mp; 3269a7e1c1dSdrochner u_int tag, len; 3279a7e1c1dSdrochner u_char *p, *limit; 3289a7e1c1dSdrochner 3299a7e1c1dSdrochner /* 3309a7e1c1dSdrochner * Is this a valid reply? 3319a7e1c1dSdrochner */ 3329a7e1c1dSdrochner if (m->m_pkthdr.len < BOOTP_SIZE_MIN) { 333a531bda0Scegger DPRINTF(("bootpcheck: short packet %d < %zu\n", 33420f56b30Smanu m->m_pkthdr.len, BOOTP_SIZE_MIN)); 3359a7e1c1dSdrochner return (-1); 3369a7e1c1dSdrochner } 3379a7e1c1dSdrochner if (m->m_pkthdr.len > BOOTP_SIZE_MAX) { 338a531bda0Scegger DPRINTF(("Bootpcheck: long packet %d > %zu\n", 33920f56b30Smanu m->m_pkthdr.len, BOOTP_SIZE_MAX)); 3409a7e1c1dSdrochner return (-1); 3419a7e1c1dSdrochner } 3429a7e1c1dSdrochner 3439a7e1c1dSdrochner /* 3449a7e1c1dSdrochner * don't make first checks more expensive than necessary 3459a7e1c1dSdrochner */ 3468cb3bbd3Ssimonb if (m->m_len < offsetof(struct bootp, bp_sname)) { 347638842f1Shikaru m = *mp = m_pullup(m, offsetof(struct bootp, bp_sname)); 348c92b23ffSchristos if (m == NULL) { 349c92b23ffSchristos DPRINTF(("bootpcheck: m_pullup failed\n")); 3509a7e1c1dSdrochner return (-1); 3519a7e1c1dSdrochner } 352c92b23ffSchristos } 3539a7e1c1dSdrochner bootp = mtod(m, struct bootp*); 3549a7e1c1dSdrochner 3559a7e1c1dSdrochner if (bootp->bp_op != BOOTREPLY) { 356c92b23ffSchristos DPRINTF(("bootpcheck: op %d is not reply\n", bootp->bp_op)); 3579a7e1c1dSdrochner return (-1); 3589a7e1c1dSdrochner } 3599a7e1c1dSdrochner if (bootp->bp_hlen != bpc->halen) { 360c92b23ffSchristos DPRINTF(("bootpcheck: hlen %d != %d\n", bootp->bp_hlen, 361c92b23ffSchristos bpc->halen)); 3629a7e1c1dSdrochner return (-1); 3639a7e1c1dSdrochner } 3644522c799Sperry if (memcmp(bootp->bp_chaddr, bpc->haddr, bpc->halen)) { 365c92b23ffSchristos #ifdef DEBUG_NFS_BOOT_DHCP 366552b98b9Srumble char *bp_chaddr, *haddr; 367552b98b9Srumble 368552b98b9Srumble bp_chaddr = malloc(3 * bpc->halen, M_TEMP, M_WAITOK); 369552b98b9Srumble haddr = malloc(3 * bpc->halen, M_TEMP, M_WAITOK); 370552b98b9Srumble 371c92b23ffSchristos DPRINTF(("bootpcheck: incorrect hwaddr %s != %s\n", 372552b98b9Srumble ether_snprintf(bp_chaddr, 3 * bpc->halen, 373c92b23ffSchristos bootp->bp_chaddr), 374552b98b9Srumble ether_snprintf(haddr, 3 * bpc->halen, bpc->haddr))); 375552b98b9Srumble 376552b98b9Srumble free(bp_chaddr, M_TEMP); 377552b98b9Srumble free(haddr, M_TEMP); 378552b98b9Srumble #endif 3799a7e1c1dSdrochner return (-1); 3809a7e1c1dSdrochner } 3819a7e1c1dSdrochner if (bootp->bp_xid != bpc->xid) { 382c92b23ffSchristos DPRINTF(("bootpcheck: xid %d != %d\n", bootp->bp_xid, 383c92b23ffSchristos bpc->xid)); 3849a7e1c1dSdrochner return (-1); 3859a7e1c1dSdrochner } 3869a7e1c1dSdrochner 3879a7e1c1dSdrochner /* 3889a7e1c1dSdrochner * OK, it's worth to look deeper. 3899a7e1c1dSdrochner * We copy the mbuf into a flat buffer here because 3909a7e1c1dSdrochner * m_pullup() is a bit limited for this purpose 3919a7e1c1dSdrochner * (doesn't allocate a cluster if necessary). 3929a7e1c1dSdrochner */ 3939a7e1c1dSdrochner bpc->replylen = m->m_pkthdr.len; 39453524e44Schristos m_copydata(m, 0, bpc->replylen, (void *)bpc->replybuf); 3959a7e1c1dSdrochner bootp = bpc->replybuf; 3969a7e1c1dSdrochner 3979a7e1c1dSdrochner /* 398bb2aeabeSdrochner * Check if the IP address we get looks correct. 399bb2aeabeSdrochner * (DHCP servers can send junk to unknown clients.) 400bb2aeabeSdrochner * XXX more checks might be needed 401bb2aeabeSdrochner */ 402bb2aeabeSdrochner if (bootp->bp_yiaddr.s_addr == INADDR_ANY || 403bb2aeabeSdrochner bootp->bp_yiaddr.s_addr == INADDR_BROADCAST) { 40441b78a54Sdrochner printf("nfs_boot: wrong IP addr %s", 40541b78a54Sdrochner inet_ntoa(bootp->bp_yiaddr)); 406bb2aeabeSdrochner goto warn; 407bb2aeabeSdrochner } 408bb2aeabeSdrochner 409bb2aeabeSdrochner /* 4109a7e1c1dSdrochner * Check the vendor data. 4119a7e1c1dSdrochner */ 4124522c799Sperry if (memcmp(bootp->bp_vend, vm_rfc1048, 4)) { 413bb2aeabeSdrochner printf("nfs_boot: reply missing options"); 414bb2aeabeSdrochner goto warn; 4159a7e1c1dSdrochner } 4169a7e1c1dSdrochner p = &bootp->bp_vend[4]; 417e58a3edbSuwe limit = ((u_char*)bootp) + bpc->replylen; 4189a7e1c1dSdrochner while (p < limit) { 4199a7e1c1dSdrochner tag = *p++; 4209a7e1c1dSdrochner if (tag == TAG_END) 4219a7e1c1dSdrochner break; 4229a7e1c1dSdrochner if (tag == TAG_PAD) 4239a7e1c1dSdrochner continue; 4249a7e1c1dSdrochner len = *p++; 4259a7e1c1dSdrochner if ((p + len) > limit) { 426bb2aeabeSdrochner printf("nfs_boot: option %d too long", tag); 427bb2aeabeSdrochner goto warn; 4289a7e1c1dSdrochner } 4299a7e1c1dSdrochner switch (tag) { 4309a7e1c1dSdrochner #ifdef NFS_BOOT_DHCP 4319a7e1c1dSdrochner case TAG_DHCP_MSGTYPE: 4329a7e1c1dSdrochner if (*p != bpc->expected_dhcpmsgtype) 4339a7e1c1dSdrochner return (-1); 4349a7e1c1dSdrochner bpc->dhcp_ok = 1; 4359a7e1c1dSdrochner break; 4369a7e1c1dSdrochner case TAG_SERVERID: 4374522c799Sperry memcpy(&bpc->dhcp_serverip.s_addr, p, 4389a7e1c1dSdrochner sizeof(bpc->dhcp_serverip.s_addr)); 4399a7e1c1dSdrochner break; 4409a7e1c1dSdrochner #endif 4419a7e1c1dSdrochner default: 4429a7e1c1dSdrochner break; 4439a7e1c1dSdrochner } 4449a7e1c1dSdrochner p += len; 4459a7e1c1dSdrochner } 4469a7e1c1dSdrochner return (0); 447bb2aeabeSdrochner 448bb2aeabeSdrochner warn: 44941b78a54Sdrochner printf(" (bad reply from %s)\n", inet_ntoa(bootp->bp_siaddr)); 450bb2aeabeSdrochner return (-1); 4519a7e1c1dSdrochner } 4529a7e1c1dSdrochner 4535f399f5cSroy static void 4545f399f5cSroy bootp_addvend(u_char *area) 4555f399f5cSroy { 4565f399f5cSroy #ifdef NFS_BOOT_DHCP 4575f399f5cSroy char vci[64]; 4585f399f5cSroy int vcilen; 4595f399f5cSroy 4605f399f5cSroy *area++ = TAG_PARAM_REQ; 461753a25a9Scyber *area++ = 7; 4625f399f5cSroy *area++ = TAG_SUBNET_MASK; 4635f399f5cSroy *area++ = TAG_GATEWAY; 4645f399f5cSroy *area++ = TAG_HOST_NAME; 4655f399f5cSroy *area++ = TAG_DOMAIN_NAME; 4665f399f5cSroy *area++ = TAG_ROOT_PATH; 4675f399f5cSroy *area++ = TAG_SWAP_SERVER; 468753a25a9Scyber *area++ = TAG_INTERFACE_MTU; 4695f399f5cSroy 4705f399f5cSroy /* Insert a NetBSD Vendor Class Identifier option. */ 4715f399f5cSroy snprintf(vci, sizeof(vci), "%s:%s:kernel:%s", ostype, MACHINE, 4725f399f5cSroy osrelease); 4735f399f5cSroy vcilen = strlen(vci); 4745f399f5cSroy *area++ = TAG_CLASSID; 4755f399f5cSroy *area++ = vcilen; 4765f399f5cSroy (void)memcpy(area, vci, vcilen); 4775f399f5cSroy area += vcilen; 4785f399f5cSroy #endif 4795f399f5cSroy *area = TAG_END; 4805f399f5cSroy } 4815f399f5cSroy 48279c1e7caSgwr static int 483c4798840Scegger bootpc_call(struct nfs_diskless *nd, struct lwp *lwp, int *flags) 48479c1e7caSgwr { 48527098b11Sdrochner struct socket *so; 48627098b11Sdrochner struct ifnet *ifp = nd->nd_ifp; 48779c1e7caSgwr static u_int32_t xid = ~0xFF; 4889a7e1c1dSdrochner struct bootp *bootp; /* request */ 489d458601fSrtr struct mbuf *m; 490d458601fSrtr struct sockaddr_in sin; 4919a7e1c1dSdrochner int error; 4921751c510Sdyoung const u_char *haddr; 49379c1e7caSgwr u_char hafmt, halen; 4949a7e1c1dSdrochner struct bootpcontext bpc; 4950aeddffdSroy unsigned int index; 49679c1e7caSgwr 49715e29e98Sad error = socreate(AF_INET, &so, SOCK_DGRAM, 0, lwp, NULL); 49827098b11Sdrochner if (error) { 49927098b11Sdrochner printf("bootp: socreate, error=%d\n", error); 50027098b11Sdrochner return (error); 50127098b11Sdrochner } 50227098b11Sdrochner 50379c1e7caSgwr /* 50479c1e7caSgwr * Initialize to NULL anything that will hold an allocation, 50579c1e7caSgwr * and free each at the end if not null. 50679c1e7caSgwr */ 5079a7e1c1dSdrochner bpc.replybuf = NULL; 508d458601fSrtr m = NULL; 50979c1e7caSgwr 51079c1e7caSgwr /* Record our H/W (Ethernet) address. */ 511a3f7b0e7Sdyoung { const struct sockaddr_dl *sdl = ifp->if_sadl; 5122179b648Sdrochner switch (sdl->sdl_type) { 5138d994b82Sgmcgarry case IFT_ISO88025: 5148d994b82Sgmcgarry hafmt = HTYPE_IEEE802; 5158d994b82Sgmcgarry break; 5162179b648Sdrochner case IFT_ETHER: 5172179b648Sdrochner case IFT_FDDI: 5182179b648Sdrochner hafmt = HTYPE_ETHERNET; 5192179b648Sdrochner break; 5202179b648Sdrochner default: 5212179b648Sdrochner printf("bootp: unsupported interface type %d\n", 5222179b648Sdrochner sdl->sdl_type); 5232179b648Sdrochner error = EINVAL; 5242179b648Sdrochner goto out; 5252179b648Sdrochner } 52679c1e7caSgwr halen = sdl->sdl_alen; 5271751c510Sdyoung haddr = (const unsigned char *)CLLADDR(sdl); 52879c1e7caSgwr } 52979c1e7caSgwr 53079c1e7caSgwr /* 53179c1e7caSgwr * Skip the route table when sending on this socket. 53279c1e7caSgwr * If this is not done, ip_output finds the loopback 53379c1e7caSgwr * interface (why?) and then fails because broadcast 53479c1e7caSgwr * is not supported on that interface... 53579c1e7caSgwr */ 536fd7356a9Splunky { int32_t opt; 537fd7356a9Splunky 538fd7356a9Splunky opt = 1; 539fd7356a9Splunky error = so_setsockopt(NULL, so, SOL_SOCKET, SO_DONTROUTE, &opt, 540fd7356a9Splunky sizeof(opt)); 54179c1e7caSgwr } 54279c1e7caSgwr if (error) { 543c92b23ffSchristos DPRINTF(("bootpc_call: SO_DONTROUTE failed %d\n", error)); 54479c1e7caSgwr goto out; 54579c1e7caSgwr } 54679c1e7caSgwr 54779c1e7caSgwr /* Enable broadcast. */ 5489a7e1c1dSdrochner if ((error = nfs_boot_enbroadcast(so))) { 549c92b23ffSchristos DPRINTF(("bootpc_call: SO_BROADCAST failed %d\n", error)); 55079c1e7caSgwr goto out; 55179c1e7caSgwr } 55279c1e7caSgwr 55392ade669Sdrochner /* 55492ade669Sdrochner * Set some TTL so we can boot through routers. 55592ade669Sdrochner * Real BOOTP forwarding agents don't need this; they obey "bp_hops" 55692ade669Sdrochner * and set "bp_giaddr", thus rewrite the packet anyway. 55792ade669Sdrochner * The "helper-address" feature of some popular router vendor seems 55892ade669Sdrochner * to do simple IP forwarding and drops packets with (ip_ttl == 1). 55992ade669Sdrochner */ 560fd7356a9Splunky { u_char opt; 561fd7356a9Splunky 562fd7356a9Splunky opt = 7; 563fd7356a9Splunky error = so_setsockopt(NULL, so, IPPROTO_IP, IP_MULTICAST_TTL, 564fd7356a9Splunky &opt, sizeof(opt)); 56592ade669Sdrochner } 56692ade669Sdrochner if (error) { 567c92b23ffSchristos DPRINTF(("bootpc_call: IP_MULTICAST_TTL failed %d\n", error)); 56892ade669Sdrochner goto out; 56992ade669Sdrochner } 57092ade669Sdrochner 57179c1e7caSgwr /* Set the receive timeout for the socket. */ 5729a7e1c1dSdrochner if ((error = nfs_boot_setrecvtimo(so))) { 573c92b23ffSchristos DPRINTF(("bootpc_call: SO_RCVTIMEO failed %d\n", error)); 57479c1e7caSgwr goto out; 57579c1e7caSgwr } 57679c1e7caSgwr 57779c1e7caSgwr /* 57879c1e7caSgwr * Bind the local endpoint to a bootp client port. 57979c1e7caSgwr */ 58095e1ffb1Schristos if ((error = nfs_boot_sobind_ipport(so, IPPORT_BOOTPC, lwp))) { 581c92b23ffSchristos DPRINTF(("bootpc_call: bind failed %d\n", error)); 58279c1e7caSgwr goto out; 58379c1e7caSgwr } 58479c1e7caSgwr 58579c1e7caSgwr /* 58679c1e7caSgwr * Setup socket address for the server. 58779c1e7caSgwr */ 588d458601fSrtr sin.sin_len = sizeof(sin); 589d458601fSrtr sin.sin_family = AF_INET; 590d458601fSrtr sin.sin_addr.s_addr = INADDR_BROADCAST; 591d458601fSrtr sin.sin_port = htons(IPPORT_BOOTPS); 59279c1e7caSgwr 59379c1e7caSgwr /* 5949a7e1c1dSdrochner * Allocate buffer used for request 59579c1e7caSgwr */ 5969a7e1c1dSdrochner m = m_gethdr(M_WAIT, MT_DATA); 597*c94bf7cbSmlelstv MCLAIM(m, &nfs_mowner); 59865e5548aSmatt m_clget(m, M_WAIT); 5999a7e1c1dSdrochner bootp = mtod(m, struct bootp*); 6009a7e1c1dSdrochner m->m_pkthdr.len = m->m_len = BOOTP_SIZE_MAX; 601d938d837Sozaki-r m_reset_rcvif(m); 60279c1e7caSgwr 60379c1e7caSgwr /* 6049c474f32Sandvar * Build the BOOTP request message. 60579c1e7caSgwr * Note: xid is host order! (opaque to server) 60679c1e7caSgwr */ 60753524e44Schristos memset((void *)bootp, 0, BOOTP_SIZE_MAX); 60879c1e7caSgwr bootp->bp_op = BOOTREQUEST; 60979c1e7caSgwr bootp->bp_htype = hafmt; 61079c1e7caSgwr bootp->bp_hlen = halen; /* Hardware address length */ 61179c1e7caSgwr bootp->bp_xid = ++xid; 6124522c799Sperry memcpy(bootp->bp_chaddr, haddr, halen); 6138f6e0882Sdrochner #ifdef NFS_BOOT_BOOTP_REQFILE 6148f6e0882Sdrochner strncpy(bootp->bp_file, NFS_BOOT_BOOTP_REQFILE, sizeof(bootp->bp_file)); 6158f6e0882Sdrochner #endif 61679c1e7caSgwr /* Fill-in the vendor data. */ 6174522c799Sperry memcpy(bootp->bp_vend, vm_rfc1048, 4); 6180aeddffdSroy index = 4; 6195f399f5cSroy #ifdef NFS_BOOT_DHCP 6200aeddffdSroy bootp->bp_vend[index++] = TAG_DHCP_MSGTYPE; 6210aeddffdSroy bootp->bp_vend[index++] = 1; 6220aeddffdSroy bootp->bp_vend[index++] = DHCPDISCOVER; 6239a7e1c1dSdrochner #endif 6245f399f5cSroy bootp_addvend(&bootp->bp_vend[index]); 62579c1e7caSgwr 6269a7e1c1dSdrochner bpc.xid = xid; 6279a7e1c1dSdrochner bpc.haddr = haddr; 6289a7e1c1dSdrochner bpc.halen = halen; 6299a7e1c1dSdrochner bpc.replybuf = malloc(BOOTP_SIZE_MAX, M_DEVBUF, M_WAITOK); 6309a7e1c1dSdrochner if (bpc.replybuf == NULL) 6319a7e1c1dSdrochner panic("nfs_boot: malloc reply buf"); 6329a7e1c1dSdrochner #ifdef NFS_BOOT_DHCP 6339a7e1c1dSdrochner bpc.expected_dhcpmsgtype = DHCPOFFER; 6349a7e1c1dSdrochner bpc.dhcp_ok = 0; 6359a7e1c1dSdrochner #endif 63679c1e7caSgwr 637d458601fSrtr error = nfs_boot_sendrecv(so, &sin, bootpset, m, 6388b82ed45Srtr bootpcheck, NULL, NULL, &bpc, lwp); 63979c1e7caSgwr if (error) 64079c1e7caSgwr goto out; 6419a7e1c1dSdrochner 6429a7e1c1dSdrochner #ifdef NFS_BOOT_DHCP 6439a7e1c1dSdrochner if (bpc.dhcp_ok) { 6449a7e1c1dSdrochner u_int32_t leasetime; 6450aeddffdSroy index = 6; 6460aeddffdSroy bootp->bp_vend[index++] = DHCPREQUEST; 6470aeddffdSroy bootp->bp_vend[index++] = TAG_REQ_ADDR; 6480aeddffdSroy bootp->bp_vend[index++] = 4; 6490aeddffdSroy memcpy(&bootp->bp_vend[index], &bpc.replybuf->bp_yiaddr, 4); 6500aeddffdSroy index += 4; 6510aeddffdSroy bootp->bp_vend[index++] = TAG_SERVERID; 6520aeddffdSroy bootp->bp_vend[index++] = 4; 6530aeddffdSroy memcpy(&bootp->bp_vend[index], &bpc.dhcp_serverip.s_addr, 4); 6540aeddffdSroy index += 4; 6550aeddffdSroy bootp->bp_vend[index++] = TAG_LEASETIME; 6560aeddffdSroy bootp->bp_vend[index++] = 4; 6579a7e1c1dSdrochner leasetime = htonl(300); 6580aeddffdSroy memcpy(&bootp->bp_vend[index], &leasetime, 4); 6590aeddffdSroy index += 4; 6605f399f5cSroy bootp_addvend(&bootp->bp_vend[index]); 6619a7e1c1dSdrochner 6629a7e1c1dSdrochner bpc.expected_dhcpmsgtype = DHCPACK; 6639a7e1c1dSdrochner 664d458601fSrtr error = nfs_boot_sendrecv(so, &sin, bootpset, m, 6658b82ed45Srtr bootpcheck, NULL, NULL, &bpc, lwp); 6669a7e1c1dSdrochner if (error) 6679a7e1c1dSdrochner goto out; 6689a7e1c1dSdrochner } 6699a7e1c1dSdrochner #endif 67079c1e7caSgwr 67179c1e7caSgwr /* 6729a7e1c1dSdrochner * bootpcheck() has copied the receive mbuf into 6739a7e1c1dSdrochner * the buffer at bpc.replybuf. 67479c1e7caSgwr */ 675bb2aeabeSdrochner #ifdef NFS_BOOT_DHCP 676549a2548Skim printf("nfs_boot: %s next-server: %s\n", 677bb2aeabeSdrochner (bpc.dhcp_ok ? "DHCP" : "BOOTP"), 678bb2aeabeSdrochner #else 679549a2548Skim printf("nfs_boot: BOOTP next-server: %s\n", 680bb2aeabeSdrochner #endif 68141b78a54Sdrochner inet_ntoa(bpc.replybuf->bp_siaddr)); 682bb2aeabeSdrochner 683c4798840Scegger bootp_extract(bpc.replybuf, bpc.replylen, nd, flags); 6849a7e1c1dSdrochner 6859a7e1c1dSdrochner out: 6869a7e1c1dSdrochner if (bpc.replybuf) 6879a7e1c1dSdrochner free(bpc.replybuf, M_DEVBUF); 6889a7e1c1dSdrochner m_freem(m); 68927098b11Sdrochner soclose(so); 6909a7e1c1dSdrochner return (error); 69179c1e7caSgwr } 69279c1e7caSgwr 693bb2aeabeSdrochner static void 694c4798840Scegger bootp_extract(struct bootp *bootp, int replylen, 695c4798840Scegger struct nfs_diskless *nd, int *flags) 6969a7e1c1dSdrochner { 6979a7e1c1dSdrochner struct sockaddr_in *sin; 6989a7e1c1dSdrochner struct in_addr netmask; 6999a7e1c1dSdrochner struct in_addr gateway; 7009a7e1c1dSdrochner struct in_addr rootserver; 7019a7e1c1dSdrochner char *myname; /* my hostname */ 7029a7e1c1dSdrochner char *mydomain; /* my domainname */ 7039a7e1c1dSdrochner char *rootpath; 704753a25a9Scyber uint16_t myinterfacemtu; 7059a7e1c1dSdrochner int mynamelen; 7069a7e1c1dSdrochner int mydomainlen; 7079a7e1c1dSdrochner int rootpathlen; 708747ef674Senami int overloaded; 7099a7e1c1dSdrochner u_int tag, len; 7109a7e1c1dSdrochner u_char *p, *limit; 7119a7e1c1dSdrochner 71279c1e7caSgwr /* Default these to "unspecified". */ 71379c1e7caSgwr netmask.s_addr = 0; 71479c1e7caSgwr gateway.s_addr = 0; 71579c1e7caSgwr mydomain = myname = rootpath = NULL; 71679c1e7caSgwr mydomainlen = mynamelen = rootpathlen = 0; 71731b57f40Smanu 7189a7e1c1dSdrochner /* default root server to bootp next-server */ 7199a7e1c1dSdrochner rootserver = bootp->bp_siaddr; 720747ef674Senami /* assume that server name field is not overloaded by default */ 721747ef674Senami overloaded = 0; 722753a25a9Scyber /* MTU can't be less than IP_MIN_MTU, set to 0 to indicate unset */ 723753a25a9Scyber myinterfacemtu = 0; 7249a7e1c1dSdrochner 72579c1e7caSgwr p = &bootp->bp_vend[4]; 726e58a3edbSuwe limit = ((u_char*)bootp) + replylen; 72779c1e7caSgwr while (p < limit) { 72879c1e7caSgwr tag = *p++; 72979c1e7caSgwr if (tag == TAG_END) 73079c1e7caSgwr break; 73179c1e7caSgwr if (tag == TAG_PAD) 73279c1e7caSgwr continue; 73379c1e7caSgwr len = *p++; 734bb2aeabeSdrochner #if 0 /* already done in bootpcheck() */ 73579c1e7caSgwr if ((p + len) > limit) { 73679c1e7caSgwr printf("nfs_boot: option %d too long\n", tag); 73779c1e7caSgwr break; 73879c1e7caSgwr } 739bb2aeabeSdrochner #endif 74079c1e7caSgwr switch (tag) { 74179c1e7caSgwr case TAG_SUBNET_MASK: 742eb664608Sroy if (len < 4) { 743eb664608Sroy printf("nfs_boot: subnet mask < 4 bytes\n"); 744eb664608Sroy break; 745eb664608Sroy } 7464522c799Sperry memcpy(&netmask, p, 4); 74779c1e7caSgwr break; 74879c1e7caSgwr case TAG_GATEWAY: 74979c1e7caSgwr /* Routers */ 750eb664608Sroy if (len < 4) { 751eb664608Sroy printf("nfs_boot: routers < 4 bytes\n"); 752eb664608Sroy break; 753eb664608Sroy } 7544522c799Sperry memcpy(&gateway, p, 4); 75579c1e7caSgwr break; 75679c1e7caSgwr case TAG_HOST_NAME: 75779c1e7caSgwr if (len >= sizeof(hostname)) { 758eb664608Sroy printf("nfs_boot: host name >= %lu bytes\n", 759a4376d6aSthorpej (u_long)sizeof(hostname)); 76079c1e7caSgwr break; 76179c1e7caSgwr } 76279c1e7caSgwr myname = p; 76379c1e7caSgwr mynamelen = len; 76479c1e7caSgwr break; 76579c1e7caSgwr case TAG_DOMAIN_NAME: 76679c1e7caSgwr if (len >= sizeof(domainname)) { 767eb664608Sroy printf("nfs_boot: domain name >= %lu bytes\n", 768a4376d6aSthorpej (u_long)sizeof(domainname)); 76979c1e7caSgwr break; 77079c1e7caSgwr } 77179c1e7caSgwr mydomain = p; 77279c1e7caSgwr mydomainlen = len; 77379c1e7caSgwr break; 77479c1e7caSgwr case TAG_ROOT_PATH: 77579c1e7caSgwr /* Leave some room for the server name. */ 77679c1e7caSgwr if (len >= (MNAMELEN-10)) { 777eb664608Sroy printf("nfs_boot: rootpath >= %d bytes\n", 77879c1e7caSgwr (MNAMELEN-10)); 77979c1e7caSgwr break; 78079c1e7caSgwr } 78179c1e7caSgwr rootpath = p; 78279c1e7caSgwr rootpathlen = len; 78379c1e7caSgwr break; 784753a25a9Scyber case TAG_INTERFACE_MTU: 785753a25a9Scyber if (len != 2) { 786753a25a9Scyber printf("nfs_boot: interface-mtu len != 2 (%d)", 787753a25a9Scyber len); 788753a25a9Scyber break; 789753a25a9Scyber } 790753a25a9Scyber memcpy(&myinterfacemtu, p, 2); 791753a25a9Scyber myinterfacemtu = ntohs(myinterfacemtu); 792753a25a9Scyber break; 7939a7e1c1dSdrochner case TAG_SWAP_SERVER: 7949a7e1c1dSdrochner /* override NFS server address */ 795eb664608Sroy if (len < 4) { 796eb664608Sroy printf("nfs_boot: swap server < 4 bytes\n"); 797eb664608Sroy break; 798eb664608Sroy } 7994522c799Sperry memcpy(&rootserver, p, 4); 8009a7e1c1dSdrochner break; 801747ef674Senami #ifdef NFS_BOOT_DHCP 802747ef674Senami case TAG_OVERLOAD: 803747ef674Senami if (len > 0 && ((*p & 0x02) != 0)) 804747ef674Senami /* 805747ef674Senami * The server name field in the dhcp packet 806747ef674Senami * is overloaded and we can't find server 807747ef674Senami * name there. 808747ef674Senami */ 809747ef674Senami overloaded = 1; 810747ef674Senami break; 811747ef674Senami #endif 81279c1e7caSgwr default: 81379c1e7caSgwr break; 81479c1e7caSgwr } 81579c1e7caSgwr p += len; 81679c1e7caSgwr } 81779c1e7caSgwr 81879c1e7caSgwr /* 81979c1e7caSgwr * Store and print network config info. 82079c1e7caSgwr */ 82179c1e7caSgwr if (myname) { 82279c1e7caSgwr myname[mynamelen] = '\0'; 82379c1e7caSgwr strncpy(hostname, myname, sizeof(hostname)); 82479c1e7caSgwr hostnamelen = mynamelen; 82579c1e7caSgwr printf("nfs_boot: my_name=%s\n", hostname); 82679c1e7caSgwr } 82779c1e7caSgwr if (mydomain) { 82879c1e7caSgwr mydomain[mydomainlen] = '\0'; 82979c1e7caSgwr strncpy(domainname, mydomain, sizeof(domainname)); 83079c1e7caSgwr domainnamelen = mydomainlen; 83179c1e7caSgwr printf("nfs_boot: my_domain=%s\n", domainname); 83279c1e7caSgwr } 833c4798840Scegger if (!(*flags & NFS_BOOT_HAS_MYIP)) { 83479c1e7caSgwr nd->nd_myip = bootp->bp_yiaddr; 83541b78a54Sdrochner printf("nfs_boot: my_addr=%s\n", inet_ntoa(nd->nd_myip)); 836c4798840Scegger *flags |= NFS_BOOT_HAS_MYIP; 837c4798840Scegger } 838c4798840Scegger if (!(*flags & NFS_BOOT_HAS_MASK)) { 83979c1e7caSgwr nd->nd_mask = netmask; 84041b78a54Sdrochner printf("nfs_boot: my_mask=%s\n", inet_ntoa(nd->nd_mask)); 841c4798840Scegger *flags |= NFS_BOOT_HAS_MASK; 842c4798840Scegger } 843c4798840Scegger if (!(*flags & NFS_BOOT_HAS_GWIP)) { 84479c1e7caSgwr nd->nd_gwip = gateway; 84541b78a54Sdrochner printf("nfs_boot: gateway=%s\n", inet_ntoa(nd->nd_gwip)); 846c4798840Scegger *flags |= NFS_BOOT_HAS_GWIP; 847c4798840Scegger } 848753a25a9Scyber if (myinterfacemtu >= IP_MIN_MTU) { 849753a25a9Scyber nd->nd_mtu = myinterfacemtu; 850753a25a9Scyber printf("nfs_boot: mtu=%d\n", nd->nd_mtu); 851753a25a9Scyber } 85279c1e7caSgwr 85379c1e7caSgwr /* 85479c1e7caSgwr * Store the information about our NFS root mount. 85579c1e7caSgwr * The caller will print it, so be silent here. 85679c1e7caSgwr */ 857c4798840Scegger do { 85879c1e7caSgwr struct nfs_dlmount *ndm = &nd->nd_root; 85979c1e7caSgwr 860c4798840Scegger 861c4798840Scegger if (!(*flags & NFS_BOOT_HAS_SERVADDR)) { 86279c1e7caSgwr /* Server IP address. */ 86379c1e7caSgwr sin = (struct sockaddr_in *) &ndm->ndm_saddr; 86453524e44Schristos memset((void *)sin, 0, sizeof(*sin)); 86579c1e7caSgwr sin->sin_len = sizeof(*sin); 86679c1e7caSgwr sin->sin_family = AF_INET; 8679a7e1c1dSdrochner sin->sin_addr = rootserver; 868c4798840Scegger *flags |= NFS_BOOT_HAS_SERVADDR; 869c4798840Scegger } 870c4798840Scegger 871c4798840Scegger if (!(*flags & NFS_BOOT_HAS_SERVER)) { 87279c1e7caSgwr /* Server name. */ 873747ef674Senami if (!overloaded && bootp->bp_sname[0] != 0 && 874747ef674Senami !memcmp(&rootserver, &bootp->bp_siaddr, 875c4798840Scegger sizeof(struct in_addr))) 876c4798840Scegger { 8779a7e1c1dSdrochner /* standard root server, we have the name */ 878c4798840Scegger strncpy(ndm->ndm_host, bootp->bp_sname, 879c4798840Scegger BP_SNAME_LEN-1); 880c4798840Scegger *flags |= NFS_BOOT_HAS_SERVER; 8819a7e1c1dSdrochner } else { 88279c1e7caSgwr /* Show the server IP address numerically. */ 88341b78a54Sdrochner strncpy(ndm->ndm_host, inet_ntoa(rootserver), 88441b78a54Sdrochner BP_SNAME_LEN-1); 885c4798840Scegger *flags |= NFS_BOOT_HAS_SERVER; 88679c1e7caSgwr } 887c4798840Scegger } 888c4798840Scegger 889c4798840Scegger if (!(*flags & NFS_BOOT_HAS_ROOTPATH)) { 8909a7e1c1dSdrochner len = strlen(ndm->ndm_host); 8919a7e1c1dSdrochner if (rootpath && 892c4798840Scegger len + 1 + rootpathlen + 1 <= sizeof(ndm->ndm_host)) 893c4798840Scegger { 89479c1e7caSgwr ndm->ndm_host[len++] = ':'; 89579c1e7caSgwr strncpy(ndm->ndm_host + len, 8969a7e1c1dSdrochner rootpath, rootpathlen); 8979a7e1c1dSdrochner ndm->ndm_host[len + rootpathlen] = '\0'; 898c4798840Scegger *flags |= NFS_BOOT_HAS_ROOTPATH; 8999a7e1c1dSdrochner } /* else: upper layer will handle error */ 90079c1e7caSgwr } 901c4798840Scegger } while(0); 90231b57f40Smanu 90331b57f40Smanu #ifdef TFTPROOT 90431b57f40Smanu #if BP_FILE_LEN > MNAMELEN 90531b57f40Smanu #define BOOTFILELEN MNAMELEN 90631b57f40Smanu #else 90731b57f40Smanu #define BOOTFILELEN BP_FILE_LEN 90831b57f40Smanu #endif 90931b57f40Smanu strncpy(nd->nd_bootfile, bootp->bp_file, BOOTFILELEN); 91031b57f40Smanu nd->nd_bootfile[BOOTFILELEN - 1] = '\0'; 91131b57f40Smanu #undef BOOTFILELEN 91231b57f40Smanu #endif /* TFTPROOT */ 91379c1e7caSgwr } 914