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