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