Home | History | Annotate | Line # | Download | only in libsa
bootp.c revision 1.13.2.2
      1  1.13.2.2   mycroft /*	$NetBSD: bootp.c,v 1.13.2.2 1998/05/08 06:59:25 mycroft Exp $	*/
      2       1.2       cgd 
      3       1.1    brezak /*
      4       1.1    brezak  * Copyright (c) 1992 Regents of the University of California.
      5       1.1    brezak  * All rights reserved.
      6       1.1    brezak  *
      7       1.1    brezak  * This software was developed by the Computer Systems Engineering group
      8       1.1    brezak  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9       1.1    brezak  * contributed to Berkeley.
     10       1.1    brezak  *
     11       1.1    brezak  * Redistribution and use in source and binary forms, with or without
     12       1.1    brezak  * modification, are permitted provided that the following conditions
     13       1.1    brezak  * are met:
     14       1.1    brezak  * 1. Redistributions of source code must retain the above copyright
     15       1.1    brezak  *    notice, this list of conditions and the following disclaimer.
     16       1.1    brezak  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.1    brezak  *    notice, this list of conditions and the following disclaimer in the
     18       1.1    brezak  *    documentation and/or other materials provided with the distribution.
     19       1.1    brezak  * 3. All advertising materials mentioning features or use of this software
     20       1.1    brezak  *    must display the following acknowledgement:
     21       1.1    brezak  *	This product includes software developed by the University of
     22       1.1    brezak  *	California, Lawrence Berkeley Laboratory and its contributors.
     23       1.1    brezak  * 4. Neither the name of the University nor the names of its contributors
     24       1.1    brezak  *    may be used to endorse or promote products derived from this software
     25       1.1    brezak  *    without specific prior written permission.
     26       1.1    brezak  *
     27       1.1    brezak  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28       1.1    brezak  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29       1.1    brezak  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30       1.1    brezak  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31       1.1    brezak  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32       1.1    brezak  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33       1.1    brezak  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34       1.1    brezak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35       1.1    brezak  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36       1.1    brezak  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37       1.1    brezak  * SUCH DAMAGE.
     38       1.1    brezak  *
     39       1.2       cgd  * @(#) Header: bootp.c,v 1.4 93/09/11 03:13:51 leres Exp  (LBL)
     40       1.1    brezak  */
     41       1.1    brezak 
     42       1.1    brezak #include <sys/types.h>
     43       1.1    brezak #include <netinet/in.h>
     44       1.1    brezak #include <netinet/in_systm.h>
     45       1.1    brezak 
     46      1.11  drochner #ifdef _STANDALONE
     47      1.11  drochner #include <lib/libkern/libkern.h>
     48      1.11  drochner #else
     49       1.1    brezak #include <string.h>
     50      1.11  drochner #endif
     51       1.1    brezak 
     52       1.1    brezak #include "stand.h"
     53       1.1    brezak #include "net.h"
     54       1.1    brezak #include "netif.h"
     55       1.1    brezak #include "bootp.h"
     56       1.1    brezak 
     57      1.13  drochner struct in_addr servip;
     58      1.13  drochner 
     59       1.1    brezak static n_long	nmask, smask;
     60       1.1    brezak 
     61       1.1    brezak static time_t	bot;
     62       1.1    brezak 
     63       1.1    brezak static	char vm_rfc1048[4] = VM_RFC1048;
     64      1.12  drochner #ifdef BOOTP_VEND_CMU
     65       1.1    brezak static	char vm_cmu[4] = VM_CMU;
     66      1.12  drochner #endif
     67       1.1    brezak 
     68       1.1    brezak /* Local forwards */
     69       1.6        pk static	ssize_t bootpsend __P((struct iodesc *, void *, size_t));
     70       1.6        pk static	ssize_t bootprecv __P((struct iodesc *, void *, size_t, time_t));
     71      1.12  drochner static	int vend_rfc1048 __P((u_char *, u_int));
     72      1.12  drochner #ifdef BOOTP_VEND_CMU
     73       1.1    brezak static	void vend_cmu __P((u_char *));
     74      1.12  drochner #endif
     75      1.12  drochner 
     76      1.12  drochner #ifdef SUPPORT_DHCP
     77      1.12  drochner static char expected_dhcpmsgtype = -1, dhcp_ok;
     78      1.12  drochner struct in_addr dhcp_serverip;
     79      1.12  drochner #endif
     80       1.1    brezak 
     81       1.1    brezak /* Fetch required bootp infomation */
     82       1.1    brezak void
     83       1.1    brezak bootp(sock)
     84       1.1    brezak 	int sock;
     85       1.1    brezak {
     86       1.1    brezak 	struct iodesc *d;
     87       1.1    brezak 	register struct bootp *bp;
     88       1.1    brezak 	struct {
     89       1.1    brezak 		u_char header[HEADER_SIZE];
     90       1.1    brezak 		struct bootp wbootp;
     91       1.1    brezak 	} wbuf;
     92       1.3   mycroft 	struct {
     93       1.3   mycroft 		u_char header[HEADER_SIZE];
     94       1.3   mycroft 		struct bootp rbootp;
     95       1.1    brezak 	} rbuf;
     96       1.1    brezak 
     97       1.1    brezak #ifdef BOOTP_DEBUG
     98       1.1    brezak  	if (debug)
     99      1.10  christos 		printf("bootp: socket=%d\n", sock);
    100       1.1    brezak #endif
    101       1.1    brezak 	if (!bot)
    102       1.1    brezak 		bot = getsecs();
    103       1.1    brezak 
    104       1.1    brezak 	if (!(d = socktodesc(sock))) {
    105      1.10  christos 		printf("bootp: bad socket. %d\n", sock);
    106       1.1    brezak 		return;
    107       1.1    brezak 	}
    108       1.1    brezak #ifdef BOOTP_DEBUG
    109       1.1    brezak  	if (debug)
    110      1.12  drochner 		printf("bootp: d=%lx\n", (long)d);
    111       1.1    brezak #endif
    112       1.4   mycroft 
    113       1.1    brezak 	bp = &wbuf.wbootp;
    114       1.1    brezak 	bzero(bp, sizeof(*bp));
    115       1.1    brezak 
    116       1.1    brezak 	bp->bp_op = BOOTREQUEST;
    117       1.1    brezak 	bp->bp_htype = 1;		/* 10Mb Ethernet (48 bits) */
    118       1.1    brezak 	bp->bp_hlen = 6;
    119       1.7        pk 	bp->bp_xid = htonl(d->xid);
    120       1.1    brezak 	MACPY(d->myea, bp->bp_chaddr);
    121      1.12  drochner 	strncpy(bp->bp_file, bootfile, sizeof(bp->bp_file));
    122       1.4   mycroft 	bcopy(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048));
    123      1.12  drochner #ifdef SUPPORT_DHCP
    124      1.12  drochner 	bp->bp_vend[4] = TAG_DHCP_MSGTYPE;
    125      1.12  drochner 	bp->bp_vend[5] = 1;
    126      1.12  drochner 	bp->bp_vend[6] = DHCPDISCOVER;
    127      1.12  drochner 	bp->bp_vend[7] = TAG_END;
    128      1.12  drochner #else
    129      1.12  drochner 	bp->bp_vend[4] = TAG_END;
    130      1.12  drochner #endif
    131       1.1    brezak 
    132  1.13.2.2   mycroft 	d->myip.s_addr = INADDR_ANY;
    133       1.7        pk 	d->myport = htons(IPPORT_BOOTPC);
    134       1.7        pk 	d->destip.s_addr = INADDR_BROADCAST;
    135       1.7        pk 	d->destport = htons(IPPORT_BOOTPS);
    136       1.1    brezak 
    137      1.12  drochner #ifdef SUPPORT_DHCP
    138      1.12  drochner 	expected_dhcpmsgtype = DHCPOFFER;
    139      1.12  drochner 	dhcp_ok = 0;
    140      1.12  drochner #endif
    141      1.12  drochner 
    142  1.13.2.1   mycroft 	if (sendrecv(d,
    143      1.12  drochner 		    bootpsend, bp, sizeof(*bp),
    144      1.12  drochner 		    bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp))
    145      1.12  drochner 	   == -1) {
    146  1.13.2.1   mycroft 		printf("bootp: no reply\n");
    147  1.13.2.1   mycroft 		return;
    148      1.12  drochner 	}
    149      1.12  drochner 
    150      1.12  drochner #ifdef SUPPORT_DHCP
    151  1.13.2.1   mycroft 	if (dhcp_ok) {
    152      1.12  drochner 		u_int32_t leasetime;
    153      1.12  drochner 		bp->bp_vend[6] = DHCPREQUEST;
    154      1.12  drochner 		bp->bp_vend[7] = TAG_REQ_ADDR;
    155      1.12  drochner 		bp->bp_vend[8] = 4;
    156      1.12  drochner 		bcopy(&rbuf.rbootp.bp_yiaddr, &bp->bp_vend[9], 4);
    157      1.12  drochner 		bp->bp_vend[13] = TAG_SERVERID;
    158      1.12  drochner 		bp->bp_vend[14] = 4;
    159      1.12  drochner 		bcopy(&dhcp_serverip.s_addr, &bp->bp_vend[15], 4);
    160      1.12  drochner 		bp->bp_vend[19] = TAG_LEASETIME;
    161      1.12  drochner 		bp->bp_vend[20] = 4;
    162      1.12  drochner 		leasetime = htonl(300);
    163      1.12  drochner 		bcopy(&leasetime, &bp->bp_vend[21], 4);
    164      1.12  drochner 		bp->bp_vend[25] = TAG_END;
    165      1.12  drochner 
    166      1.12  drochner 		expected_dhcpmsgtype = DHCPACK;
    167      1.12  drochner 
    168  1.13.2.1   mycroft 		if (sendrecv(d,
    169      1.12  drochner 			    bootpsend, bp, sizeof(*bp),
    170      1.12  drochner 			    bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp))
    171      1.12  drochner 		   == -1) {
    172      1.12  drochner 			printf("DHCPREQUEST failed\n");
    173      1.12  drochner 			return;
    174      1.12  drochner 		}
    175      1.12  drochner 	}
    176      1.12  drochner #endif
    177      1.12  drochner 
    178      1.12  drochner 	myip = d->myip = rbuf.rbootp.bp_yiaddr;
    179      1.13  drochner 	servip = rbuf.rbootp.bp_siaddr;
    180  1.13.2.1   mycroft 	if (rootip.s_addr == INADDR_ANY)
    181  1.13.2.1   mycroft 		rootip = servip;
    182      1.12  drochner 	bcopy(rbuf.rbootp.bp_file, bootfile, sizeof(bootfile));
    183      1.12  drochner 	bootfile[sizeof(bootfile) - 1] = '\0';
    184      1.12  drochner 
    185      1.12  drochner 	if (IN_CLASSA(myip.s_addr))
    186      1.12  drochner 		nmask = IN_CLASSA_NET;
    187      1.12  drochner 	else if (IN_CLASSB(myip.s_addr))
    188      1.12  drochner 		nmask = IN_CLASSB_NET;
    189      1.12  drochner 	else
    190      1.12  drochner 		nmask = IN_CLASSC_NET;
    191      1.12  drochner #ifdef BOOTP_DEBUG
    192      1.12  drochner 	if (debug)
    193      1.12  drochner 		printf("'native netmask' is %s\n", intoa(nmask));
    194      1.12  drochner #endif
    195      1.12  drochner 
    196      1.12  drochner 	/* Check subnet mask against net mask; toss if bogus */
    197      1.12  drochner 	if ((nmask & smask) != nmask) {
    198      1.12  drochner #ifdef BOOTP_DEBUG
    199      1.12  drochner 		if (debug)
    200      1.12  drochner 			printf("subnet mask (%s) bad\n", intoa(smask));
    201      1.12  drochner #endif
    202      1.12  drochner 		smask = 0;
    203      1.12  drochner 	}
    204      1.12  drochner 
    205      1.12  drochner 	/* Get subnet (or natural net) mask */
    206      1.12  drochner 	netmask = nmask;
    207      1.12  drochner 	if (smask)
    208      1.12  drochner 		netmask = smask;
    209      1.12  drochner #ifdef BOOTP_DEBUG
    210      1.12  drochner 	if (debug)
    211      1.12  drochner 		printf("mask: %s\n", intoa(netmask));
    212      1.12  drochner #endif
    213      1.12  drochner 
    214      1.12  drochner 	/* We need a gateway if root is on a different net */
    215      1.12  drochner 	if (!SAMENET(myip, rootip, netmask)) {
    216      1.12  drochner #ifdef BOOTP_DEBUG
    217      1.12  drochner 		if (debug)
    218      1.12  drochner 			printf("need gateway for root ip\n");
    219      1.12  drochner #endif
    220      1.12  drochner 	}
    221      1.12  drochner 
    222      1.12  drochner 	/* Toss gateway if on a different net */
    223      1.12  drochner 	if (!SAMENET(myip, gateip, netmask)) {
    224      1.12  drochner #ifdef BOOTP_DEBUG
    225      1.12  drochner 		if (debug)
    226      1.12  drochner 			printf("gateway ip (%s) bad\n", inet_ntoa(gateip));
    227      1.12  drochner #endif
    228      1.12  drochner 		gateip.s_addr = 0;
    229      1.12  drochner 	}
    230       1.4   mycroft 
    231       1.4   mycroft 	/* Bump xid so next request will be unique. */
    232       1.4   mycroft 	++d->xid;
    233       1.1    brezak }
    234       1.1    brezak 
    235       1.1    brezak /* Transmit a bootp request */
    236       1.6        pk static ssize_t
    237       1.1    brezak bootpsend(d, pkt, len)
    238       1.1    brezak 	register struct iodesc *d;
    239       1.1    brezak 	register void *pkt;
    240       1.3   mycroft 	register size_t len;
    241       1.1    brezak {
    242       1.1    brezak 	register struct bootp *bp;
    243       1.1    brezak 
    244       1.1    brezak #ifdef BOOTP_DEBUG
    245       1.1    brezak 	if (debug)
    246      1.12  drochner 		printf("bootpsend: d=%lx called.\n", (long)d);
    247       1.1    brezak #endif
    248       1.3   mycroft 
    249       1.1    brezak 	bp = pkt;
    250       1.7        pk 	bp->bp_secs = htons((u_short)(getsecs() - bot));
    251       1.3   mycroft 
    252       1.1    brezak #ifdef BOOTP_DEBUG
    253       1.1    brezak 	if (debug)
    254      1.10  christos 		printf("bootpsend: calling sendudp\n");
    255       1.1    brezak #endif
    256       1.3   mycroft 
    257       1.1    brezak 	return (sendudp(d, pkt, len));
    258       1.1    brezak }
    259       1.1    brezak 
    260       1.6        pk static ssize_t
    261       1.3   mycroft bootprecv(d, pkt, len, tleft)
    262  1.13.2.1   mycroft 	register struct iodesc *d;
    263  1.13.2.1   mycroft 	register void *pkt;
    264  1.13.2.1   mycroft 	register size_t len;
    265  1.13.2.1   mycroft 	time_t tleft;
    266       1.1    brezak {
    267       1.6        pk 	register ssize_t n;
    268       1.1    brezak 	register struct bootp *bp;
    269       1.1    brezak 
    270      1.12  drochner #ifdef BOOTP_DEBUGx
    271       1.1    brezak 	if (debug)
    272      1.12  drochner 		printf("bootp_recvoffer: called\n");
    273       1.1    brezak #endif
    274       1.3   mycroft 
    275       1.6        pk 	n = readudp(d, pkt, len, tleft);
    276      1.12  drochner 	if (n == -1 || n < sizeof(struct bootp) - BOOTP_VENDSIZE)
    277       1.3   mycroft 		goto bad;
    278       1.3   mycroft 
    279       1.3   mycroft 	bp = (struct bootp *)pkt;
    280      1.12  drochner 
    281       1.1    brezak #ifdef BOOTP_DEBUG
    282       1.1    brezak 	if (debug)
    283      1.12  drochner 		printf("bootprecv: checked.  bp = 0x%lx, n = %d\n",
    284      1.12  drochner 		    (long)bp, (int)n);
    285       1.1    brezak #endif
    286       1.7        pk 	if (bp->bp_xid != htonl(d->xid)) {
    287       1.1    brezak #ifdef BOOTP_DEBUG
    288       1.1    brezak 		if (debug) {
    289      1.12  drochner 			printf("bootprecv: expected xid 0x%lx, got 0x%x\n",
    290       1.7        pk 			    d->xid, ntohl(bp->bp_xid));
    291       1.1    brezak 		}
    292       1.1    brezak #endif
    293       1.3   mycroft 		goto bad;
    294       1.1    brezak 	}
    295       1.1    brezak 
    296  1.13.2.1   mycroft 	/* protect against bogus addresses sent by DHCP servers */
    297  1.13.2.1   mycroft 	if (bp->bp_yiaddr.s_addr == INADDR_ANY ||
    298  1.13.2.1   mycroft 	    bp->bp_yiaddr.s_addr == INADDR_BROADCAST)
    299  1.13.2.1   mycroft 		goto bad;
    300  1.13.2.1   mycroft 
    301       1.1    brezak #ifdef BOOTP_DEBUG
    302       1.1    brezak 	if (debug)
    303      1.10  christos 		printf("bootprecv: got one!\n");
    304       1.1    brezak #endif
    305       1.1    brezak 
    306      1.12  drochner 	/* Suck out vendor info */
    307      1.12  drochner 	if (bcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0) {
    308  1.13.2.1   mycroft 		if (vend_rfc1048(bp->bp_vend, sizeof(bp->bp_vend)) != 0)
    309  1.13.2.1   mycroft 			goto bad;
    310       1.1    brezak 	}
    311      1.12  drochner #ifdef BOOTP_VEND_CMU
    312      1.12  drochner 	else if (bcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0)
    313       1.1    brezak 		vend_cmu(bp->bp_vend);
    314      1.12  drochner #endif
    315       1.1    brezak 	else
    316      1.10  christos 		printf("bootprecv: unknown vendor 0x%lx\n", (long)bp->bp_vend);
    317       1.1    brezak 
    318  1.13.2.1   mycroft 	return (n);
    319       1.3   mycroft bad:
    320       1.3   mycroft 	errno = 0;
    321       1.3   mycroft 	return (-1);
    322       1.1    brezak }
    323       1.1    brezak 
    324      1.12  drochner static int
    325       1.1    brezak vend_rfc1048(cp, len)
    326       1.1    brezak 	register u_char *cp;
    327       1.1    brezak 	u_int len;
    328       1.1    brezak {
    329       1.1    brezak 	register u_char *ep;
    330       1.1    brezak 	register int size;
    331       1.1    brezak 	register u_char tag;
    332       1.1    brezak 
    333       1.1    brezak #ifdef BOOTP_DEBUG
    334       1.1    brezak 	if (debug)
    335      1.10  christos 		printf("vend_rfc1048 bootp info. len=%d\n", len);
    336       1.1    brezak #endif
    337       1.1    brezak 	ep = cp + len;
    338       1.1    brezak 
    339       1.1    brezak 	/* Step over magic cookie */
    340       1.8       cgd 	cp += sizeof(int);
    341       1.1    brezak 
    342       1.1    brezak 	while (cp < ep) {
    343       1.1    brezak 		tag = *cp++;
    344       1.1    brezak 		size = *cp++;
    345       1.1    brezak 		if (tag == TAG_END)
    346       1.1    brezak 			break;
    347       1.1    brezak 
    348       1.1    brezak 		if (tag == TAG_SUBNET_MASK) {
    349       1.1    brezak 			bcopy(cp, &smask, sizeof(smask));
    350       1.1    brezak 		}
    351       1.1    brezak 		if (tag == TAG_GATEWAY) {
    352       1.7        pk 			bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr));
    353       1.1    brezak 		}
    354       1.1    brezak 		if (tag == TAG_SWAPSERVER) {
    355      1.12  drochner 			/* let it override bp_siaddr */
    356      1.12  drochner 			bcopy(cp, &rootip.s_addr, sizeof(swapip.s_addr));
    357       1.1    brezak 		}
    358       1.1    brezak 		if (tag == TAG_ROOTPATH) {
    359       1.7        pk 			strncpy(rootpath, (char *)cp, sizeof(rootpath));
    360       1.1    brezak 			rootpath[size] = '\0';
    361       1.1    brezak 		}
    362       1.1    brezak 		if (tag == TAG_HOSTNAME) {
    363       1.7        pk 			strncpy(hostname, (char *)cp, sizeof(hostname));
    364       1.1    brezak 			hostname[size] = '\0';
    365       1.1    brezak 		}
    366      1.12  drochner #ifdef SUPPORT_DHCP
    367      1.12  drochner 		if (tag == TAG_DHCP_MSGTYPE) {
    368  1.13.2.1   mycroft 			if (*cp != expected_dhcpmsgtype)
    369  1.13.2.1   mycroft 				return (-1);
    370      1.12  drochner 			dhcp_ok = 1;
    371      1.12  drochner 		}
    372      1.12  drochner 		if (tag == TAG_SERVERID) {
    373      1.12  drochner 			bcopy(cp, &dhcp_serverip.s_addr,
    374      1.12  drochner 			      sizeof(dhcp_serverip.s_addr));
    375       1.1    brezak 		}
    376      1.12  drochner #endif
    377       1.1    brezak 		cp += size;
    378       1.1    brezak 	}
    379  1.13.2.1   mycroft 	return (0);
    380       1.1    brezak }
    381      1.12  drochner 
    382      1.12  drochner #ifdef BOOTP_VEND_CMU
    383      1.12  drochner static void
    384      1.12  drochner vend_cmu(cp)
    385      1.12  drochner 	u_char *cp;
    386      1.12  drochner {
    387      1.12  drochner 	register struct cmu_vend *vp;
    388      1.12  drochner 
    389      1.12  drochner #ifdef BOOTP_DEBUG
    390      1.12  drochner 	if (debug)
    391      1.12  drochner 		printf("vend_cmu bootp info.\n");
    392      1.12  drochner #endif
    393      1.12  drochner 	vp = (struct cmu_vend *)cp;
    394      1.12  drochner 
    395      1.12  drochner 	if (vp->v_smask.s_addr != 0) {
    396      1.12  drochner 		smask = vp->v_smask.s_addr;
    397      1.12  drochner 	}
    398      1.12  drochner 	if (vp->v_dgate.s_addr != 0) {
    399      1.12  drochner 		gateip = vp->v_dgate;
    400      1.12  drochner 	}
    401      1.12  drochner }
    402      1.12  drochner #endif
    403