1 /** $MirOS: src/sys/lib/libsa/bootp.c,v 1.4 2009/01/03 17:37:49 tg Exp $ */
2 /* $OpenBSD: bootp.c,v 1.12 2006/02/06 17:37:28 jmc Exp $ */
3 /* $NetBSD: bootp.c,v 1.10 1996/10/13 02:28:59 christos Exp $ */
4
5 /*
6 * Copyright (c) 1992 Regents of the University of California.
7 * All rights reserved.
8 *
9 * This software was developed by the Computer Systems Engineering group
10 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
11 * contributed to Berkeley.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Lawrence Berkeley Laboratory and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 * @(#) Header: bootp.c,v 1.4 93/09/11 03:13:51 leres Exp (LBL)
42 */
43
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <netinet/in_systm.h>
48
49 #include "stand.h"
50 #include "net.h"
51 #include "netif.h"
52 #include "bootp.h"
53
54 static n_long nmask, smask;
55
56 static time_t bot;
57
58 static char vm_rfc1048[4] = VM_RFC1048;
59 static char vm_cmu[4] = VM_CMU;
60
61 /* Local forwards */
62 static ssize_t bootpsend(struct iodesc *, void *, size_t);
63 static ssize_t bootprecv(struct iodesc *, void *, size_t, time_t);
64 static void vend_cmu(u_char *);
65 static void vend_rfc1048(u_char *, u_int);
66
67 /* Fetch required bootp information */
68 void
bootp(int sock)69 bootp(int sock)
70 {
71 struct iodesc *d;
72 struct bootp *bp;
73 struct bootp_xbuf {
74 u_char header[HEADER_SIZE];
75 struct bootp xbootp;
76 } *rbuf, *wbuf;
77
78 #ifdef BOOTP_DEBUG
79 if (debug)
80 printf("bootp: socket=%d\n", sock);
81 #endif
82 if (!bot)
83 bot = getsecs();
84
85 if (!(d = socktodesc(sock))) {
86 printf("bootp: bad socket. %d\n", sock);
87 return;
88 }
89 #ifdef BOOTP_DEBUG
90 if (debug)
91 printf("bootp: d=%x\n", (u_int)d);
92 #endif
93
94 rbuf = alloc(sizeof (struct bootp_xbuf));
95 wbuf = alloc(sizeof (struct bootp_xbuf));
96
97 bp = &wbuf->xbootp;
98 bzero(bp, sizeof(*bp));
99
100 bp->bp_op = BOOTREQUEST;
101 bp->bp_htype = HTYPE_ETHERNET; /* 10Mb Ethernet (48 bits) */
102 bp->bp_hlen = 6;
103 bp->bp_xid = htonl(d->xid);
104 MACPY(d->myea, bp->bp_chaddr);
105 bzero(bp->bp_file, sizeof(bp->bp_file));
106 bcopy(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048));
107
108 d->myip = myip;
109 d->myport = htons(IPPORT_BOOTPC);
110 d->destip.s_addr = INADDR_BROADCAST;
111 d->destport = htons(IPPORT_BOOTPS);
112
113 (void)sendrecv(d,
114 bootpsend, bp, sizeof(*bp),
115 bootprecv, &rbuf->xbootp, sizeof(rbuf->xbootp));
116
117 free(wbuf, sizeof (struct bootp_xbuf));
118 free(rbuf, sizeof (struct bootp_xbuf));
119
120 /* Bump xid so next request will be unique. */
121 ++d->xid;
122 }
123
124 /* Transmit a bootp request */
125 static ssize_t
bootpsend(struct iodesc * d,void * pkt,size_t len)126 bootpsend(struct iodesc *d, void *pkt, size_t len)
127 {
128 struct bootp *bp;
129
130 #ifdef BOOTP_DEBUG
131 if (debug)
132 printf("bootpsend: d=%x called.\n", (u_int)d);
133 #endif
134
135 bp = pkt;
136 bp->bp_secs = htons((u_short)(getsecs() - bot));
137
138 #ifdef BOOTP_DEBUG
139 if (debug)
140 printf("bootpsend: calling sendudp\n");
141 #endif
142
143 return (sendudp(d, pkt, len));
144 }
145
146 /* Returns 0 if this is the packet we're waiting for else -1 (and errno == 0) */
147 static ssize_t
bootprecv(struct iodesc * d,void * pkt,size_t len,time_t tleft)148 bootprecv(struct iodesc *d, void *pkt, size_t len, time_t tleft)
149 {
150 ssize_t n;
151 struct bootp *bp;
152
153 #ifdef BOOTP_DEBUG
154 if (debug)
155 printf("bootprecv: called\n");
156 #endif
157
158 n = readudp(d, pkt, len, tleft);
159 if (n < 0 || (size_t)n < sizeof(struct bootp))
160 goto bad;
161
162 bp = (struct bootp *)pkt;
163
164 #ifdef BOOTP_DEBUG
165 if (debug)
166 printf("bootprecv: checked. bp = 0x%x, n = %d\n",
167 (unsigned)bp, n);
168 #endif
169 if (bp->bp_xid != htonl(d->xid)) {
170 #ifdef BOOTP_DEBUG
171 if (debug) {
172 printf("bootprecv: expected xid 0x%lx, got 0x%lx\n",
173 d->xid, ntohl(bp->bp_xid));
174 }
175 #endif
176 goto bad;
177 }
178
179 #ifdef BOOTP_DEBUG
180 if (debug)
181 printf("bootprecv: got one!\n");
182 #endif
183
184 /* Pick up our ip address (and natural netmask) */
185 myip = d->myip = bp->bp_yiaddr;
186 #ifdef BOOTP_DEBUG
187 if (debug)
188 printf("our ip address is %s\n", inet_ntoa(d->myip));
189 #endif
190 if (IN_CLASSA(d->myip.s_addr))
191 nmask = IN_CLASSA_NET;
192 else if (IN_CLASSB(d->myip.s_addr))
193 nmask = IN_CLASSB_NET;
194 else
195 nmask = IN_CLASSC_NET;
196 #ifdef BOOTP_DEBUG
197 if (debug)
198 printf("'native netmask' is %s\n", intoa(nmask));
199 #endif
200
201 /* Pick up root or swap server address and file spec. */
202 if (bp->bp_siaddr.s_addr != 0)
203 rootip = bp->bp_siaddr;
204 if (bp->bp_file[0] != '\0') {
205 strncpy(bootfile, (char *)bp->bp_file, sizeof(bootfile));
206 bootfile[sizeof(bootfile) - 1] = '\0';
207 }
208
209 /* Suck out vendor info */
210 if (bcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0)
211 vend_cmu(bp->bp_vend);
212 else if (bcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0)
213 vend_rfc1048(bp->bp_vend, sizeof(bp->bp_vend));
214 else
215 printf("bootprecv: unknown vendor 0x%lx\n", (long)bp->bp_vend);
216
217 /* Check subnet mask against net mask; toss if bogus */
218 if ((nmask & smask) != nmask) {
219 #ifdef BOOTP_DEBUG
220 if (debug)
221 printf("subnet mask (%s) bad\n", intoa(smask));
222 #endif
223 smask = 0;
224 }
225
226 /* Get subnet (or natural net) mask */
227 netmask = nmask;
228 if (smask)
229 netmask = smask;
230 #ifdef BOOTP_DEBUG
231 if (debug)
232 printf("mask: %s\n", intoa(netmask));
233 #endif
234
235 /* We need a gateway if root or swap is on a different net */
236 if (!SAMENET(d->myip, rootip, netmask)) {
237 #ifdef BOOTP_DEBUG
238 if (debug)
239 printf("need gateway for root ip\n");
240 #endif
241 }
242
243 if (!SAMENET(d->myip, swapip, netmask)) {
244 #ifdef BOOTP_DEBUG
245 if (debug)
246 printf("need gateway for swap ip\n");
247 #endif
248 }
249
250 /* Toss gateway if on a different net */
251 if (!SAMENET(d->myip, gateip, netmask)) {
252 #ifdef BOOTP_DEBUG
253 if (debug)
254 printf("gateway ip (%s) bad\n", inet_ntoa(gateip));
255 #endif
256 gateip.s_addr = 0;
257 }
258
259 return (n);
260
261 bad:
262 errno = 0;
263 return (-1);
264 }
265
266 static void
vend_cmu(u_char * cp)267 vend_cmu(u_char *cp)
268 {
269 struct cmu_vend *vp;
270
271 #ifdef BOOTP_DEBUG
272 if (debug)
273 printf("vend_cmu bootp info.\n");
274 #endif
275 vp = (struct cmu_vend *)cp;
276
277 if (vp->v_smask.s_addr != 0)
278 smask = vp->v_smask.s_addr;
279 if (vp->v_dgate.s_addr != 0)
280 gateip = vp->v_dgate;
281 }
282
283 static void
vend_rfc1048(u_char * cp,u_int len)284 vend_rfc1048(u_char *cp, u_int len)
285 {
286 u_char *ep;
287 int size;
288 u_char tag;
289
290 #ifdef BOOTP_DEBUG
291 if (debug)
292 printf("vend_rfc1048 bootp info. len=%d\n", len);
293 #endif
294 ep = cp + len;
295
296 /* Step over magic cookie */
297 cp += sizeof(int);
298
299 while (cp < ep) {
300 tag = *cp++;
301 size = *cp++;
302 if (tag == TAG_END)
303 break;
304
305 if (tag == TAG_SUBNET_MASK)
306 bcopy(cp, &smask, sizeof(smask));
307 if (tag == TAG_GATEWAY)
308 bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr));
309 if (tag == TAG_SWAPSERVER)
310 bcopy(cp, &swapip.s_addr, sizeof(swapip.s_addr));
311 if (tag == TAG_DOMAIN_SERVER)
312 bcopy(cp, &nameip.s_addr, sizeof(nameip.s_addr));
313 if (tag == TAG_ROOTPATH) {
314 strncpy(rootpath, (char *)cp, sizeof(rootpath));
315 rootpath[size] = '\0';
316 }
317 if (tag == TAG_HOSTNAME) {
318 strncpy(hostname, (char *)cp, sizeof(hostname));
319 hostname[size] = '\0';
320 }
321 cp += size;
322 }
323 }
324