Use CSTD and remove local CSTD settings which are no longer needed.
[dragonfly.git] / lib / libstand / bootp.c
CommitLineData
984263bc
MD
1/* $NetBSD: bootp.c,v 1.14 1998/02/16 11:10:54 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 * $FreeBSD: src/lib/libstand/bootp.c,v 1.1.1.1.6.2 2000/09/20 18:37:25 ps Exp $
ff2aef30 41 * $DragonFly: src/lib/libstand/bootp.c,v 1.5 2005/12/11 02:27:26 swildner Exp $
984263bc
MD
42 */
43
47883879 44#include <sys/param.h>
984263bc
MD
45#include <netinet/in.h>
46#include <netinet/in_systm.h>
47
48#include <string.h>
49
50#define BOOTP_DEBUGxx
51#define SUPPORT_DHCP
52
53#include "stand.h"
54#include "net.h"
55#include "netif.h"
56#include "bootp.h"
57
58
59struct in_addr servip;
60
61static n_long nmask, smask;
62
63static time_t bot;
64
65static char vm_rfc1048[4] = VM_RFC1048;
66#ifdef BOOTP_VEND_CMU
67static char vm_cmu[4] = VM_CMU;
68#endif
69
70/* Local forwards */
71static ssize_t bootpsend(struct iodesc *, void *, size_t);
72static ssize_t bootprecv(struct iodesc *, void *, size_t, time_t);
73static int vend_rfc1048(u_char *, u_int);
74#ifdef BOOTP_VEND_CMU
75static void vend_cmu(u_char *);
76#endif
77
78#ifdef SUPPORT_DHCP
79static char expected_dhcpmsgtype = -1, dhcp_ok;
80struct in_addr dhcp_serverip;
81#endif
82
83/* Fetch required bootp infomation */
84void
ff2aef30 85bootp(int sock, int flag)
984263bc
MD
86{
87 struct iodesc *d;
660c873b 88 struct bootp *bp;
984263bc
MD
89 struct {
90 u_char header[HEADER_SIZE];
91 struct bootp wbootp;
92 } wbuf;
93 struct {
94 u_char header[HEADER_SIZE];
95 struct bootp rbootp;
96 } rbuf;
97
98#ifdef BOOTP_DEBUG
99 if (debug)
100 printf("bootp: socket=%d\n", sock);
101#endif
102 if (!bot)
103 bot = getsecs();
104
105 if (!(d = socktodesc(sock))) {
106 printf("bootp: bad socket. %d\n", sock);
107 return;
108 }
109#ifdef BOOTP_DEBUG
110 if (debug)
111 printf("bootp: d=%lx\n", (long)d);
112#endif
113
114 bp = &wbuf.wbootp;
115 bzero(bp, sizeof(*bp));
116
117 bp->bp_op = BOOTREQUEST;
118 bp->bp_htype = 1; /* 10Mb Ethernet (48 bits) */
119 bp->bp_hlen = 6;
120 bp->bp_xid = htonl(d->xid);
121 MACPY(d->myea, bp->bp_chaddr);
122 strncpy(bp->bp_file, bootfile, sizeof(bp->bp_file));
123 bcopy(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048));
124#ifdef SUPPORT_DHCP
125 bp->bp_vend[4] = TAG_DHCP_MSGTYPE;
126 bp->bp_vend[5] = 1;
127 bp->bp_vend[6] = DHCPDISCOVER;
128
129 /*
130 * If we are booting from PXE, we want to send the string
131 * 'PXEClient' to the DHCP server so you have the option of
132 * only responding to PXE aware dhcp requests.
133 */
134 if (flag & BOOTP_PXE) {
135 bp->bp_vend[7] = TAG_CLASSID;
136 bp->bp_vend[8] = 9;
137 bcopy("PXEClient", &bp->bp_vend[9], 9);
138 bp->bp_vend[18] = TAG_END;
139 } else
140 bp->bp_vend[7] = TAG_END;
141#else
142 bp->bp_vend[4] = TAG_END;
143#endif
144
145 d->myip.s_addr = INADDR_ANY;
146 d->myport = htons(IPPORT_BOOTPC);
147 d->destip.s_addr = INADDR_BROADCAST;
148 d->destport = htons(IPPORT_BOOTPS);
149
150#ifdef SUPPORT_DHCP
151 expected_dhcpmsgtype = DHCPOFFER;
152 dhcp_ok = 0;
153#endif
154
155 if(sendrecv(d,
156 bootpsend, bp, sizeof(*bp),
157 bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp))
158 == -1) {
159 printf("bootp: no reply\n");
160 return;
161 }
162
163#ifdef SUPPORT_DHCP
164 if(dhcp_ok) {
165 u_int32_t leasetime;
166 bp->bp_vend[6] = DHCPREQUEST;
167 bp->bp_vend[7] = TAG_REQ_ADDR;
168 bp->bp_vend[8] = 4;
169 bcopy(&rbuf.rbootp.bp_yiaddr, &bp->bp_vend[9], 4);
170 bp->bp_vend[13] = TAG_SERVERID;
171 bp->bp_vend[14] = 4;
172 bcopy(&dhcp_serverip.s_addr, &bp->bp_vend[15], 4);
173 bp->bp_vend[19] = TAG_LEASETIME;
174 bp->bp_vend[20] = 4;
175 leasetime = htonl(300);
176 bcopy(&leasetime, &bp->bp_vend[21], 4);
177 if (flag & BOOTP_PXE) {
178 bp->bp_vend[25] = TAG_CLASSID;
179 bp->bp_vend[26] = 9;
180 bcopy("PXEClient", &bp->bp_vend[27], 9);
181 bp->bp_vend[36] = TAG_END;
182 } else
183 bp->bp_vend[25] = TAG_END;
184
185 expected_dhcpmsgtype = DHCPACK;
186
187 if(sendrecv(d,
188 bootpsend, bp, sizeof(*bp),
189 bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp))
190 == -1) {
191 printf("DHCPREQUEST failed\n");
192 return;
193 }
194 }
195#endif
196
197 myip = d->myip = rbuf.rbootp.bp_yiaddr;
198 servip = rbuf.rbootp.bp_siaddr;
199 if(rootip.s_addr == INADDR_ANY) rootip = servip;
200 bcopy(rbuf.rbootp.bp_file, bootfile, sizeof(bootfile));
201 bootfile[sizeof(bootfile) - 1] = '\0';
202
203 if (IN_CLASSA(ntohl(myip.s_addr)))
204 nmask = htonl(IN_CLASSA_NET);
205 else if (IN_CLASSB(ntohl(myip.s_addr)))
206 nmask = htonl(IN_CLASSB_NET);
207 else
208 nmask = htonl(IN_CLASSC_NET);
209#ifdef BOOTP_DEBUG
210 if (debug)
211 printf("'native netmask' is %s\n", intoa(nmask));
212#endif
213
214 /* Check subnet mask against net mask; toss if bogus */
215 if ((nmask & smask) != nmask) {
216#ifdef BOOTP_DEBUG
217 if (debug)
218 printf("subnet mask (%s) bad\n", intoa(smask));
219#endif
220 smask = 0;
221 }
222
223 /* Get subnet (or natural net) mask */
224 netmask = nmask;
225 if (smask)
226 netmask = smask;
227#ifdef BOOTP_DEBUG
228 if (debug)
229 printf("mask: %s\n", intoa(netmask));
230#endif
231
232 /* We need a gateway if root is on a different net */
233 if (!SAMENET(myip, rootip, netmask)) {
234#ifdef BOOTP_DEBUG
235 if (debug)
236 printf("need gateway for root ip\n");
237#endif
238 }
239
240 /* Toss gateway if on a different net */
241 if (!SAMENET(myip, gateip, netmask)) {
242#ifdef BOOTP_DEBUG
243 if (debug)
244 printf("gateway ip (%s) bad\n", inet_ntoa(gateip));
245#endif
246 gateip.s_addr = 0;
247 }
248
249 /* Bump xid so next request will be unique. */
250 ++d->xid;
251}
252
253/* Transmit a bootp request */
254static ssize_t
ff2aef30 255bootpsend(struct iodesc *d, void *pkt, size_t len)
984263bc 256{
660c873b 257 struct bootp *bp;
984263bc
MD
258
259#ifdef BOOTP_DEBUG
260 if (debug)
261 printf("bootpsend: d=%lx called.\n", (long)d);
262#endif
263
264 bp = pkt;
265 bp->bp_secs = htons((u_short)(getsecs() - bot));
266
267#ifdef BOOTP_DEBUG
268 if (debug)
269 printf("bootpsend: calling sendudp\n");
270#endif
271
272 return (sendudp(d, pkt, len));
273}
274
275static ssize_t
ff2aef30 276bootprecv(struct iodesc *d, void *pkt, size_t len, time_t tleft)
984263bc 277{
660c873b
DR
278 ssize_t n;
279 struct bootp *bp;
984263bc
MD
280
281#ifdef BOOTP_DEBUGx
282 if (debug)
283 printf("bootp_recvoffer: called\n");
284#endif
285
286 n = readudp(d, pkt, len, tleft);
287 if (n == -1 || n < sizeof(struct bootp) - BOOTP_VENDSIZE)
288 goto bad;
289
290 bp = (struct bootp *)pkt;
291
292#ifdef BOOTP_DEBUG
293 if (debug)
294 printf("bootprecv: checked. bp = 0x%lx, n = %d\n",
295 (long)bp, (int)n);
296#endif
297 if (bp->bp_xid != htonl(d->xid)) {
298#ifdef BOOTP_DEBUG
299 if (debug) {
300 printf("bootprecv: expected xid 0x%lx, got 0x%x\n",
301 d->xid, ntohl(bp->bp_xid));
302 }
303#endif
304 goto bad;
305 }
306
307#ifdef BOOTP_DEBUG
308 if (debug)
309 printf("bootprecv: got one!\n");
310#endif
311
312 /* Suck out vendor info */
313 if (bcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0) {
314 if(vend_rfc1048(bp->bp_vend, sizeof(bp->bp_vend)) != 0)
315 goto bad;
316 }
317#ifdef BOOTP_VEND_CMU
318 else if (bcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0)
319 vend_cmu(bp->bp_vend);
320#endif
321 else
322 printf("bootprecv: unknown vendor 0x%lx\n", (long)bp->bp_vend);
323
324 return(n);
325bad:
326 errno = 0;
327 return (-1);
328}
329
330static int
ff2aef30 331vend_rfc1048(u_char *cp, u_int len)
984263bc 332{
660c873b
DR
333 u_char *ep;
334 int size;
335 u_char tag;
984263bc
MD
336
337#ifdef BOOTP_DEBUG
338 if (debug)
339 printf("vend_rfc1048 bootp info. len=%d\n", len);
340#endif
341 ep = cp + len;
342
343 /* Step over magic cookie */
344 cp += sizeof(int);
345
346 while (cp < ep) {
347 tag = *cp++;
348 size = *cp++;
349 if (tag == TAG_END)
350 break;
351
352 if (tag == TAG_SUBNET_MASK) {
353 bcopy(cp, &smask, sizeof(smask));
354 }
355 if (tag == TAG_GATEWAY) {
356 bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr));
357 }
358 if (tag == TAG_SWAPSERVER) {
359 /* let it override bp_siaddr */
360 bcopy(cp, &rootip.s_addr, sizeof(swapip.s_addr));
361 }
362 if (tag == TAG_ROOTPATH) {
363 strncpy(rootpath, (char *)cp, sizeof(rootpath));
364 rootpath[size] = '\0';
365 }
366 if (tag == TAG_HOSTNAME) {
367 strncpy(hostname, (char *)cp, sizeof(hostname));
368 hostname[size] = '\0';
369 }
370#ifdef SUPPORT_DHCP
371 if (tag == TAG_DHCP_MSGTYPE) {
372 if(*cp != expected_dhcpmsgtype)
373 return(-1);
374 dhcp_ok = 1;
375 }
376 if (tag == TAG_SERVERID) {
377 bcopy(cp, &dhcp_serverip.s_addr,
378 sizeof(dhcp_serverip.s_addr));
379 }
380#endif
381 cp += size;
382 }
383 return(0);
384}
385
386#ifdef BOOTP_VEND_CMU
387static void
ff2aef30 388vend_cmu(u_char *cp)
984263bc 389{
660c873b 390 struct cmu_vend *vp;
984263bc
MD
391
392#ifdef BOOTP_DEBUG
393 if (debug)
394 printf("vend_cmu bootp info.\n");
395#endif
396 vp = (struct cmu_vend *)cp;
397
398 if (vp->v_smask.s_addr != 0) {
399 smask = vp->v_smask.s_addr;
400 }
401 if (vp->v_dgate.s_addr != 0) {
402 gateip = vp->v_dgate;
403 }
404}
405#endif