1 /* $NetBSD: bootp.c,v 1.22 2002/03/20 23:10:39 thorpej 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/param.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 struct in_addr servip; 58 #ifdef SUPPORT_LINUX 59 char linuxcmdline[256]; 60 #ifndef TAG_LINUX_CMDLINE 61 #define TAG_LINUX_CMDLINE 123 62 #endif 63 #endif 64 65 static n_long nmask, smask; 66 67 static time_t bot; 68 69 static char vm_rfc1048[4] = VM_RFC1048; 70 #ifdef BOOTP_VEND_CMU 71 static char vm_cmu[4] = VM_CMU; 72 #endif 73 74 /* Local forwards */ 75 static ssize_t bootpsend __P((struct iodesc *, void *, size_t)); 76 static ssize_t bootprecv __P((struct iodesc *, void *, size_t, time_t)); 77 static int vend_rfc1048 __P((u_char *, u_int)); 78 #ifdef BOOTP_VEND_CMU 79 static void vend_cmu __P((u_char *)); 80 #endif 81 82 #ifdef SUPPORT_DHCP 83 static char expected_dhcpmsgtype = -1, dhcp_ok; 84 struct in_addr dhcp_serverip; 85 #endif 86 87 /* 88 * Boot programs can patch this at run-time to change the behavior 89 * of bootp/dhcp. 90 */ 91 int bootp_flags; 92 93 /* Fetch required bootp information */ 94 void 95 bootp(sock) 96 int sock; 97 { 98 struct iodesc *d; 99 struct bootp *bp; 100 struct { 101 u_char header[HEADER_SIZE]; 102 struct bootp wbootp; 103 } wbuf; 104 struct { 105 u_char header[HEADER_SIZE]; 106 struct bootp rbootp; 107 } rbuf; 108 #ifdef SUPPORT_DHCP 109 char vci[64]; 110 int vcilen; 111 #endif 112 113 #ifdef BOOTP_DEBUG 114 if (debug) 115 printf("bootp: socket=%d\n", sock); 116 #endif 117 if (!bot) 118 bot = getsecs(); 119 120 if (!(d = socktodesc(sock))) { 121 printf("bootp: bad socket. %d\n", sock); 122 return; 123 } 124 #ifdef BOOTP_DEBUG 125 if (debug) 126 printf("bootp: d=%lx\n", (long)d); 127 #endif 128 129 bp = &wbuf.wbootp; 130 bzero(bp, sizeof(*bp)); 131 132 bp->bp_op = BOOTREQUEST; 133 bp->bp_htype = 1; /* 10Mb Ethernet (48 bits) */ 134 bp->bp_hlen = 6; 135 bp->bp_xid = htonl(d->xid); 136 MACPY(d->myea, bp->bp_chaddr); 137 strncpy(bp->bp_file, bootfile, sizeof(bp->bp_file)); 138 bcopy(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)); 139 #ifdef SUPPORT_DHCP 140 bp->bp_vend[4] = TAG_DHCP_MSGTYPE; 141 bp->bp_vend[5] = 1; 142 bp->bp_vend[6] = DHCPDISCOVER; 143 /* 144 * Insert a NetBSD Vendor Class Identifier option. 145 */ 146 sprintf(vci, "NetBSD:%s:libsa", MACHINE); 147 vcilen = strlen(vci); 148 bp->bp_vend[7] = TAG_CLASSID; 149 bp->bp_vend[8] = vcilen; 150 bcopy(vci, &bp->bp_vend[9], vcilen); 151 bp->bp_vend[9 + vcilen] = TAG_END; 152 #else 153 bp->bp_vend[4] = TAG_END; 154 #endif 155 156 d->myip.s_addr = INADDR_ANY; 157 d->myport = htons(IPPORT_BOOTPC); 158 d->destip.s_addr = INADDR_BROADCAST; 159 d->destport = htons(IPPORT_BOOTPS); 160 161 #ifdef SUPPORT_DHCP 162 expected_dhcpmsgtype = DHCPOFFER; 163 dhcp_ok = 0; 164 #endif 165 166 if (sendrecv(d, 167 bootpsend, bp, sizeof(*bp), 168 bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp)) 169 == -1) { 170 printf("bootp: no reply\n"); 171 return; 172 } 173 174 #ifdef SUPPORT_DHCP 175 if (dhcp_ok) { 176 u_int32_t leasetime; 177 bp->bp_vend[6] = DHCPREQUEST; 178 bp->bp_vend[7] = TAG_REQ_ADDR; 179 bp->bp_vend[8] = 4; 180 bcopy(&rbuf.rbootp.bp_yiaddr, &bp->bp_vend[9], 4); 181 bp->bp_vend[13] = TAG_SERVERID; 182 bp->bp_vend[14] = 4; 183 bcopy(&dhcp_serverip.s_addr, &bp->bp_vend[15], 4); 184 bp->bp_vend[19] = TAG_LEASETIME; 185 bp->bp_vend[20] = 4; 186 leasetime = htonl(300); 187 bcopy(&leasetime, &bp->bp_vend[21], 4); 188 /* 189 * Insert a NetBSD Vendor Class Identifier option. 190 */ 191 sprintf(vci, "NetBSD:%s:libsa", MACHINE); 192 vcilen = strlen(vci); 193 bp->bp_vend[25] = TAG_CLASSID; 194 bp->bp_vend[26] = vcilen; 195 bcopy(vci, &bp->bp_vend[27], vcilen); 196 bp->bp_vend[27 + vcilen] = TAG_END; 197 198 expected_dhcpmsgtype = DHCPACK; 199 200 if (sendrecv(d, 201 bootpsend, bp, sizeof(*bp), 202 bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp)) 203 == -1) { 204 printf("DHCPREQUEST failed\n"); 205 return; 206 } 207 } 208 #endif 209 210 myip = d->myip = rbuf.rbootp.bp_yiaddr; 211 servip = rbuf.rbootp.bp_siaddr; 212 if (rootip.s_addr == INADDR_ANY) 213 rootip = servip; 214 bcopy(rbuf.rbootp.bp_file, bootfile, sizeof(bootfile)); 215 bootfile[sizeof(bootfile) - 1] = '\0'; 216 217 if (IN_CLASSA(myip.s_addr)) 218 nmask = IN_CLASSA_NET; 219 else if (IN_CLASSB(myip.s_addr)) 220 nmask = IN_CLASSB_NET; 221 else 222 nmask = IN_CLASSC_NET; 223 #ifdef BOOTP_DEBUG 224 if (debug) 225 printf("'native netmask' is %s\n", intoa(nmask)); 226 #endif 227 228 /* Check subnet mask against net mask; toss if bogus */ 229 if ((nmask & smask) != nmask) { 230 #ifdef BOOTP_DEBUG 231 if (debug) 232 printf("subnet mask (%s) bad\n", intoa(smask)); 233 #endif 234 smask = 0; 235 } 236 237 /* Get subnet (or natural net) mask */ 238 netmask = nmask; 239 if (smask) 240 netmask = smask; 241 #ifdef BOOTP_DEBUG 242 if (debug) 243 printf("mask: %s\n", intoa(netmask)); 244 #endif 245 246 /* We need a gateway if root is on a different net */ 247 if (!SAMENET(myip, rootip, netmask)) { 248 #ifdef BOOTP_DEBUG 249 if (debug) 250 printf("need gateway for root ip\n"); 251 #endif 252 } 253 254 /* Toss gateway if on a different net */ 255 if (!SAMENET(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 printf("net_open: client addr: %s\n", inet_ntoa(myip)); 264 if (smask) 265 printf("net_open: subnet mask: %s\n", intoa(smask)); 266 if (gateip.s_addr != 0) 267 printf("net_open: net gateway: %s\n", inet_ntoa(gateip)); 268 printf("net_open: server addr: %s\n", inet_ntoa(rootip)); 269 if (rootpath[0] != '\0') 270 printf("net_open: server path: %s\n", rootpath); 271 if (bootfile[0] != '\0') 272 printf("net_open: file name: %s\n", bootfile); 273 274 /* Bump xid so next request will be unique. */ 275 ++d->xid; 276 } 277 278 /* Transmit a bootp request */ 279 static ssize_t 280 bootpsend(d, pkt, len) 281 struct iodesc *d; 282 void *pkt; 283 size_t len; 284 { 285 struct bootp *bp; 286 287 #ifdef BOOTP_DEBUG 288 if (debug) 289 printf("bootpsend: d=%lx called.\n", (long)d); 290 #endif 291 292 bp = pkt; 293 bp->bp_secs = htons((u_short)(getsecs() - bot)); 294 295 #ifdef BOOTP_DEBUG 296 if (debug) 297 printf("bootpsend: calling sendudp\n"); 298 #endif 299 300 return (sendudp(d, pkt, len)); 301 } 302 303 static ssize_t 304 bootprecv(d, pkt, len, tleft) 305 struct iodesc *d; 306 void *pkt; 307 size_t len; 308 time_t tleft; 309 { 310 ssize_t n; 311 struct bootp *bp; 312 313 #ifdef BOOTP_DEBUGx 314 if (debug) 315 printf("bootp_recvoffer: called\n"); 316 #endif 317 318 n = readudp(d, pkt, len, tleft); 319 if (n == -1 || n < sizeof(struct bootp) - BOOTP_VENDSIZE) 320 goto bad; 321 322 bp = (struct bootp *)pkt; 323 324 #ifdef BOOTP_DEBUG 325 if (debug) 326 printf("bootprecv: checked. bp = 0x%lx, n = %d\n", 327 (long)bp, (int)n); 328 #endif 329 if (bp->bp_xid != htonl(d->xid)) { 330 #ifdef BOOTP_DEBUG 331 if (debug) { 332 printf("bootprecv: expected xid 0x%lx, got 0x%x\n", 333 d->xid, ntohl(bp->bp_xid)); 334 } 335 #endif 336 goto bad; 337 } 338 339 /* protect against bogus addresses sent by DHCP servers */ 340 if (bp->bp_yiaddr.s_addr == INADDR_ANY || 341 bp->bp_yiaddr.s_addr == INADDR_BROADCAST) 342 goto bad; 343 344 #ifdef BOOTP_DEBUG 345 if (debug) 346 printf("bootprecv: got one!\n"); 347 #endif 348 349 /* Suck out vendor info */ 350 if (bcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0) { 351 if (vend_rfc1048(bp->bp_vend, sizeof(bp->bp_vend)) != 0) 352 goto bad; 353 } 354 #ifdef BOOTP_VEND_CMU 355 else if (bcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0) 356 vend_cmu(bp->bp_vend); 357 #endif 358 else 359 printf("bootprecv: unknown vendor 0x%lx\n", (long)bp->bp_vend); 360 361 return (n); 362 bad: 363 errno = 0; 364 return (-1); 365 } 366 367 static int 368 vend_rfc1048(cp, len) 369 u_char *cp; 370 u_int len; 371 { 372 u_char *ep; 373 int size; 374 u_char tag; 375 376 #ifdef BOOTP_DEBUG 377 if (debug) 378 printf("vend_rfc1048 bootp info. len=%d\n", len); 379 #endif 380 ep = cp + len; 381 382 /* Step over magic cookie */ 383 cp += sizeof(int); 384 385 while (cp < ep) { 386 tag = *cp++; 387 size = *cp++; 388 if (tag == TAG_END) 389 break; 390 391 if (tag == TAG_SUBNET_MASK) { 392 bcopy(cp, &smask, sizeof(smask)); 393 } 394 if (tag == TAG_GATEWAY) { 395 bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr)); 396 } 397 if (tag == TAG_SWAPSERVER) { 398 /* let it override bp_siaddr */ 399 bcopy(cp, &rootip.s_addr, sizeof(rootip.s_addr)); 400 } 401 if (tag == TAG_ROOTPATH) { 402 strncpy(rootpath, (char *)cp, sizeof(rootpath)); 403 rootpath[size] = '\0'; 404 } 405 if (tag == TAG_HOSTNAME) { 406 strncpy(hostname, (char *)cp, sizeof(hostname)); 407 hostname[size] = '\0'; 408 } 409 #ifdef SUPPORT_DHCP 410 if (tag == TAG_DHCP_MSGTYPE) { 411 if (*cp != expected_dhcpmsgtype) 412 return (-1); 413 dhcp_ok = 1; 414 } 415 if (tag == TAG_SERVERID) { 416 bcopy(cp, &dhcp_serverip.s_addr, 417 sizeof(dhcp_serverip.s_addr)); 418 } 419 #endif 420 #ifdef SUPPORT_LINUX 421 if (tag == TAG_LINUX_CMDLINE) { 422 strncpy(linuxcmdline, (char *)cp, sizeof(linuxcmdline)); 423 linuxcmdline[size] = '\0'; 424 } 425 #endif 426 cp += size; 427 } 428 return (0); 429 } 430 431 #ifdef BOOTP_VEND_CMU 432 static void 433 vend_cmu(cp) 434 u_char *cp; 435 { 436 struct cmu_vend *vp; 437 438 #ifdef BOOTP_DEBUG 439 if (debug) 440 printf("vend_cmu bootp info.\n"); 441 #endif 442 vp = (struct cmu_vend *)cp; 443 444 if (vp->v_smask.s_addr != 0) { 445 smask = vp->v_smask.s_addr; 446 } 447 if (vp->v_dgate.s_addr != 0) { 448 gateip = vp->v_dgate; 449 } 450 } 451 #endif 452