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