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