xref: /freebsd/libexec/bootpd/dovend.c (revision 5ed136e814a98e845927fffa6aa7c497dbd8fa5e)
1 /*
2  * dovend.c : Inserts all but the first few vendor options.
3  *
4  * $FreeBSD$
5  */
6 
7 #include <sys/types.h>
8 
9 #include <netinet/in.h>
10 #include <arpa/inet.h>			/* inet_ntoa */
11 
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <syslog.h>
17 
18 #ifndef USE_BFUNCS
19 # include <memory.h>
20 /* Yes, memcpy is OK here (no overlapped copies). */
21 # define bcopy(a,b,c)    memcpy(b,a,c)
22 # define bzero(p,l)      memset(p,0,l)
23 # define bcmp(a,b,c)     memcmp(a,b,c)
24 # define index           strchr
25 #endif
26 
27 #include "bootp.h"
28 #include "bootpd.h"
29 #include "report.h"
30 #include "dovend.h"
31 
32 #define P(args) args
33 
34 PRIVATE int insert_generic P((struct shared_bindata *, byte **, int *));
35 
36 /*
37  * Insert the 2nd part of the options into an option buffer.
38  * Return amount of space used.
39  *
40  * This inserts everything EXCEPT:
41  *   magic cookie, subnet mask, gateway, bootsize, extension file
42  * Those are handled separately (in bootpd.c) to allow this function
43  * to be shared between bootpd and bootpef.
44  *
45  * When an "extension file" is in use, the options inserted by
46  * this function go into the exten_file, not the bootp response.
47  */
48 
49 int
50 dovend_rfc1497(hp, buf, len)
51 	struct host *hp;
52 	byte *buf;
53 	int len;
54 {
55 	int bytesleft = len;
56 	byte *vp = buf;
57 
58 	static const char noroom[] = "%s: No room for \"%s\" option";
59 #define	NEED(LEN, MSG) do                       \
60 		if (bytesleft < (LEN)) {         	    \
61 			report(LOG_NOTICE, noroom,          \
62 				   hp->hostname->string, MSG);  \
63 			return (vp - buf);                  \
64 		} while (0)
65 
66 	/*
67 	 * Note that the following have already been inserted:
68 	 *   magic_cookie, subnet_mask, gateway, bootsize
69 	 *
70 	 * The remaining options are inserted in order of importance.
71 	 * (Of course the importance of each is a matter of opinion.)
72 	 * The option insertion order should probably be configurable.
73 	 *
74 	 * This is the order used in the NetBSD version.  Can anyone
75 	 * explain why the time_offset and swap_server are first?
76 	 * Also, why is the hostname so far down the list?  -gwr
77 	 */
78 
79 	if (hp->flags.time_offset) {
80 		NEED(6, "to");
81 		*vp++ = TAG_TIME_OFFSET;/* -1 byte  */
82 		*vp++ = 4;				/* -1 byte  */
83 		insert_u_long(htonl(hp->time_offset), &vp);	/* -4 bytes */
84 		bytesleft -= 6;
85 	}
86 	/*
87 	 * swap server, root path, dump path
88 	 */
89 	if (hp->flags.swap_server) {
90 		NEED(6, "sw");
91 		/* There is just one SWAP_SERVER, so it is not an iplist. */
92 		*vp++ = TAG_SWAP_SERVER;/* -1 byte  */
93 		*vp++ = 4;				/* -1 byte  */
94 		insert_u_long(hp->swap_server.s_addr, &vp);	/* -4 bytes */
95 		bytesleft -= 6;			/* Fix real count */
96 	}
97 	if (hp->flags.root_path) {
98 		/*
99 		 * Check for room for root_path.  Add 2 to account for
100 		 * TAG_ROOT_PATH and length.
101 		 */
102 		len = strlen(hp->root_path->string);
103 		NEED((len + 2), "rp");
104 		*vp++ = TAG_ROOT_PATH;
105 		*vp++ = (byte) (len & 0xFF);
106 		bcopy(hp->root_path->string, vp, len);
107 		vp += len;
108 		bytesleft -= len + 2;
109 	}
110 	if (hp->flags.dump_file) {
111 		/*
112 		 * Check for room for dump_file.  Add 2 to account for
113 		 * TAG_DUMP_FILE and length.
114 		 */
115 		len = strlen(hp->dump_file->string);
116 		NEED((len + 2), "df");
117 		*vp++ = TAG_DUMP_FILE;
118 		*vp++ = (byte) (len & 0xFF);
119 		bcopy(hp->dump_file->string, vp, len);
120 		vp += len;
121 		bytesleft -= len + 2;
122 	}
123 	/*
124 	 * DNS server and domain
125 	 */
126 	if (hp->flags.domain_server) {
127 		if (insert_ip(TAG_DOMAIN_SERVER,
128 					  hp->domain_server,
129 					  &vp, &bytesleft))
130 			NEED(8, "ds");
131 	}
132 	if (hp->flags.domain_name) {
133 		/*
134 		 * Check for room for domain_name.  Add 2 to account for
135 		 * TAG_DOMAIN_NAME and length.
136 		 */
137 		len = strlen(hp->domain_name->string);
138 		NEED((len + 2), "dn");
139 		*vp++ = TAG_DOMAIN_NAME;
140 		*vp++ = (byte) (len & 0xFF);
141 		bcopy(hp->domain_name->string, vp, len);
142 		vp += len;
143 		bytesleft -= len + 2;
144 	}
145 	/*
146 	 * NIS (YP) server and domain
147 	 */
148 	if (hp->flags.nis_server) {
149 		if (insert_ip(TAG_NIS_SERVER,
150 					  hp->nis_server,
151 					  &vp, &bytesleft))
152 			NEED(8, "ds");
153 	}
154 	if (hp->flags.nis_domain) {
155 		/*
156 		 * Check for room for nis_domain.  Add 2 to account for
157 		 * TAG_NIS_DOMAIN and length.
158 		 */
159 		len = strlen(hp->nis_domain->string);
160 		NEED((len + 2), "dn");
161 		*vp++ = TAG_NIS_DOMAIN;
162 		*vp++ = (byte) (len & 0xFF);
163 		bcopy(hp->nis_domain->string, vp, len);
164 		vp += len;
165 		bytesleft -= len + 2;
166 	}
167 	/* IEN 116 name server */
168 	if (hp->flags.name_server) {
169 		if (insert_ip(TAG_NAME_SERVER,
170 					  hp->name_server,
171 					  &vp, &bytesleft))
172 			NEED(8, "ns");
173 	}
174 	if (hp->flags.rlp_server) {
175 		if (insert_ip(TAG_RLP_SERVER,
176 					  hp->rlp_server,
177 					  &vp, &bytesleft))
178 			NEED(8, "rl");
179 	}
180 	/* Time server (RFC 868) */
181 	if (hp->flags.time_server) {
182 		if (insert_ip(TAG_TIME_SERVER,
183 					  hp->time_server,
184 					  &vp, &bytesleft))
185 			NEED(8, "ts");
186 	}
187 	/* NTP (time) Server (RFC 1129) */
188 	if (hp->flags.ntp_server) {
189 		if (insert_ip(TAG_NTP_SERVER,
190 					  hp->ntp_server,
191 					  &vp, &bytesleft))
192 			NEED(8, "ts");
193 	}
194 	/*
195 	 * I wonder:  If the hostname were "promoted" into the BOOTP
196 	 * response part, might these "extension" files possibly be
197 	 * shared between several clients?
198 	 *
199 	 * Also, why not just use longer BOOTP packets with all the
200 	 * additional length used as option data.  This bootpd version
201 	 * already supports that feature by replying with the same
202 	 * packet length as the client request packet. -gwr
203 	 */
204 	if (hp->flags.name_switch && hp->flags.send_name) {
205 		/*
206 		 * Check for room for hostname.  Add 2 to account for
207 		 * TAG_HOST_NAME and length.
208 		 */
209 		len = strlen(hp->hostname->string);
210 #if 0
211 		/*
212 		 * XXX - Too much magic.  The user can always set the hostname
213 		 * to the short version in the bootptab file. -gwr
214 		 */
215 		if ((len + 2) > bytesleft) {
216 			/*
217 			 * Not enough room for full (domain-qualified) hostname, try
218 			 * stripping it down to just the first field (host).
219 			 */
220 			char *tmpstr = hp->hostname->string;
221 			len = 0;
222 			while (*tmpstr && (*tmpstr != '.')) {
223 				tmpstr++;
224 				len++;
225 			}
226 		}
227 #endif
228 		NEED((len + 2), "hn");
229 		*vp++ = TAG_HOST_NAME;
230 		*vp++ = (byte) (len & 0xFF);
231 		bcopy(hp->hostname->string, vp, len);
232 		vp += len;
233 		bytesleft -= len + 2;
234 	}
235 	/*
236 	 * The rest of these are less important, so they go last.
237 	 */
238 	if (hp->flags.lpr_server) {
239 		if (insert_ip(TAG_LPR_SERVER,
240 					  hp->lpr_server,
241 					  &vp, &bytesleft))
242 			NEED(8, "lp");
243 	}
244 	if (hp->flags.cookie_server) {
245 		if (insert_ip(TAG_COOKIE_SERVER,
246 					  hp->cookie_server,
247 					  &vp, &bytesleft))
248 			NEED(8, "cs");
249 	}
250 	if (hp->flags.log_server) {
251 		if (insert_ip(TAG_LOG_SERVER,
252 					  hp->log_server,
253 					  &vp, &bytesleft))
254 			NEED(8, "lg");
255 	}
256 	/*
257 	 * XXX - Add new tags here (to insert options)
258 	 */
259 	if (hp->flags.generic) {
260 		if (insert_generic(hp->generic, &vp, &bytesleft))
261 			NEED(64, "(generic)");
262 	}
263 	/*
264 	 * The end marker is inserted by the caller.
265 	 */
266 	return (vp - buf);
267 #undef	NEED
268 }								/* dovend_rfc1497 */
269 
270 
271 
272 /*
273  * Insert a tag value, a length value, and a list of IP addresses into the
274  * memory buffer indirectly pointed to by "dest".  "tag" is the RFC1048 tag
275  * number to use, "iplist" is a pointer to a list of IP addresses
276  * (struct in_addr_list), and "bytesleft" points to an integer which
277  * indicates the size of the "dest" buffer.
278  *
279  * Return zero if everything fits.
280  *
281  * This is used to fill the vendor-specific area of a bootp packet in
282  * conformance to RFC1048.
283  */
284 
285 int
286 insert_ip(tag, iplist, dest, bytesleft)
287 	byte tag;
288 	struct in_addr_list *iplist;
289 	byte **dest;
290 	int *bytesleft;
291 {
292 	struct in_addr *addrptr;
293 	unsigned addrcount = 1;
294 	byte *d;
295 
296 	if (iplist == NULL)
297 		return (0);
298 
299 	if (*bytesleft >= 6) {
300 		d = *dest;				/* Save pointer for later */
301 		**dest = tag;
302 		(*dest) += 2;
303 		(*bytesleft) -= 2;		/* Account for tag and length */
304 		addrptr = iplist->addr;
305 		addrcount = iplist->addrcount;
306 		while ((*bytesleft >= 4) && (addrcount > 0)) {
307 			insert_u_long(addrptr->s_addr, dest);
308 			addrptr++;
309 			addrcount--;
310 			(*bytesleft) -= 4;	/* Four bytes per address */
311 		}
312 		d[1] = (byte) ((*dest - d - 2) & 0xFF);
313 	}
314 	return (addrcount);
315 }
316 
317 
318 
319 /*
320  * Insert generic data into a bootp packet.  The data is assumed to already
321  * be in RFC1048 format.  It is inserted using a first-fit algorithm which
322  * attempts to insert as many tags as possible.  Tags and data which are
323  * too large to fit are skipped; any remaining tags are tried until they
324  * have all been exhausted.
325  * Return zero if everything fits.
326  */
327 
328 static int
329 insert_generic(gendata, buff, bytesleft)
330 	struct shared_bindata *gendata;
331 	byte **buff;
332 	int *bytesleft;
333 {
334 	byte *srcptr;
335 	int length, numbytes;
336 	int skipped = 0;
337 
338 	if (gendata == NULL)
339 		return (0);
340 
341 	srcptr = gendata->data;
342 	length = gendata->length;
343 	while ((length > 0) && (*bytesleft > 0)) {
344 		switch (*srcptr) {
345 		case TAG_END:
346 			length = 0;			/* Force an exit on next iteration */
347 			break;
348 		case TAG_PAD:
349 			*(*buff)++ = *srcptr++;
350 			(*bytesleft)--;
351 			length--;
352 			break;
353 		default:
354 			numbytes = srcptr[1] + 2;
355 			if (*bytesleft < numbytes)
356 				skipped += numbytes;
357 			else {
358 				bcopy(srcptr, *buff, numbytes);
359 				(*buff) += numbytes;
360 				(*bytesleft) -= numbytes;
361 			}
362 			srcptr += numbytes;
363 			length -= numbytes;
364 			break;
365 		}
366 	} /* while */
367 	return (skipped);
368 }
369 
370 /*
371  * Insert the unsigned long "value" into memory starting at the byte
372  * pointed to by the byte pointer (*dest).  (*dest) is updated to
373  * point to the next available byte.
374  *
375  * Since it is desirable to internally store network addresses in network
376  * byte order (in struct in_addr's), this routine expects longs to be
377  * passed in network byte order.
378  *
379  * However, due to the nature of the main algorithm, the long must be in
380  * host byte order, thus necessitating the use of ntohl() first.
381  */
382 
383 void
384 insert_u_long(value, dest)
385 	u_int32 value;
386 	byte **dest;
387 {
388 	byte *temp;
389 	int n;
390 
391 	value = ntohl(value);		/* Must use host byte order here */
392 	temp = (*dest += 4);
393 	for (n = 4; n > 0; n--) {
394 		*--temp = (byte) (value & 0xFF);
395 		value >>= 8;
396 	}
397 	/* Final result is network byte order */
398 }
399 
400 /*
401  * Local Variables:
402  * tab-width: 4
403  * c-indent-level: 4
404  * c-argdecl-indent: 4
405  * c-continued-statement-offset: 4
406  * c-continued-brace-offset: -4
407  * c-label-offset: -4
408  * c-brace-offset: 0
409  * End:
410  */
411