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