bootp.c revision 1.5
1/* $OpenBSD: bootp.c,v 1.5 1996/10/16 14:18:35 mickey Exp $ */ 2/* $NetBSD: bootp.c,v 1.7 1995/09/18 21:19:20 pk Exp $ */ 3 4/* 5 * Copyright (c) 1992 Regents of the University of California. 6 * All rights reserved. 7 * 8 * This software was developed by the Computer Systems Engineering group 9 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 10 * contributed to Berkeley. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Lawrence Berkeley Laboratory and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * @(#) Header: bootp.c,v 1.4 93/09/11 03:13:51 leres Exp (LBL) 41 */ 42 43#include <sys/types.h> 44#include <netinet/in.h> 45#include <netinet/in_systm.h> 46 47#include "stand.h" 48#include "net.h" 49#include "netif.h" 50#include "bootp.h" 51 52static n_long nmask, smask; 53 54static time_t bot; 55 56static char vm_rfc1048[4] = VM_RFC1048; 57static char vm_cmu[4] = VM_CMU; 58 59/* Local forwards */ 60static ssize_t bootpsend __P((struct iodesc *, void *, size_t)); 61static ssize_t bootprecv __P((struct iodesc *, void *, size_t, time_t)); 62static void vend_cmu __P((u_char *)); 63static void vend_rfc1048 __P((u_char *, u_int)); 64 65/* Fetch required bootp infomation */ 66void 67bootp(sock) 68 int sock; 69{ 70 struct iodesc *d; 71 register struct bootp *bp; 72 struct { 73 u_char header[HEADER_SIZE]; 74 struct bootp wbootp; 75 } wbuf; 76 struct { 77 u_char header[HEADER_SIZE]; 78 struct bootp rbootp; 79 } rbuf; 80 81#ifdef BOOTP_DEBUG 82 if (debug) 83 printf("bootp: socket=%d\n", sock); 84#endif 85 if (!bot) 86 bot = getsecs(); 87 88 if (!(d = socktodesc(sock))) { 89 printf("bootp: bad socket. %d\n", sock); 90 return; 91 } 92#ifdef BOOTP_DEBUG 93 if (debug) 94 printf("bootp: d=%x\n", (u_int)d); 95#endif 96 97 bp = &wbuf.wbootp; 98 bzero(bp, sizeof(*bp)); 99 100 bp->bp_op = BOOTREQUEST; 101 bp->bp_htype = HTYPE_ETHERNET; /* 10Mb Ethernet (48 bits) */ 102 bp->bp_hlen = 6; 103 bp->bp_xid = htonl(d->xid); 104 MACPY(d->myea, bp->bp_chaddr); 105 bzero(bp->bp_file, sizeof(bp->bp_file)); 106 bcopy(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)); 107 108 d->myip = myip; 109 d->myport = htons(IPPORT_BOOTPC); 110 d->destip.s_addr = INADDR_BROADCAST; 111 d->destport = htons(IPPORT_BOOTPS); 112 113 (void)sendrecv(d, 114 bootpsend, bp, sizeof(*bp), 115 bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp)); 116 117 /* Bump xid so next request will be unique. */ 118 ++d->xid; 119} 120 121/* Transmit a bootp request */ 122static ssize_t 123bootpsend(d, pkt, len) 124 register struct iodesc *d; 125 register void *pkt; 126 register size_t len; 127{ 128 register struct bootp *bp; 129 130#ifdef BOOTP_DEBUG 131 if (debug) 132 printf("bootpsend: d=%x called.\n", (u_int)d); 133#endif 134 135 bp = pkt; 136 bp->bp_secs = htons((u_short)(getsecs() - bot)); 137 138#ifdef BOOTP_DEBUG 139 if (debug) 140 printf("bootpsend: calling sendudp\n"); 141#endif 142 143 return (sendudp(d, pkt, len)); 144} 145 146/* Returns 0 if this is the packet we're waiting for else -1 (and errno == 0) */ 147static ssize_t 148bootprecv(d, pkt, len, tleft) 149 register struct iodesc *d; 150 register void *pkt; 151 register size_t len; 152 time_t tleft; 153{ 154 register ssize_t n; 155 register struct bootp *bp; 156 157#ifdef BOOTP_DEBUG 158 if (debug) 159 printf("bootprecv: called\n"); 160#endif 161 162 n = readudp(d, pkt, len, tleft); 163 if (n == -1 || n < sizeof(struct bootp)) 164 goto bad; 165 166 bp = (struct bootp *)pkt; 167 168#ifdef BOOTP_DEBUG 169 if (debug) 170 printf("bootprecv: checked. bp = 0x%x, n = %d\n", 171 (unsigned)bp, n); 172#endif 173 if (bp->bp_xid != htonl(d->xid)) { 174#ifdef BOOTP_DEBUG 175 if (debug) { 176 printf("bootprecv: expected xid 0x%lx, got 0x%lx\n", 177 d->xid, ntohl(bp->bp_xid)); 178 } 179#endif 180 goto bad; 181 } 182 183#ifdef BOOTP_DEBUG 184 if (debug) 185 printf("bootprecv: got one!\n"); 186#endif 187 188 /* Pick up our ip address (and natural netmask) */ 189 myip = d->myip = bp->bp_yiaddr; 190#ifdef BOOTP_DEBUG 191 if (debug) 192 printf("our ip address is %s\n", inet_ntoa(d->myip)); 193#endif 194 if (IN_CLASSA(d->myip.s_addr)) 195 nmask = IN_CLASSA_NET; 196 else if (IN_CLASSB(d->myip.s_addr)) 197 nmask = IN_CLASSB_NET; 198 else 199 nmask = IN_CLASSC_NET; 200#ifdef BOOTP_DEBUG 201 if (debug) 202 printf("'native netmask' is %s\n", intoa(nmask)); 203#endif 204 205 /* Pick up root or swap server address and file spec. */ 206 if (bp->bp_siaddr.s_addr != 0) 207 rootip = bp->bp_siaddr; 208 if (bp->bp_file[0] != '\0') { 209 strncpy(bootfile, (char *)bp->bp_file, sizeof(bootfile)); 210 bootfile[sizeof(bootfile) - 1] = '\0'; 211 } 212 213 /* Suck out vendor info */ 214 if (bcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0) 215 vend_cmu(bp->bp_vend); 216 else if (bcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0) 217 vend_rfc1048(bp->bp_vend, sizeof(bp->bp_vend)); 218 else 219 printf("bootprecv: unknown vendor 0x%lx\n", (long)bp->bp_vend); 220 221 /* Check subnet mask against net mask; toss if bogus */ 222 if ((nmask & smask) != nmask) { 223#ifdef BOOTP_DEBUG 224 if (debug) 225 printf("subnet mask (%s) bad\n", intoa(smask)); 226#endif 227 smask = 0; 228 } 229 230 /* Get subnet (or natural net) mask */ 231 netmask = nmask; 232 if (smask) 233 netmask = smask; 234#ifdef BOOTP_DEBUG 235 if (debug) 236 printf("mask: %s\n", intoa(netmask)); 237#endif 238 239 /* We need a gateway if root or swap is on a different net */ 240 if (!SAMENET(d->myip, rootip, netmask)) { 241#ifdef BOOTP_DEBUG 242 if (debug) 243 printf("need gateway for root ip\n"); 244#endif 245 } 246 247 if (!SAMENET(d->myip, swapip, netmask)) { 248#ifdef BOOTP_DEBUG 249 if (debug) 250 printf("need gateway for swap ip\n"); 251#endif 252 } 253 254 /* Toss gateway if on a different net */ 255 if (!SAMENET(d->myip, gateip, netmask)) { 256#ifdef BOOTP_DEBUG 257 if (debug) 258 printf("gateway ip (%s) bad\n", inet_ntoa(gateip)); 259#endif 260 gateip.s_addr = 0; 261 } 262 263 return (n); 264 265bad: 266 errno = 0; 267 return (-1); 268} 269 270static void 271vend_cmu(cp) 272 u_char *cp; 273{ 274 register struct cmu_vend *vp; 275 276#ifdef BOOTP_DEBUG 277 if (debug) 278 printf("vend_cmu bootp info.\n"); 279#endif 280 vp = (struct cmu_vend *)cp; 281 282 if (vp->v_smask.s_addr != 0) { 283 smask = vp->v_smask.s_addr; 284 } 285 if (vp->v_dgate.s_addr != 0) { 286 gateip = vp->v_dgate; 287 } 288} 289 290static void 291vend_rfc1048(cp, len) 292 register u_char *cp; 293 u_int len; 294{ 295 register u_char *ep; 296 register int size; 297 register u_char tag; 298 299#ifdef BOOTP_DEBUG 300 if (debug) 301 printf("vend_rfc1048 bootp info. len=%d\n", len); 302#endif 303 ep = cp + len; 304 305 /* Step over magic cookie */ 306 cp += sizeof(long); 307 308 while (cp < ep) { 309 tag = *cp++; 310 size = *cp++; 311 if (tag == TAG_END) 312 break; 313 314 if (tag == TAG_SUBNET_MASK) { 315 bcopy(cp, &smask, sizeof(smask)); 316 } 317 if (tag == TAG_GATEWAY) { 318 bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr)); 319 } 320 if (tag == TAG_SWAPSERVER) { 321 bcopy(cp, &swapip.s_addr, sizeof(swapip.s_addr)); 322 } 323 if (tag == TAG_DOMAIN_SERVER) { 324 bcopy(cp, &nameip.s_addr, sizeof(nameip.s_addr)); 325 } 326 if (tag == TAG_ROOTPATH) { 327 strncpy(rootpath, (char *)cp, sizeof(rootpath)); 328 rootpath[size] = '\0'; 329 } 330 if (tag == TAG_HOSTNAME) { 331 strncpy(hostname, (char *)cp, sizeof(hostname)); 332 hostname[size] = '\0'; 333 } 334 if (tag == TAG_DOMAINNAME) { 335 strncpy(domainname, (char *)cp, sizeof(domainname)); 336 domainname[size] = '\0'; 337 } 338 cp += size; 339 } 340} 341