2 * dovend.c : Inserts all but the first few vendor options.
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)
32 PRIVATE int insert_generic(struct shared_bindata *, byte **, int *);
35 * Insert the 2nd part of the options into an option buffer.
36 * Return amount of space used.
38 * This inserts everything EXCEPT:
39 * magic cookie, subnet mask, gateway, bootsize, extension file
40 * Those are handled separately (in bootpd.c) to allow this function
41 * to be shared between bootpd and bootpef.
43 * When an "extension file" is in use, the options inserted by
44 * this function go into the exten_file, not the bootp response.
48 dovend_rfc1497(hp, buf, len)
56 static const char noroom[] = "%s: No room for \"%s\" option";
57 #define NEED(LEN, MSG) do \
58 if (bytesleft < (LEN)) { \
59 report(LOG_NOTICE, noroom, \
60 hp->hostname->string, MSG); \
65 * Note that the following have already been inserted:
66 * magic_cookie, subnet_mask, gateway, bootsize
68 * The remaining options are inserted in order of importance.
69 * (Of course the importance of each is a matter of opinion.)
70 * The option insertion order should probably be configurable.
72 * This is the order used in the NetBSD version. Can anyone
73 * explain why the time_offset and swap_server are first?
74 * Also, why is the hostname so far down the list? -gwr
77 if (hp->flags.time_offset) {
79 *vp++ = TAG_TIME_OFFSET;/* -1 byte */
80 *vp++ = 4; /* -1 byte */
81 insert_u_long(htonl(hp->time_offset), &vp); /* -4 bytes */
85 * swap server, root path, dump path
87 if (hp->flags.swap_server) {
89 /* There is just one SWAP_SERVER, so it is not an iplist. */
90 *vp++ = TAG_SWAP_SERVER;/* -1 byte */
91 *vp++ = 4; /* -1 byte */
92 insert_u_long(hp->swap_server.s_addr, &vp); /* -4 bytes */
93 bytesleft -= 6; /* Fix real count */
95 if (hp->flags.root_path) {
97 * Check for room for root_path. Add 2 to account for
98 * TAG_ROOT_PATH and length.
100 len = strlen(hp->root_path->string);
101 NEED((len + 2), "rp");
102 *vp++ = TAG_ROOT_PATH;
103 *vp++ = (byte) (len & 0xFF);
104 bcopy(hp->root_path->string, vp, len);
106 bytesleft -= len + 2;
108 if (hp->flags.dump_file) {
110 * Check for room for dump_file. Add 2 to account for
111 * TAG_DUMP_FILE and length.
113 len = strlen(hp->dump_file->string);
114 NEED((len + 2), "df");
115 *vp++ = TAG_DUMP_FILE;
116 *vp++ = (byte) (len & 0xFF);
117 bcopy(hp->dump_file->string, vp, len);
119 bytesleft -= len + 2;
122 * DNS server and domain
124 if (hp->flags.domain_server) {
125 if (insert_ip(TAG_DOMAIN_SERVER,
130 if (hp->flags.domain_name) {
132 * Check for room for domain_name. Add 2 to account for
133 * TAG_DOMAIN_NAME and length.
135 len = strlen(hp->domain_name->string);
136 NEED((len + 2), "dn");
137 *vp++ = TAG_DOMAIN_NAME;
138 *vp++ = (byte) (len & 0xFF);
139 bcopy(hp->domain_name->string, vp, len);
141 bytesleft -= len + 2;
144 * NIS (YP) server and domain
146 if (hp->flags.nis_server) {
147 if (insert_ip(TAG_NIS_SERVER,
152 if (hp->flags.nis_domain) {
154 * Check for room for nis_domain. Add 2 to account for
155 * TAG_NIS_DOMAIN and length.
157 len = strlen(hp->nis_domain->string);
158 NEED((len + 2), "dn");
159 *vp++ = TAG_NIS_DOMAIN;
160 *vp++ = (byte) (len & 0xFF);
161 bcopy(hp->nis_domain->string, vp, len);
163 bytesleft -= len + 2;
165 /* IEN 116 name server */
166 if (hp->flags.name_server) {
167 if (insert_ip(TAG_NAME_SERVER,
172 if (hp->flags.rlp_server) {
173 if (insert_ip(TAG_RLP_SERVER,
178 /* Time server (RFC 868) */
179 if (hp->flags.time_server) {
180 if (insert_ip(TAG_TIME_SERVER,
185 /* NTP (time) Server (RFC 1129) */
186 if (hp->flags.ntp_server) {
187 if (insert_ip(TAG_NTP_SERVER,
193 * I wonder: If the hostname were "promoted" into the BOOTP
194 * response part, might these "extension" files possibly be
195 * shared between several clients?
197 * Also, why not just use longer BOOTP packets with all the
198 * additional length used as option data. This bootpd version
199 * already supports that feature by replying with the same
200 * packet length as the client request packet. -gwr
202 if (hp->flags.name_switch && hp->flags.send_name) {
204 * Check for room for hostname. Add 2 to account for
205 * TAG_HOST_NAME and length.
207 len = strlen(hp->hostname->string);
210 * XXX - Too much magic. The user can always set the hostname
211 * to the short version in the bootptab file. -gwr
213 if ((len + 2) > bytesleft) {
215 * Not enough room for full (domain-qualified) hostname, try
216 * stripping it down to just the first field (host).
218 char *tmpstr = hp->hostname->string;
220 while (*tmpstr && (*tmpstr != '.')) {
226 NEED((len + 2), "hn");
227 *vp++ = TAG_HOST_NAME;
228 *vp++ = (byte) (len & 0xFF);
229 bcopy(hp->hostname->string, vp, len);
231 bytesleft -= len + 2;
234 * The rest of these are less important, so they go last.
236 if (hp->flags.lpr_server) {
237 if (insert_ip(TAG_LPR_SERVER,
242 if (hp->flags.cookie_server) {
243 if (insert_ip(TAG_COOKIE_SERVER,
248 if (hp->flags.log_server) {
249 if (insert_ip(TAG_LOG_SERVER,
255 * XXX - Add new tags here (to insert options)
257 if (hp->flags.generic) {
258 if (insert_generic(hp->generic, &vp, &bytesleft))
259 NEED(64, "(generic)");
262 * The end marker is inserted by the caller.
266 } /* dovend_rfc1497 */
271 * Insert a tag value, a length value, and a list of IP addresses into the
272 * memory buffer indirectly pointed to by "dest". "tag" is the RFC1048 tag
273 * number to use, "iplist" is a pointer to a list of IP addresses
274 * (struct in_addr_list), and "bytesleft" points to an integer which
275 * indicates the size of the "dest" buffer.
277 * Return zero if everything fits.
279 * This is used to fill the vendor-specific area of a bootp packet in
280 * conformance to RFC1048.
284 insert_ip(tag, iplist, dest, bytesleft)
286 struct in_addr_list *iplist;
290 struct in_addr *addrptr;
291 unsigned addrcount = 1;
297 if (*bytesleft >= 6) {
298 d = *dest; /* Save pointer for later */
301 (*bytesleft) -= 2; /* Account for tag and length */
302 addrptr = iplist->addr;
303 addrcount = iplist->addrcount;
304 while ((*bytesleft >= 4) && (addrcount > 0)) {
305 insert_u_long(addrptr->s_addr, dest);
308 (*bytesleft) -= 4; /* Four bytes per address */
310 d[1] = (byte) ((*dest - d - 2) & 0xFF);
318 * Insert generic data into a bootp packet. The data is assumed to already
319 * be in RFC1048 format. It is inserted using a first-fit algorithm which
320 * attempts to insert as many tags as possible. Tags and data which are
321 * too large to fit are skipped; any remaining tags are tried until they
322 * have all been exhausted.
323 * Return zero if everything fits.
327 insert_generic(gendata, buff, bytesleft)
328 struct shared_bindata *gendata;
333 int length, numbytes;
339 srcptr = gendata->data;
340 length = gendata->length;
341 while ((length > 0) && (*bytesleft > 0)) {
344 length = 0; /* Force an exit on next iteration */
347 *(*buff)++ = *srcptr++;
352 numbytes = srcptr[1] + 2;
353 if (*bytesleft < numbytes)
356 bcopy(srcptr, *buff, numbytes);
358 (*bytesleft) -= numbytes;
369 * Insert the unsigned long "value" into memory starting at the byte
370 * pointed to by the byte pointer (*dest). (*dest) is updated to
371 * point to the next available byte.
373 * Since it is desirable to internally store network addresses in network
374 * byte order (in struct in_addr's), this routine expects longs to be
375 * passed in network byte order.
377 * However, due to the nature of the main algorithm, the long must be in
378 * host byte order, thus necessitating the use of ntohl() first.
382 insert_u_long(value, dest)
389 value = ntohl(value); /* Must use host byte order here */
391 for (n = 4; n > 0; n--) {
392 *--temp = (byte) (value & 0xFF);
395 /* Final result is network byte order */
402 * c-argdecl-indent: 4
403 * c-continued-statement-offset: 4
404 * c-continued-brace-offset: -4