2 * dovend.c : Inserts all but the first few vendor options.
4 * $FreeBSD: src/libexec/bootpd/dovend.c,v 1.5.2.1 2003/02/15 05:36:01 kris Exp $
9 #include <netinet/in.h>
10 #include <arpa/inet.h> /* inet_ntoa */
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)
38 PRIVATE int insert_generic P((struct shared_bindata *, byte **, int *));
41 * Insert the 2nd part of the options into an option buffer.
42 * Return amount of space used.
44 * This inserts everything EXCEPT:
45 * magic cookie, subnet mask, gateway, bootsize, extension file
46 * Those are handled separately (in bootpd.c) to allow this function
47 * to be shared between bootpd and bootpef.
49 * When an "extension file" is in use, the options inserted by
50 * this function go into the exten_file, not the bootp response.
54 dovend_rfc1497(hp, buf, len)
62 static const char noroom[] = "%s: No room for \"%s\" option";
63 #define NEED(LEN, MSG) do \
64 if (bytesleft < (LEN)) { \
65 report(LOG_NOTICE, noroom, \
66 hp->hostname->string, MSG); \
71 * Note that the following have already been inserted:
72 * magic_cookie, subnet_mask, gateway, bootsize
74 * The remaining options are inserted in order of importance.
75 * (Of course the importance of each is a matter of opinion.)
76 * The option insertion order should probably be configurable.
78 * This is the order used in the NetBSD version. Can anyone
79 * explain why the time_offset and swap_server are first?
80 * Also, why is the hostname so far down the list? -gwr
83 if (hp->flags.time_offset) {
85 *vp++ = TAG_TIME_OFFSET;/* -1 byte */
86 *vp++ = 4; /* -1 byte */
87 insert_u_long(htonl(hp->time_offset), &vp); /* -4 bytes */
91 * swap server, root path, dump path
93 if (hp->flags.swap_server) {
95 /* There is just one SWAP_SERVER, so it is not an iplist. */
96 *vp++ = TAG_SWAP_SERVER;/* -1 byte */
97 *vp++ = 4; /* -1 byte */
98 insert_u_long(hp->swap_server.s_addr, &vp); /* -4 bytes */
99 bytesleft -= 6; /* Fix real count */
101 if (hp->flags.root_path) {
103 * Check for room for root_path. Add 2 to account for
104 * TAG_ROOT_PATH and length.
106 len = strlen(hp->root_path->string);
107 NEED((len + 2), "rp");
108 *vp++ = TAG_ROOT_PATH;
109 *vp++ = (byte) (len & 0xFF);
110 bcopy(hp->root_path->string, vp, len);
112 bytesleft -= len + 2;
114 if (hp->flags.dump_file) {
116 * Check for room for dump_file. Add 2 to account for
117 * TAG_DUMP_FILE and length.
119 len = strlen(hp->dump_file->string);
120 NEED((len + 2), "df");
121 *vp++ = TAG_DUMP_FILE;
122 *vp++ = (byte) (len & 0xFF);
123 bcopy(hp->dump_file->string, vp, len);
125 bytesleft -= len + 2;
128 * DNS server and domain
130 if (hp->flags.domain_server) {
131 if (insert_ip(TAG_DOMAIN_SERVER,
136 if (hp->flags.domain_name) {
138 * Check for room for domain_name. Add 2 to account for
139 * TAG_DOMAIN_NAME and length.
141 len = strlen(hp->domain_name->string);
142 NEED((len + 2), "dn");
143 *vp++ = TAG_DOMAIN_NAME;
144 *vp++ = (byte) (len & 0xFF);
145 bcopy(hp->domain_name->string, vp, len);
147 bytesleft -= len + 2;
150 * NIS (YP) server and domain
152 if (hp->flags.nis_server) {
153 if (insert_ip(TAG_NIS_SERVER,
158 if (hp->flags.nis_domain) {
160 * Check for room for nis_domain. Add 2 to account for
161 * TAG_NIS_DOMAIN and length.
163 len = strlen(hp->nis_domain->string);
164 NEED((len + 2), "dn");
165 *vp++ = TAG_NIS_DOMAIN;
166 *vp++ = (byte) (len & 0xFF);
167 bcopy(hp->nis_domain->string, vp, len);
169 bytesleft -= len + 2;
171 /* IEN 116 name server */
172 if (hp->flags.name_server) {
173 if (insert_ip(TAG_NAME_SERVER,
178 if (hp->flags.rlp_server) {
179 if (insert_ip(TAG_RLP_SERVER,
184 /* Time server (RFC 868) */
185 if (hp->flags.time_server) {
186 if (insert_ip(TAG_TIME_SERVER,
191 /* NTP (time) Server (RFC 1129) */
192 if (hp->flags.ntp_server) {
193 if (insert_ip(TAG_NTP_SERVER,
199 * I wonder: If the hostname were "promoted" into the BOOTP
200 * response part, might these "extension" files possibly be
201 * shared between several clients?
203 * Also, why not just use longer BOOTP packets with all the
204 * additional length used as option data. This bootpd version
205 * already supports that feature by replying with the same
206 * packet length as the client request packet. -gwr
208 if (hp->flags.name_switch && hp->flags.send_name) {
210 * Check for room for hostname. Add 2 to account for
211 * TAG_HOST_NAME and length.
213 len = strlen(hp->hostname->string);
216 * XXX - Too much magic. The user can always set the hostname
217 * to the short version in the bootptab file. -gwr
219 if ((len + 2) > bytesleft) {
221 * Not enough room for full (domain-qualified) hostname, try
222 * stripping it down to just the first field (host).
224 char *tmpstr = hp->hostname->string;
226 while (*tmpstr && (*tmpstr != '.')) {
232 NEED((len + 2), "hn");
233 *vp++ = TAG_HOST_NAME;
234 *vp++ = (byte) (len & 0xFF);
235 bcopy(hp->hostname->string, vp, len);
237 bytesleft -= len + 2;
240 * The rest of these are less important, so they go last.
242 if (hp->flags.lpr_server) {
243 if (insert_ip(TAG_LPR_SERVER,
248 if (hp->flags.cookie_server) {
249 if (insert_ip(TAG_COOKIE_SERVER,
254 if (hp->flags.log_server) {
255 if (insert_ip(TAG_LOG_SERVER,
261 * XXX - Add new tags here (to insert options)
263 if (hp->flags.generic) {
264 if (insert_generic(hp->generic, &vp, &bytesleft))
265 NEED(64, "(generic)");
268 * The end marker is inserted by the caller.
272 } /* dovend_rfc1497 */
277 * Insert a tag value, a length value, and a list of IP addresses into the
278 * memory buffer indirectly pointed to by "dest". "tag" is the RFC1048 tag
279 * number to use, "iplist" is a pointer to a list of IP addresses
280 * (struct in_addr_list), and "bytesleft" points to an integer which
281 * indicates the size of the "dest" buffer.
283 * Return zero if everything fits.
285 * This is used to fill the vendor-specific area of a bootp packet in
286 * conformance to RFC1048.
290 insert_ip(tag, iplist, dest, bytesleft)
292 struct in_addr_list *iplist;
296 struct in_addr *addrptr;
297 unsigned addrcount = 1;
303 if (*bytesleft >= 6) {
304 d = *dest; /* Save pointer for later */
307 (*bytesleft) -= 2; /* Account for tag and length */
308 addrptr = iplist->addr;
309 addrcount = iplist->addrcount;
310 while ((*bytesleft >= 4) && (addrcount > 0)) {
311 insert_u_long(addrptr->s_addr, dest);
314 (*bytesleft) -= 4; /* Four bytes per address */
316 d[1] = (byte) ((*dest - d - 2) & 0xFF);
324 * Insert generic data into a bootp packet. The data is assumed to already
325 * be in RFC1048 format. It is inserted using a first-fit algorithm which
326 * attempts to insert as many tags as possible. Tags and data which are
327 * too large to fit are skipped; any remaining tags are tried until they
328 * have all been exhausted.
329 * Return zero if everything fits.
333 insert_generic(gendata, buff, bytesleft)
334 struct shared_bindata *gendata;
339 int length, numbytes;
345 srcptr = gendata->data;
346 length = gendata->length;
347 while ((length > 0) && (*bytesleft > 0)) {
350 length = 0; /* Force an exit on next iteration */
353 *(*buff)++ = *srcptr++;
358 numbytes = srcptr[1] + 2;
359 if (*bytesleft < numbytes)
362 bcopy(srcptr, *buff, numbytes);
364 (*bytesleft) -= numbytes;
375 * Insert the unsigned long "value" into memory starting at the byte
376 * pointed to by the byte pointer (*dest). (*dest) is updated to
377 * point to the next available byte.
379 * Since it is desirable to internally store network addresses in network
380 * byte order (in struct in_addr's), this routine expects longs to be
381 * passed in network byte order.
383 * However, due to the nature of the main algorithm, the long must be in
384 * host byte order, thus necessitating the use of ntohl() first.
388 insert_u_long(value, dest)
395 value = ntohl(value); /* Must use host byte order here */
397 for (n = 4; n > 0; n--) {
398 *--temp = (byte) (value & 0xFF);
401 /* Final result is network byte order */
408 * c-argdecl-indent: 4
409 * c-continued-statement-offset: 4
410 * c-continued-brace-offset: -4