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