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