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 $
5 * $DragonFly: src/libexec/bootpd/dovend.c,v 1.2 2003/06/17 04:27:07 dillon Exp $
10 #include <netinet/in.h>
11 #include <arpa/inet.h> /* inet_ntoa */
21 /* Yes, memcpy is OK here (no overlapped copies). */
22 # define bcopy(a,b,c) memcpy(b,a,c)
23 # define bzero(p,l) memset(p,0,l)
24 # define bcmp(a,b,c) memcmp(a,b,c)
33 PRIVATE
int insert_generic(struct shared_bindata
*, byte
**, int *);
36 * Insert the 2nd part of the options into an option buffer.
37 * Return amount of space used.
39 * This inserts everything EXCEPT:
40 * magic cookie, subnet mask, gateway, bootsize, extension file
41 * Those are handled separately (in bootpd.c) to allow this function
42 * to be shared between bootpd and bootpef.
44 * When an "extension file" is in use, the options inserted by
45 * this function go into the exten_file, not the bootp response.
49 dovend_rfc1497(struct host
*hp
, byte
*buf
, int len
)
54 static const char noroom
[] = "%s: No room for \"%s\" option";
55 #define NEED(LEN, MSG) do \
56 if (bytesleft < (LEN)) { \
57 report(LOG_NOTICE, noroom, \
58 hp->hostname->string, MSG); \
63 * Note that the following have already been inserted:
64 * magic_cookie, subnet_mask, gateway, bootsize
66 * The remaining options are inserted in order of importance.
67 * (Of course the importance of each is a matter of opinion.)
68 * The option insertion order should probably be configurable.
70 * This is the order used in the NetBSD version. Can anyone
71 * explain why the time_offset and swap_server are first?
72 * Also, why is the hostname so far down the list? -gwr
75 if (hp
->flags
.time_offset
) {
77 *vp
++ = TAG_TIME_OFFSET
;/* -1 byte */
78 *vp
++ = 4; /* -1 byte */
79 insert_u_long(htonl(hp
->time_offset
), &vp
); /* -4 bytes */
83 * swap server, root path, dump path
85 if (hp
->flags
.swap_server
) {
87 /* There is just one SWAP_SERVER, so it is not an iplist. */
88 *vp
++ = TAG_SWAP_SERVER
;/* -1 byte */
89 *vp
++ = 4; /* -1 byte */
90 insert_u_long(hp
->swap_server
.s_addr
, &vp
); /* -4 bytes */
91 bytesleft
-= 6; /* Fix real count */
93 if (hp
->flags
.root_path
) {
95 * Check for room for root_path. Add 2 to account for
96 * TAG_ROOT_PATH and length.
98 len
= strlen(hp
->root_path
->string
);
99 NEED((len
+ 2), "rp");
100 *vp
++ = TAG_ROOT_PATH
;
101 *vp
++ = (byte
) (len
& 0xFF);
102 bcopy(hp
->root_path
->string
, vp
, len
);
104 bytesleft
-= len
+ 2;
106 if (hp
->flags
.dump_file
) {
108 * Check for room for dump_file. Add 2 to account for
109 * TAG_DUMP_FILE and length.
111 len
= strlen(hp
->dump_file
->string
);
112 NEED((len
+ 2), "df");
113 *vp
++ = TAG_DUMP_FILE
;
114 *vp
++ = (byte
) (len
& 0xFF);
115 bcopy(hp
->dump_file
->string
, vp
, len
);
117 bytesleft
-= len
+ 2;
120 * DNS server and domain
122 if (hp
->flags
.domain_server
) {
123 if (insert_ip(TAG_DOMAIN_SERVER
,
128 if (hp
->flags
.domain_name
) {
130 * Check for room for domain_name. Add 2 to account for
131 * TAG_DOMAIN_NAME and length.
133 len
= strlen(hp
->domain_name
->string
);
134 NEED((len
+ 2), "dn");
135 *vp
++ = TAG_DOMAIN_NAME
;
136 *vp
++ = (byte
) (len
& 0xFF);
137 bcopy(hp
->domain_name
->string
, vp
, len
);
139 bytesleft
-= len
+ 2;
142 * NIS (YP) server and domain
144 if (hp
->flags
.nis_server
) {
145 if (insert_ip(TAG_NIS_SERVER
,
150 if (hp
->flags
.nis_domain
) {
152 * Check for room for nis_domain. Add 2 to account for
153 * TAG_NIS_DOMAIN and length.
155 len
= strlen(hp
->nis_domain
->string
);
156 NEED((len
+ 2), "dn");
157 *vp
++ = TAG_NIS_DOMAIN
;
158 *vp
++ = (byte
) (len
& 0xFF);
159 bcopy(hp
->nis_domain
->string
, vp
, len
);
161 bytesleft
-= len
+ 2;
163 /* IEN 116 name server */
164 if (hp
->flags
.name_server
) {
165 if (insert_ip(TAG_NAME_SERVER
,
170 if (hp
->flags
.rlp_server
) {
171 if (insert_ip(TAG_RLP_SERVER
,
176 /* Time server (RFC 868) */
177 if (hp
->flags
.time_server
) {
178 if (insert_ip(TAG_TIME_SERVER
,
183 /* NTP (time) Server (RFC 1129) */
184 if (hp
->flags
.ntp_server
) {
185 if (insert_ip(TAG_NTP_SERVER
,
191 * I wonder: If the hostname were "promoted" into the BOOTP
192 * response part, might these "extension" files possibly be
193 * shared between several clients?
195 * Also, why not just use longer BOOTP packets with all the
196 * additional length used as option data. This bootpd version
197 * already supports that feature by replying with the same
198 * packet length as the client request packet. -gwr
200 if (hp
->flags
.name_switch
&& hp
->flags
.send_name
) {
202 * Check for room for hostname. Add 2 to account for
203 * TAG_HOST_NAME and length.
205 len
= strlen(hp
->hostname
->string
);
208 * XXX - Too much magic. The user can always set the hostname
209 * to the short version in the bootptab file. -gwr
211 if ((len
+ 2) > bytesleft
) {
213 * Not enough room for full (domain-qualified) hostname, try
214 * stripping it down to just the first field (host).
216 char *tmpstr
= hp
->hostname
->string
;
218 while (*tmpstr
&& (*tmpstr
!= '.')) {
224 NEED((len
+ 2), "hn");
225 *vp
++ = TAG_HOST_NAME
;
226 *vp
++ = (byte
) (len
& 0xFF);
227 bcopy(hp
->hostname
->string
, vp
, len
);
229 bytesleft
-= len
+ 2;
232 * The rest of these are less important, so they go last.
234 if (hp
->flags
.lpr_server
) {
235 if (insert_ip(TAG_LPR_SERVER
,
240 if (hp
->flags
.cookie_server
) {
241 if (insert_ip(TAG_COOKIE_SERVER
,
246 if (hp
->flags
.log_server
) {
247 if (insert_ip(TAG_LOG_SERVER
,
253 * XXX - Add new tags here (to insert options)
255 if (hp
->flags
.generic
) {
256 if (insert_generic(hp
->generic
, &vp
, &bytesleft
))
257 NEED(64, "(generic)");
260 * The end marker is inserted by the caller.
264 } /* dovend_rfc1497 */
269 * Insert a tag value, a length value, and a list of IP addresses into the
270 * memory buffer indirectly pointed to by "dest". "tag" is the RFC1048 tag
271 * number to use, "iplist" is a pointer to a list of IP addresses
272 * (struct in_addr_list), and "bytesleft" points to an integer which
273 * indicates the size of the "dest" buffer.
275 * Return zero if everything fits.
277 * This is used to fill the vendor-specific area of a bootp packet in
278 * conformance to RFC1048.
282 insert_ip(byte tag
, struct in_addr_list
*iplist
, byte
**dest
, int *bytesleft
)
284 struct in_addr
*addrptr
;
285 unsigned addrcount
= 1;
291 if (*bytesleft
>= 6) {
292 d
= *dest
; /* Save pointer for later */
295 (*bytesleft
) -= 2; /* Account for tag and length */
296 addrptr
= iplist
->addr
;
297 addrcount
= iplist
->addrcount
;
298 while ((*bytesleft
>= 4) && (addrcount
> 0)) {
299 insert_u_long(addrptr
->s_addr
, dest
);
302 (*bytesleft
) -= 4; /* Four bytes per address */
304 d
[1] = (byte
) ((*dest
- d
- 2) & 0xFF);
312 * Insert generic data into a bootp packet. The data is assumed to already
313 * be in RFC1048 format. It is inserted using a first-fit algorithm which
314 * attempts to insert as many tags as possible. Tags and data which are
315 * too large to fit are skipped; any remaining tags are tried until they
316 * have all been exhausted.
317 * Return zero if everything fits.
321 insert_generic(struct shared_bindata
*gendata
, byte
**buff
, int *bytesleft
)
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(u_int32 value
, byte
**dest
)
378 value
= ntohl(value
); /* Must use host byte order here */
380 for (n
= 4; n
> 0; n
--) {
381 *--temp
= (byte
) (value
& 0xFF);
384 /* Final result is network byte order */
391 * c-argdecl-indent: 4
392 * c-continued-statement-offset: 4
393 * c-continued-brace-offset: -4