2 * dovend.c : Inserts all but the first few vendor options.
9 #include <netinet/in.h>
10 #include <arpa/inet.h> /* inet_ntoa */
23 PRIVATE int insert_generic(struct shared_bindata *, byte **, int *);
26 * Insert the 2nd part of the options into an option buffer.
27 * Return amount of space used.
29 * This inserts everything EXCEPT:
30 * magic cookie, subnet mask, gateway, bootsize, extension file
31 * Those are handled separately (in bootpd.c) to allow this function
32 * to be shared between bootpd and bootpef.
34 * When an "extension file" is in use, the options inserted by
35 * this function go into the exten_file, not the bootp response.
39 dovend_rfc1497(hp, buf, len)
47 static const char noroom[] = "%s: No room for \"%s\" option";
48 #define NEED(LEN, MSG) do \
49 if (bytesleft < (LEN)) { \
50 report(LOG_NOTICE, noroom, \
51 hp->hostname->string, MSG); \
56 * Note that the following have already been inserted:
57 * magic_cookie, subnet_mask, gateway, bootsize
59 * The remaining options are inserted in order of importance.
60 * (Of course the importance of each is a matter of opinion.)
61 * The option insertion order should probably be configurable.
63 * This is the order used in the NetBSD version. Can anyone
64 * explain why the time_offset and swap_server are first?
65 * Also, why is the hostname so far down the list? -gwr
68 if (hp->flags.time_offset) {
70 *vp++ = TAG_TIME_OFFSET;/* -1 byte */
71 *vp++ = 4; /* -1 byte */
72 insert_u_long(htonl(hp->time_offset), &vp); /* -4 bytes */
76 * swap server, root path, dump path
78 if (hp->flags.swap_server) {
80 /* There is just one SWAP_SERVER, so it is not an iplist. */
81 *vp++ = TAG_SWAP_SERVER;/* -1 byte */
82 *vp++ = 4; /* -1 byte */
83 insert_u_long(hp->swap_server.s_addr, &vp); /* -4 bytes */
84 bytesleft -= 6; /* Fix real count */
86 if (hp->flags.root_path) {
88 * Check for room for root_path. Add 2 to account for
89 * TAG_ROOT_PATH and length.
91 len = strlen(hp->root_path->string);
92 NEED((len + 2), "rp");
93 *vp++ = TAG_ROOT_PATH;
94 *vp++ = (byte) (len & 0xFF);
95 bcopy(hp->root_path->string, vp, len);
99 if (hp->flags.dump_file) {
101 * Check for room for dump_file. Add 2 to account for
102 * TAG_DUMP_FILE and length.
104 len = strlen(hp->dump_file->string);
105 NEED((len + 2), "df");
106 *vp++ = TAG_DUMP_FILE;
107 *vp++ = (byte) (len & 0xFF);
108 bcopy(hp->dump_file->string, vp, len);
110 bytesleft -= len + 2;
113 * DNS server and domain
115 if (hp->flags.domain_server) {
116 if (insert_ip(TAG_DOMAIN_SERVER,
121 if (hp->flags.domain_name) {
123 * Check for room for domain_name. Add 2 to account for
124 * TAG_DOMAIN_NAME and length.
126 len = strlen(hp->domain_name->string);
127 NEED((len + 2), "dn");
128 *vp++ = TAG_DOMAIN_NAME;
129 *vp++ = (byte) (len & 0xFF);
130 bcopy(hp->domain_name->string, vp, len);
132 bytesleft -= len + 2;
135 * NIS (YP) server and domain
137 if (hp->flags.nis_server) {
138 if (insert_ip(TAG_NIS_SERVER,
143 if (hp->flags.nis_domain) {
145 * Check for room for nis_domain. Add 2 to account for
146 * TAG_NIS_DOMAIN and length.
148 len = strlen(hp->nis_domain->string);
149 NEED((len + 2), "yn");
150 *vp++ = TAG_NIS_DOMAIN;
151 *vp++ = (byte) (len & 0xFF);
152 bcopy(hp->nis_domain->string, vp, len);
154 bytesleft -= len + 2;
156 /* IEN 116 name server */
157 if (hp->flags.name_server) {
158 if (insert_ip(TAG_NAME_SERVER,
163 if (hp->flags.rlp_server) {
164 if (insert_ip(TAG_RLP_SERVER,
169 /* Time server (RFC 868) */
170 if (hp->flags.time_server) {
171 if (insert_ip(TAG_TIME_SERVER,
176 /* NTP (time) Server (RFC 1129) */
177 if (hp->flags.ntp_server) {
178 if (insert_ip(TAG_NTP_SERVER,
184 * I wonder: If the hostname were "promoted" into the BOOTP
185 * response part, might these "extension" files possibly be
186 * shared between several clients?
188 * Also, why not just use longer BOOTP packets with all the
189 * additional length used as option data. This bootpd version
190 * already supports that feature by replying with the same
191 * packet length as the client request packet. -gwr
193 if (hp->flags.name_switch && hp->flags.send_name) {
195 * Check for room for hostname. Add 2 to account for
196 * TAG_HOST_NAME and length.
198 len = strlen(hp->hostname->string);
201 * XXX - Too much magic. The user can always set the hostname
202 * to the short version in the bootptab file. -gwr
204 if ((len + 2) > bytesleft) {
206 * Not enough room for full (domain-qualified) hostname, try
207 * stripping it down to just the first field (host).
209 char *tmpstr = hp->hostname->string;
211 while (*tmpstr && (*tmpstr != '.')) {
217 NEED((len + 2), "hn");
218 *vp++ = TAG_HOST_NAME;
219 *vp++ = (byte) (len & 0xFF);
220 bcopy(hp->hostname->string, vp, len);
222 bytesleft -= len + 2;
225 * The rest of these are less important, so they go last.
227 if (hp->flags.lpr_server) {
228 if (insert_ip(TAG_LPR_SERVER,
233 if (hp->flags.cookie_server) {
234 if (insert_ip(TAG_COOKIE_SERVER,
239 if (hp->flags.log_server) {
240 if (insert_ip(TAG_LOG_SERVER,
246 * XXX - Add new tags here (to insert options)
248 if (hp->flags.generic) {
249 if (insert_generic(hp->generic, &vp, &bytesleft))
250 NEED(64, "(generic)");
253 * The end marker is inserted by the caller.
257 } /* dovend_rfc1497 */
262 * Insert a tag value, a length value, and a list of IP addresses into the
263 * memory buffer indirectly pointed to by "dest". "tag" is the RFC1048 tag
264 * number to use, "iplist" is a pointer to a list of IP addresses
265 * (struct in_addr_list), and "bytesleft" points to an integer which
266 * indicates the size of the "dest" buffer.
268 * Return zero if everything fits.
270 * This is used to fill the vendor-specific area of a bootp packet in
271 * conformance to RFC1048.
275 insert_ip(tag, iplist, dest, bytesleft)
277 struct in_addr_list *iplist;
281 struct in_addr *addrptr;
282 unsigned addrcount = 1;
288 if (*bytesleft >= 6) {
289 d = *dest; /* Save pointer for later */
292 (*bytesleft) -= 2; /* Account for tag and length */
293 addrptr = iplist->addr;
294 addrcount = iplist->addrcount;
295 while ((*bytesleft >= 4) && (addrcount > 0)) {
296 insert_u_long(addrptr->s_addr, dest);
299 (*bytesleft) -= 4; /* Four bytes per address */
301 d[1] = (byte) ((*dest - d - 2) & 0xFF);
309 * Insert generic data into a bootp packet. The data is assumed to already
310 * be in RFC1048 format. It is inserted using a first-fit algorithm which
311 * attempts to insert as many tags as possible. Tags and data which are
312 * too large to fit are skipped; any remaining tags are tried until they
313 * have all been exhausted.
314 * Return zero if everything fits.
318 insert_generic(gendata, buff, bytesleft)
319 struct shared_bindata *gendata;
324 int length, numbytes;
330 srcptr = gendata->data;
331 length = gendata->length;
332 while ((length > 0) && (*bytesleft > 0)) {
335 length = 0; /* Force an exit on next iteration */
338 *(*buff)++ = *srcptr++;
343 numbytes = srcptr[1] + 2;
344 if (*bytesleft < numbytes)
347 bcopy(srcptr, *buff, numbytes);
349 (*bytesleft) -= numbytes;
360 * Insert the unsigned long "value" into memory starting at the byte
361 * pointed to by the byte pointer (*dest). (*dest) is updated to
362 * point to the next available byte.
364 * Since it is desirable to internally store network addresses in network
365 * byte order (in struct in_addr's), this routine expects longs to be
366 * passed in network byte order.
368 * However, due to the nature of the main algorithm, the long must be in
369 * host byte order, thus necessitating the use of ntohl() first.
373 insert_u_long(value, dest)
380 value = ntohl(value); /* Must use host byte order here */
382 for (n = 4; n > 0; n--) {
383 *--temp = (byte) (value & 0xFF);
386 /* Final result is network byte order */
393 * c-argdecl-indent: 4
394 * c-continued-statement-offset: 4
395 * c-continued-brace-offset: -4