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