xref: /netbsd-src/sys/lib/libsa/bootp.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: bootp.c,v 1.13 1997/09/17 16:25:29 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 
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 = myip;
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) rootip = servip;
181 	bcopy(rbuf.rbootp.bp_file, bootfile, sizeof(bootfile));
182 	bootfile[sizeof(bootfile) - 1] = '\0';
183 
184 	if (IN_CLASSA(myip.s_addr))
185 		nmask = IN_CLASSA_NET;
186 	else if (IN_CLASSB(myip.s_addr))
187 		nmask = IN_CLASSB_NET;
188 	else
189 		nmask = IN_CLASSC_NET;
190 #ifdef BOOTP_DEBUG
191 	if (debug)
192 		printf("'native netmask' is %s\n", intoa(nmask));
193 #endif
194 
195 	/* Check subnet mask against net mask; toss if bogus */
196 	if ((nmask & smask) != nmask) {
197 #ifdef BOOTP_DEBUG
198 		if (debug)
199 			printf("subnet mask (%s) bad\n", intoa(smask));
200 #endif
201 		smask = 0;
202 	}
203 
204 	/* Get subnet (or natural net) mask */
205 	netmask = nmask;
206 	if (smask)
207 		netmask = smask;
208 #ifdef BOOTP_DEBUG
209 	if (debug)
210 		printf("mask: %s\n", intoa(netmask));
211 #endif
212 
213 	/* We need a gateway if root is on a different net */
214 	if (!SAMENET(myip, rootip, netmask)) {
215 #ifdef BOOTP_DEBUG
216 		if (debug)
217 			printf("need gateway for root ip\n");
218 #endif
219 	}
220 
221 	/* Toss gateway if on a different net */
222 	if (!SAMENET(myip, gateip, netmask)) {
223 #ifdef BOOTP_DEBUG
224 		if (debug)
225 			printf("gateway ip (%s) bad\n", inet_ntoa(gateip));
226 #endif
227 		gateip.s_addr = 0;
228 	}
229 
230 	/* Bump xid so next request will be unique. */
231 	++d->xid;
232 }
233 
234 /* Transmit a bootp request */
235 static ssize_t
236 bootpsend(d, pkt, len)
237 	register struct iodesc *d;
238 	register void *pkt;
239 	register size_t len;
240 {
241 	register struct bootp *bp;
242 
243 #ifdef BOOTP_DEBUG
244 	if (debug)
245 		printf("bootpsend: d=%lx called.\n", (long)d);
246 #endif
247 
248 	bp = pkt;
249 	bp->bp_secs = htons((u_short)(getsecs() - bot));
250 
251 #ifdef BOOTP_DEBUG
252 	if (debug)
253 		printf("bootpsend: calling sendudp\n");
254 #endif
255 
256 	return (sendudp(d, pkt, len));
257 }
258 
259 static ssize_t
260 bootprecv(d, pkt, len, tleft)
261 register struct iodesc *d;
262 register void *pkt;
263 register size_t len;
264 time_t tleft;
265 {
266 	register ssize_t n;
267 	register struct bootp *bp;
268 
269 #ifdef BOOTP_DEBUGx
270 	if (debug)
271 		printf("bootp_recvoffer: called\n");
272 #endif
273 
274 	n = readudp(d, pkt, len, tleft);
275 	if (n == -1 || n < sizeof(struct bootp) - BOOTP_VENDSIZE)
276 		goto bad;
277 
278 	bp = (struct bootp *)pkt;
279 
280 #ifdef BOOTP_DEBUG
281 	if (debug)
282 		printf("bootprecv: checked.  bp = 0x%lx, n = %d\n",
283 		    (long)bp, (int)n);
284 #endif
285 	if (bp->bp_xid != htonl(d->xid)) {
286 #ifdef BOOTP_DEBUG
287 		if (debug) {
288 			printf("bootprecv: expected xid 0x%lx, got 0x%x\n",
289 			    d->xid, ntohl(bp->bp_xid));
290 		}
291 #endif
292 		goto bad;
293 	}
294 
295 #ifdef BOOTP_DEBUG
296 	if (debug)
297 		printf("bootprecv: got one!\n");
298 #endif
299 
300 	/* Suck out vendor info */
301 	if (bcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0) {
302 		if(vend_rfc1048(bp->bp_vend, sizeof(bp->bp_vend)) != 0)
303 		    goto bad;
304 	}
305 #ifdef BOOTP_VEND_CMU
306 	else if (bcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0)
307 		vend_cmu(bp->bp_vend);
308 #endif
309 	else
310 		printf("bootprecv: unknown vendor 0x%lx\n", (long)bp->bp_vend);
311 
312 	return(n);
313 bad:
314 	errno = 0;
315 	return (-1);
316 }
317 
318 static int
319 vend_rfc1048(cp, len)
320 	register u_char *cp;
321 	u_int len;
322 {
323 	register u_char *ep;
324 	register int size;
325 	register u_char tag;
326 
327 #ifdef BOOTP_DEBUG
328 	if (debug)
329 		printf("vend_rfc1048 bootp info. len=%d\n", len);
330 #endif
331 	ep = cp + len;
332 
333 	/* Step over magic cookie */
334 	cp += sizeof(int);
335 
336 	while (cp < ep) {
337 		tag = *cp++;
338 		size = *cp++;
339 		if (tag == TAG_END)
340 			break;
341 
342 		if (tag == TAG_SUBNET_MASK) {
343 			bcopy(cp, &smask, sizeof(smask));
344 		}
345 		if (tag == TAG_GATEWAY) {
346 			bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr));
347 		}
348 		if (tag == TAG_SWAPSERVER) {
349 			/* let it override bp_siaddr */
350 			bcopy(cp, &rootip.s_addr, sizeof(swapip.s_addr));
351 		}
352 		if (tag == TAG_ROOTPATH) {
353 			strncpy(rootpath, (char *)cp, sizeof(rootpath));
354 			rootpath[size] = '\0';
355 		}
356 		if (tag == TAG_HOSTNAME) {
357 			strncpy(hostname, (char *)cp, sizeof(hostname));
358 			hostname[size] = '\0';
359 		}
360 #ifdef SUPPORT_DHCP
361 		if (tag == TAG_DHCP_MSGTYPE) {
362 			if(*cp != expected_dhcpmsgtype)
363 			    return(-1);
364 			dhcp_ok = 1;
365 		}
366 		if (tag == TAG_SERVERID) {
367 			bcopy(cp, &dhcp_serverip.s_addr,
368 			      sizeof(dhcp_serverip.s_addr));
369 		}
370 #endif
371 		cp += size;
372 	}
373 	return(0);
374 }
375 
376 #ifdef BOOTP_VEND_CMU
377 static void
378 vend_cmu(cp)
379 	u_char *cp;
380 {
381 	register struct cmu_vend *vp;
382 
383 #ifdef BOOTP_DEBUG
384 	if (debug)
385 		printf("vend_cmu bootp info.\n");
386 #endif
387 	vp = (struct cmu_vend *)cp;
388 
389 	if (vp->v_smask.s_addr != 0) {
390 		smask = vp->v_smask.s_addr;
391 	}
392 	if (vp->v_dgate.s_addr != 0) {
393 		gateip = vp->v_dgate;
394 	}
395 }
396 #endif
397