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