]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libstand/bootp.c
Merge libc++ r291274, and update the library Makefile.
[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 <sys/endian.h>
43 #include <netinet/in.h>
44 #include <netinet/in_systm.h>
45
46 #include <string.h>
47
48 #define BOOTP_DEBUGxx
49 #define SUPPORT_DHCP
50
51 #define DHCP_ENV_NOVENDOR       1       /* do not parse vendor options */
52 #define DHCP_ENV_PXE            10      /* assume pxe vendor options */
53 #define DHCP_ENV_FREEBSD        11      /* assume freebsd vendor options */
54 /* set DHCP_ENV to one of the values above to export dhcp options to kenv */
55 #define DHCP_ENV                DHCP_ENV_NO_VENDOR
56
57 #include "stand.h"
58 #include "net.h"
59 #include "netif.h"
60 #include "bootp.h"
61
62
63 struct in_addr servip;
64
65 static time_t   bot;
66
67 static  char vm_rfc1048[4] = VM_RFC1048;
68 #ifdef BOOTP_VEND_CMU
69 static  char vm_cmu[4] = VM_CMU;
70 #endif
71
72 /* Local forwards */
73 static  ssize_t bootpsend(struct iodesc *, void *, size_t);
74 static  ssize_t bootprecv(struct iodesc *, void *, size_t, time_t);
75 static  int vend_rfc1048(u_char *, u_int);
76 #ifdef BOOTP_VEND_CMU
77 static  void vend_cmu(u_char *);
78 #endif
79
80 #ifdef DHCP_ENV         /* export the dhcp response to kenv */
81 struct dhcp_opt;
82 static void setenv_(u_char *cp,  u_char *ep, struct dhcp_opt *opts);
83 #else
84 #define setenv_(a, b, c)
85 #endif
86
87 #ifdef SUPPORT_DHCP
88 static char expected_dhcpmsgtype = -1, dhcp_ok;
89 struct in_addr dhcp_serverip;
90 #endif
91
92 /* Fetch required bootp infomation */
93 void
94 bootp(sock, flag)
95         int sock;
96         int flag;
97 {
98         struct iodesc *d;
99         struct bootp *bp;
100         struct {
101                 u_char header[HEADER_SIZE];
102                 struct bootp wbootp;
103         } wbuf;
104         struct {
105                 u_char header[HEADER_SIZE];
106                 struct bootp rbootp;
107         } rbuf;
108
109 #ifdef BOOTP_DEBUG
110         if (debug)
111                 printf("bootp: socket=%d\n", sock);
112 #endif
113         if (!bot)
114                 bot = getsecs();
115         
116         if (!(d = socktodesc(sock))) {
117                 printf("bootp: bad socket. %d\n", sock);
118                 return;
119         }
120 #ifdef BOOTP_DEBUG
121         if (debug)
122                 printf("bootp: d=%lx\n", (long)d);
123 #endif
124
125         bp = &wbuf.wbootp;
126         bzero(bp, sizeof(*bp));
127
128         bp->bp_op = BOOTREQUEST;
129         bp->bp_htype = 1;               /* 10Mb Ethernet (48 bits) */
130         bp->bp_hlen = 6;
131         bp->bp_xid = htonl(d->xid);
132         MACPY(d->myea, bp->bp_chaddr);
133         strncpy(bp->bp_file, bootfile, sizeof(bp->bp_file));
134         bcopy(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048));
135 #ifdef SUPPORT_DHCP
136         bp->bp_vend[4] = TAG_DHCP_MSGTYPE;
137         bp->bp_vend[5] = 1;
138         bp->bp_vend[6] = DHCPDISCOVER;
139
140         /*
141          * If we are booting from PXE, we want to send the string
142          * 'PXEClient' to the DHCP server so you have the option of
143          * only responding to PXE aware dhcp requests.
144          */
145         if (flag & BOOTP_PXE) {
146                 bp->bp_vend[7] = TAG_CLASSID;
147                 bp->bp_vend[8] = 9;
148                 bcopy("PXEClient", &bp->bp_vend[9], 9);
149                 bp->bp_vend[18] = TAG_PARAM_REQ;
150                 bp->bp_vend[19] = 8;
151                 bp->bp_vend[20] = TAG_ROOTPATH;
152                 bp->bp_vend[21] = TAG_TFTP_SERVER;
153                 bp->bp_vend[22] = TAG_HOSTNAME;
154                 bp->bp_vend[23] = TAG_SWAPSERVER;
155                 bp->bp_vend[24] = TAG_GATEWAY;
156                 bp->bp_vend[25] = TAG_SUBNET_MASK;
157                 bp->bp_vend[26] = TAG_INTF_MTU;
158                 bp->bp_vend[27] = TAG_SERVERID;
159                 bp->bp_vend[28] = TAG_END;
160         } else
161                 bp->bp_vend[7] = TAG_END;
162 #else
163         bp->bp_vend[4] = TAG_END;
164 #endif
165
166         d->myip.s_addr = INADDR_ANY;
167         d->myport = htons(IPPORT_BOOTPC);
168         d->destip.s_addr = INADDR_BROADCAST;
169         d->destport = htons(IPPORT_BOOTPS);
170
171 #ifdef SUPPORT_DHCP
172         expected_dhcpmsgtype = DHCPOFFER;
173         dhcp_ok = 0;
174 #endif
175
176         if(sendrecv(d,
177                     bootpsend, bp, sizeof(*bp),
178                     bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp))
179            == -1) {
180             printf("bootp: no reply\n");
181             return;
182         }
183
184 #ifdef SUPPORT_DHCP
185         if(dhcp_ok) {
186                 u_int32_t leasetime;
187                 bp->bp_vend[6] = DHCPREQUEST;
188                 bp->bp_vend[7] = TAG_REQ_ADDR;
189                 bp->bp_vend[8] = 4;
190                 bcopy(&rbuf.rbootp.bp_yiaddr, &bp->bp_vend[9], 4);
191                 bp->bp_vend[13] = TAG_SERVERID;
192                 bp->bp_vend[14] = 4;
193                 bcopy(&dhcp_serverip.s_addr, &bp->bp_vend[15], 4);
194                 bp->bp_vend[19] = TAG_LEASETIME;
195                 bp->bp_vend[20] = 4;
196                 leasetime = htonl(300);
197                 bcopy(&leasetime, &bp->bp_vend[21], 4);
198                 if (flag & BOOTP_PXE) {
199                         bp->bp_vend[25] = TAG_CLASSID;
200                         bp->bp_vend[26] = 9;
201                         bcopy("PXEClient", &bp->bp_vend[27], 9);
202                         bp->bp_vend[36] = TAG_END;
203                 } else
204                         bp->bp_vend[25] = TAG_END;
205
206                 expected_dhcpmsgtype = DHCPACK;
207
208                 if(sendrecv(d,
209                             bootpsend, bp, sizeof(*bp),
210                             bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp))
211                    == -1) {
212                         printf("DHCPREQUEST failed\n");
213                         return;
214                 }
215         }
216 #endif
217
218         myip = d->myip = rbuf.rbootp.bp_yiaddr;
219         servip = rbuf.rbootp.bp_siaddr;
220         if(rootip.s_addr == INADDR_ANY) rootip = servip;
221         bcopy(rbuf.rbootp.bp_file, bootfile, sizeof(bootfile));
222         bootfile[sizeof(bootfile) - 1] = '\0';
223
224         if (!netmask) {
225                 if (IN_CLASSA(ntohl(myip.s_addr)))
226                         netmask = htonl(IN_CLASSA_NET);
227                 else if (IN_CLASSB(ntohl(myip.s_addr)))
228                         netmask = htonl(IN_CLASSB_NET);
229                 else
230                         netmask = htonl(IN_CLASSC_NET);
231 #ifdef BOOTP_DEBUG
232                 if (debug)
233                         printf("'native netmask' is %s\n", intoa(netmask));
234 #endif
235         }
236
237 #ifdef BOOTP_DEBUG
238         if (debug)
239                 printf("mask: %s\n", intoa(netmask));
240 #endif
241
242         /* We need a gateway if root is on a different net */
243         if (!SAMENET(myip, rootip, netmask)) {
244 #ifdef BOOTP_DEBUG
245                 if (debug)
246                         printf("need gateway for root ip\n");
247 #endif
248         }
249
250         /* Toss gateway if on a different net */
251         if (!SAMENET(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         /* Bump xid so next request will be unique. */
260         ++d->xid;
261 }
262
263 /* Transmit a bootp request */
264 static ssize_t
265 bootpsend(d, pkt, len)
266         struct iodesc *d;
267         void *pkt;
268         size_t len;
269 {
270         struct bootp *bp;
271
272 #ifdef BOOTP_DEBUG
273         if (debug)
274                 printf("bootpsend: d=%lx called.\n", (long)d);
275 #endif
276
277         bp = pkt;
278         bp->bp_secs = htons((u_short)(getsecs() - bot));
279
280 #ifdef BOOTP_DEBUG
281         if (debug)
282                 printf("bootpsend: calling sendudp\n");
283 #endif
284
285         return (sendudp(d, pkt, len));
286 }
287
288 static ssize_t
289 bootprecv(d, pkt, len, tleft)
290 struct iodesc *d;
291 void *pkt;
292 size_t len;
293 time_t tleft;
294 {
295         ssize_t n;
296         struct bootp *bp;
297
298 #ifdef BOOTP_DEBUGx
299         if (debug)
300                 printf("bootp_recvoffer: called\n");
301 #endif
302
303         n = readudp(d, pkt, len, tleft);
304         if (n == -1 || n < sizeof(struct bootp) - BOOTP_VENDSIZE)
305                 goto bad;
306
307         bp = (struct bootp *)pkt;
308         
309 #ifdef BOOTP_DEBUG
310         if (debug)
311                 printf("bootprecv: checked.  bp = 0x%lx, n = %d\n",
312                     (long)bp, (int)n);
313 #endif
314         if (bp->bp_xid != htonl(d->xid)) {
315 #ifdef BOOTP_DEBUG
316                 if (debug) {
317                         printf("bootprecv: expected xid 0x%lx, got 0x%x\n",
318                             d->xid, ntohl(bp->bp_xid));
319                 }
320 #endif
321                 goto bad;
322         }
323
324 #ifdef BOOTP_DEBUG
325         if (debug)
326                 printf("bootprecv: got one!\n");
327 #endif
328
329         /* Suck out vendor info */
330         if (bcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0) {
331                 if(vend_rfc1048(bp->bp_vend, sizeof(bp->bp_vend)) != 0)
332                     goto bad;
333         }
334 #ifdef BOOTP_VEND_CMU
335         else if (bcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0)
336                 vend_cmu(bp->bp_vend);
337 #endif
338         else
339                 printf("bootprecv: unknown vendor 0x%lx\n", (long)bp->bp_vend);
340
341         return(n);
342 bad:
343         errno = 0;
344         return (-1);
345 }
346
347 static int
348 vend_rfc1048(cp, len)
349         u_char *cp;
350         u_int len;
351 {
352         u_char *ep;
353         int size;
354         u_char tag;
355         const char *val;
356
357 #ifdef BOOTP_DEBUG
358         if (debug)
359                 printf("vend_rfc1048 bootp info. len=%d\n", len);
360 #endif
361         ep = cp + len;
362
363         /* Step over magic cookie */
364         cp += sizeof(int);
365
366         setenv_(cp, ep, NULL);
367
368         while (cp < ep) {
369                 tag = *cp++;
370                 size = *cp++;
371                 if (tag == TAG_END)
372                         break;
373
374                 if (tag == TAG_SUBNET_MASK) {
375                         bcopy(cp, &netmask, sizeof(netmask));
376                 }
377                 if (tag == TAG_GATEWAY) {
378                         bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr));
379                 }
380                 if (tag == TAG_SWAPSERVER) {
381                         /* let it override bp_siaddr */
382                         bcopy(cp, &rootip.s_addr, sizeof(rootip.s_addr));
383                 }
384                 if (tag == TAG_ROOTPATH) {
385                         if ((val = getenv("dhcp.root-path")) == NULL)
386                                 val = (const char *)cp;
387                         strlcpy(rootpath, val, sizeof(rootpath));
388                 }
389                 if (tag == TAG_HOSTNAME) {
390                         if ((val = getenv("dhcp.host-name")) == NULL)
391                                 val = (const char *)cp;
392                         strlcpy(hostname, val, sizeof(hostname));
393                 }
394                 if (tag == TAG_INTF_MTU) {
395                         if ((val = getenv("dhcp.interface-mtu")) != NULL) {
396                                 intf_mtu = (u_int)strtoul(val, NULL, 0);
397                         } else {
398                                 intf_mtu = be16dec(cp);
399                         }
400                 }
401 #ifdef SUPPORT_DHCP
402                 if (tag == TAG_DHCP_MSGTYPE) {
403                         if(*cp != expected_dhcpmsgtype)
404                             return(-1);
405                         dhcp_ok = 1;
406                 }
407                 if (tag == TAG_SERVERID) {
408                         bcopy(cp, &dhcp_serverip.s_addr,
409                               sizeof(dhcp_serverip.s_addr));
410                 }
411                 if (tag == TAG_TFTP_SERVER) {
412                         bcopy(cp, &tftpip.s_addr,
413                               sizeof(tftpip.s_addr));
414                 }
415 #endif
416                 cp += size;
417         }
418         return(0);
419 }
420
421 #ifdef BOOTP_VEND_CMU
422 static void
423 vend_cmu(cp)
424         u_char *cp;
425 {
426         struct cmu_vend *vp;
427
428 #ifdef BOOTP_DEBUG
429         if (debug)
430                 printf("vend_cmu bootp info.\n");
431 #endif
432         vp = (struct cmu_vend *)cp;
433
434         if (vp->v_smask.s_addr != 0) {
435                 netmask = vp->v_smask.s_addr;
436         }
437         if (vp->v_dgate.s_addr != 0) {
438                 gateip = vp->v_dgate;
439         }
440 }
441 #endif
442
443 #ifdef DHCP_ENV
444 /*
445  * Parse DHCP options and store them into kenv variables.
446  * Original code from Danny Braniss, modifications by Luigi Rizzo.
447  *
448  * The parser is driven by tables which specify the type and name of
449  * each dhcp option and how it appears in kenv.
450  * The first entry in the list contains the prefix used to set the kenv
451  * name (including the . if needed), the last entry must have a 0 tag.
452  * Entries do not need to be sorted though it helps for readability.
453  *
454  * Certain vendor-specific tables can be enabled according to DHCP_ENV.
455  * Set it to 0 if you don't want any.
456  */
457 enum opt_fmt { __NONE = 0,
458         __8 = 1, __16 = 2, __32 = 4,    /* Unsigned fields, value=size  */
459         __IP,                           /* IPv4 address                 */
460         __TXT,                          /* C string                     */
461         __BYTES,                        /* byte sequence, printed %02x  */
462         __INDIR,                        /* name=value                   */
463         __ILIST,                        /* name=value;name=value ... */
464         __VE,                           /* vendor specific, recurse     */
465 };
466
467 struct dhcp_opt {
468         uint8_t tag;
469         uint8_t fmt;
470         const char      *desc;
471 };
472
473 static struct dhcp_opt vndr_opt[] = { /* Vendor Specific Options */
474 #if DHCP_ENV == DHCP_ENV_FREEBSD /* FreeBSD table in the original code */
475         {0,     0,      "FreeBSD"},             /* prefix */
476         {1,     __TXT,  "kernel"},
477         {2,     __TXT,  "kernelname"},
478         {3,     __TXT,  "kernel_options"},
479         {4,     __IP,   "usr-ip"},
480         {5,     __TXT,  "conf-path"},
481         {6,     __TXT,  "rc.conf0"},
482         {7,     __TXT,  "rc.conf1"},
483         {8,     __TXT,  "rc.conf2"},
484         {9,     __TXT,  "rc.conf3"},
485         {10,    __TXT,  "rc.conf4"},
486         {11,    __TXT,  "rc.conf5"},
487         {12,    __TXT,  "rc.conf6"},
488         {13,    __TXT,  "rc.conf7"},
489         {14,    __TXT,  "rc.conf8"},
490         {15,    __TXT,  "rc.conf9"},
491
492         {20,    __TXT,  "boot.nfsroot.options"},
493
494         {245,   __INDIR, ""},
495         {246,   __INDIR, ""},
496         {247,   __INDIR, ""},
497         {248,   __INDIR, ""},
498         {249,   __INDIR, ""},
499         {250,   __INDIR, ""},
500         {251,   __INDIR, ""},
501         {252,   __INDIR, ""},
502         {253,   __INDIR, ""},
503         {254,   __INDIR, ""},
504
505 #elif DHCP_ENV == DHCP_ENV_PXE          /* some pxe options, RFC4578 */
506         {0,     0,      "pxe"},         /* prefix */
507         {93,    __16,   "system-architecture"},
508         {94,    __BYTES,        "network-interface"},
509         {97,    __BYTES,        "machine-identifier"},
510 #else                                   /* default (empty) table */
511         {0,     0,      "dhcp.vendor."},                /* prefix */
512 #endif
513         {0,     __TXT,  "%soption-%d"}
514 };
515
516 static struct dhcp_opt dhcp_opt[] = {
517         /* DHCP Option names, formats and codes, from RFC2132. */
518         {0,     0,      "dhcp."},       // prefix
519         {1,     __IP,   "subnet-mask"},
520         {2,     __32,   "time-offset"}, /* this is signed */
521         {3,     __IP,   "routers"},
522         {4,     __IP,   "time-servers"},
523         {5,     __IP,   "ien116-name-servers"},
524         {6,     __IP,   "domain-name-servers"},
525         {7,     __IP,   "log-servers"},
526         {8,     __IP,   "cookie-servers"},
527         {9,     __IP,   "lpr-servers"},
528         {10,    __IP,   "impress-servers"},
529         {11,    __IP,   "resource-location-servers"},
530         {12,    __TXT,  "host-name"},
531         {13,    __16,   "boot-size"},
532         {14,    __TXT,  "merit-dump"},
533         {15,    __TXT,  "domain-name"},
534         {16,    __IP,   "swap-server"},
535         {17,    __TXT,  "root-path"},
536         {18,    __TXT,  "extensions-path"},
537         {19,    __8,    "ip-forwarding"},
538         {20,    __8,    "non-local-source-routing"},
539         {21,    __IP,   "policy-filter"},
540         {22,    __16,   "max-dgram-reassembly"},
541         {23,    __8,    "default-ip-ttl"},
542         {24,    __32,   "path-mtu-aging-timeout"},
543         {25,    __16,   "path-mtu-plateau-table"},
544         {26,    __16,   "interface-mtu"},
545         {27,    __8,    "all-subnets-local"},
546         {28,    __IP,   "broadcast-address"},
547         {29,    __8,    "perform-mask-discovery"},
548         {30,    __8,    "mask-supplier"},
549         {31,    __8,    "perform-router-discovery"},
550         {32,    __IP,   "router-solicitation-address"},
551         {33,    __IP,   "static-routes"},
552         {34,    __8,    "trailer-encapsulation"},
553         {35,    __32,   "arp-cache-timeout"},
554         {36,    __8,    "ieee802-3-encapsulation"},
555         {37,    __8,    "default-tcp-ttl"},
556         {38,    __32,   "tcp-keepalive-interval"},
557         {39,    __8,    "tcp-keepalive-garbage"},
558         {40,    __TXT,  "nis-domain"},
559         {41,    __IP,   "nis-servers"},
560         {42,    __IP,   "ntp-servers"},
561         {43,    __VE,   "vendor-encapsulated-options"},
562         {44,    __IP,   "netbios-name-servers"},
563         {45,    __IP,   "netbios-dd-server"},
564         {46,    __8,    "netbios-node-type"},
565         {47,    __TXT,  "netbios-scope"},
566         {48,    __IP,   "x-font-servers"},
567         {49,    __IP,   "x-display-managers"},
568         {50,    __IP,   "dhcp-requested-address"},
569         {51,    __32,   "dhcp-lease-time"},
570         {52,    __8,    "dhcp-option-overload"},
571         {53,    __8,    "dhcp-message-type"},
572         {54,    __IP,   "dhcp-server-identifier"},
573         {55,    __8,    "dhcp-parameter-request-list"},
574         {56,    __TXT,  "dhcp-message"},
575         {57,    __16,   "dhcp-max-message-size"},
576         {58,    __32,   "dhcp-renewal-time"},
577         {59,    __32,   "dhcp-rebinding-time"},
578         {60,    __TXT,  "vendor-class-identifier"},
579         {61,    __TXT,  "dhcp-client-identifier"},
580         {64,    __TXT,  "nisplus-domain"},
581         {65,    __IP,   "nisplus-servers"},
582         {66,    __TXT,  "tftp-server-name"},
583         {67,    __TXT,  "bootfile-name"},
584         {68,    __IP,   "mobile-ip-home-agent"},
585         {69,    __IP,   "smtp-server"},
586         {70,    __IP,   "pop-server"},
587         {71,    __IP,   "nntp-server"},
588         {72,    __IP,   "www-server"},
589         {73,    __IP,   "finger-server"},
590         {74,    __IP,   "irc-server"},
591         {75,    __IP,   "streettalk-server"},
592         {76,    __IP,   "streettalk-directory-assistance-server"},
593         {77,    __TXT,  "user-class"},
594         {85,    __IP,   "nds-servers"},
595         {86,    __TXT,  "nds-tree-name"},
596         {87,    __TXT,  "nds-context"},
597         {210,   __TXT,  "authenticate"},
598
599         /* use the following entries for arbitrary variables */
600         {246,   __ILIST, ""},
601         {247,   __ILIST, ""},
602         {248,   __ILIST, ""},
603         {249,   __ILIST, ""},
604         {250,   __INDIR, ""},
605         {251,   __INDIR, ""},
606         {252,   __INDIR, ""},
607         {253,   __INDIR, ""},
608         {254,   __INDIR, ""},
609         {0,     __TXT,  "%soption-%d"}
610 };
611
612 /*
613  * parse a dhcp response, set environment variables translating options
614  * names and values according to the tables above. Also set dhcp.tags
615  * to the list of selected tags.
616  */
617 static void
618 setenv_(u_char *cp,  u_char *ep, struct dhcp_opt *opts)
619 {
620     u_char      *ncp;
621     u_char      tag;
622     char        tags[512], *tp; /* the list of tags */
623
624 #define FLD_SEP ','     /* separator in list of elements */
625     ncp = cp;
626     tp = tags;
627     if (opts == NULL)
628         opts = dhcp_opt;
629
630     while (ncp < ep) {
631         unsigned int    size;           /* option size */
632         char *vp, *endv, buf[256];      /* the value buffer */
633         struct dhcp_opt *op;
634
635         tag = *ncp++;                   /* extract tag and size */
636         size = *ncp++;
637         cp = ncp;                       /* current payload */
638         ncp += size;                    /* point to the next option */
639
640         if (tag == TAG_END)
641             break;
642         if (tag == 0)
643             continue;
644
645         for (op = opts+1; op->tag && op->tag != tag; op++)
646                 ;
647         /* if not found we end up on the default entry */
648
649         /*
650          * Copy data into the buffer. libstand does not have snprintf so we
651          * need to be careful with sprintf(). With strings, the source is
652          * always <256 char so shorter than the buffer so we are safe; with
653          * other arguments, the longest string is inet_ntoa which is 16 bytes
654          * so we make sure to have always enough room in the string before
655          * trying an sprint.
656          */
657         vp = buf;
658         *vp = '\0';
659         endv = buf + sizeof(buf) - 1 - 16;      /* last valid write position */
660
661         switch(op->fmt) {
662         case __NONE:
663             break;      /* should not happen */
664
665         case __VE: /* recurse, vendor specific */
666             setenv_(cp, cp+size, vndr_opt);
667             break;
668
669         case __IP:      /* ip address */
670             for (; size > 0 && vp < endv; size -= 4, cp += 4) {
671                 struct  in_addr in_ip;          /* ip addresses */
672                 if (vp != buf)
673                     *vp++ = FLD_SEP;
674                 bcopy(cp, &in_ip.s_addr, sizeof(in_ip.s_addr));
675                 sprintf(vp, "%s", inet_ntoa(in_ip));
676                 vp += strlen(vp);
677             }
678             break;
679
680         case __BYTES:   /* opaque byte string */
681             for (; size > 0 && vp < endv; size -= 1, cp += 1) {
682                 sprintf(vp, "%02x", *cp);
683                 vp += strlen(vp);
684             }
685             break;
686
687         case __TXT:
688             bcopy(cp, buf, size);       /* cannot overflow */
689             buf[size] = 0;
690             break;
691
692         case __32:
693         case __16:
694         case __8:       /* op->fmt is also the length of each field */
695             for (; size > 0 && vp < endv; size -= op->fmt, cp += op->fmt) {
696                 uint32_t v;
697                 if (op->fmt == __32)
698                         v = (cp[0]<<24) + (cp[1]<<16) + (cp[2]<<8) + cp[3];
699                 else if (op->fmt == __16)
700                         v = (cp[0]<<8) + cp[1];
701                 else
702                         v = cp[0];
703                 if (vp != buf)
704                     *vp++ = FLD_SEP;
705                 sprintf(vp, "%u", v);
706                 vp += strlen(vp);
707             }
708             break;
709
710         case __INDIR:   /* name=value */
711         case __ILIST:   /* name=value;name=value... */
712             bcopy(cp, buf, size);       /* cannot overflow */
713             buf[size] = '\0';
714             for (endv = buf; endv; endv = vp) {
715                 u_char *s = NULL;       /* semicolon ? */
716
717                 /* skip leading whitespace */
718                 while (*endv && strchr(" \t\n\r", *endv))
719                     endv++;
720                 vp = strchr(endv, '='); /* find name=value separator */
721                 if (!vp)
722                     break;
723                 *vp++ = 0;
724                 if (op->fmt == __ILIST && (s = strchr(vp, ';')))
725                     *s++ = '\0';
726                 setenv(endv, vp, 1);
727                 vp = s; /* prepare for next round */
728             }
729             buf[0] = '\0';      /* option already done */
730         }
731
732         if (tp - tags < sizeof(tags) - 5) {     /* add tag to the list */
733             if (tp != tags)
734                 *tp++ = FLD_SEP;
735             sprintf(tp, "%d", tag);
736             tp += strlen(tp);
737         }
738         if (buf[0]) {
739             char        env[128];       /* the string name */
740
741             if (op->tag == 0)
742                 sprintf(env, op->desc, opts[0].desc, tag);
743             else
744                 sprintf(env, "%s%s", opts[0].desc, op->desc);
745             /*
746              * Do not replace existing values in the environment, so that
747              * locally-obtained values can override server-provided values.
748              */
749             setenv(env, buf, 0);
750         }
751     }
752     if (tp != tags) {
753         char    env[128];       /* the string name */
754         sprintf(env, "%stags", opts[0].desc);
755         setenv(env, tags, 1);
756     }
757 }
758 #endif /* additional dhcp */