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