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