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