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