xref: /openbsd-src/sys/lib/libsa/bootp.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: bootp.c,v 1.8 1999/12/18 16:42:19 deraadt Exp $	*/
2 /*	$NetBSD: bootp.c,v 1.10 1996/10/13 02:28:59 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1992 Regents of the University of California.
6  * All rights reserved.
7  *
8  * This software was developed by the Computer Systems Engineering group
9  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10  * contributed to Berkeley.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Lawrence Berkeley Laboratory and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  * @(#) Header: bootp.c,v 1.4 93/09/11 03:13:51 leres Exp  (LBL)
41  */
42 
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <netinet/in_systm.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	ssize_t bootpsend __P((struct iodesc *, void *, size_t));
62 static	ssize_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 = HTYPE_ETHERNET;	/* 10Mb Ethernet (48 bits) */
103 	bp->bp_hlen = 6;
104 	bp->bp_xid = htonl(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 = htons(IPPORT_BOOTPC);
111 	d->destip.s_addr = INADDR_BROADCAST;
112 	d->destport = htons(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 ssize_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 = htons((u_short)(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 ssize_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 ssize_t n;
156 	register struct bootp *bp;
157 
158 #ifdef BOOTP_DEBUG
159 	if (debug)
160 		printf("bootprecv: called\n");
161 #endif
162 
163 	n = readudp(d, pkt, len, tleft);
164 	if (n < 0 || (size_t)n < sizeof(struct bootp))
165 		goto bad;
166 
167 	bp = (struct bootp *)pkt;
168 
169 #ifdef BOOTP_DEBUG
170 	if (debug)
171 		printf("bootprecv: checked.  bp = 0x%x, n = %d\n",
172 		    (unsigned)bp, n);
173 #endif
174 	if (bp->bp_xid != htonl(d->xid)) {
175 #ifdef BOOTP_DEBUG
176 		if (debug) {
177 			printf("bootprecv: expected xid 0x%lx, got 0x%lx\n",
178 			    d->xid, ntohl(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 = bp->bp_yiaddr;
191 #ifdef BOOTP_DEBUG
192 	if (debug)
193 		printf("our ip address is %s\n", inet_ntoa(d->myip));
194 #endif
195 	if (IN_CLASSA(d->myip.s_addr))
196 		nmask = IN_CLASSA_NET;
197 	else if (IN_CLASSB(d->myip.s_addr))
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 = bp->bp_siaddr;
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%lx\n", (long)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 	netmask = nmask;
233 	if (smask)
234 		netmask = smask;
235 #ifdef BOOTP_DEBUG
236 	if (debug)
237 		printf("mask: %s\n", intoa(netmask));
238 #endif
239 
240 	/* We need a gateway if root or swap is on a different net */
241 	if (!SAMENET(d->myip, rootip, netmask)) {
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, netmask)) {
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, netmask)) {
257 #ifdef BOOTP_DEBUG
258 		if (debug)
259 			printf("gateway ip (%s) bad\n", inet_ntoa(gateip));
260 #endif
261 		gateip.s_addr = 0;
262 	}
263 
264 	return (n);
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 = vp->v_smask.s_addr;
285 	}
286 	if (vp->v_dgate.s_addr != 0) {
287 		gateip = vp->v_dgate;
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(int);
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 		}
318 		if (tag == TAG_GATEWAY) {
319 			bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr));
320 		}
321 		if (tag == TAG_SWAPSERVER) {
322 			bcopy(cp, &swapip.s_addr, sizeof(swapip.s_addr));
323 		}
324 		if (tag == TAG_DOMAIN_SERVER) {
325 			bcopy(cp, &nameip.s_addr, sizeof(nameip.s_addr));
326 		}
327 		if (tag == TAG_ROOTPATH) {
328 			strncpy(rootpath, (char *)cp, sizeof(rootpath));
329 			rootpath[size] = '\0';
330 		}
331 		if (tag == TAG_HOSTNAME) {
332 			strncpy(hostname, (char *)cp, sizeof(hostname));
333 			hostname[size] = '\0';
334 		}
335 		if (tag == TAG_DOMAINNAME) {
336 			strncpy(domainname, (char *)cp, sizeof(domainname));
337 			domainname[size] = '\0';
338 		}
339 		cp += size;
340 	}
341 }
342