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