bootp.c revision 84221
1/* $NetBSD: bootp.c,v 1.14 1998/02/16 11:10:54 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/cdefs.h> 43__FBSDID("$FreeBSD: head/lib/libstand/bootp.c 84221 2001-09-30 22:28:01Z dillon $"); 44 45#include <sys/types.h> 46#include <netinet/in.h> 47#include <netinet/in_systm.h> 48 49#include <string.h> 50 51#define BOOTP_DEBUGxx 52#define SUPPORT_DHCP 53 54#include "stand.h" 55#include "net.h" 56#include "netif.h" 57#include "bootp.h" 58 59 60struct in_addr servip; 61 62static n_long nmask, smask; 63 64static time_t bot; 65 66static char vm_rfc1048[4] = VM_RFC1048; 67#ifdef BOOTP_VEND_CMU 68static char vm_cmu[4] = VM_CMU; 69#endif 70 71/* Local forwards */ 72static ssize_t bootpsend(struct iodesc *, void *, size_t); 73static ssize_t bootprecv(struct iodesc *, void *, size_t, time_t); 74static int vend_rfc1048(u_char *, u_int); 75#ifdef BOOTP_VEND_CMU 76static void vend_cmu(u_char *); 77#endif 78 79#ifdef SUPPORT_DHCP 80static char expected_dhcpmsgtype = -1, dhcp_ok; 81struct in_addr dhcp_serverip; 82#endif 83 84/* Fetch required bootp infomation */ 85void 86bootp(sock, flag) 87 int sock; 88 int flag; 89{ 90 struct iodesc *d; 91 register struct bootp *bp; 92 struct { 93 u_char header[HEADER_SIZE]; 94 struct bootp wbootp; 95 } wbuf; 96 struct { 97 u_char header[HEADER_SIZE]; 98 struct bootp rbootp; 99 } rbuf; 100 101#ifdef BOOTP_DEBUG 102 if (debug) 103 printf("bootp: socket=%d\n", sock); 104#endif 105 if (!bot) 106 bot = getsecs(); 107 108 if (!(d = socktodesc(sock))) { 109 printf("bootp: bad socket. %d\n", sock); 110 return; 111 } 112#ifdef BOOTP_DEBUG 113 if (debug) 114 printf("bootp: d=%lx\n", (long)d); 115#endif 116 117 bp = &wbuf.wbootp; 118 bzero(bp, sizeof(*bp)); 119 120 bp->bp_op = BOOTREQUEST; 121 bp->bp_htype = 1; /* 10Mb Ethernet (48 bits) */ 122 bp->bp_hlen = 6; 123 bp->bp_xid = htonl(d->xid); 124 MACPY(d->myea, bp->bp_chaddr); 125 strncpy(bp->bp_file, bootfile, sizeof(bp->bp_file)); 126 bcopy(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)); 127#ifdef SUPPORT_DHCP 128 bp->bp_vend[4] = TAG_DHCP_MSGTYPE; 129 bp->bp_vend[5] = 1; 130 bp->bp_vend[6] = DHCPDISCOVER; 131 132 /* 133 * If we are booting from PXE, we want to send the string 134 * 'PXEClient' to the DHCP server so you have the option of 135 * only responding to PXE aware dhcp requests. 136 */ 137 if (flag & BOOTP_PXE) { 138 bp->bp_vend[7] = TAG_CLASSID; 139 bp->bp_vend[8] = 9; 140 bcopy("PXEClient", &bp->bp_vend[9], 9); 141 bp->bp_vend[18] = TAG_END; 142 } else 143 bp->bp_vend[7] = TAG_END; 144#else 145 bp->bp_vend[4] = TAG_END; 146#endif 147 148 d->myip.s_addr = INADDR_ANY; 149 d->myport = htons(IPPORT_BOOTPC); 150 d->destip.s_addr = INADDR_BROADCAST; 151 d->destport = htons(IPPORT_BOOTPS); 152 153#ifdef SUPPORT_DHCP 154 expected_dhcpmsgtype = DHCPOFFER; 155 dhcp_ok = 0; 156#endif 157 158 if(sendrecv(d, 159 bootpsend, bp, sizeof(*bp), 160 bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp)) 161 == -1) { 162 printf("bootp: no reply\n"); 163 return; 164 } 165 166#ifdef SUPPORT_DHCP 167 if(dhcp_ok) { 168 u_int32_t leasetime; 169 bp->bp_vend[6] = DHCPREQUEST; 170 bp->bp_vend[7] = TAG_REQ_ADDR; 171 bp->bp_vend[8] = 4; 172 bcopy(&rbuf.rbootp.bp_yiaddr, &bp->bp_vend[9], 4); 173 bp->bp_vend[13] = TAG_SERVERID; 174 bp->bp_vend[14] = 4; 175 bcopy(&dhcp_serverip.s_addr, &bp->bp_vend[15], 4); 176 bp->bp_vend[19] = TAG_LEASETIME; 177 bp->bp_vend[20] = 4; 178 leasetime = htonl(300); 179 bcopy(&leasetime, &bp->bp_vend[21], 4); 180 if (flag & BOOTP_PXE) { 181 bp->bp_vend[25] = TAG_CLASSID; 182 bp->bp_vend[26] = 9; 183 bcopy("PXEClient", &bp->bp_vend[27], 9); 184 bp->bp_vend[36] = TAG_END; 185 } else 186 bp->bp_vend[25] = TAG_END; 187 188 expected_dhcpmsgtype = DHCPACK; 189 190 if(sendrecv(d, 191 bootpsend, bp, sizeof(*bp), 192 bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp)) 193 == -1) { 194 printf("DHCPREQUEST failed\n"); 195 return; 196 } 197 } 198#endif 199 200 myip = d->myip = rbuf.rbootp.bp_yiaddr; 201 servip = rbuf.rbootp.bp_siaddr; 202 if(rootip.s_addr == INADDR_ANY) rootip = servip; 203 bcopy(rbuf.rbootp.bp_file, bootfile, sizeof(bootfile)); 204 bootfile[sizeof(bootfile) - 1] = '\0'; 205 206 if (IN_CLASSA(ntohl(myip.s_addr))) 207 nmask = htonl(IN_CLASSA_NET); 208 else if (IN_CLASSB(ntohl(myip.s_addr))) 209 nmask = htonl(IN_CLASSB_NET); 210 else 211 nmask = htonl(IN_CLASSC_NET); 212#ifdef BOOTP_DEBUG 213 if (debug) 214 printf("'native netmask' is %s\n", intoa(nmask)); 215#endif 216 217 /* Check subnet mask against net mask; toss if bogus */ 218 if ((nmask & smask) != nmask) { 219#ifdef BOOTP_DEBUG 220 if (debug) 221 printf("subnet mask (%s) bad\n", intoa(smask)); 222#endif 223 smask = 0; 224 } 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 /* Bump xid so next request will be unique. */ 253 ++d->xid; 254} 255 256/* Transmit a bootp request */ 257static ssize_t 258bootpsend(d, pkt, len) 259 register struct iodesc *d; 260 register void *pkt; 261 register size_t len; 262{ 263 register struct bootp *bp; 264 265#ifdef BOOTP_DEBUG 266 if (debug) 267 printf("bootpsend: d=%lx called.\n", (long)d); 268#endif 269 270 bp = pkt; 271 bp->bp_secs = htons((u_short)(getsecs() - bot)); 272 273#ifdef BOOTP_DEBUG 274 if (debug) 275 printf("bootpsend: calling sendudp\n"); 276#endif 277 278 return (sendudp(d, pkt, len)); 279} 280 281static ssize_t 282bootprecv(d, pkt, len, tleft) 283register struct iodesc *d; 284register void *pkt; 285register size_t len; 286time_t tleft; 287{ 288 register ssize_t n; 289 register struct bootp *bp; 290 291#ifdef BOOTP_DEBUGx 292 if (debug) 293 printf("bootp_recvoffer: called\n"); 294#endif 295 296 n = readudp(d, pkt, len, tleft); 297 if (n == -1 || n < sizeof(struct bootp) - BOOTP_VENDSIZE) 298 goto bad; 299 300 bp = (struct bootp *)pkt; 301 302#ifdef BOOTP_DEBUG 303 if (debug) 304 printf("bootprecv: checked. bp = 0x%lx, n = %d\n", 305 (long)bp, (int)n); 306#endif 307 if (bp->bp_xid != htonl(d->xid)) { 308#ifdef BOOTP_DEBUG 309 if (debug) { 310 printf("bootprecv: expected xid 0x%lx, got 0x%x\n", 311 d->xid, ntohl(bp->bp_xid)); 312 } 313#endif 314 goto bad; 315 } 316 317#ifdef BOOTP_DEBUG 318 if (debug) 319 printf("bootprecv: got one!\n"); 320#endif 321 322 /* Suck out vendor info */ 323 if (bcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0) { 324 if(vend_rfc1048(bp->bp_vend, sizeof(bp->bp_vend)) != 0) 325 goto bad; 326 } 327#ifdef BOOTP_VEND_CMU 328 else if (bcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0) 329 vend_cmu(bp->bp_vend); 330#endif 331 else 332 printf("bootprecv: unknown vendor 0x%lx\n", (long)bp->bp_vend); 333 334 return(n); 335bad: 336 errno = 0; 337 return (-1); 338} 339 340static int 341vend_rfc1048(cp, len) 342 register u_char *cp; 343 u_int len; 344{ 345 register u_char *ep; 346 register int size; 347 register u_char tag; 348 349#ifdef BOOTP_DEBUG 350 if (debug) 351 printf("vend_rfc1048 bootp info. len=%d\n", len); 352#endif 353 ep = cp + len; 354 355 /* Step over magic cookie */ 356 cp += sizeof(int); 357 358 while (cp < ep) { 359 tag = *cp++; 360 size = *cp++; 361 if (tag == TAG_END) 362 break; 363 364 if (tag == TAG_SUBNET_MASK) { 365 bcopy(cp, &smask, sizeof(smask)); 366 } 367 if (tag == TAG_GATEWAY) { 368 bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr)); 369 } 370 if (tag == TAG_SWAPSERVER) { 371 /* let it override bp_siaddr */ 372 bcopy(cp, &rootip.s_addr, sizeof(swapip.s_addr)); 373 } 374 if (tag == TAG_ROOTPATH) { 375 strncpy(rootpath, (char *)cp, sizeof(rootpath)); 376 rootpath[size] = '\0'; 377 } 378 if (tag == TAG_HOSTNAME) { 379 strncpy(hostname, (char *)cp, sizeof(hostname)); 380 hostname[size] = '\0'; 381 } 382#ifdef SUPPORT_DHCP 383 if (tag == TAG_DHCP_MSGTYPE) { 384 if(*cp != expected_dhcpmsgtype) 385 return(-1); 386 dhcp_ok = 1; 387 } 388 if (tag == TAG_SERVERID) { 389 bcopy(cp, &dhcp_serverip.s_addr, 390 sizeof(dhcp_serverip.s_addr)); 391 } 392#endif 393 cp += size; 394 } 395 return(0); 396} 397 398#ifdef BOOTP_VEND_CMU 399static void 400vend_cmu(cp) 401 u_char *cp; 402{ 403 register struct cmu_vend *vp; 404 405#ifdef BOOTP_DEBUG 406 if (debug) 407 printf("vend_cmu bootp info.\n"); 408#endif 409 vp = (struct cmu_vend *)cp; 410 411 if (vp->v_smask.s_addr != 0) { 412 smask = vp->v_smask.s_addr; 413 } 414 if (vp->v_dgate.s_addr != 0) { 415 gateip = vp->v_dgate; 416 } 417} 418#endif 419