bootp.c revision 1.30 1 /* $NetBSD: bootp.c,v 1.30 2007/10/01 13:15:06 martin 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/param.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 "bootp.h"
55
56 struct in_addr servip;
57 #ifdef SUPPORT_LINUX
58 char linuxcmdline[256];
59 #ifndef TAG_LINUX_CMDLINE
60 #define TAG_LINUX_CMDLINE 123
61 #endif
62 #endif
63
64 static n_long nmask, smask;
65
66 static time_t bot;
67
68 static char vm_rfc1048[4] = VM_RFC1048;
69 #ifdef BOOTP_VEND_CMU
70 static char vm_cmu[4] = VM_CMU;
71 #endif
72
73 /* Local forwards */
74 static ssize_t bootpsend __P((struct iodesc *, void *, size_t));
75 static ssize_t bootprecv __P((struct iodesc *, void *, size_t, time_t));
76 static int vend_rfc1048 __P((u_char *, u_int));
77 #ifdef BOOTP_VEND_CMU
78 static void vend_cmu __P((u_char *));
79 #endif
80
81 #ifdef SUPPORT_DHCP
82 static char expected_dhcpmsgtype = -1, dhcp_ok;
83 struct in_addr dhcp_serverip;
84 #endif
85
86 /*
87 * Boot programs can patch this at run-time to change the behavior
88 * of bootp/dhcp.
89 */
90 int bootp_flags;
91
92 /* Fetch required bootp information */
93 void
94 bootp(sock)
95 int sock;
96 {
97 struct iodesc *d;
98 struct bootp *bp;
99 struct {
100 u_char header[HEADER_SIZE];
101 struct bootp wbootp;
102 } wbuf;
103 struct {
104 u_char header[HEADER_SIZE];
105 struct bootp rbootp;
106 } rbuf;
107 #ifdef SUPPORT_DHCP
108 char vci[64];
109 int vcilen;
110 #endif
111
112 #ifdef BOOTP_DEBUG
113 if (debug)
114 printf("bootp: socket=%d\n", sock);
115 #endif
116 if (!bot)
117 bot = getsecs();
118
119 if (!(d = socktodesc(sock))) {
120 printf("bootp: bad socket. %d\n", sock);
121 return;
122 }
123 #ifdef BOOTP_DEBUG
124 if (debug)
125 printf("bootp: d=%lx\n", (long)d);
126 #endif
127
128 bp = &wbuf.wbootp;
129 bzero(bp, sizeof(*bp));
130
131 bp->bp_op = BOOTREQUEST;
132 bp->bp_htype = 1; /* 10Mb Ethernet (48 bits) */
133 bp->bp_hlen = 6;
134 bp->bp_xid = htonl(d->xid);
135 MACPY(d->myea, bp->bp_chaddr);
136 strncpy((char *)bp->bp_file, bootfile, sizeof(bp->bp_file));
137 bcopy(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048));
138 #ifdef SUPPORT_DHCP
139 bp->bp_vend[4] = TAG_DHCP_MSGTYPE;
140 bp->bp_vend[5] = 1;
141 bp->bp_vend[6] = DHCPDISCOVER;
142 /*
143 * Insert a NetBSD Vendor Class Identifier option.
144 */
145 sprintf(vci, "NetBSD:%s:libsa", MACHINE);
146 vcilen = strlen(vci);
147 bp->bp_vend[7] = TAG_CLASSID;
148 bp->bp_vend[8] = vcilen;
149 bcopy(vci, &bp->bp_vend[9], vcilen);
150 bp->bp_vend[9 + vcilen] = TAG_END;
151 #else
152 bp->bp_vend[4] = TAG_END;
153 #endif
154
155 d->myip.s_addr = INADDR_ANY;
156 d->myport = htons(IPPORT_BOOTPC);
157 d->destip.s_addr = INADDR_BROADCAST;
158 d->destport = htons(IPPORT_BOOTPS);
159
160 #ifdef SUPPORT_DHCP
161 expected_dhcpmsgtype = DHCPOFFER;
162 dhcp_ok = 0;
163 #endif
164
165 if (sendrecv(d,
166 bootpsend, bp, sizeof(*bp),
167 bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp))
168 == -1) {
169 printf("bootp: no reply\n");
170 return;
171 }
172
173 #ifdef SUPPORT_DHCP
174 if (dhcp_ok) {
175 u_int32_t leasetime;
176 bp->bp_vend[6] = DHCPREQUEST;
177 bp->bp_vend[7] = TAG_REQ_ADDR;
178 bp->bp_vend[8] = 4;
179 bcopy(&rbuf.rbootp.bp_yiaddr, &bp->bp_vend[9], 4);
180 bp->bp_vend[13] = TAG_SERVERID;
181 bp->bp_vend[14] = 4;
182 bcopy(&dhcp_serverip.s_addr, &bp->bp_vend[15], 4);
183 bp->bp_vend[19] = TAG_LEASETIME;
184 bp->bp_vend[20] = 4;
185 leasetime = htonl(300);
186 bcopy(&leasetime, &bp->bp_vend[21], 4);
187 /*
188 * Insert a NetBSD Vendor Class Identifier option.
189 */
190 sprintf(vci, "NetBSD:%s:libsa", MACHINE);
191 vcilen = strlen(vci);
192 bp->bp_vend[25] = TAG_CLASSID;
193 bp->bp_vend[26] = vcilen;
194 bcopy(vci, &bp->bp_vend[27], vcilen);
195 bp->bp_vend[27 + vcilen] = TAG_END;
196
197 expected_dhcpmsgtype = DHCPACK;
198
199 if (sendrecv(d,
200 bootpsend, bp, sizeof(*bp),
201 bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp))
202 == -1) {
203 printf("DHCPREQUEST failed\n");
204 return;
205 }
206 }
207 #endif
208
209 myip = d->myip = rbuf.rbootp.bp_yiaddr;
210 servip = rbuf.rbootp.bp_siaddr;
211 if (rootip.s_addr == INADDR_ANY)
212 rootip = servip;
213 bcopy(rbuf.rbootp.bp_file, bootfile, sizeof(bootfile));
214 bootfile[sizeof(bootfile) - 1] = '\0';
215
216 if (IN_CLASSA(myip.s_addr))
217 nmask = IN_CLASSA_NET;
218 else if (IN_CLASSB(myip.s_addr))
219 nmask = IN_CLASSB_NET;
220 else
221 nmask = IN_CLASSC_NET;
222 #ifdef BOOTP_DEBUG
223 if (debug)
224 printf("'native netmask' is %s\n", intoa(nmask));
225 #endif
226
227 /* Get subnet (or natural net) mask */
228 netmask = nmask;
229 if (smask)
230 netmask = smask;
231 #ifdef BOOTP_DEBUG
232 if (debug)
233 printf("mask: %s\n", intoa(netmask));
234 #endif
235
236 /* We need a gateway if root is on a different net */
237 if (!SAMENET(myip, rootip, netmask)) {
238 #ifdef BOOTP_DEBUG
239 if (debug)
240 printf("need gateway for root ip\n");
241 #endif
242 }
243
244 /* Toss gateway if on a different net */
245 if (!SAMENET(myip, gateip, netmask)) {
246 #ifdef BOOTP_DEBUG
247 if (debug)
248 printf("gateway ip (%s) bad\n", inet_ntoa(gateip));
249 #endif
250 gateip.s_addr = 0;
251 }
252
253 #ifdef BOOTP_DEBUG
254 if (debug) {
255 printf("client addr: %s\n", inet_ntoa(myip));
256 if (smask)
257 printf("subnet mask: %s\n", intoa(smask));
258 if (gateip.s_addr != 0)
259 printf("net gateway: %s\n", inet_ntoa(gateip));
260 printf("server addr: %s\n", inet_ntoa(rootip));
261 if (rootpath[0] != '\0')
262 printf("server path: %s\n", rootpath);
263 if (bootfile[0] != '\0')
264 printf("file name: %s\n", bootfile);
265 }
266 #endif
267
268 /* Bump xid so next request will be unique. */
269 ++d->xid;
270 }
271
272 /* Transmit a bootp request */
273 static ssize_t
274 bootpsend(d, pkt, len)
275 struct iodesc *d;
276 void *pkt;
277 size_t len;
278 {
279 struct bootp *bp;
280
281 #ifdef BOOTP_DEBUG
282 if (debug)
283 printf("bootpsend: d=%lx called.\n", (long)d);
284 #endif
285
286 bp = pkt;
287 bp->bp_secs = htons((u_short)(getsecs() - bot));
288
289 #ifdef BOOTP_DEBUG
290 if (debug)
291 printf("bootpsend: calling sendudp\n");
292 #endif
293
294 return (sendudp(d, pkt, len));
295 }
296
297 static ssize_t
298 bootprecv(d, pkt, len, tleft)
299 struct iodesc *d;
300 void *pkt;
301 size_t len;
302 time_t tleft;
303 {
304 ssize_t n;
305 struct bootp *bp;
306
307 #ifdef BOOTP_DEBUGx
308 if (debug)
309 printf("bootp_recvoffer: called\n");
310 #endif
311
312 n = readudp(d, pkt, len, tleft);
313 if (n == -1 || (size_t)n < sizeof(struct bootp) - BOOTP_VENDSIZE)
314 goto bad;
315
316 bp = (struct bootp *)pkt;
317
318 #ifdef BOOTP_DEBUG
319 if (debug)
320 printf("bootprecv: checked. bp = 0x%lx, n = %d\n",
321 (long)bp, (int)n);
322 #endif
323 if (bp->bp_xid != htonl(d->xid)) {
324 #ifdef BOOTP_DEBUG
325 if (debug) {
326 printf("bootprecv: expected xid 0x%lx, got 0x%x\n",
327 d->xid, ntohl(bp->bp_xid));
328 }
329 #endif
330 goto bad;
331 }
332
333 /* protect against bogus addresses sent by DHCP servers */
334 if (bp->bp_yiaddr.s_addr == INADDR_ANY ||
335 bp->bp_yiaddr.s_addr == INADDR_BROADCAST)
336 goto bad;
337
338 #ifdef BOOTP_DEBUG
339 if (debug)
340 printf("bootprecv: got one!\n");
341 #endif
342
343 /* Suck out vendor info */
344 if (memcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0) {
345 if (vend_rfc1048(bp->bp_vend, sizeof(bp->bp_vend)) != 0)
346 goto bad;
347 }
348 #ifdef BOOTP_VEND_CMU
349 else if (memcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0)
350 vend_cmu(bp->bp_vend);
351 #endif
352 else
353 printf("bootprecv: unknown vendor 0x%lx\n", (long)bp->bp_vend);
354
355 return (n);
356 bad:
357 errno = 0;
358 return (-1);
359 }
360
361 static int
362 vend_rfc1048(cp, len)
363 u_char *cp;
364 u_int len;
365 {
366 u_char *ep;
367 int size;
368 u_char tag;
369
370 #ifdef BOOTP_DEBUG
371 if (debug)
372 printf("vend_rfc1048 bootp info. len=%d\n", len);
373 #endif
374 ep = cp + len;
375
376 /* Step over magic cookie */
377 cp += sizeof(int);
378
379 while (cp < ep) {
380 tag = *cp++;
381 size = *cp++;
382 if (tag == TAG_END)
383 break;
384
385 if (tag == TAG_SUBNET_MASK) {
386 bcopy(cp, &smask, sizeof(smask));
387 }
388 if (tag == TAG_GATEWAY) {
389 bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr));
390 }
391 if (tag == TAG_SWAPSERVER) {
392 /* let it override bp_siaddr */
393 bcopy(cp, &rootip.s_addr, sizeof(rootip.s_addr));
394 }
395 if (tag == TAG_ROOTPATH) {
396 strncpy(rootpath, (char *)cp, sizeof(rootpath));
397 rootpath[size] = '\0';
398 }
399 if (tag == TAG_HOSTNAME) {
400 strncpy(hostname, (char *)cp, sizeof(hostname));
401 hostname[size] = '\0';
402 }
403 #ifdef SUPPORT_DHCP
404 if (tag == TAG_DHCP_MSGTYPE) {
405 if (*cp != expected_dhcpmsgtype)
406 return (-1);
407 dhcp_ok = 1;
408 }
409 if (tag == TAG_SERVERID) {
410 bcopy(cp, &dhcp_serverip.s_addr,
411 sizeof(dhcp_serverip.s_addr));
412 }
413 #endif
414 #ifdef SUPPORT_LINUX
415 if (tag == TAG_LINUX_CMDLINE) {
416 strncpy(linuxcmdline, (char *)cp, sizeof(linuxcmdline));
417 linuxcmdline[size] = '\0';
418 }
419 #endif
420 cp += size;
421 }
422 return (0);
423 }
424
425 #ifdef BOOTP_VEND_CMU
426 static void
427 vend_cmu(cp)
428 u_char *cp;
429 {
430 struct cmu_vend *vp;
431
432 #ifdef BOOTP_DEBUG
433 if (debug)
434 printf("vend_cmu bootp info.\n");
435 #endif
436 vp = (struct cmu_vend *)cp;
437
438 if (vp->v_smask.s_addr != 0) {
439 smask = vp->v_smask.s_addr;
440 }
441 if (vp->v_dgate.s_addr != 0) {
442 gateip = vp->v_dgate;
443 }
444 }
445 #endif
446