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