xref: /netbsd-src/sys/lib/libsa/bootp.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 /*	$NetBSD: bootp.c,v 1.4 1995/02/21 10:14:53 mycroft 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 <string.h>
47 
48 #include "stand.h"
49 #include "net.h"
50 #include "netif.h"
51 #include "bootp.h"
52 
53 static n_long	nmask, smask;
54 
55 static time_t	bot;
56 
57 static	char vm_rfc1048[4] = VM_RFC1048;
58 static	char vm_cmu[4] = VM_CMU;
59 
60 /* Local forwards */
61 static	size_t bootpsend __P((struct iodesc *, void *, size_t));
62 static	size_t bootprecv __P((struct iodesc *, void *, size_t, time_t));
63 static	void vend_cmu __P((u_char *));
64 static	void vend_rfc1048 __P((u_char *, u_int));
65 
66 /* Fetch required bootp infomation */
67 void
68 bootp(sock)
69 	int sock;
70 {
71 	struct iodesc *d;
72 	register struct bootp *bp;
73 	struct {
74 		u_char header[HEADER_SIZE];
75 		struct bootp wbootp;
76 	} wbuf;
77 	struct {
78 		u_char header[HEADER_SIZE];
79 		struct bootp rbootp;
80 	} rbuf;
81 
82 #ifdef BOOTP_DEBUG
83  	if (debug)
84 		printf("bootp: socket=%d\n", sock);
85 #endif
86 	if (!bot)
87 		bot = getsecs();
88 
89 	if (!(d = socktodesc(sock))) {
90 		printf("bootp: bad socket. %d\n", sock);
91 		return;
92 	}
93 #ifdef BOOTP_DEBUG
94  	if (debug)
95 		printf("bootp: d=%x\n", (u_int)d);
96 #endif
97 
98 	bp = &wbuf.wbootp;
99 	bzero(bp, sizeof(*bp));
100 
101 	bp->bp_op = BOOTREQUEST;
102 	bp->bp_htype = 1;		/* 10Mb Ethernet (48 bits) */
103 	bp->bp_hlen = 6;
104 	bp->bp_xid = d->xid;
105 	MACPY(d->myea, bp->bp_chaddr);
106 	bzero(bp->bp_file, sizeof(bp->bp_file));
107 	bcopy(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048));
108 
109 	d->myip = myip;
110 	d->myport = IPPORT_BOOTPC;
111 	d->destip = INADDR_BROADCAST;
112 	d->destport = IPPORT_BOOTPS;
113 
114 	(void)sendrecv(d,
115 	    bootpsend, bp, sizeof(*bp),
116 	    bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp));
117 
118 	/* Bump xid so next request will be unique. */
119 	++d->xid;
120 }
121 
122 /* Transmit a bootp request */
123 static size_t
124 bootpsend(d, pkt, len)
125 	register struct iodesc *d;
126 	register void *pkt;
127 	register size_t len;
128 {
129 	register struct bootp *bp;
130 
131 #ifdef BOOTP_DEBUG
132 	if (debug)
133 		printf("bootpsend: d=%x called.\n", (u_int)d);
134 #endif
135 
136 	bp = pkt;
137 	bp->bp_secs = (u_long)(getsecs() - bot);
138 
139 #ifdef BOOTP_DEBUG
140 	if (debug)
141 		printf("bootpsend: calling sendudp\n");
142 #endif
143 
144 	return (sendudp(d, pkt, len));
145 }
146 
147 /* Returns 0 if this is the packet we're waiting for else -1 (and errno == 0) */
148 static size_t
149 bootprecv(d, pkt, len, tleft)
150 	register struct iodesc *d;
151 	register void *pkt;
152 	register size_t len;
153 	time_t tleft;
154 {
155 	register struct bootp *bp;
156 
157 #ifdef BOOTP_DEBUG
158 	if (debug)
159 		printf("bootprecv: called\n");
160 #endif
161 
162 	len = readudp(d, pkt, len, tleft);
163 	if (len == -1 || len < sizeof(struct bootp))
164 		goto bad;
165 
166 	bp = (struct bootp *)pkt;
167 	NTOHL(bp->bp_xid);
168 
169 #ifdef BOOTP_DEBUG
170 	if (debug)
171 		printf("bootprecv: checked.  bp = 0x%x, len = %d\n",
172 		    (unsigned)bp, len);
173 #endif
174 	if (bp->bp_xid != d->xid) {
175 #ifdef BOOTP_DEBUG
176 		if (debug) {
177 			printf("bootprecv: expected xid 0x%x, got 0x%x\n",
178 			    d->xid, bp->bp_xid);
179 		}
180 #endif
181 		goto bad;
182 	}
183 
184 #ifdef BOOTP_DEBUG
185 	if (debug)
186 		printf("bootprecv: got one!\n");
187 #endif
188 
189 	/* Pick up our ip address (and natural netmask) */
190 	myip = d->myip = ntohl(bp->bp_yiaddr.s_addr);
191 #ifdef BOOTP_DEBUG
192 	if (debug)
193 		printf("our ip address is %s\n", intoa(d->myip));
194 #endif
195 	if (IN_CLASSA(d->myip))
196 		nmask = IN_CLASSA_NET;
197 	else if (IN_CLASSB(d->myip))
198 		nmask = IN_CLASSB_NET;
199 	else
200 		nmask = IN_CLASSC_NET;
201 #ifdef BOOTP_DEBUG
202 	if (debug)
203 		printf("'native netmask' is %s\n", intoa(nmask));
204 #endif
205 
206 	/* Pick up root or swap server address and file spec. */
207 	if (bp->bp_siaddr.s_addr != 0)
208 		rootip = ntohl(bp->bp_siaddr.s_addr);
209 	if (bp->bp_file[0] != '\0') {
210 		strncpy(bootfile, (char *)bp->bp_file, sizeof(bootfile));
211 		bootfile[sizeof(bootfile) - 1] = '\0';
212 	}
213 
214 	/* Suck out vendor info */
215 	if (bcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0)
216 		vend_cmu(bp->bp_vend);
217 	else if (bcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0)
218 		vend_rfc1048(bp->bp_vend, sizeof(bp->bp_vend));
219 	else
220 		printf("bootprecv: unknown vendor 0x%x\n", (int)bp->bp_vend);
221 
222 	/* Check subnet mask against net mask; toss if bogus */
223 	if ((nmask & smask) != nmask) {
224 #ifdef BOOTP_DEBUG
225 		if (debug)
226 			printf("subnet mask (%s) bad\n", intoa(smask));
227 #endif
228 		smask = 0;
229 	}
230 
231 	/* Get subnet (or natural net) mask */
232 	mask = nmask;
233 	if (smask)
234 		mask = smask;
235 #ifdef BOOTP_DEBUG
236 	if (debug)
237 		printf("mask: %s\n", intoa(mask));
238 #endif
239 
240 	/* We need a gateway if root or swap is on a different net */
241 	if (!SAMENET(d->myip, rootip, mask)) {
242 #ifdef BOOTP_DEBUG
243 		if (debug)
244 			printf("need gateway for root ip\n");
245 #endif
246 	}
247 
248 	if (!SAMENET(d->myip, swapip, mask)) {
249 #ifdef BOOTP_DEBUG
250 		if (debug)
251 			printf("need gateway for swap ip\n");
252 #endif
253 	}
254 
255 	/* Toss gateway if on a different net */
256 	if (!SAMENET(d->myip, gateip, mask)) {
257 #ifdef BOOTP_DEBUG
258 		if (debug)
259 			printf("gateway ip (%s) bad\n", intoa(gateip));
260 #endif
261 		gateip = 0;
262 	}
263 
264 	return (0);
265 
266 bad:
267 	errno = 0;
268 	return (-1);
269 }
270 
271 static void
272 vend_cmu(cp)
273 	u_char *cp;
274 {
275 	register struct cmu_vend *vp;
276 
277 #ifdef BOOTP_DEBUG
278 	if (debug)
279 		printf("vend_cmu bootp info.\n");
280 #endif
281 	vp = (struct cmu_vend *)cp;
282 
283 	if (vp->v_smask.s_addr != 0) {
284 		smask = ntohl(vp->v_smask.s_addr);
285 	}
286 	if (vp->v_dgate.s_addr != 0) {
287 		gateip = ntohl(vp->v_dgate.s_addr);
288 	}
289 }
290 
291 static void
292 vend_rfc1048(cp, len)
293 	register u_char *cp;
294 	u_int len;
295 {
296 	register u_char *ep;
297 	register int size;
298 	register u_char tag;
299 
300 #ifdef BOOTP_DEBUG
301 	if (debug)
302 		printf("vend_rfc1048 bootp info. len=%d\n", len);
303 #endif
304 	ep = cp + len;
305 
306 	/* Step over magic cookie */
307 	cp += sizeof(long);
308 
309 	while (cp < ep) {
310 		tag = *cp++;
311 		size = *cp++;
312 		if (tag == TAG_END)
313 			break;
314 
315 		if (tag == TAG_SUBNET_MASK) {
316 			bcopy(cp, &smask, sizeof(smask));
317 			smask = ntohl(smask);
318 		}
319 		if (tag == TAG_GATEWAY) {
320 			bcopy(cp, &gateip, sizeof(gateip));
321 			gateip = ntohl(gateip);
322 		}
323 		if (tag == TAG_SWAPSERVER) {
324 			bcopy(cp, &swapip, sizeof(swapip));
325 			swapip = ntohl(swapip);
326 		}
327 		if (tag == TAG_DOMAIN_SERVER) {
328 			bcopy(cp, &nameip, sizeof(nameip));
329 			nameip = ntohl(nameip);
330 		}
331 		if (tag == TAG_ROOTPATH) {
332 			strncpy(rootpath, cp, sizeof(rootpath));
333 			rootpath[size] = '\0';
334 		}
335 		if (tag == TAG_HOSTNAME) {
336 			strncpy(hostname, cp, sizeof(hostname));
337 			hostname[size] = '\0';
338 		}
339 		if (tag == TAG_DOMAINNAME) {
340 			strncpy(domainname, cp, sizeof(domainname));
341 			domainname[size] = '\0';
342 		}
343 		cp += size;
344 	}
345 }
346